Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chance

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chance - npm Package Compare versions

Comparing version 0.7.3 to 0.7.4

LICENSE

2

bin/chance.js

@@ -15,4 +15,6 @@ #!/usr/bin/env node

process.stdout.write(chance[generator](options));
if (process.stdout.isTTY) process.stdout.write('\n');
} else {
process.stderr.write('Unknown generator "' + generator + '"');
if (process.stderr.isTTY) process.stderr.write('\n');
}

7

bower.json
{
"name": "chance",
"version": "0.7.3",
"version": "0.7.4",
"main": "chance.js",

@@ -20,7 +20,4 @@ "ignore": [

"devDependencies": {
"chai": "~1.10.0",
"mocha": "~2.1.0",
"phone_number_js": "~1.0.0",
"requirejs": "~2.1.15"
"phone_number_js": "~1.0.0"
}
}

@@ -5,3 +5,3 @@ {

"description": "Minimalist generator of random strings, numbers, etc.",
"version": "0.7.3",
"version": "0.7.4",
"keywords": ["chance", "random", "generator", "test"],

@@ -8,0 +8,0 @@ "main": "chance.js",

@@ -5,2 +5,20 @@ module.exports = function (grunt) {

grunt.initConfig({
'js-test': {
options: {
coverage: true,
coverageTool: 'istanbul',
coverageFormat: 'lcov',
identifier: 'chance-coverage',
reporter: "Spec",
pattern: 'test/*.js'
},
watch: {
options: {
coverage: false,
reporter: "Dot",
pattern: 'test/*.js',
root: '.'
}
}
},
jshint: {

@@ -32,5 +50,10 @@ options: {

globals: {
_: true,
chai: true,
chance: true,
Chance: true,
define: false,
mocha: true,
mochaPhantomJS: false,
chance: true
phoneNumber: true
}

@@ -40,4 +63,6 @@ },

},
mocha_phantomjs: {
all: ["test/runner.html"]
shell: {
target: {
command: 'cat coverage/chance-coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage'
}
},

@@ -58,14 +83,15 @@ uglify: {

files: js_files,
tasks: ['jshint', 'mocha_phantomjs', 'uglify']
tasks: ['jshint', 'js-test:watch', 'uglify']
}
});
grunt.loadNpmTasks('grunt-bump');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-phantomjs');
grunt.loadNpmTasks('grunt-js-test');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['watch']);
grunt.registerTask('test', ['mocha_phantomjs']);
grunt.registerTask('test', ['js-test', 'shell']);
grunt.registerTask('test-ci', ['jshint', 'js-test', 'shell']);
};
{
"name": "chance",
"main": "./chance.js",
"version": "0.7.3",
"version": "0.7.4",
"description": "Chance - Utility library to generate anything random",

@@ -15,22 +15,19 @@ "homepage": "http://chancejs.com",

},
"licenses": [
{
"type": "MIT",
"url": "http://chancejs.com/license/"
}
],
"license": "MIT",
"devDependencies": {
"coveralls": "^2.11.2",
"grunt": "0.4.5",
"grunt-bump": "0.0.16",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.7.0",
"grunt-contrib-watch": "0.6.1",
"grunt-mocha-phantomjs": "^0.6.0",
"grunt-js-test": "git://github.com/victorquinn/grunt-js-test#ffc580375f0ff7b496026ab87b4a9666992d2f33",
"grunt-shell": "^1.1.2",
"istanbul": "^0.3.13",
"line-by-line": "^0.1.3",
"lodash-cli": "^2.4.2",
"mocha-phantomjs": "^3.5.3"
"minimatch": "^2.0.4",
"mocha-lcov-reporter": "0.0.2",
"underscore": "^1.8.3"
},
"scripts": {
"pretest": "lodash compat -o test/lib/lodash/lodash.build.js",
"test": "./node_modules/mocha-phantomjs/bin/mocha-phantomjs test/runner.html"
"test": "grunt test-ci"
},

@@ -37,0 +34,0 @@ "bin": "./bin/chance.js",

@@ -5,3 +5,3 @@ # Chance

[![Build Status](https://travis-ci.org/victorquinn/chancejs.png)](https://travis-ci.org/victorquinn/chancejs)
[![Build Status](https://travis-ci.org/victorquinn/chancejs.svg?branch=develop)](https://travis-ci.org/victorquinn/chancejs) [![GitHub license](https://img.shields.io/github/license/victorquinn/chancejs.svg)](https://github.com/victorquinn/chancejs) [![GitHub stars](https://img.shields.io/github/stars/victorquinn/chancejs.svg)](https://github.com/victorquinn/chancejs) [![npm](https://img.shields.io/npm/dm/chance.svg)](https://npmjs.com/package/chance) [![npm](https://img.shields.io/npm/v/chance.svg)](https://npmjs.com/package/chance) [![Coverage Status](https://coveralls.io/repos/victorquinn/chancejs/badge.svg?branch=develop)](https://coveralls.io/r/victorquinn/chancejs?branch=develop) [![awesomeness](https://img.shields.io/badge/awesomeness-maximum-red.svg)](https://github.com/victorquinn/chancejs)

@@ -144,24 +144,25 @@ Chance - Random generator helper for JavaScript

project : chancejs
repo age : 1 year, 7 months
active : 120 days
commits : 447
files : 24
repo age : 1 year, 10 months
active : 137 days
commits : 492
files : 23
authors :
305 Victor Quinn 68.2%
26 Oliver Salzburg 5.8%
13 Tim Petricola 2.9%
12 Alex DiLiberto 2.7%
11 davmillar 2.5%
7 Jan Tojnar 1.6%
5 Michael Cordingley 1.1%
5 Matt Klaber 1.1%
4 Chris Villarreal 0.9%
4 Abhijeet Pawar 0.9%
4 Avishaan 0.9%
4 Kevin Garnett 0.9%
3 Nicholas Johnson 0.7%
3 qjcg 0.7%
3 Alexandr Lozovyuk 0.7%
3 Dominic Barnes 0.7%
2 xshyamx 0.4%
339 Victor Quinn 68.9%
26 Oliver Salzburg 5.3%
13 Tim Petricola 2.6%
12 Alex DiLiberto 2.4%
11 davmillar 2.2%
7 Jan Tojnar 1.4%
5 Matt Klaber 1.0%
5 Michael Cordingley 1.0%
4 Kevin Garnett 0.8%
4 Avishaan 0.8%
4 Abhijeet Pawar 0.8%
4 Chris Villarreal 0.8%
3 Dominic Barnes 0.6%
3 leesei 0.6%
3 qjcg 0.6%
3 Nicholas Johnson 0.6%
3 Alexandr Lozovyuk 0.6%
2 dhilipsiva 0.4%
2 Adam Krebs 0.4%

@@ -172,20 +173,26 @@ 2 Andreas Koeberle 0.4%

2 Pascal Borreli 0.4%
2 Patrick Mowrer 0.4%
2 SeeSchloss 0.4%
2 Stefan Penner 0.4%
2 dhilipsiva 0.4%
2 path411 0.4%
2 shyam 0.4%
1 Richard Anaya 0.2%
2 somejeff 0.4%
2 xshyamx 0.4%
1 mamrehn 0.2%
1 Johannes Stein 0.2%
1 Doug Lawrence 0.2%
1 afc163 0.2%
1 Johannes Stein 0.2%
1 Danny Yates 0.2%
1 ddunning 0.2%
1 Doug Lawrence 0.2%
1 Brian.Barnett 0.2%
1 flrent 0.2%
1 kkroner 0.2%
1 leesei 0.2%
1 Ng Patrick 0.2%
1 Matt Altermatt 0.2%
1 Billy Moon 0.2%
1 lkptrzk 0.2%
1 Richard Anaya 0.2%
1 Ryan Tenney 0.2%
1 Samuel Greene 0.2%
1 lkptrzk 0.2%
1 Brian.Barnett 0.2%
1 Ng Patrick 0.2%
1 Adam Pointer 0.2%
```

@@ -196,2 +203,3 @@

Be a part of this project! You can run the test using the following.
1. Install dependencies from package.json by running `npm install`

@@ -198,0 +206,0 @@ 2. Install dependencies for testing by running `bower install`

@@ -1,334 +0,400 @@

define(['Chance', 'mocha', 'chai', 'underscore', 'phoneTest'], function (Chance, mocha, chai, _, phoneTest) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
/// <reference path="lib/phone_number_js/dist/phoneNumber.min.js" />
describe("Address", function () {
var zip, suffix, suffixes, state, address, country, chance = new Chance();
var expect = chai.expect;
describe("Zip", function () {
it("returns a valid basic zip code", function () {
_(1000).times(function () {
zip = chance.zip();
expect(zip.length).to.equal(5);
expect(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip)).to.be.true;
});
});
describe("Address", function () {
var zip, postal, suffix, suffixes, state, address, country, chance = new Chance();
it("returns a valid zip+4 code", function () {
_(1000).times(function () {
zip = chance.zip({plusfour: true});
expect(zip.length).to.equal(10);
expect(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip)).to.be.true;
});
describe("Zip", function () {
it("returns a valid basic zip code", function () {
_(1000).times(function () {
zip = chance.zip();
expect(zip.length).to.equal(5);
expect(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip)).to.be.true;
});
});
describe("Street", function () {
it("street suffixes returns the suffix array", function () {
suffixes = chance.street_suffixes();
expect(suffixes).to.be.an('array');
_.each(suffixes, function (suffix) {
expect(suffix).to.have.keys('name', 'abbreviation');
});
it("returns a valid zip+4 code", function () {
_(1000).times(function () {
zip = chance.zip({plusfour: true});
expect(zip.length).to.equal(10);
expect(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip)).to.be.true;
});
});
});
it("street suffixes are short", function () {
suffixes = chance.street_suffixes();
_.each(suffixes, function (suffix) {
expect(suffix.abbreviation).to.have.length.below(5);
describe("Postal", function () {
it("returns a valid basic postal code", function () {
_(1000).times(function () {
postal = chance.postal();
expect(postal.length).to.equal(7);
_.times(6, function(n) {
expect(postal.charAt(n).toUpperCase()).to.equal(postal.charAt(n));
});
});
});
});
it("full street suffixes are longish", function () {
suffixes = chance.street_suffixes();
_.each(suffixes, function (suffix) {
expect(suffix.name).to.have.length.above(2);
});
describe("Street", function () {
it("street suffixes returns the suffix array", function () {
suffixes = chance.street_suffixes();
expect(suffixes).to.be.an('array');
_.each(suffixes, function (suffix) {
expect(suffix).to.have.keys('name', 'abbreviation');
});
it("street suffix returns a single suffix", function () {
_(1000).times(function () {
suffix = chance.street_suffix();
expect(suffix).to.be.an('object');
expect(suffix.name).to.be.a('string');
expect(suffix.abbreviation).to.be.a('string');
});
});
});
describe("State", function () {
it("states() returns an array of states", function () {
expect(chance.states()).to.be.an('array');
it("street suffixes are short", function () {
suffixes = chance.street_suffixes();
_.each(suffixes, function (suffix) {
expect(suffix.abbreviation).to.have.length.below(5);
});
});
it("state() returns a random (short) state name", function () {
_(1000).times(function () {
state = chance.state();
expect(state.length).to.be.below(3);
});
it("full street suffixes are longish", function () {
suffixes = chance.street_suffixes();
_.each(suffixes, function (suffix) {
expect(suffix.name).to.have.length.above(2);
});
});
it("state({full: true}) returns a random (long) state name", function () {
_(1000).times(function () {
state = chance.state({full: true});
expect(state.length).to.be.above(2);
});
it("street suffix returns a single suffix", function () {
_(1000).times(function () {
suffix = chance.street_suffix();
expect(suffix).to.be.an('object');
expect(suffix.name).to.be.a('string');
expect(suffix.abbreviation).to.be.a('string');
});
});
});
it("states() returns all 50 US States and DC", function () {
expect(chance.states()).to.have.length(51);
});
describe("State", function () {
it("states() returns an array of states", function () {
expect(chance.states()).to.be.an('array');
});
it("states({territories: true}) returns 50 US States, DC, and 7 US Territories", function () {
expect(chance.states({territories: true})).to.have.length(58);
it("state() returns a random (short) state name", function () {
_(1000).times(function () {
state = chance.state();
expect(state.length).to.be.below(3);
});
});
it("states({armed_forces: true}) returns 50 US States, DC, and 3 Armed Forces Military States", function () {
expect(chance.states({armed_forces: true})).to.have.length(54);
it("state({full: true}) returns a random (long) state name", function () {
_(1000).times(function () {
state = chance.state({full: true});
expect(state.length).to.be.above(2);
});
});
it("states({territories: true, armed_forces: true}) returns 50 US States, DC, 7 US Territories, and 3 Armed Forces Military States", function () {
expect(chance.states({territories: true, armed_forces: true})).to.have.length(61);
});
it("states() returns all 50 US States and DC", function () {
expect(chance.states()).to.have.length(51);
});
describe("Province", function () {
it("provinces() returns an array of provinces", function () {
expect(chance.provinces()).to.be.an('array');
});
it("states({territories: true}) returns 50 US States, DC, and 7 US Territories", function () {
expect(chance.states({territories: true})).to.have.length(58);
});
it("province() returns a random (short) province name", function () {
_(1000).times(function () {
expect(chance.province()).to.have.length.below(3);
});
});
it("states({armed_forces: true}) returns 50 US States, DC, and 3 Armed Forces Military States", function () {
expect(chance.states({armed_forces: true})).to.have.length(54);
});
it("province({full: true}) returns a random (long) province name", function () {
_(1000).times(function () {
expect(chance.province({full: true})).to.have.length.above(2);
});
});
it("states({territories: true, armed_forces: true}) returns 50 US States, DC, 7 US Territories, and 3 Armed Forces Military States", function () {
expect(chance.states({territories: true, armed_forces: true})).to.have.length(61);
});
});
describe("Address", function () {
it("address() returns a string", function () {
expect(chance.address()).to.be.an('string');
});
describe("Province", function () {
it("provinces() returns an array of provinces", function () {
expect(chance.provinces()).to.be.an('array');
});
it("address() starts with a number", function () {
_(1000).times(function () {
expect(chance.address()).to.match(/[0-9]+.+/);
});
it("province() returns a random (short) province name", function () {
_(1000).times(function () {
expect(chance.province()).to.have.length.below(3);
});
});
it("address() can take short_suffix arg and obey it", function () {
_(1000).times(function () {
address = chance.address({short_suffix: true});
expect(address.split(' ')[2].length).to.be.below(5);
});
it("province({full: true}) returns a random (long) province name", function () {
_(1000).times(function () {
expect(chance.province({full: true})).to.have.length.above(2);
});
});
});
describe("Phone Number", function () {
it("areacode() looks right", function () {
expect(chance.areacode()).to.be.a('string');
_(1000).times(function () {
expect(chance.areacode()).to.match(/^\(([2-9][0-8][0-9])\)$/);
});
});
describe("Address", function () {
it("address() returns a string", function () {
expect(chance.address()).to.be.an('string');
});
it("areacode({parens: false}) looks right", function () {
_(1000).times(function () {
expect(chance.areacode({parens: false})).to.be.a('string');
expect(chance.areacode({parens: false})).to.match(/^([2-9][0-8][0-9])$/);
});
it("address() starts with a number", function () {
_(1000).times(function () {
expect(chance.address()).to.match(/[0-9]+.+/);
});
});
it("phone() returns a string", function () {
expect(chance.phone()).to.be.a('string');
it("address() can take short_suffix arg and obey it", function () {
_(1000).times(function () {
address = chance.address({short_suffix: true});
expect(address.split(' ')[2].length).to.be.below(5);
});
});
});
it("phone() looks like an actual phone number", function () {
_(1000).times(function () {
expect(chance.phone()).to.match(/^\(([2-9][0-8][0-9])\)?[\-. ]?([2-9][0-9]{2,2})[\-. ]?([0-9]{4,4})$/);
});
describe("Phone Number", function () {
it("areacode() looks right", function () {
expect(chance.areacode()).to.be.a('string');
_(1000).times(function () {
expect(chance.areacode()).to.match(/^\(([2-9][0-8][0-9])\)$/);
});
});
it('phone({formatted: false}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({formatted : false})).to.be.a('string');
expect(chance.phone({formatted : false})).to.match(/^[2-9][0-8]\d[2-9]\d{6,6}$/);
});
it("areacode({parens: false}) looks right", function () {
_(1000).times(function () {
expect(chance.areacode({parens: false})).to.be.a('string');
expect(chance.areacode({parens: false})).to.match(/^([2-9][0-8][0-9])$/);
});
});
it('phone({formatted: false, parens: true}) is unformatted', function () {
_(1000).times(function () {
expect(chance.phone({formatted : false, parens: true})).to.be.a('string');
expect(chance.phone({formatted : false, parens: true})).to.match(/^[2-9][0-8]\d[2-9]\d{6,6}$/);
});
});
it("phone() returns a string", function () {
expect(chance.phone()).to.be.a('string');
});
it("phone({country: 'uk'}) returns a string", function () {
expect(chance.phone({country: 'uk'})).to.be.a('string');
it("phone() looks like an actual phone number", function () {
_(1000).times(function () {
expect(chance.phone()).to.match(/^\(([2-9][0-8][0-9])\)?[\-. ]?([2-9][0-9]{2,2})[\-. ]?([0-9]{4,4})$/);
});
});
it("phone({country: 'uk', mobile: true}) returns a string", function () {
expect(chance.phone({country: 'uk', mobile: true})).to.be.a('string');
it('phone({formatted: false}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({formatted : false})).to.be.a('string');
expect(chance.phone({formatted : false})).to.match(/^[2-9][0-8]\d[2-9]\d{6,6}$/);
});
});
it('phone({country: "uk"}) looks right', function () {
_(1000).times(function () {
expect(phoneTest.isValid(chance.phone({country: 'uk'}))).to.be.true;
});
it('phone({formatted: false, parens: true}) is unformatted', function () {
_(1000).times(function () {
expect(chance.phone({formatted : false, parens: true})).to.be.a('string');
expect(chance.phone({formatted : false, parens: true})).to.match(/^[2-9][0-8]\d[2-9]\d{6,6}$/);
});
});
it('phone({country: "uk", formatted: false}) looks right', function () {
_(1000).times(function () {
expect(phoneTest.isValid(phoneTest.format(chance.phone({country: 'uk', formatted: false})))).to.be.true;
});
it("phone({country: 'uk'}) returns a string", function () {
expect(chance.phone({country: 'uk'})).to.be.a('string');
});
it("phone({country: 'uk', mobile: true}) returns a string", function () {
expect(chance.phone({country: 'uk', mobile: true})).to.be.a('string');
});
it('phone({country: "uk"}) looks right', function () {
_(1000).times(function () {
expect(phoneNumber.isValid(chance.phone({country: 'uk'}))).to.be.true;
});
});
it('phone({country: "uk", mobile: true}) looks right', function () {
_(1000).times(function () {
expect(phoneTest.isValid(chance.phone({country: 'uk', mobile: true}))).to.be.true;
});
it('phone({country: "uk", formatted: false}) looks right', function () {
_(1000).times(function () {
expect(phoneNumber.isValid(phoneNumber.format(chance.phone({country: 'uk', formatted: false})))).to.be.true;
});
});
it('phone({country: "uk", mobile: true, formatted: false}) looks right', function () {
_(1000).times(function () {
expect(phoneTest.isValid(phoneTest.format(chance.phone({country: 'uk', mobile: true, formatted: false})))).to.be.true;
});
it('phone({country: "uk", mobile: true}) looks right', function () {
_(1000).times(function () {
expect(phoneNumber.isValid(chance.phone({country: 'uk', mobile: true}))).to.be.true;
});
});
it("phone({country: 'fr'}) returns a string", function () {
expect(chance.phone({country: 'fr'})).to.be.a('string');
it('phone({country: "uk", mobile: true, formatted: false}) looks right', function () {
_(1000).times(function () {
expect(phoneNumber.isValid(phoneNumber.format(chance.phone({country: 'uk', mobile: true, formatted: false})))).to.be.true;
});
});
it("phone({country: 'fr', mobile: true}) returns a string", function () {
expect(chance.phone({country: 'fr', mobile: true})).to.be.a('string');
it("phone({country: 'fr'}) returns a string", function () {
expect(chance.phone({country: 'fr'})).to.be.a('string');
});
it("phone({country: 'fr', mobile: true}) returns a string", function () {
expect(chance.phone({country: 'fr', mobile: true})).to.be.a('string');
});
it('phone({country: "fr"}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr'})).match(/0[123459] .. .. .. ../);
});
});
it('phone({country: "fr"}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr'})).match(/0[123459] .. .. .. ../);
});
it('phone({country: "fr", formatted: false}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr', formatted: false})).match(/0........./);
});
});
it('phone({country: "fr", formatted: false}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr', formatted: false})).match(/0........./);
});
it('phone({country: "fr", mobile: true}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr', mobile: true})).match(/0[67] .. .. .. ../);
});
});
it('phone({country: "fr", mobile: true}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr', mobile: true})).match(/0[67] .. .. .. ../);
});
it('phone({country: "fr", mobile: true, formatted: false}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr', mobile: true, formatted: false})).match(/0[67]......../);
});
});
});
it('phone({country: "fr", mobile: true, formatted: false}) looks right', function () {
_(1000).times(function () {
expect(chance.phone({country: 'fr', mobile: true, formatted: false})).match(/0[67]......../);
});
describe("Country", function () {
it("countries() returns an array of countries", function () {
expect(chance.countries()).to.be.an('array');
});
it("country() returns a random (short) country name", function () {
_(1000).times(function () {
country = chance.country();
expect(country.length).to.equal(2);
});
});
describe("Country", function () {
it("countries() returns an array of countries", function () {
expect(chance.countries()).to.be.an('array');
it("country({full: true}) returns a random country name", function () {
_(1000).times(function () {
country = chance.country({full: true});
expect(country.length).to.be.above(2);
});
});
});
it("country() returns a random (short) country name", function () {
_(1000).times(function () {
country = chance.country();
expect(country.length).to.equal(2);
});
describe("City", function () {
it("city() looks right", function () {
expect(chance.city()).to.be.a('string');
});
});
describe("Latitude", function () {
it("latitude() looks right", function () {
expect(chance.latitude()).to.be.a('number');
});
it("latitude() is in the right range", function () {
_(1000).times(function () {
expect(chance.latitude()).to.be.within(-90, 90);
});
});
it("country({full: true}) returns a random country name", function () {
_(1000).times(function () {
country = chance.country({full: true});
expect(country.length).to.be.above(2);
});
it("latitude() will accept a min and obey it", function () {
_(1000).times(function () {
var min = chance.floating({min: -90, max: 90});
expect(chance.latitude({min: min})).to.be.within(min, 90);
});
});
describe("City", function () {
it("city() looks right", function () {
expect(chance.city()).to.be.a('string');
it("latitude() will accept a max and obey it", function () {
_(1000).times(function () {
var max = chance.floating({min: -90, max: 90});
expect(chance.latitude({max: max})).to.be.within(-90, max);
});
});
});
describe("Latitude", function () {
it("latitude() looks right", function () {
expect(chance.latitude()).to.be.a('number');
describe("Altitude", function () {
it("looks right", function () {
expect(chance.altitude()).to.be.a('number');
});
it("is in the right range", function () {
_(1000).times(function () {
expect(chance.altitude()).to.be.within(0, 8848);
});
});
it("latitude() is in the right range", function () {
_(1000).times(function () {
expect(chance.latitude()).to.be.within(-90, 90);
});
it("will accept a min and obey it", function () {
_(1000).times(function () {
var min = chance.floating({min: 0, max: 8848});
expect(chance.altitude({min: min})).to.be.within(min, 8848);
});
});
it("latitude() will accept a min and obey it", function () {
_(1000).times(function () {
var min = chance.floating({min: -90, max: 90});
expect(chance.latitude({min: min})).to.be.within(min, 90);
});
it("will accept a max and obey it", function () {
_(1000).times(function () {
var max = chance.floating({min: 1000, max: 9000});
expect(chance.altitude({max: max})).to.be.within(0, max);
});
});
});
it("latitude() will accept a max and obey it", function () {
_(1000).times(function () {
var max = chance.floating({min: -90, max: 90});
expect(chance.latitude({max: max})).to.be.within(-90, max);
});
describe("Depth", function () {
it("looks right", function () {
expect(chance.depth()).to.be.a('number');
});
it("is in the right range", function () {
_(1000).times(function () {
expect(chance.depth()).to.be.within(-2550, 0);
});
});
describe("Longitude", function () {
it("longitude() looks right", function () {
expect(chance.longitude()).to.be.a('number');
it("will accept a min and obey it", function () {
_(1000).times(function () {
var min = chance.floating({min: -2500, max: 0});
expect(chance.depth({min: min})).to.be.within(min, 0);
});
});
it("longitude() is in the right range", function () {
_(1000).times(function () {
expect(chance.longitude()).to.be.within(-180, 180);
});
it("will accept a max and obey it", function () {
_(1000).times(function () {
var max = chance.floating({min: -2500, max: 0});
expect(chance.depth({max: max})).to.be.within(-2550, max);
});
});
});
it("longitude() will accept a min and obey it", function () {
_(1000).times(function () {
var min = chance.floating({min: -180, max: 180});
expect(chance.longitude({min: min})).to.be.within(min, 180);
});
describe("Longitude", function () {
it("longitude() looks right", function () {
expect(chance.longitude()).to.be.a('number');
});
it("longitude() is in the right range", function () {
_(1000).times(function () {
expect(chance.longitude()).to.be.within(-180, 180);
});
});
it("longitude() will accept a max and obey it", function () {
_(1000).times(function () {
var max = chance.floating({min: -180, max: 180});
expect(chance.longitude({max: max})).to.be.within(-180, max);
});
it("longitude() will accept a min and obey it", function () {
_(1000).times(function () {
var min = chance.floating({min: -180, max: 180});
expect(chance.longitude({min: min})).to.be.within(min, 180);
});
});
describe("Geohash", function () {
it("geohash() looks right", function() {
expect(chance.geohash()).to.be.a('string');
expect(chance.geohash()).to.have.length(7);
it("longitude() will accept a max and obey it", function () {
_(1000).times(function () {
var max = chance.floating({min: -180, max: 180});
expect(chance.longitude({max: max})).to.be.within(-180, max);
});
});
});
it("geohash() will accept a length and obey it", function() {
_(1000).times(function () {
var length = chance.d10();
expect(chance.geohash({ length: length })).to.have.length(length);
});
describe("Geohash", function () {
it("geohash() looks right", function() {
expect(chance.geohash()).to.be.a('string');
expect(chance.geohash()).to.have.length(7);
});
it("geohash() will accept a length and obey it", function() {
_(1000).times(function () {
var length = chance.d10();
expect(chance.geohash({ length: length })).to.have.length(length);
});
});
});
describe("Coordinates", function () {
it("coordinates() looks about right", function () {
_(1000).times(function () {
expect(chance.coordinates()).to.be.a('string');
expect(chance.coordinates().split(',')).to.have.length(2);
});
describe("Coordinates", function () {
it("coordinates() looks about right", function () {
_(1000).times(function () {
expect(chance.coordinates()).to.be.a('string');
expect(chance.coordinates().split(',')).to.have.length(2);
});

@@ -335,0 +401,0 @@ });

@@ -1,266 +0,311 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/* jshint newcap:false */
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Basics", function () {
var bool, integer, natural, floating, character, string, temp, chance = new Chance(),
data, cData;
var expect = chai.expect;
describe("Data", function () {
it("get data", function () {
data = chance.get("lastNames");
expect(data).to.be.an('array');
});
describe("Basics", function () {
var bool, integer, natural, floating, character, string, temp, chance = new Chance(),
data, cData;
it("set custom data", function () {
cData = {lastNames: ["customName", "testLast"]};
chance.set(cData);
data = chance.get("lastNames");
expect(data).to.be.an('array');
expect(data).to.have.length(2);
});
describe("Data", function () {
it("get data", function () {
data = chance.get("lastNames");
expect(data).to.be.an('array');
});
describe("Bool", function () {
it("returns a random boolean", function () {
it("set custom data", function () {
cData = {lastNames: ["customName", "testLast"]};
chance.set(cData);
data = chance.get("lastNames");
expect(data).to.be.an('array');
expect(data).to.have.length(2);
});
});
describe("Bool", function () {
it("returns a random boolean", function () {
bool = chance.bool();
expect(bool).to.be.a('boolean');
});
it("is within the bounds of what we'd call random", function () {
var true_count = 0;
_(1000).times(function () {
bool = chance.bool();
expect(bool).to.be.a('boolean');
if (bool) {
true_count++;
}
});
it("is within the bounds of what we'd call random", function () {
var true_count = 0;
_(1000).times(function () {
bool = chance.bool();
if (bool) {
true_count++;
}
});
// Note: In very extreme circumstances this test may fail as, by its
// nature it's random. But it's a low enough percentage that I'm
// willing to accept it.
// Award to anyone that calculates the actual probability of this
// test failing and submits a pull request adding it to this comment!
expect(true_count).to.be.within(200, 800);
});
// Note: In very extreme circumstances this test may fail as, by its
// nature it's random. But it's a low enough percentage that I'm
// willing to accept it.
// Award to anyone that calculates the actual probability of this
// test failing and submits a pull request adding it to this comment!
expect(true_count).to.be.within(200, 800);
it("takes and obeys likelihood", function () {
var true_count = 0;
_(1000).times(function () {
if (chance.bool({likelihood: 30})) {
true_count++;
}
});
it("takes and obeys likelihood", function () {
var true_count = 0;
_(1000).times(function () {
if (chance.bool({likelihood: 30})) {
true_count++;
}
});
// Expect it to average around 300
expect(true_count).to.be.within(200, 400);
// Expect it to average around 300
expect(true_count).to.be.within(200, 400);
true_count = 0;
_(1000).times(function () {
if (chance.bool({likelihood: 99})) {
true_count++;
}
});
true_count = 0;
_(1000).times(function () {
if (chance.bool({likelihood: 99})) {
true_count++;
}
});
// Expect it to average at 990
expect(true_count).to.be.above(900);
});
// Expect it to average at 990
expect(true_count).to.be.above(900);
});
it("throws an error if likelihood < 0 or > 100", function () {
expect(function () { chance.bool({likelihood: -23}); }).to.throw(RangeError);
expect(function () { chance.bool({likelihood: 7933}); }).to.throw(RangeError);
});
});
it("throws an error if likelihood < 0 or > 100", function () {
expect(function () { chance.bool({likelihood: -23}); }).to.throw(RangeError);
expect(function () { chance.bool({likelihood: 7933}); }).to.throw(RangeError);
});
describe("Integer", function () {
it("returns a random integer", function () {
integer = chance.integer();
expect(integer).to.be.a('number');
});
describe("Integer", function () {
it("returns a random integer", function () {
it("is sometimes negative, sometimes positive", function () {
var positive_count = 0;
_(1000).times(function () {
integer = chance.integer();
expect(integer).to.be.a('number');
if (integer > 0) {
positive_count++;
}
});
it("is sometimes negative, sometimes positive", function () {
var positive_count = 0;
_(1000).times(function () {
integer = chance.integer();
if (integer > 0) {
positive_count++;
}
});
// Note: In very extreme circumstances this test may fail as, by its
// nature it's random. But it's a low enough percentage that I'm
// willing to accept it.
expect(positive_count).to.be.within(200, 800);
});
// Note: In very extreme circumstances this test may fail as, by its
// nature it's random. But it's a low enough percentage that I'm
// willing to accept it.
expect(positive_count).to.be.within(200, 800);
it("can take a zero min and obey it", function () {
_(1000).times(function () {
integer = chance.integer({min: 0});
expect(integer).to.be.above(0);
});
});
it("can take a zero min and obey it", function () {
_(1000).times(function () {
integer = chance.integer({min: 0});
expect(integer).to.be.above(0);
});
it("can take a negative min and obey it", function () {
_(1000).times(function () {
integer = chance.integer({min: -25});
expect(integer).to.be.above(-26);
});
});
it("can take a negative min and obey it", function () {
_(1000).times(function () {
integer = chance.integer({min: -25});
expect(integer).to.be.above(-26);
});
it("can take a negative min and negative max and obey both", function () {
_(1000).times(function () {
integer = chance.integer({min: -25, max: -1});
expect(integer).to.be.within(-26, 0);
});
});
it("can take a negative min and negative max and obey both", function () {
_(1000).times(function () {
integer = chance.integer({min: -25, max: -1});
expect(integer).to.be.within(-26, 0);
});
it("can take a min with absolute value less than the max and return in range above", function () {
var count = 0;
_(1000).times(function () {
// With a range this large we'd expect most values to be
// greater than 1 if this works correctly.
integer = chance.integer({min: -1, max: 1000000});
if (Math.abs(integer) < 2) {
count++;
}
});
expect(count).to.not.be.above(900);
});
it("can take a min with absolute value less than the max and return in range above", function () {
var count = 0;
_(1000).times(function () {
// With a range this large we'd expect most values to be
// greater than 1 if this works correctly.
integer = chance.integer({min: -1, max: 1000000});
if (Math.abs(integer) < 2) {
count++;
}
});
expect(count).to.not.be.above(900);
});
it("throws an error if min > max", function () {
expect(function () { chance.natural({min: 1000, max: 500}); }).to.throw(RangeError);
});
});
it("throws an error if min > max", function () {
expect(function () { chance.natural({min: 1000, max: 500}); }).to.throw(RangeError);
});
describe("Natural", function () {
it("returns a random natural", function () {
natural = chance.natural();
expect(natural).to.be.a('number');
});
describe("Natural", function () {
it("returns a random natural", function () {
it("throws an error if min < 0", function () {
expect(function () { chance.natural({min: -23}); }).to.throw(RangeError);
});
it("is always positive", function () {
var positive_count = 0;
_(1000).times(function () {
natural = chance.natural();
expect(natural).to.be.a('number');
if (natural > 0) {
positive_count++;
}
});
it("is always positive", function () {
var positive_count = 0;
_(1000).times(function () {
natural = chance.natural();
if (natural > 0) {
positive_count++;
}
});
expect(positive_count).to.equal(1000);
});
expect(positive_count).to.equal(1000);
it("can take just a min and obey it", function () {
_(1000).times(function () {
natural = chance.natural({min: 9007199254740991});
expect(natural).to.be.above(9007199254740990);
});
});
it("can take just a min and obey it", function () {
_(1000).times(function () {
natural = chance.natural({min: 9007199254740991});
expect(natural).to.be.above(9007199254740990);
});
it("can take just a max and obey it", function () {
_(1000).times(function () {
natural = chance.natural({max: 100});
expect(natural).to.be.below(101);
});
});
it("can take just a max and obey it", function () {
_(1000).times(function () {
natural = chance.natural({max: 100});
expect(natural).to.be.below(101);
});
it("can take both a max and min and obey them both", function () {
_(1000).times(function () {
natural = chance.natural({min: 90, max: 100});
expect(natural).to.be.within(89, 101);
});
});
it("can take both a max and min and obey them both", function () {
_(1000).times(function () {
natural = chance.natural({min: 90, max: 100});
expect(natural).to.be.within(89, 101);
});
it("works with both bounds 0", function () {
_(1000).times(function () {
natural = chance.natural({min: 0, max: 0});
expect(natural).to.equal(0);
});
});
it("works with both bounds 0", function () {
_(1000).times(function () {
natural = chance.natural({min: 0, max: 0});
expect(natural).to.equal(0);
});
});
it("throws an error if min > max", function () {
expect(function () { chance.natural({min: 1000, max: 500}); }).to.throw(RangeError);
});
});
it("throws an error if min > max", function () {
expect(function () { chance.natural({min: 1000, max: 500}); }).to.throw(RangeError);
describe("Floating", function () {
it("returns a random floating", function () {
floating = chance.floating();
expect(floating).to.be.a('number');
});
it("can take both a max and min and obey them both", function () {
_(1000).times(function () {
floating = chance.floating({min: 90, max: 100});
expect(floating).to.be.within(90, 100);
});
});
describe("Floating", function () {
it("returns a random floating", function () {
floating = chance.floating();
expect(floating).to.be.a('number');
it("won't take fixed + min that would be out of range", function () {
expect(function () { chance.floating({fixed: 13, min: -9007199254740992}); }).to.throw(RangeError);
});
it("won't take fixed + max that would be out of range", function () {
expect(function () { chance.floating({fixed: 13, max: 9007199254740992}); }).to.throw(RangeError);
});
it("obeys the fixed parameter, when present", function () {
_(100).times(function () {
floating = chance.floating({fixed: 4});
floating = floating.toString().split('.')[1] ? floating.toString().split('.')[1] : '';
expect(floating).to.have.length.below(5);
});
});
it("can take both a max and min and obey them both", function () {
_(1000).times(function () {
floating = chance.floating({min: 90, max: 100});
expect(floating).to.be.within(90, 100);
});
it("can take fixed and obey it", function () {
_(1000).times(function () {
floating = chance.floating({fixed: 3});
temp = parseFloat(floating.toFixed(3));
expect(floating).to.equal(temp);
});
});
it("won't take fixed + min that would be out of range", function () {
expect(function () { chance.floating({fixed: 13, min: -9007199254740992}); }).to.throw(RangeError);
it("won't take both fixed and precision", function () {
expect(function () { chance.floating({fixed: 2, precision: 8}); }).to.throw(RangeError);
});
});
describe("Character", function () {
it("returns a character", function () {
character = chance.character();
expect(character).to.be.a('string');
expect(character).to.have.length(1);
});
it("pulls only from pool, when specified", function () {
var pool = 'abcde';
_(1000).times(function () {
character = chance.character({pool: pool});
expect(character).to.match(/[abcde]/);
});
});
it("won't take fixed + max that would be out of range", function () {
expect(function () { chance.floating({fixed: 13, max: 9007199254740992}); }).to.throw(RangeError);
it("allows only alpha", function () {
_(1000).times(function () {
character = chance.character({alpha: true});
expect(character).to.match(/[a-zA-Z]/);
});
it("obeys the fixed parameter, when present", function () {
_(100).times(function () {
floating = chance.floating({fixed: 4});
floating = floating.toString().split('.')[1] ? floating.toString().split('.')[1] : '';
expect(floating).to.have.length.below(5);
});
expect(function () { chance.character({alpha: true, symbols: true}); }).to.throw(RangeError);
});
it("obeys case", function () {
_(1000).times(function () {
character = chance.character({alpha: true});
expect(character).to.match(/[a-zA-Z]/);
});
it("can take fixed and obey it", function () {
_(1000).times(function () {
floating = chance.floating({fixed: 3});
temp = parseFloat(floating.toFixed(3));
expect(floating).to.equal(temp);
});
_(1000).times(function () {
character = chance.character({alpha: true, casing: 'upper'});
expect(character).to.match(/[A-Z]/);
});
it("won't take both fixed and precision", function () {
expect(function () { chance.floating({fixed: 2, precision: 8}); }).to.throw(RangeError);
_(1000).times(function () {
character = chance.character({alpha: true, casing: 'lower'});
expect(character).to.match(/[a-z]/);
});
});
describe("Character", function () {
it("returns a character", function () {
character = chance.character();
expect(character).to.be.a('string');
expect(character).to.have.length(1);
});
});
it("pulls only from pool, when specified", function () {
var pool = 'abcde';
_(1000).times(function () {
character = chance.character({pool: pool});
expect(character).to.match(/[abcde]/);
});
describe("String", function () {
it("returns a string", function () {
string = chance.string();
expect(string).to.be.a('string');
});
it("obeys length, when specified", function () {
var length;
_(1000).times(function () {
// Generate a random length from 1-25
length = chance.natural({min: 1, max: 25});
string = chance.string({length: length});
expect(string).to.have.length(length);
});
});
it("allows only alpha", function () {
_(1000).times(function () {
character = chance.character({alpha: true});
expect(character).to.match(/[a-zA-Z]/);
});
it("throws an error if length < 0", function () {
expect(function () { chance.string({length: -23}); }).to.throw(RangeError);
});
expect(function () { chance.character({alpha: true, symbols: true}); }).to.throw(RangeError);
it("obeys case", function () {
_(1000).times(function () {
string = chance.string({alpha: true});
expect(string).to.match(/[a-zA-Z]+/);
});
it("obeys case", function () {
_(1000).times(function () {
character = chance.character({alpha: true});
expect(character).to.match(/[a-zA-Z]/);
});
_(1000).times(function () {
string = chance.string({alpha: true, casing: 'upper'});
expect(string).to.match(/[A-Z]+/);
});
_(1000).times(function () {
character = chance.character({alpha: true, casing: 'upper'});
expect(character).to.match(/[A-Z]/);
});
_(1000).times(function () {
character = chance.character({alpha: true, casing: 'lower'});
expect(character).to.match(/[a-z]/);
});
_(1000).times(function () {
string = chance.string({alpha: true, casing: 'lower'});
expect(string).to.match(/[a-z]+/);
});

@@ -270,94 +315,99 @@

describe("String", function () {
it("returns a string", function () {
string = chance.string();
expect(string).to.be.a('string');
it("obeys symbol", function () {
_(1000).times(function () {
string = chance.string({symbols: true});
expect(string).to.match(/[\!\@\#\$\%\^\&\*\(\)\[\]]+/);
});
});
it("obeys length, when specified", function () {
var length;
_(1000).times(function () {
// Generate a random length from 1-25
length = chance.natural({min: 1, max: 25});
string = chance.string({length: length});
expect(string).to.have.length(length);
});
});
});
});
});
describe("Seed", function () {
var seed, chance1, chance2;
describe("Seed", function () {
var seed, chance1, chance2;
describe("random", function () {
it("does return differing results if differing seeds provided", function (done) {
chance1 = new Chance(12345);
// Wait 5 ms before creating chance2 else sometimes they happen on the same
// tick and end up with the same seed!
setTimeout(function () {
chance2 = new Chance(54321);
expect(chance1.random()).to.not.equal(chance2.random());
done();
}, 5);
});
describe("null works", function () {
it("works with a null seed", function(done) {
chance1 = Chance(null);
it("does not return repeatable results if no seed provided", function (done) {
chance1 = new Chance();
// Wait 5 ms before creating chance2 else sometimes they happen on the same
// tick and end up with the same seed!
setTimeout(function () {
chance2 = new Chance();
_(1000).times(function () {
expect(chance1.random()).to.not.equal(chance2.random());
});
done();
}, 5);
});
// Wait 5 ms before creating chance2 else sometimes they happen on the same
// tick and end up with the same seed!
setTimeout(function () {
chance2 = Chance(null);
expect(chance1.random()).to.not.equal(chance2.random());
done();
}, 5);
});
});
it("returns repeatable results if seed provided on the Chance object", function () {
seed = new Date().getTime();
chance1 = new Chance(seed);
chance2 = new Chance(seed);
describe("random", function () {
it("does return differing results if differing seeds provided", function (done) {
chance1 = new Chance(12345);
// Wait 5 ms before creating chance2 else sometimes they happen on the same
// tick and end up with the same seed!
setTimeout(function () {
chance2 = new Chance(54321);
expect(chance1.random()).to.not.equal(chance2.random());
done();
}, 5);
});
it("does not return repeatable results if no seed provided", function (done) {
chance1 = new Chance();
// Wait 5 ms before creating chance2 else sometimes they happen on the same
// tick and end up with the same seed!
setTimeout(function () {
chance2 = new Chance();
_(1000).times(function () {
expect(chance1.random()).to.equal(chance2.random());
expect(chance1.random()).to.not.equal(chance2.random());
});
done();
}, 5);
});
it("returns repeatable results if seed provided on the Chance object", function () {
seed = new Date().getTime();
chance1 = new Chance(seed);
chance2 = new Chance(seed);
_(1000).times(function () {
expect(chance1.random()).to.equal(chance2.random());
});
});
it("returns repeatable results if a string is provided as a seed", function () {
seed = "foo";
chance1 = new Chance(seed);
chance2 = new Chance(seed);
it("returns repeatable results if a string is provided as a seed", function () {
seed = "foo";
chance1 = new Chance(seed);
chance2 = new Chance(seed);
_(1000).times(function () {
expect(chance1.random()).to.equal(chance2.random());
});
_(1000).times(function () {
expect(chance1.random()).to.equal(chance2.random());
});
});
it("returns different results if two different strings are provided", function () {
chance1 = new Chance("foo");
chance2 = new Chance("baz");
it("returns different results if two different strings are provided", function () {
chance1 = new Chance("foo");
chance2 = new Chance("baz");
_(1000).times(function () {
expect(chance1.random()).to.not.equal(chance2.random());
});
_(1000).times(function () {
expect(chance1.random()).to.not.equal(chance2.random());
});
});
it("returns different results if multiple arguments are provided", function () {
seed = new Date().getTime();
chance1 = new Chance(seed, "foo");
chance2 = new Chance(seed, "bar");
it("returns different results if multiple arguments are provided", function () {
seed = new Date().getTime();
chance1 = new Chance(seed, "foo");
chance2 = new Chance(seed, "bar");
_(1000).times(function () {
expect(chance1.random()).to.not.equal(chance2.random());
});
_(1000).times(function () {
expect(chance1.random()).to.not.equal(chance2.random());
});
});
});
describe("arbitrary function", function () {
it("will take an arbitrary function for random, use it", function () {
var chance = new Chance(function () { return 123; });
_(1000).times(function () {
expect(chance.random()).to.equal(123);
});
describe("arbitrary function", function () {
it("will take an arbitrary function for random, use it", function () {
var chance = new Chance(function () { return 123; });
_(1000).times(function () {
expect(chance.random()).to.equal(123);
});

@@ -364,0 +414,0 @@ });

@@ -1,148 +0,182 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Credit Card", function () {
var type, number, exp, month, year, chance = new Chance();
var expect = chai.expect;
describe("Luhn Check", function () {
it("checks if number passes Luhn algorithm", function () {
expect(chance.luhn_check(49927398716)).to.be.true;
expect(chance.luhn_check(1234567812345670)).to.be.true;
expect(chance.luhn_check(49927398717)).to.be.false;
expect(chance.luhn_check(1234567812345678)).to.be.false;
describe("Credit Card", function () {
var type, number, exp, month, year, chance = new Chance();
describe("Luhn Check", function () {
it("checks if number passes Luhn algorithm", function () {
expect(chance.luhn_check(49927398716)).to.be.true;
expect(chance.luhn_check(1234567812345670)).to.be.true;
expect(chance.luhn_check(49927398717)).to.be.false;
expect(chance.luhn_check(1234567812345678)).to.be.false;
});
});
describe("Types", function () {
it("cc_types() returns an array of credit card types", function () {
expect(chance.cc_types()).to.be.an('array');
});
it("cc_type() returns a random credit card type", function () {
_(1000).times(function () {
type = chance.cc_type();
expect(type).to.be.a("string");
});
});
describe("Types", function () {
it("cc_types() returns an array of credit card types", function () {
expect(chance.cc_types()).to.be.an('array');
it("cc_type() can take a raw arg and obey it", function () {
_(1000).times(function () {
type = chance.cc_type({ raw: true });
expect(type).to.have.property('name').with.a('string');
expect(type).to.have.property('short_name').with.a('string');
expect(type).to.have.property('prefix').with.a('string');
expect(type).to.have.property('length').with.a('number');
});
});
it("cc_type() returns a random credit card type", function () {
_(1000).times(function () {
type = chance.cc_type();
expect(type).to.be.a("string");
});
it("cc_type() can take a name arg and obey to it", function () {
_(1000).times(function () {
type = chance.cc_type({ name: "Visa", raw: true });
expect(type.name).to.equal("Visa");
});
});
it("cc_type() can take a raw arg and obey it", function () {
_(1000).times(function () {
type = chance.cc_type({ raw: true });
expect(type).to.have.property('name').with.a('string');
expect(type).to.have.property('short_name').with.a('string');
expect(type).to.have.property('prefix').with.a('string');
expect(type).to.have.property('length').with.a('number');
});
it("bogus cc_type() throws an error", function () {
expect(function() { chance.cc_type({ name: 'potato' }); }).to.throw(RangeError);
});
});
describe("Number", function () {
it("passes the Luhn algorithm", function () {
_(1000).times(function () {
number = chance.cc();
expect(chance.luhn_check(number)).to.equal(true);
});
});
it("cc_type() can take a name arg and obey to it", function () {
_(1000).times(function () {
type = chance.cc_type({ name: "Visa", raw: true });
expect(type.name).to.equal("Visa");
});
it("cc() can take a type arg and obey to it", function () {
_(1000).times(function () {
type = chance.cc_type({ raw: true });
number = chance.cc({ type: type.name });
expect(number).to.have.length(type.length);
});
});
describe("Number", function () {
it("cc() can take a short_name type arg and obey to it", function () {
_(1000).times(function () {
type = chance.cc_type({ raw: true });
number = chance.cc({ type: type.short_name });
expect(number).to.have.length(type.length);
});
});
});
it("passes the Luhn algorithm", function () {
_(1000).times(function () {
number = chance.cc();
expect(chance.luhn_check(number)).to.equal(true);
});
describe("Dollar", function () {
var dollar, chance = new Chance();
it("returns a proper dollar amount", function () {
_(1000).times(function () {
dollar = chance.dollar();
expect(dollar).to.match(/\$[0-9]+\.[0-9]+/);
dollar = parseFloat(dollar.substring(1, dollar.length));
expect(dollar).to.be.below(10001);
});
});
it("cc() can take a type arg and obey to it", function () {
_(1000).times(function () {
type = chance.cc_type({ raw: true });
number = chance.cc({ type: type.name });
expect(number).to.have.length(type.length);
});
it("obeys min and max", function () {
_(1000).times(function () {
dollar = chance.dollar({max: 20});
dollar = parseFloat(dollar.substring(1, dollar.length));
expect(dollar).to.not.be.above(20);
});
});
it("cc() can take a short_name type arg and obey to it", function () {
_(1000).times(function () {
type = chance.cc_type({ raw: true });
number = chance.cc({ type: type.short_name });
expect(number).to.have.length(type.length);
});
it("negative works", function () {
_(1000).times(function () {
dollar = chance.dollar({min: -200, max: -1});
expect(dollar.charAt(0)).to.equal('-');
});
});
});
describe("Dollar", function () {
var dollar, chance = new Chance();
describe("currency", function () {
var currency, chance = new Chance();
it("returns a proper dollar amount", function () {
_(1000).times(function () {
dollar = chance.dollar();
expect(dollar).to.match(/\$[0-9]+\.[0-9]+/);
dollar = parseFloat(dollar.substring(1, dollar.length));
expect(dollar).to.be.below(10001);
});
it("returns a currency", function () {
_(1000).times(function () {
currency = chance.currency();
expect(currency).to.be.an("object");
expect(currency.code).to.exist;
expect(currency.code.length).to.equal(3);
expect(currency.name).to.be.ok;
});
});
it("obeys min and max", function () {
_(1000).times(function () {
dollar = chance.dollar({max: 20});
dollar = parseFloat(dollar.substring(1, dollar.length));
expect(dollar).to.not.be.above(20);
});
it("returns a currency pair", function () {
_(1000).times(function () {
var currency_pair = chance.currency_pair();
expect(currency_pair).to.be.an("array");
expect(currency_pair.length).to.equal(2);
expect(currency_pair[0].code).to.not.equal(currency_pair[1].code);
});
});
});
describe("currency", function () {
var currency, chance = new Chance();
describe("currency_pair", function () {
var currency_pair, chance = new Chance();
it("returns a currency", function () {
_(1000).times(function () {
currency = chance.currency();
expect(currency).to.be.an("object");
expect(currency.code).to.exist;
expect(currency.code.length).to.equal(3);
expect(currency.name).to.be.ok;
});
it("returns a currency_pair", function () {
_(1000).times(function () {
currency_pair = chance.currency_pair();
expect(currency_pair).to.be.an("array");
expect(currency_pair.length).to.equal(2);
expect(currency_pair[0]).to.be.an("object");
});
});
it("returns a currency pair", function () {
_(1000).times(function () {
var currency_pair = chance.currency_pair();
expect(currency_pair).to.be.an("array");
expect(currency_pair.length).to.equal(2);
expect(currency_pair[0].code).to.not.equal(currency_pair[1].code);
});
it("returns a currency pair", function () {
_(1000).times(function () {
currency_pair = chance.currency_pair(true);
expect(currency_pair).to.be.a("string");
expect(currency_pair.length).to.equal(7);
expect(currency_pair).to.match(/^[A-Z][A-Z][A-Z]+\/[A-Z][A-Z][A-Z]$/);
});
});
});
describe("Expiration", function () {
it("exp() looks correct", function () {
_(1000).times(function () {
exp = chance.exp();
expect(exp).to.be.a('string');
expect(exp).to.have.length(7);
expect(exp).to.match(/([0-9]{2})\/([0-9]{4})/);
});
describe("Expiration", function () {
it("exp() looks correct", function () {
_(1000).times(function () {
exp = chance.exp();
expect(exp).to.be.a('string');
expect(exp).to.have.length(7);
expect(exp).to.match(/([0-9]{2})\/([0-9]{4})/);
});
});
it("exp() will respect object argument", function () {
_(1000).times(function () {
exp = chance.exp({raw: true});
expect(exp).to.be.an('object');
expect(exp).to.have.property('month').with.a('string');
expect(exp).to.have.property('year').with.a('string');
});
it("exp() will respect object argument", function () {
_(1000).times(function () {
exp = chance.exp({raw: true});
expect(exp).to.be.an('object');
expect(exp).to.have.property('month').with.a('string');
expect(exp).to.have.property('year').with.a('string');
});
});
it("exp_month() returns a numeric month with leading 0", function () {
_(1000).times(function () {
month = chance.exp_month();
expect(month).to.be.a('string');
expect(month).to.have.length(2);
});
it("exp_month() returns a numeric month with leading 0", function () {
_(1000).times(function () {
month = chance.exp_month();
expect(month).to.be.a('string');
expect(month).to.have.length(2);
});
});
it("exp_year() returns an expiration year", function () {
_(1000).times(function () {
year = chance.exp_year();
expect(year).to.be.within(new Date().getFullYear(), new Date().getFullYear() + 10);
});
it("exp_year() returns an expiration year", function () {
_(1000).times(function () {
year = chance.exp_year();
expect(year).to.be.within(new Date().getFullYear(), new Date().getFullYear() + 10);
});

@@ -149,0 +183,0 @@ });

@@ -1,287 +0,300 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Helpers", function () {
var word, arr, picked, chance = new Chance();
var expect = chai.expect;
describe("capitalize works as expected", function () {
it("returns a random name", function () {
_(1000).times(function () {
word = chance.capitalize(chance.word());
expect(word).to.be.a('string');
expect(word[0]).to.match(/[A-Z]/);
});
describe("Helpers", function () {
var word, arr, picked, chance = new Chance();
describe("capitalize works as expected", function () {
it("returns a random name", function () {
_(1000).times(function () {
word = chance.capitalize(chance.word());
expect(word).to.be.a('string');
expect(word[0]).to.match(/[A-Z]/);
});
});
});
describe("pick()", function () {
it("returns a single element when called without a count argument", function () {
arr = ['a', 'b', 'c', 'd'];
_(1000).times(function () {
picked = chance.pick(arr);
expect(picked).to.have.length(1);
});
describe("pick()", function () {
it("returns a single element when called without a count argument", function () {
arr = ['a', 'b', 'c', 'd'];
_(1000).times(function () {
picked = chance.pick(arr);
expect(picked).to.have.length(1);
});
});
it("returns multiple elements when called with a count argument", function () {
arr = ['a', 'b', 'c', 'd'];
_(1000).times(function () {
picked = chance.pick(arr, 3);
expect(picked).to.have.length(3);
});
it("returns multiple elements when called with a count argument", function () {
arr = ['a', 'b', 'c', 'd'];
_(1000).times(function () {
picked = chance.pick(arr, 3);
expect(picked).to.have.length(3);
});
});
it("doesn't destroy the original array when called with a count argument", function () {
arr = ['a', 'b', 'c', 'd', 'e', 'f'];
_(1000).times(function () {
picked = chance.pick(arr, 3);
expect(arr).to.have.length(6);
});
it("doesn't destroy the original array when called with a count argument", function () {
arr = ['a', 'b', 'c', 'd', 'e', 'f'];
_(1000).times(function () {
picked = chance.pick(arr, 3);
expect(arr).to.have.length(6);
});
});
it("throws good error if zero elements in array", function () {
arr = [];
expect(function () {
chance.pick(arr);
}).to.throw(RangeError, "Chance: Cannot pick() from an empty array");
it("throws good error if zero elements in array", function () {
arr = [];
expect(function () {
chance.pick(arr);
}).to.throw(RangeError, "Chance: Cannot pick() from an empty array");
});
});
describe("weighted()", function () {
this.timeout(10000);
it("returns an element", function () {
_(1000).times(function () {
picked = chance.weighted(['a', 'b', 'c', 'd'], [1, 1, 1, 1]);
expect(picked).to.be.a('string');
});
});
describe("weighted()", function () {
it("returns an element", function () {
_(1000).times(function () {
picked = chance.weighted(['a', 'b', 'c', 'd'], [1, 1, 1, 1]);
expect(picked).to.be.a('string');
});
it("works with just 2 items", function() {
// Use Math.random as the random function rather than our Mersenne twister just to
// speed things up here because this test takes awhile to gather enough data to
// have a large enough sample size to adequately test. This increases speed
// by a few orders of magnitude at the cost of repeatability (which we aren't using here)
var chance = new Chance(Math.random);
var picked = { a: 0, b: 0 };
// This makes it a tad slow, but we need a large enough sample size to adequately test
_(50000).times(function () {
picked[chance.weighted(['a', 'b'], [1, 100])]++;
});
it("works with just 2 items", function() {
// Use Math.random as the random function rather than our Mersenne twister just to
// speed things up here because this test takes awhile to gather enough data to
// have a large enough sample size to adequately test. This increases speed
// by a few orders of magnitude at the cost of repeatability (which we aren't using here)
var chance = new Chance(Math.random);
var picked = { a: 0, b: 0 };
// This range is somewhat arbitrary, but good enough to test our constraints
expect(picked.b / picked.a).to.be.within(80, 120);
picked = { a: 0, b: 0 };
// This makes it a tad slow, but we need a large enough sample size to adequately test
_(50000).times(function () {
picked[chance.weighted(['a', 'b'], [100, 1])]++;
});
// This range is somewhat arbitrary, but good enough to test our constraints
expect(picked.a / picked.b).to.be.within(80, 120);
});
it("throws an error if called with an array of weights with length different from options", function() {
expect(function () {
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3]);
}).to.throw(RangeError, /length of array and weights must match/);
expect(function () {
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4]);
}).to.not.throw(RangeError, /length of array and weights must match/);
expect(function () {
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4, 5]);
}).to.throw(RangeError, /length of array and weights must match/);
});
it("returns with results properly weighted", function() {
// Use Math.random as the random function rather than our Mersenne twister just to
// speed things up here because this test takes awhile to gather enough data to
// have a large enough sample size to adequately test. This increases performance
// by a few orders of magnitude
var chance = new Chance(Math.random);
_(10).times(function() {
var picked = { a: 0, b: 0, c: 0, d: 0 };
// This makes it a tad slow, but we need a large enough sample size to adequately test
_(100000).times(function () {
picked[chance.weighted(['a', 'b'], [1, 100])]++;
_(50000).times(function () {
picked[chance.weighted(['a', 'b', 'c', 'd'], [1, 100, 100, 1])]++;
});
// This range is somewhat arbitrary, but good enough to test our constraints
expect(picked.b / picked.a).to.be.within(80, 120);
expect(picked.b / picked.a).to.be.within(60, 140);
expect(picked.c / picked.d).to.be.within(60, 140);
expect((picked.c / picked.b) * 100).to.be.within(50, 150);
});
});
picked = { a: 0, b: 0 };
it("works with fractional weights", function() {
// Use Math.random as the random function rather than our Mersenne twister just to
// speed things up here because this test takes awhile to gather enough data to
// have a large enough sample size to adequately test. This increases performance
// by a few orders of magnitude
var chance = new Chance(Math.random);
_(10).times(function() {
var picked = { a: 0, b: 0, c: 0, d: 0 };
// This makes it a tad slow, but we need a large enough sample size to adequately test
_(100000).times(function () {
picked[chance.weighted(['a', 'b'], [100, 1])]++;
_(50000).times(function () {
picked[chance.weighted(['a', 'b', 'c', 'd'], [0.001, 0.1, 0.1, 0.001])]++;
});
// This range is somewhat arbitrary, but good enough to test our constraints
expect(picked.a / picked.b).to.be.within(80, 120);
expect(picked.b / picked.a).to.be.within(60, 140);
expect(picked.c / picked.d).to.be.within(60, 140);
expect((picked.c / picked.b) * 100).to.be.within(50, 150);
});
});
it("throws an error if called with an array of weights with length different from options", function() {
expect(function () {
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3]);
}).to.throw(RangeError, /length of array and weights must match/);
it("works with weights of 0", function () {
picked = chance.weighted(['a', 'b', 'c'], [1, 0, 1]);
expect(picked).to.be.a('string');
});
expect(function () {
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4]);
}).to.not.throw(RangeError, /length of array and weights must match/);
it("works with negative weights", function () {
picked = chance.weighted(['a', 'b', 'c'], [1, -2, 1]);
expect(picked).to.be.a('string');
});
});
expect(function () {
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4, 5]);
}).to.throw(RangeError, /length of array and weights must match/);
describe("shuffle()", function () {
it("returns an array of the same size", function () {
arr = ['a', 'b', 'c', 'd', 'e'];
_(1000).times(function () {
expect(chance.shuffle(_.clone(arr))).to.have.length(5);
expect(chance.shuffle(_.clone(arr))).to.contain('a');
var arr2 = _.clone(arr);
chance.shuffle(arr2);
expect(arr2).to.not.be.empty;
});
});
it("returns with results properly weighted", function() {
// Use Math.random as the random function rather than our Mersenne twister just to
// speed things up here because this test takes awhile to gather enough data to
// have a large enough sample size to adequately test. This increases performance
// by a few orders of magnitude
var chance = new Chance(Math.random);
_(10).times(function() {
var picked = { a: 0, b: 0, c: 0, d: 0 };
// This makes it a tad slow, but we need a large enough sample size to adequately test
_(100000).times(function () {
picked[chance.weighted(['a', 'b', 'c', 'd'], [1, 100, 100, 1])]++;
});
it("returns a well shuffled array", function () {
// See http://vq.io/1lEhbim checking it isn't doing that!
arr = ['a', 'b', 'c', 'd', 'e'];
var positions = {
a: [0, 0, 0, 0, 0],
b: [0, 0, 0, 0, 0],
c: [0, 0, 0, 0, 0],
d: [0, 0, 0, 0, 0],
e: [0, 0, 0, 0, 0]
};
// This range is somewhat arbitrary, but good enough to test our constraints
expect(picked.b / picked.a).to.be.within(80, 120);
expect(picked.c / picked.d).to.be.within(80, 120);
expect((picked.c / picked.b) * 100).to.be.within(50, 150);
_(10000).times(function () {
arr = chance.shuffle(arr);
// Accumulate the position of the a each time
arr.forEach(function(item, index) {
positions[item][index]++;
});
});
it("works with fractional weights", function() {
// Use Math.random as the random function rather than our Mersenne twister just to
// speed things up here because this test takes awhile to gather enough data to
// have a large enough sample size to adequately test. This increases performance
// by a few orders of magnitude
var chance = new Chance(Math.random);
_(10).times(function() {
var picked = { a: 0, b: 0, c: 0, d: 0 };
// This makes it a tad slow, but we need a large enough sample size to adequately test
_(100000).times(function () {
picked[chance.weighted(['a', 'b', 'c', 'd'], [0.001, 0.1, 0.1, 0.001])]++;
});
// This range is somewhat arbitrary, but good enough to test our constraints
expect(picked.b / picked.a).to.be.within(80, 120);
expect(picked.c / picked.d).to.be.within(80, 120);
expect((picked.c / picked.b) * 100).to.be.within(50, 150);
// Divide by the estimated number of a's which should appear in each
// position
_.forEach(positions, function(position, index) {
positions[index] = _.map(position, function(item) {
return item/10000;
});
});
it("works with weights of 0", function () {
picked = chance.weighted(['a', 'b', 'c'], [1, 0, 1]);
expect(picked).to.be.a('string');
_.forEach(positions, function(position) {
_.forEach(position, function(item) {
// This should be around 20% give or take a bit since there are
// 5 elements and they should be evenly distributed
expect(item).to.be.within(0.18, 0.22);
});
});
});
});
it("works with negative weights", function () {
picked = chance.weighted(['a', 'b', 'c'], [1, -2, 1]);
expect(picked).to.be.a('string');
describe("unique", function () {
it("gives a unique array of the selected function", function () {
_(500).times(function () {
var arr = chance.unique(chance.character, 25, {pool: "abcdefghijklmnopqrstuvwxyz"});
expect(arr).to.be.an('array');
expect(_.uniq(arr).length).to.equal(25);
});
});
describe("shuffle()", function () {
it("returns an array of the same size", function () {
arr = ['a', 'b', 'c', 'd', 'e'];
_(1000).times(function () {
expect(chance.shuffle(_.clone(arr))).to.have.length(5);
expect(chance.shuffle(_.clone(arr))).to.contain('a');
var arr2 = _.clone(arr);
chance.shuffle(arr2);
expect(arr2).to.not.be.empty;
});
});
it("throws a RangeError when num is likely out of range", function () {
expect(function () {
chance.unique(chance.character, 10, {pool: 'abcde'});
}).to.throw(RangeError, /too large/);
});
it("returns a well shuffled array", function () {
// See http://vq.io/1lEhbim checking it isn't doing that!
arr = ['a', 'b', 'c', 'd', 'e'];
var positions = {
a: [0, 0, 0, 0, 0],
b: [0, 0, 0, 0, 0],
c: [0, 0, 0, 0, 0],
d: [0, 0, 0, 0, 0],
e: [0, 0, 0, 0, 0]
};
it("throws a RangeError when first argument is not a function", function () {
expect(function () {
chance.unique(chance.character({pool: 'abcde'}), 10);
}).to.throw(RangeError, /first argument must be a function/);
});
_(10000).times(function () {
arr = chance.shuffle(arr);
it("will take a custom comparator for comparing complex objects", function () {
_(1000).times(function () {
var arr = chance.unique(chance.currency, 25, {
comparator: function(arr, val) {
// If this is the first element, we know it doesn't exist
if (arr.length === 0) {
return false;
}
// Accumulate the position of the a each time
arr.forEach(function(item, index) {
positions[item][index]++;
});
return arr.reduce(function(acc, item) {
// If a match has been found, short circuit check and just return
if (acc) {
return acc;
}
return item.name === val.name;
}, false);
}
});
// Divide by the estimated number of a's which should appear in each
// position
_.forEach(positions, function(position, index) {
positions[index] = _.map(position, function(item) {
return item/10000;
});
});
_.forEach(positions, function(position) {
_.forEach(position, function(item) {
// This should be around 20% give or take a bit since there are
// 5 elements and they should be evenly distributed
expect(item).to.be.within(0.18, 0.22);
});
});
expect(_.uniq(arr).length).to.equal(25);
});
});
});
describe("unique", function () {
it("gives a unique array of the selected function", function () {
_(1000).times(function () {
var arr = chance.unique(chance.email, 25, {domain: "example.com"});
expect(arr).to.be.an('array');
expect(arr[0]).to.match(/example\.com$/);
expect(_.uniq(arr).length).to.equal(25);
});
});
describe("n random terms", function () {
it("gives an array of n terms for the given function", function () {
var arr = chance.n(chance.email, 25, {domain: "example.com"});
expect(arr).to.be.an('array');
expect(arr[0]).to.match(/example\.com$/);
expect(arr.length).to.equal(25);
});
it("throws a RangeError when num is likely out of range", function () {
_(1000).times(function () {
expect(function () {
chance.unique(chance.character, 10, {pool: 'abcde'});
}).to.throw(RangeError, /too large/);
});
});
it("gives an array of 1 term when n is not given", function () {
var arr = chance.n(chance.email);
expect(arr).to.be.an('array');
expect(arr.length).to.equal(1);
});
it("will take a custom comparator for comparing complex objects", function () {
_(1000).times(function () {
var arr = chance.unique(chance.currency, 25, {
comparator: function(arr, val) {
// If this is the first element, we know it doesn't exist
if (arr.length === 0) {
return false;
}
it("throws a RangeError when first argument is not a function", function () {
expect(function () {
chance.n(chance.character({pool: 'abcde'}), 10);
}).to.throw(RangeError, /first argument must be a function/);
});
return arr.reduce(function(acc, item) {
// If a match has been found, short circuit check and just return
if (acc) {
return acc;
}
return item.name === val.name;
}, false);
}
});
expect(_.uniq(arr).length).to.equal(25);
});
});
it("gives an empty array when n is set to 0", function () {
var arr = chance.n(chance.email, 0);
expect(arr).to.be.an('array');
expect(arr.length).to.equal(0);
});
});
describe("n random terms", function () {
it("gives an array of n terms for the given function", function () {
var arr = chance.n(chance.email, 25, {domain: "example.com"});
expect(arr).to.be.an('array');
expect(arr[0]).to.match(/example\.com$/);
expect(arr.length).to.equal(25);
describe("pad()", function () {
it("always returns same number when width same as the length of the number", function () {
_(1000).times(function () {
var num = chance.natural({min: 10000, max: 99999});
expect(chance.pad(num, 5)).to.be.a('string');
expect(chance.pad(num, 5)).to.have.length(5);
});
it("gives an array of 1 term when n is not given", function () {
var arr = chance.n(chance.email);
expect(arr).to.be.an('array');
expect(arr.length).to.equal(1);
});
it("gives an empty array when n is set to 0", function () {
var arr = chance.n(chance.email, 0);
expect(arr).to.be.an('array');
expect(arr.length).to.equal(0);
});
});
describe("pad()", function () {
it("always returns same number when width same as the length of the number", function () {
_(1000).times(function () {
var num = chance.natural({min: 10000, max: 99999});
expect(chance.pad(num, 5)).to.be.a('string');
expect(chance.pad(num, 5)).to.have.length(5);
});
it("will pad a smaller number to the right width", function () {
_(1000).times(function () {
var num = chance.natural({max: 99999});
expect(chance.pad(num, 10)).to.be.a('string');
expect(chance.pad(num, 10)).to.have.length(10);
expect(chance.pad(num, 10).indexOf('00000')).to.not.equal(-1);
});
});
it("will pad a smaller number to the right width", function () {
_(1000).times(function () {
var num = chance.natural({max: 99999});
expect(chance.pad(num, 10)).to.be.a('string');
expect(chance.pad(num, 10)).to.have.length(10);
expect(chance.pad(num, 10).indexOf('00000')).to.not.equal(-1);
});
it("can specify pad element", function () {
_(1000).times(function () {
var num = chance.natural({max: 99999});
expect(chance.pad(num, 10, 'V')).to.be.a('string');
expect(chance.pad(num, 10, 'V')).to.have.length(10);
expect(chance.pad(num, 10, 'V').indexOf('VVVVV')).to.not.equal(-1);
});
it("can specify pad element", function () {
_(1000).times(function () {
var num = chance.natural({max: 99999});
expect(chance.pad(num, 10, 'V')).to.be.a('string');
expect(chance.pad(num, 10, 'V')).to.have.length(10);
expect(chance.pad(num, 10, 'V').indexOf('VVVVV')).to.not.equal(-1);
});
});
});
});
});
});

@@ -1,248 +0,295 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Dice", function () {
var die, dice, chance = new Chance();
var expect = chai.expect;
it("returns a properly bounded d4", function () {
_(1000).times(function () {
die = chance.d4();
expect(die).to.be.within(1, 4);
});
describe("Dice", function () {
var die, dice, chance = new Chance();
it("returns a properly bounded d4", function () {
_(1000).times(function () {
die = chance.d4();
expect(die).to.be.within(1, 4);
});
});
it("returns a properly bounded d6", function () {
_(1000).times(function () {
die = chance.d6();
expect(die).to.be.within(1, 6);
});
it("returns a properly bounded d6", function () {
_(1000).times(function () {
die = chance.d6();
expect(die).to.be.within(1, 6);
});
});
it("returns a properly bounded d8", function () {
_(1000).times(function () {
die = chance.d8();
expect(die).to.be.within(1, 8);
});
it("returns a properly bounded d8", function () {
_(1000).times(function () {
die = chance.d8();
expect(die).to.be.within(1, 8);
});
});
it("returns a properly bounded d10", function () {
_(1000).times(function () {
die = chance.d10();
expect(die).to.be.within(1, 10);
});
it("returns a properly bounded d10", function () {
_(1000).times(function () {
die = chance.d10();
expect(die).to.be.within(1, 10);
});
});
it("returns a properly bounded d12", function () {
_(1000).times(function () {
die = chance.d12();
expect(die).to.be.within(1, 12);
});
it("returns a properly bounded d12", function () {
_(1000).times(function () {
die = chance.d12();
expect(die).to.be.within(1, 12);
});
});
it("returns a properly bounded d20", function () {
_(1000).times(function () {
die = chance.d20();
expect(die).to.be.within(1, 20);
});
it("returns a properly bounded d20", function () {
_(1000).times(function () {
die = chance.d20();
expect(die).to.be.within(1, 20);
});
});
it("returns a properly bounded d30", function () {
_(1000).times(function () {
die = chance.d30();
expect(die).to.be.within(1, 30);
});
it("returns a properly bounded d30", function () {
_(1000).times(function () {
die = chance.d30();
expect(die).to.be.within(1, 30);
});
});
it("returns a properly bounded d100", function () {
_(1000).times(function () {
die = chance.d100();
expect(die).to.be.within(1, 100);
});
it("returns a properly bounded d100", function () {
_(1000).times(function () {
die = chance.d100();
expect(die).to.be.within(1, 100);
});
});
it("rpg() appears to work as expected", function () {
_(1000).times(function () {
expect(function () { chance.rpg(); }).to.throw(Error);
expect(function () { chance.rpg("3"); }).to.throw(Error);
expect(function () { chance.rpg("hd23"); }).to.throw(Error);
expect(function () { chance.rpg("3d23d2"); }).to.throw(Error);
dice = chance.rpg('5d20');
expect(dice).to.be.an.array;
expect(dice).to.have.length(5);
_.map(dice, function (die) {
expect(die).to.be.within(1, 20);
});
it("rpg() appears to work as expected", function () {
_(1000).times(function () {
expect(function () { chance.rpg(); }).to.throw(Error);
expect(function () { chance.rpg("3"); }).to.throw(Error);
expect(function () { chance.rpg("hd23"); }).to.throw(Error);
expect(function () { chance.rpg("3d23d2"); }).to.throw(Error);
dice = chance.rpg('5d20');
expect(dice).to.be.an.array;
expect(dice).to.have.length(5);
_.map(dice, function (die) {
expect(die).to.be.within(1, 20);
});
});
});
it("rpg() will take and obey a sum", function () {
var rpg = 0;
_(1000).times(function () {
rpg = chance.rpg('4d20', {sum: true});
expect(rpg).to.be.a.number;
expect(rpg).to.be.within(4, 80);
});
it("rpg() will take and obey a sum", function () {
var rpg = 0;
_(1000).times(function () {
rpg = chance.rpg('4d20', {sum: true});
expect(rpg).to.be.a.number;
expect(rpg).to.be.within(4, 80);
});
});
describe("Mac Address", function () {
var mac, chance = new Chance();
it("rpg without a die roll throws an error", function () {
expect(function() { chance.rpg(); }).to.throw(RangeError);
});
});
it("returns a proper mac address", function () {
_(1000).times(function () {
mac = chance.mac_address();
expect(mac).to.match(/([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}/);
});
describe("tv and radio", function() {
it("radio works", function() {
var radio;
_(1000).times(function () {
radio = chance.radio();
expect(radio).to.be.a.string;
expect(radio).to.have.length(4);
expect(radio).to.match(/^[KW][A-Z][A-Z][A-Z]/);
});
});
it("returns a proper colon separated mac address", function () {
_(1000).times(function () {
mac = chance.mac_address({separator: ":"});
expect(mac).to.match(/([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}/);
});
it("radio accepts east", function() {
var radio;
_(1000).times(function () {
radio = chance.radio({ side: 'east' });
expect(radio).to.be.a.string;
expect(radio).to.have.length(4);
expect(radio).to.match(/^W[A-Z][A-Z][A-Z]/);
});
});
it("returns a proper hyphen separated mac address", function () {
_(1000).times(function () {
mac = chance.mac_address({separator:"-"});
expect(mac).to.match(/([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}/);
});
it("radio accepts west", function() {
var radio;
_(1000).times(function () {
radio = chance.radio({ side: 'west' });
expect(radio).to.be.a.string;
expect(radio).to.have.length(4);
expect(radio).to.match(/^K[A-Z][A-Z][A-Z]/);
});
});
it("returns a proper network version mac address", function () {
_(1000).times(function () {
mac = chance.mac_address({networkVersion:true});
expect(mac).to.match(/([0-9a-fA-F]){4}.([0-9a-fA-F]){4}.([0-9a-fA-F]){4}/);
});
it("tv works", function() {
var tv;
_(1000).times(function () {
tv = chance.tv();
expect(tv).to.be.a.string;
expect(tv).to.have.length(4);
expect(tv).to.match(/^[KW][A-Z][A-Z][A-Z]/);
});
});
});
describe("Guid", function () {
var guid, chance = new Chance();
describe("Mac Address", function () {
var mac, chance = new Chance();
it("returns a proper guid", function () {
_(1000).times(function () {
guid = chance.guid();
expect(guid).to.match(/([0-9a-fA-F]){8}(-([0-9a-fA-F]){4}){3}-([0-9a-fA-F]){12}/);
});
it("returns a proper mac address", function () {
_(1000).times(function () {
mac = chance.mac_address();
expect(mac).to.match(/([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}/);
});
});
it("returns a proper version 1 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 1});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-1([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
it("returns a proper colon separated mac address", function () {
_(1000).times(function () {
mac = chance.mac_address({separator: ":"});
expect(mac).to.match(/([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}/);
});
});
it("returns a proper version 2 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 2});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-2([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
it("returns a proper hyphen separated mac address", function () {
_(1000).times(function () {
mac = chance.mac_address({separator:"-"});
expect(mac).to.match(/([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}/);
});
});
it("returns a proper version 3 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 3});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-3([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
it("returns a proper network version mac address", function () {
_(1000).times(function () {
mac = chance.mac_address({networkVersion:true});
expect(mac).to.match(/([0-9a-fA-F]){4}.([0-9a-fA-F]){4}.([0-9a-fA-F]){4}/);
});
});
it("returns a proper version 4 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 4});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-4([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
});
describe("Guid", function () {
var guid, chance = new Chance();
it("returns a proper guid", function () {
_(1000).times(function () {
guid = chance.guid();
expect(guid).to.match(/([0-9a-fA-F]){8}(-([0-9a-fA-F]){4}){3}-([0-9a-fA-F]){12}/);
});
});
it("returns a proper version 5 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 5});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-5([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
it("returns a proper version 1 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 1});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-1([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
});
describe("Hash", function () {
var hash, length, chance = new Chance();
it("returns a proper version 2 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 2});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-2([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
});
it("returns a proper hash", function () {
_(1000).times(function () {
hash = chance.hash();
expect(hash).to.match(/([0-9a-f]){40}/);
expect(hash).to.have.length(40);
});
it("returns a proper version 3 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 3});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-3([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
});
it("obeys length, if supplied", function () {
_(1000).times(function () {
length = chance.natural({min: 1, max: 64});
hash = chance.hash({length: length});
expect(hash).to.have.length(length);
});
it("returns a proper version 4 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 4});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-4([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
});
describe("Mixin", function () {
var chance = new Chance();
it("exists", function () {
expect(chance).to.have.property('mixin');
it("returns a proper version 5 guid", function () {
_(1000).times(function () {
guid = chance.guid({version: 5});
expect(guid).to.match(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-5([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/);
});
});
});
it("works with a simple function", function () {
chance.mixin({
'user': function () {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
};
}
});
expect(chance).to.have.property('user');
_(1000).times(function () {
expect(chance.user()).to.be.ok;
expect(chance.user()).to.have.property('first');
});
describe("Hash", function () {
var hash, length, chance = new Chance();
it("returns a proper hash", function () {
_(1000).times(function () {
hash = chance.hash();
expect(hash).to.match(/([0-9a-f]){40}/);
expect(hash).to.have.length(40);
});
});
it("multiple mixins work, can call previously defined mixins", function () {
chance.mixin({
'user': function () {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
};
},
'social_user': function () {
var user = chance.user();
user.network = chance.pick(['facebook', 'twitter']);
return user;
}
});
expect(chance).to.have.property('social_user');
_(1000).times(function () {
expect(chance.social_user()).to.be.ok;
expect(chance.social_user()).to.have.property('first');
expect(chance.social_user()).to.have.property('network');
});
it("obeys length, if supplied", function () {
_(1000).times(function () {
length = chance.natural({min: 1, max: 64});
hash = chance.hash({length: length});
expect(hash).to.have.length(length);
});
});
});
describe("CNPJ", function () {
var chance = new Chance();
var cnpj;
describe("Mixin", function () {
var chance = new Chance();
it("exists", function () {
expect(chance).to.have.property('mixin');
});
it("returns a valid Brazil company ID (CNPJ)", function () {
_(1000).times(function () {
cnpj = chance.cnpj();
expect(cnpj).to.be.a('string');
expect(cnpj).to.match(/^\d{2}.\d{3}.\d{3}\/\d{4}-\d{2}$/m);
expect(cnpj).to.have.length(18);
});
it("works with a simple function", function () {
chance.mixin({
'user': function () {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
};
}
});
expect(chance).to.have.property('user');
_(1000).times(function () {
expect(chance.user()).to.be.ok;
expect(chance.user()).to.have.property('first');
});
});
it("multiple mixins work, can call previously defined mixins", function () {
chance.mixin({
'user': function () {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
};
},
'social_user': function () {
var user = chance.user();
user.network = chance.pick(['facebook', 'twitter']);
return user;
}
});
expect(chance).to.have.property('social_user');
_(1000).times(function () {
expect(chance.social_user()).to.be.ok;
expect(chance.social_user()).to.have.property('first');
expect(chance.social_user()).to.have.property('network');
});
});
});
describe("CNPJ", function () {
var chance = new Chance();
var cnpj;
it("returns a valid Brazil company ID (CNPJ)", function () {
_(1000).times(function () {
cnpj = chance.cnpj();
expect(cnpj).to.be.a('string');
expect(cnpj).to.match(/^\d{2}.\d{3}.\d{3}\/\d{4}-\d{2}$/m);
expect(cnpj).to.have.length(18);
});
});
});

@@ -1,58 +0,60 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Android Registration ID", function(){
var android_id, chance = new Chance();
var expect = chai.expect;
it("returns a proper android id", function () {
_(1000).times(function () {
android_id = chance.android_id();
expect(android_id).to.match(/APA91([0-9a-zA-Z-_]){178}/);
});
describe("Android Registration ID", function(){
this.timeout(8000);
var android_id, chance = new Chance();
it("returns a proper android id", function () {
_(1000).times(function () {
android_id = chance.android_id();
expect(android_id).to.match(/APA91([0-9a-zA-Z-_]){178}/);
});
});
});
describe("Apple Token", function(){
var apple_token, chance = new Chance();
describe("Apple Token", function(){
var apple_token, chance = new Chance();
it("returns a proper apple token", function () {
_(1000).times(function () {
apple_token = chance.apple_token();
expect(apple_token).to.match(/([0-9a-fA-F]){64}/);
});
it("returns a proper apple token", function () {
_(1000).times(function () {
apple_token = chance.apple_token();
expect(apple_token).to.match(/([0-9a-fA-F]){64}/);
});
});
});
describe("Windows Phone 8 ANID2", function(){
var wp8_anid2, chance = new Chance();
describe("Windows Phone 8 ANID2", function(){
var wp8_anid2, chance = new Chance();
it("returns a proper windows phone 8 anid2", function () {
_(1000).times(function () {
wp8_anid2 = chance.wp8_anid2();
expect(wp8_anid2).to.match(/^([0-9a-zA-Z]){43}=$/);
});
it("returns a proper windows phone 8 anid2", function () {
_(1000).times(function () {
wp8_anid2 = chance.wp8_anid2();
expect(wp8_anid2).to.match(/^([0-9a-zA-Z]){43}=$/);
});
});
});
describe("Windows Phone 7 ANID", function(){
var wp7_anid, chance = new Chance();
describe("Windows Phone 7 ANID", function(){
var wp7_anid, chance = new Chance();
it("returns a proper windows phone 7 anid", function () {
_(1000).times(function () {
wp7_anid = chance.wp7_anid();
expect(wp7_anid).to.match(/^A=[0-9A-F]{32}&E=[0-9a-f]{3}&W=\d$/);
});
it("returns a proper windows phone 7 anid", function () {
_(1000).times(function () {
wp7_anid = chance.wp7_anid();
expect(wp7_anid).to.match(/^A=[0-9A-F]{32}&E=[0-9a-f]{3}&W=\d$/);
});
});
});
describe("BlackBerry Device PIN", function(){
var bb_pin, chance = new Chance();
describe("BlackBerry Device PIN", function(){
var bb_pin, chance = new Chance();
it("returns a proper blackberry pin", function () {
_(1000).times(function () {
bb_pin = chance.bb_pin();
expect(bb_pin).to.match(/([0-9a-f]){8}/);
});
it("returns a proper blackberry pin", function () {
_(1000).times(function () {
bb_pin = chance.bb_pin();
expect(bb_pin).to.match(/([0-9a-f]){8}/);
});
});
});

@@ -1,47 +0,49 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Person", function () {
var name, first, last, prefix, suffix, ssn, chance = new Chance();
var expect = chai.expect;
describe("age()", function () {
it("returns a random age within expected bounds", function () {
_(1000).times(function () {
expect(chance.age()).to.be.a('number');
expect(chance.age()).to.be.within(1, 120);
});
});
describe("Person", function () {
var name, first, last, prefix, suffix, ssn, chance = new Chance();
it("can have the type specified", function () {
_(1000).times(function () {
expect(chance.age({type: 'child'})).to.be.within(1, 13);
expect(chance.age({type: 'senior'})).to.be.within(65, 120);
});
describe("age()", function () {
it("returns a random age within expected bounds", function () {
_(1000).times(function () {
expect(chance.age()).to.be.a('number');
expect(chance.age()).to.be.within(1, 100);
});
});
describe("birthday()", function () {
it("returns a random birthday", function () {
describe("ranges", function() {
var age;
it("child", function () {
_(1000).times(function () {
expect(chance.birthday()).to.be.a('Date');
expect(chance.birthday().getFullYear()).to.be.within((new Date().getFullYear() - 120), (new Date().getFullYear()));
age = chance.age({ type: 'child' });
expect(age).to.be.a('number');
expect(age).to.be.within(1, 12);
});
});
it("can have a string returned", function () {
it("teen", function () {
_(1000).times(function () {
expect(chance.birthday({string: true})).to.be.a('string');
expect(chance.birthday({string: true})).to.match(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m);
age = chance.age({ type: 'teen' });
expect(age).to.be.a('number');
expect(age).to.be.within(13, 19);
});
});
it("can have a year specified", function () {
it("adult", function () {
_(1000).times(function () {
expect(chance.birthday({year: 1983}).getFullYear()).to.equal(1983);
age = chance.age({ type: 'adult' });
expect(age).to.be.a('number');
expect(age).to.be.within(18, 65);
});
});
it("can have an age range specified", function () {
it("senior", function () {
_(1000).times(function () {
expect(chance.birthday({type: 'child'}).getFullYear()).to.be.within((new Date().getFullYear() - 13), (new Date().getFullYear()));
age = chance.age({ type: 'senior' });
expect(age).to.be.a('number');
expect(age).to.be.within(65, 100);
});

@@ -51,140 +53,244 @@ });

describe("gender()", function () {
it("returns a random gender", function () {
_(1000).times(function () {
expect(chance.gender()).to.be.match(/(Male|Female)/);
});
it("can have the type specified", function () {
_(1000).times(function () {
expect(chance.age({type: 'child'})).to.be.within(1, 13);
expect(chance.age({type: 'senior'})).to.be.within(65, 120);
});
});
});
describe("name()", function () {
it("returns a random name", function () {
_(1000).times(function () {
name = chance.name();
expect(name).to.be.a('string');
expect(name).to.have.length.within(2, 30);
expect(name.split(' ')).to.have.length(2);
});
describe("birthday()", function () {
it("returns a random birthday", function () {
_(1000).times(function () {
expect(chance.birthday()).to.be.a('Date');
expect(chance.birthday().getFullYear()).to.be.within((new Date().getFullYear() - 120), (new Date().getFullYear()));
});
});
it("can have the middle name specified", function () {
_(1000).times(function () {
name = chance.name({middle: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
});
it("can have a string returned", function () {
_(1000).times(function () {
expect(chance.birthday({string: true})).to.be.a('string');
expect(chance.birthday({string: true})).to.match(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m);
});
});
it("can have the middle initial specified", function () {
_(1000).times(function () {
name = chance.name({middle_initial: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
expect(name.split(' ')[1]).to.match(/[A-Z]\./);
expect(name.indexOf('.')).to.be.ok;
});
it("can have a year specified", function () {
_(1000).times(function () {
expect(chance.birthday({year: 1983}).getFullYear()).to.equal(1983);
});
it("can have the prefix specified", function () {
_(1000).times(function () {
name = chance.name({prefix: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
});
});
it("can have an age range specified", function () {
_(1000).times(function () {
expect(chance.birthday({type: 'child'}).getFullYear()).to.be.within((new Date().getFullYear() - 13), (new Date().getFullYear()));
});
});
});
describe("gender()", function () {
it("returns a random gender", function () {
_(1000).times(function () {
expect(chance.gender()).to.be.match(/(Male|Female)/);
});
});
});
describe("first()", function () {
it("returns a random first name", function () {
_(1000).times(function () {
first = chance.first();
expect(first).to.be.a('string');
expect(first).to.have.length.within(2, 20);
expect(first.split(' ')).to.have.length(1);
});
describe("name()", function () {
it("returns a random name", function () {
_(1000).times(function () {
name = chance.name();
expect(name).to.be.a('string');
expect(name).to.have.length.within(2, 30);
expect(name.split(' ')).to.have.length(2);
});
});
it("can have the middle name specified", function () {
_(1000).times(function () {
name = chance.name({middle: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
});
});
describe("last()", function () {
it("returns a random last name", function () {
_(1000).times(function () {
last = chance.last();
expect(last).to.be.a('string');
expect(last).to.have.length.within(2, 20);
expect(last.split(' ')).to.have.length(1);
});
it("can have the middle initial specified", function () {
_(1000).times(function () {
name = chance.name({middle_initial: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
expect(name.split(' ')[1]).to.match(/[A-Z]\./);
expect(name.indexOf('.')).to.be.ok;
});
});
it("can have the prefix specified", function () {
_(1000).times(function () {
name = chance.name({prefix: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
});
});
describe("name_prefix()", function () {
it("returns a random prefix", function () {
_(1000).times(function () {
prefix = chance.name_prefix();
expect(prefix).to.be.a('string');
expect(prefix).to.have.length.below(5);
});
it("can have the suffix specified", function () {
_(1000).times(function () {
name = chance.name({suffix: true});
expect(name).to.be.a('string');
expect(name.split(' ')).to.have.length(3);
});
});
});
it("returns a correctly gendered prefix", function () {
_(1000).times(function () {
prefix = chance.name_prefix({ gender: "female" });
expect(prefix).to.not.equal("Mr.");
prefix = chance.name_prefix({ gender: "male" });
expect(prefix).to.not.equal("Mrs.");
expect(prefix).to.not.equal("Miss");
});
describe("first()", function () {
it("returns a random first name", function () {
_(1000).times(function () {
first = chance.first();
expect(first).to.be.a('string');
expect(first).to.have.length.within(2, 20);
expect(first.split(' ')).to.have.length(1);
});
});
});
it("can get full prefix", function () {
_(1000).times(function () {
prefix = chance.name_prefix({full: true});
expect(prefix).to.be.a('string');
expect(prefix).to.have.length.above(3);
});
describe("last()", function () {
it("returns a random last name", function () {
_(1000).times(function () {
last = chance.last();
expect(last).to.be.a('string');
expect(last).to.have.length.within(2, 20);
expect(last.split(' ')).to.have.length(1);
});
});
describe("name_suffix()", function () {
it("returns a random suffix", function () {
_(1000).times(function () {
suffix = chance.name_suffix();
expect(suffix).to.be.a('string');
expect(suffix).to.have.length.below(7);
});
});
describe("name_prefix()", function () {
it("returns a random prefix", function () {
_(1000).times(function () {
prefix = chance.name_prefix();
expect(prefix).to.be.a('string');
expect(prefix).to.have.length.below(5);
});
});
it("can get full suffix", function () {
_(1000).times(function () {
suffix = chance.name_suffix({full: true});
expect(suffix).to.be.a('string');
expect(suffix).to.have.length.above(5);
});
it("returns a correctly gendered prefix", function () {
_(1000).times(function () {
prefix = chance.name_prefix({ gender: "female" });
expect(prefix).to.not.equal("Mr.");
prefix = chance.name_prefix({ gender: "male" });
expect(prefix).to.not.equal("Mrs.");
expect(prefix).to.not.equal("Miss");
});
});
describe("ssn()", function () {
it("returns a random socal security number", function () {
_(1000).times(function () {
ssn = chance.ssn();
expect(ssn).to.be.a('string');
expect(ssn).to.match(/^\d{3}-\d{2}-\d{4}$/m);
expect(ssn).to.have.length(11);
});
it("can get full prefix", function () {
_(1000).times(function () {
prefix = chance.name_prefix({full: true});
expect(prefix).to.be.a('string');
expect(prefix).to.have.length.above(3);
});
});
});
describe("cpf()", function () {
var cpf;
it("returns a random valid taxpayer number for Brazil citizens (CPF)", function () {
_(1000).times(function () {
cpf = chance.cpf();
expect(cpf).to.be.a('string');
expect(cpf).to.match(/^\d{3}.\d{3}.\d{3}-\d{2}$/m);
expect(cpf).to.have.length(14);
describe("name_suffix()", function () {
it("returns a random suffix", function () {
_(1000).times(function () {
suffix = chance.name_suffix();
expect(suffix).to.be.a('string');
expect(suffix).to.have.length.below(7);
});
});
it("can get full suffix", function () {
_(1000).times(function () {
suffix = chance.name_suffix({full: true});
expect(suffix).to.be.a('string');
expect(suffix).to.have.length.above(5);
});
});
});
describe("suffix()", function () {
it("returns a random suffix", function () {
_(1000).times(function () {
suffix = chance.suffix();
expect(suffix).to.be.a('string');
expect(suffix).to.have.length.below(7);
});
});
it("can get full suffix", function () {
_(1000).times(function () {
suffix = chance.suffix({full: true});
expect(suffix).to.be.a('string');
expect(suffix).to.have.length.above(5);
});
});
});
describe("ssn()", function () {
it("returns a random socal security number", function () {
_(1000).times(function () {
ssn = chance.ssn();
expect(ssn).to.be.a('string');
expect(ssn).to.match(/^\d{3}-\d{2}-\d{4}$/m);
expect(ssn).to.have.length(11);
});
});
it("can get just last 4", function () {
_(1000).times(function () {
ssn = chance.ssn({ ssnFour: true });
expect(ssn).to.be.a('string');
expect(ssn).to.match(/^\d{4}$/m);
expect(ssn).to.have.length(4);
});
});
});
describe("cpf()", function () {
var cpf;
it("returns a random valid taxpayer number for Brazil citizens (CPF)", function () {
_(1000).times(function () {
cpf = chance.cpf();
expect(cpf).to.be.a('string');
expect(cpf).to.match(/^\d{3}.\d{3}.\d{3}-\d{2}$/m);
expect(cpf).to.have.length(14);
});
});
});
describe('mrz()', function() {
it('should return a valid passport number when given valid inputs', function() {
var sample = "P<GBRFOLKS<<JOANNE<<<<<<<<<<<<<<<<<<<<<<<<<<2321126135GBR6902069F1601013<<<<<<<<<<<<<<02",
actual = chance.mrz({
first: 'Joanne',
last: 'Folks',
gender: 'F',
dob: '690206',
expiry: '160101',
passportNumber: '232112613',
});
expect(actual).to.equal(sample);
sample = "P<GBRKELLY<<LIDA<<<<<<<<<<<<<<<<<<<<<<<<<<<<3071365913GBR6606068F2003131<<<<<<<<<<<<<<04";
actual = chance.mrz({
first: 'Lida',
last: 'Kelly',
gender: 'F',
dob: '660606',
expiry: '200313',
passportNumber: '307136591',
});
expect(actual).to.equal(sample);
});
it('should return a valid random passport number when not given any inputs', function() {
var actual = chance.mrz();
expect(actual).to.be.a.string;
expect(actual.substr(0, 5)).to.equal('P<GBR');
expect(actual).to.have.length(88);
expect(actual.substr(44)).to.match(/^[A-Z0-9<]{9}[0-9]{1}[A-Z]{3}[0-9]{7}[A-Z]{1}[0-9]{7}[A-Z0-9<]{14}[0-9]{2}$/);
});
});
});

@@ -1,96 +0,96 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Text", function () {
var syllable, word, sentence, paragraph, chance = new Chance();
var expect = chai.expect;
describe("Syllable", function () {
it("returns a random syllable", function () {
_(1000).times(function () {
syllable = chance.syllable();
expect(syllable).to.be.a('string');
expect(syllable).to.have.length.within(2, 3);
});
describe("Text", function () {
var syllable, word, sentence, paragraph, chance = new Chance();
describe("Syllable", function () {
it("returns a random syllable", function () {
_(1000).times(function () {
syllable = chance.syllable();
expect(syllable).to.be.a('string');
expect(syllable).to.have.length.within(2, 3);
});
});
});
describe("Word", function () {
it("returns a random word", function () {
_(1000).times(function () {
word = chance.word();
expect(word).to.be.a('string');
expect(syllable).to.have.length.within(2, 9);
});
describe("Word", function () {
it("returns a random word", function () {
_(1000).times(function () {
word = chance.word();
expect(word).to.be.a('string');
expect(syllable).to.have.length.within(2, 9);
});
});
it("can have a set number of syllables", function () {
_(1000).times(function () {
word = chance.word({syllables: 3});
expect(word).to.be.a('string');
expect(word).to.have.length.within(6, 9);
});
it("can have a set number of syllables", function () {
_(1000).times(function () {
word = chance.word({syllables: 3});
expect(word).to.be.a('string');
expect(word).to.have.length.within(6, 9);
});
});
it("can have a set length", function () {
_(1000).times(function () {
word = chance.word({length: 5});
expect(word).to.be.a('string');
expect(word).to.have.length(5);
});
it("can have a set length", function () {
_(1000).times(function () {
word = chance.word({length: 5});
expect(word).to.be.a('string');
expect(word).to.have.length(5);
});
});
it("throws an exception if both syllables and length specified", function () {
_(1000).times(function () {
// This is a bit ugly, but in essence Chai needs to receive
// a function here to work as it puts it in its own
// try/catch block. So we have to create an anonymous
// function to pass along for it to do that.
expect(function () { chance.word({syllables: 3, length: 20 }); }).to.throw(RangeError);
});
it("throws an exception if both syllables and length specified", function () {
_(1000).times(function () {
// This is a bit ugly, but in essence Chai needs to receive
// a function here to work as it puts it in its own
// try/catch block. So we have to create an anonymous
// function to pass along for it to do that.
expect(function () { chance.word({syllables: 3, length: 20 }); }).to.throw(RangeError);
});
});
});
describe("Sentence", function () {
it("returns a random sentence", function () {
_(1000).times(function () {
sentence = chance.sentence();
expect(sentence).to.be.a('string');
expect(sentence.split(' ')).to.have.length.within(12, 18);
});
describe("Sentence", function () {
it("returns a random sentence", function () {
_(1000).times(function () {
sentence = chance.sentence();
expect(sentence).to.be.a('string');
expect(sentence.split(' ')).to.have.length.within(12, 18);
});
});
it("will obey bounds", function () {
_(1000).times(function () {
sentence = chance.sentence({words: 5});
expect(sentence).to.be.a('string');
expect(sentence.split(' ')).to.have.length(5);
});
it("will obey bounds", function () {
_(1000).times(function () {
sentence = chance.sentence({words: 5});
expect(sentence).to.be.a('string');
expect(sentence.split(' ')).to.have.length(5);
});
});
});
describe("Paragraph", function () {
it("returns a random paragraph", function () {
_(1000).times(function () {
paragraph = chance.paragraph();
expect(paragraph).to.be.a('string');
// Have to account for the fact that the period at the end will add
// to the count of sentences. This is the fault of our hackish way
// of detecting sentences -- by splitting on '.'
expect(paragraph.split('.')).to.have.length.within(3, 8);
});
describe("Paragraph", function () {
it("returns a random paragraph", function () {
_(100).times(function () {
paragraph = chance.paragraph();
expect(paragraph).to.be.a('string');
// Have to account for the fact that the period at the end will add
// to the count of sentences. This is the fault of our hackish way
// of detecting sentences -- by splitting on '.'
expect(paragraph.split('.')).to.have.length.within(3, 8);
});
});
it("will obey bounds", function () {
_(1000).times(function () {
paragraph = chance.paragraph({sentences: 5});
expect(paragraph).to.be.a('string');
// Have to account for the fact that the period at the end will add
// to the count of sentences. This is the fault of our hackish way
// of detecting sentences -- by splitting on '.'
expect(paragraph.split('.')).to.have.length(6);
});
it("will obey bounds", function () {
_(100).times(function () {
paragraph = chance.paragraph({sentences: 5});
expect(paragraph).to.be.a('string');
// Have to account for the fact that the period at the end will add
// to the count of sentences. This is the fault of our hackish way
// of detecting sentences -- by splitting on '.'
expect(paragraph.split('.')).to.have.length(6);
});
});
});
});

@@ -1,222 +0,250 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Time", function () {
var date, hour, minute, timestamp, month, year, bounds, chance = new Chance();
var expect = chai.expect;
it("date() returns a date", function () {
_(1000).times(function () {
date = chance.date();
expect(date).to.be.a('Date');
expect(date.getTime()).to.be.ok;
});
describe("Time", function () {
var ampm, date, hammertime, hour, minute, timestamp, month, year, bounds, chance = new Chance();
it("ampm() returns am or pm", function () {
_(1000).times(function () {
ampm = chance.ampm();
expect(ampm).to.be.a('string');
expect(ampm).to.match(/^([ap]m)$/m);
});
});
it("date() can have some defaults provided and obeys them", function () {
_(1000).times(function () {
date = chance.date({year: 1983});
expect(date).to.be.a('Date');
expect(date.getFullYear()).to.equal(1983);
it("date() returns a date", function () {
_(1000).times(function () {
date = chance.date();
expect(date).to.be.a('Date');
expect(date.getTime()).to.be.ok;
});
});
date = chance.date({month: 0});
expect(date).to.be.a('Date');
expect(date.getMonth()).to.equal(0);
it("date() accepts an american option", function () {
_(1000).times(function () {
date = chance.date({ american: true });
expect(date).to.be.a('Date');
expect(date.getTime()).to.be.ok;
});
});
date = chance.date({day: 21});
expect(date).to.be.a('Date');
expect(date.getDate()).to.equal(21);
});
it("hammertime() works", function () {
_(1000).times(function () {
hammertime = chance.hammertime();
expect(hammertime).to.be.a('number');
expect(hammertime).to.be.within(1, 8640000000000000);
});
});
it("date() returns a date, can specify min and max", function () {
_(1000).times(function () {
bounds = {min: new Date(), max: new Date(new Date().getTime() + 1234567890123)};
date = chance.date(bounds);
expect(date).to.be.a('Date');
expect(date).to.be.within(bounds.min, bounds.max);
});
it("date() can have some defaults provided and obeys them", function () {
_(1000).times(function () {
// One of each month type in terms of number of days.
var month = [0, 1, 3][Math.floor(Math.random() * 3)];
date = chance.date({year: 1983});
expect(date).to.be.a('Date');
expect(date.getFullYear()).to.equal(1983);
date = chance.date({month: month});
expect(date).to.be.a('Date');
expect(date.getMonth()).to.equal(month);
date = chance.date({day: 21});
expect(date).to.be.a('Date');
expect(date.getDate()).to.equal(21);
});
});
it("date() returns a date, can specify just min", function () {
_(1000).times(function () {
bounds = {min: new Date()};
date = chance.date(bounds);
expect(date).to.be.a('Date');
expect(date).to.be.above(bounds.min);
});
it("date() returns a date, can specify min and max", function () {
_(1000).times(function () {
bounds = {min: new Date(), max: new Date(new Date().getTime() + 1234567890123)};
date = chance.date(bounds);
expect(date).to.be.a('Date');
expect(date).to.be.within(bounds.min, bounds.max);
});
});
it("date() returns a date, can specify just max", function () {
_(1000).times(function () {
bounds = {max: new Date()};
date = chance.date(bounds);
expect(date).to.be.below(bounds.max);
// Ensure date not negative. Perhaps add BCE/AD and such later,
// but for now just positive is good enough.
expect(date).to.be.above(0);
});
it("date() returns a date, can specify just min", function () {
_(1000).times(function () {
bounds = {min: new Date()};
date = chance.date(bounds);
expect(date).to.be.a('Date');
expect(date).to.be.above(bounds.min);
});
});
it("date() can return a string date", function () {
_(1000).times(function () {
expect(chance.date({string: true})).to.be.a('string');
expect(chance.date({string: true})).to.match(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m);
});
it("date() returns a date, can specify just max", function () {
_(1000).times(function () {
bounds = {max: new Date()};
date = chance.date(bounds);
expect(date).to.be.below(bounds.max);
// Ensure date not negative. Perhaps add BCE/AD and such later,
// but for now just positive is good enough.
expect(date).to.be.above(0);
});
});
it("hour() returns a hour", function () {
_(1000).times(function () {
hour = chance.hour();
expect(hour).to.be.a('number');
expect(hour).to.be.within(1, 12);
});
it("date() can return a string date", function () {
_(1000).times(function () {
expect(chance.date({string: true})).to.be.a('string');
expect(chance.date({string: true})).to.match(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m);
});
});
it("hour() returns a hour in 24 hour format", function () {
_(1000).times(function () {
hour = chance.hour({twentyfour: true});
expect(hour).to.be.a('number');
expect(hour).to.be.within(1, 24);
});
it("hour() returns a hour", function () {
_(1000).times(function () {
hour = chance.hour();
expect(hour).to.be.a('number');
expect(hour).to.be.within(1, 12);
});
});
it("hour() returns a hour, can specify min and max", function () {
_(1000).times(function () {
hour = chance.hour({min: 7, max: 10});
expect(hour).to.be.a('number');
expect(hour).to.be.within(7, 10);
});
it("hour() returns a hour in 24 hour format", function () {
_(1000).times(function () {
hour = chance.hour({twentyfour: true});
expect(hour).to.be.a('number');
expect(hour).to.be.within(1, 24);
});
});
it("hour() returns a hour, can specify just min", function () {
_(1000).times(function () {
hour = chance.hour({min: 5});
expect(hour).to.be.a('number');
expect(hour).to.be.within(5, 12);
});
it("hour() returns a hour, can specify min and max", function () {
_(1000).times(function () {
hour = chance.hour({min: 7, max: 10});
expect(hour).to.be.a('number');
expect(hour).to.be.within(7, 10);
});
});
it("hour() returns a hour, can specify just max", function () {
_(1000).times(function () {
hour = chance.hour({max: 7});
expect(hour).to.be.a('number');
expect(hour).to.be.within(1, 7);
});
it("hour() returns a hour, can specify just min", function () {
_(1000).times(function () {
hour = chance.hour({min: 5});
expect(hour).to.be.a('number');
expect(hour).to.be.within(5, 12);
});
});
it("minute() returns a minute", function () {
_(1000).times(function () {
minute = chance.minute();
expect(minute).to.be.a('number');
expect(minute).to.be.within(0, 59);
});
it("hour() returns a hour, can specify just max", function () {
_(1000).times(function () {
hour = chance.hour({max: 7});
expect(hour).to.be.a('number');
expect(hour).to.be.within(1, 7);
});
});
it("minute() returns a minute, can specify min and max", function () {
_(1000).times(function () {
minute = chance.minute({min: 18, max: 35});
expect(minute).to.be.a('number');
expect(minute).to.be.within(18, 35);
});
it("minute() returns a minute", function () {
_(1000).times(function () {
minute = chance.minute();
expect(minute).to.be.a('number');
expect(minute).to.be.within(0, 59);
});
});
it("minute() returns a minute, can specify just min", function () {
_(1000).times(function () {
minute = chance.minute({min: 5});
expect(minute).to.be.a('number');
expect(minute).to.be.within(5, 59);
});
it("minute() returns a minute, can specify min and max", function () {
_(1000).times(function () {
minute = chance.minute({min: 18, max: 35});
expect(minute).to.be.a('number');
expect(minute).to.be.within(18, 35);
});
});
it("minute() returns a minute, can specify just max", function () {
_(1000).times(function () {
minute = chance.minute({max: 32});
expect(minute).to.be.a('number');
expect(minute).to.be.within(0, 32);
});
it("minute() returns a minute, can specify just min", function () {
_(1000).times(function () {
minute = chance.minute({min: 5});
expect(minute).to.be.a('number');
expect(minute).to.be.within(5, 59);
});
});
it("month() returns a month", function () {
_(1000).times(function () {
month = chance.month();
expect(month).to.be.a('string');
});
it("minute() returns a minute, can specify just max", function () {
_(1000).times(function () {
minute = chance.minute({max: 32});
expect(minute).to.be.a('number');
expect(minute).to.be.within(0, 32);
});
});
it("month() will return a raw month", function () {
_(1000).times(function () {
month = chance.month({raw: true});
expect(month).to.not.be.a('string');
expect(month).to.be.an('object');
});
it("month() returns a month", function () {
_(1000).times(function () {
month = chance.month();
expect(month).to.be.a('string');
});
});
it("month() returns a month, can specify min and max", function () {
_(1000).times(function () {
month = chance.month({raw: true, min: 5, max: 10});
expect(month).to.not.be.a('string');
expect(month.numeric).to.be.within(5, 10);
});
it("month() will return a raw month", function () {
_(1000).times(function () {
month = chance.month({raw: true});
expect(month).to.not.be.a('string');
expect(month).to.be.an('object');
});
});
it("month() returns a month, can specify just min", function () {
_(1000).times(function () {
month = chance.month({raw: true, min: 5});
expect(month).to.not.be.a('string');
expect(month.numeric).to.be.within(5, 12);
});
it("month() returns a month, can specify min and max", function () {
_(1000).times(function () {
month = chance.month({raw: true, min: 5, max: 10});
expect(month).to.not.be.a('string');
expect(month.numeric).to.be.within(5, 10);
});
});
it("month() returns a month, can specify just max", function () {
_(1000).times(function () {
month = chance.month({raw: true, max: 7});
expect(month).to.not.be.a('string');
expect(month.numeric).to.be.within(1, 7);
});
it("month() returns a month, can specify just min", function () {
_(1000).times(function () {
month = chance.month({raw: true, min: 5});
expect(month).to.not.be.a('string');
expect(month.numeric).to.be.within(5, 12);
});
});
it("month() returns a month, can specify just max", function () {
_(1000).times(function () {
month = chance.month({raw: true, max: 7});
expect(month).to.not.be.a('string');
expect(month.numeric).to.be.within(1, 7);
});
});
it("timestamp() returns a timestamp", function () {
_(1000).times(function () {
timestamp = chance.timestamp();
expect(timestamp).to.be.a('number');
expect(timestamp).to.be.within(1, parseInt(new Date().getTime() / 1000, 10));
});
it("timestamp() returns a timestamp", function () {
_(1000).times(function () {
timestamp = chance.timestamp();
expect(timestamp).to.be.a('number');
expect(timestamp).to.be.within(1, parseInt(new Date().getTime() / 1000, 10));
});
});
it("year() returns a year, default between today and a century after", function () {
_(1000).times(function () {
year = chance.year();
expect(year).to.be.a('string');
expect(year).to.be.within(new Date().getFullYear(), new Date().getFullYear() + 100);
});
it("year() returns a year, default between today and a century after", function () {
_(1000).times(function () {
year = chance.year();
expect(year).to.be.a('string');
expect(year).to.be.within(new Date().getFullYear(), new Date().getFullYear() + 100);
});
});
it("year() returns a year, can specify min and max", function () {
_(1000).times(function () {
year = chance.year({min: 2500, max: 2600});
expect(year).to.be.a('string');
expect(year).to.be.within(2500, 2600);
});
it("year() returns a year, can specify min and max", function () {
_(1000).times(function () {
year = chance.year({min: 2500, max: 2600});
expect(year).to.be.a('string');
expect(year).to.be.within(2500, 2600);
});
});
it("year() returns a year, can specify just min", function () {
_(1000).times(function () {
year = chance.year({min: 2500});
expect(year).to.be.a('string');
expect(year).to.be.above(2499);
});
it("year() returns a year, can specify just min", function () {
_(1000).times(function () {
year = chance.year({min: 2500});
expect(year).to.be.a('string');
expect(year).to.be.above(2499);
});
});
it("year() returns a year, can specify just max", function () {
_(1000).times(function () {
year = chance.year({max: 2500});
expect(year).to.be.below(2501);
// Ensure year not negative. Perhaps add BCE/AD and such later,
// but for now just positive is good enough.
expect(year).to.be.above(0);
});
it("year() returns a year, can specify just max", function () {
_(1000).times(function () {
year = chance.year({max: 2500});
expect(year).to.be.below(2501);
// Ensure year not negative. Perhaps add BCE/AD and such later,
// but for now just positive is good enough.
expect(year).to.be.above(0);
});
});
});
});

@@ -1,237 +0,285 @@

define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) {
var expect = chai.expect;
/// <reference path="../chance.js" />
/// <reference path="../node_modules/underscore/underscore-min.js" />
describe("Web", function () {
var tld, domain, email, hashtag, ip, ipv6, tracking_code, twitter, url, chance = new Chance();
var expect = chai.expect;
it("tld() returns a tld", function () {
_(1000).times(function () {
tld = chance.tld();
expect(tld).to.be.a('string');
expect(tld).to.have.length.below(6);
});
describe("Web", function () {
var tld, domain, email, hashtag, ip, ipv6, tracking_code, twitter, url, chance = new Chance();
it("tld() returns a tld", function () {
_(1000).times(function () {
tld = chance.tld();
expect(tld).to.be.a('string');
expect(tld).to.have.length.below(6);
});
});
it("domain() returns a domain", function () {
_(1000).times(function () {
domain = chance.domain();
expect(domain).to.be.a('string');
expect(domain.split('.')).to.have.length.above(1);
});
it("domain() returns a domain", function () {
_(1000).times(function () {
domain = chance.domain();
expect(domain).to.be.a('string');
expect(domain.split('.')).to.have.length.above(1);
});
});
it("domain() obeys tld, if specified", function () {
_(1000).times(function () {
domain = chance.domain({tld: 'com'});
expect(domain).to.be.a('string');
expect(domain.split('.')[1]).to.equal('com');
});
it("domain() obeys tld, if specified", function () {
_(1000).times(function () {
domain = chance.domain({tld: 'com'});
expect(domain).to.be.a('string');
expect(domain.split('.')[1]).to.equal('com');
});
});
it("email() returns what looks like an email", function () {
it("email() returns what looks like an email", function () {
_(1000).times(function () {
email = chance.email();
expect(email).to.be.a('string');
expect(email.split('@')).to.have.length.above(1);
});
});
it("email() obeys domain, if specified", function () {
_(1000).times(function () {
email = chance.email({domain: 'example.com'});
expect(email).to.be.a('string');
expect(email.split('@')[1]).to.equal('example.com');
});
});
it("email() has a length specified, it should generate a string before the domain with an equal length", function () {
_(1000).times(function () {
email = chance.email({length: 5});
expect(email.split('@')[0].length).to.equal(5);
});
});
it("fbid() returns what looks like a Facebook id", function () {
_(1000).times(function () {
expect(chance.fbid()).to.be.a('number');
});
});
it("google_analytics() returns what looks like a valid tracking code", function () {
_(1000).times(function () {
tracking_code = chance.google_analytics();
expect(tracking_code).to.be.a('string');
expect(tracking_code).to.have.length(12);
expect(tracking_code.indexOf('UA-')).to.not.equal(-1);
});
});
it("hashtag() returns what looks like a hashtag", function () {
_(1000).times(function () {
hashtag = chance.hashtag();
expect(hashtag).to.be.a('string');
expect(hashtag).to.match(/^\#\w+$/m);
});
});
it("ip() returns what looks like an IP address", function () {
_(1000).times(function () {
ip = chance.ip();
expect(ip).to.be.a('string');
expect(ip.split('.')).to.have.length(4);
});
});
it("ipv6() returns what looks like an IP address (v6)", function () {
_(1000).times(function () {
ipv6 = chance.ipv6();
expect(ipv6).to.be.a('string');
expect(ipv6.split(':')).to.have.length(8);
});
});
it("klout() returns what looks like a legit Klout score", function () {
_(1000).times(function () {
expect(chance.klout()).to.be.a('number');
expect(chance.klout()).to.be.within(1, 100);
});
});
it("twitter() returns what looks like a Twitter handle", function () {
_(1000).times(function () {
twitter = chance.twitter();
expect(twitter).to.be.a('string');
expect(twitter).to.match(/\@[A-Za-z]+/m);
});
});
describe('color', function () {
it("({format: 'hex'}) returns what looks a hex color", function () {
_(1000).times(function () {
email = chance.email();
expect(email).to.be.a('string');
expect(email.split('@')).to.have.length.above(1);
var color = chance.color({format: 'hex'});
expect(color).to.be.a('string');
expect(color).to.have.length(7);
expect(color).to.match(/#[a-z0-9]+/m);
});
});
it("email() obeys domain, if specified", function () {
it("({format: 'hex', grayScale: true}) returns what looks a gray scale hex color", function () {
_(1000).times(function () {
email = chance.email({domain: 'example.com'});
expect(email).to.be.a('string');
expect(email.split('@')[1]).to.equal('example.com');
var color = chance.color({format: 'hex', grayscale: true});
expect(color).to.be.a('string');
expect(color).to.have.length(7);
expect(color).to.match(/#[a-z0-9]+/m);
expect(color.slice(1, 3)).to.equal(color.slice(3, 5));
expect(color.slice(1, 3)).to.equal(color.slice(5, 7));
});
});
it("email() has a length specified, it should generate a string before the domain with an equal length", function () {
it("({format: 'shorthex'}) returns what looks a short hex color", function () {
_(1000).times(function () {
email = chance.email({length: 5});
expect(email.split('@')[0].length).to.equal(5);
var color = chance.color({format: 'shorthex'});
expect(color).to.be.a('string');
expect(color).to.have.length(4);
expect(color).to.match(/#[a-z0-9]+/m);
});
});
it("fbid() returns what looks like a Facebook id", function () {
it("({format: 'shorthex', grayScale: true}) returns what looks a gray scale short hex color", function () {
_(1000).times(function () {
expect(chance.fbid()).to.be.a('number');
var color = chance.color({format: 'shorthex', grayscale: true});
expect(color).to.be.a('string');
expect(color).to.have.length(4);
expect(color).to.match(/#[a-z0-9]+/m);
expect(color.slice(1, 2)).to.equal(color.slice(2, 3));
expect(color.slice(1, 2)).to.equal(color.slice(3, 4));
});
});
it("google_analytics() returns what looks like a valid tracking code", function () {
it("({format: 'rgb'}) returns what looks a rgb color", function () {
_(1000).times(function () {
tracking_code = chance.google_analytics();
expect(tracking_code).to.be.a('string');
expect(tracking_code).to.have.length(12);
expect(tracking_code.indexOf('UA-')).to.not.equal(-1);
var color = chance.color({format: 'rgb'});
expect(color).to.be.a('string');
var matchColors = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
var match = matchColors.exec(color);
expect(match).to.have.length.of(4);
expect(match[1]).to.be.within(0, 255);
expect(match[2]).to.be.within(0, 255);
expect(match[3]).to.be.within(0, 255);
});
});
it("hashtag() returns what looks like a hashtag", function () {
it("({format: 'rgb', grayScale: true}) returns what looks a gray scale rgb color", function () {
_(1000).times(function () {
hashtag = chance.hashtag();
expect(hashtag).to.be.a('string');
expect(hashtag).to.match(/^\#\w+$/m);
var color = chance.color({format: 'rgb', grayscale: true});
expect(color).to.be.a('string');
var matchColors = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
var match = matchColors.exec(color);
expect(match).to.have.length.of(4);
expect(match[1]).to.be.within(0, 255);
expect(match[1]).to.equal(match[2]);
expect(match[1]).to.equal(match[3]);
});
});
it("ip() returns what looks like an IP address", function () {
it("({format: 'rgba'}) returns what looks a rgba color", function () {
_(1000).times(function () {
ip = chance.ip();
expect(ip).to.be.a('string');
expect(ip.split('.')).to.have.length(4);
var color = chance.color({format: 'rgba'});
expect(color).to.be.a('string');
var matchColors = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]\.?\d*?)\)/;
var match = matchColors.exec(color);
expect(match).to.have.length.of(5);
expect(match[1]).to.be.within(0, 255);
expect(match[2]).to.be.within(0, 255);
expect(match[3]).to.be.within(0, 255);
expect(match[4]).to.be.within(0, 1);
});
});
it("ipv6() returns what looks like an IP address (v6)", function () {
it("({format: 'rgba', grayScale: true}) returns what looks a gray scale rgba color", function () {
_(1000).times(function () {
ipv6 = chance.ipv6();
expect(ipv6).to.be.a('string');
expect(ipv6.split(':')).to.have.length(8);
var color = chance.color({format: 'rgba', grayscale: true});
expect(color).to.be.a('string');
var matchColors = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]\.?\d*?)\)/;
var match = matchColors.exec(color);
expect(match).to.have.length.of(5);
expect(match[1]).to.be.within(0, 255);
expect(match[1]).to.equal(match[2]);
expect(match[1]).to.equal(match[3]);
expect(match[4]).to.be.within(0, 1);
});
});
it("klout() returns what looks like a legit Klout score", function () {
it("({format: '0x'}) returns what looks a 0x color", function () {
_(1000).times(function () {
expect(chance.klout()).to.be.a('number');
expect(chance.klout()).to.be.within(1, 100);
var color = chance.color({format: '0x'});
expect(color).to.be.a('string');
expect(color).to.have.length(8);
expect(color).to.match(/0x[a-z0-9]+/m);
});
});
it("twitter() returns what looks like a Twitter handle", function () {
it("({casing: upper}) returns upper cased color", function () {
_(1000).times(function () {
twitter = chance.twitter();
expect(twitter).to.be.a('string');
expect(twitter).to.match(/\@[A-Za-z]+/m);
var color = chance.color({format: 'hex', casing: 'upper'});
console.log(color);
expect(color).to.be.a('string');
expect(color).to.have.length(7);
expect(color.charAt(1).toUpperCase()).to.equal(color.charAt(1));
expect(color.charAt(2).toUpperCase()).to.equal(color.charAt(2));
expect(color.charAt(3).toUpperCase()).to.equal(color.charAt(3));
expect(color.charAt(4).toUpperCase()).to.equal(color.charAt(4));
expect(color.charAt(5).toUpperCase()).to.equal(color.charAt(5));
expect(color.charAt(6).toUpperCase()).to.equal(color.charAt(6));
});
});
describe('color', function () {
it("bogus format throws an error", function () {
expect(function() { chance.color({ format: "banana" }); }).to.throw(RangeError);
});
});
it("({format: 'hex'}) returns what looks a hex color", function () {
_(1000).times(function () {
var color = chance.color({format: 'hex'});
expect(color).to.be.a('string');
expect(color).to.have.length(7);
expect(color).to.match(/#[a-z0-9]+/m);
});
describe("url()", function() {
it("returns a legit url", function () {
_(1000).times(function () {
url = chance.url();
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
});
});
it("({format: 'hex', grayScale: true}) returns what looks a gray scale hex color", function () {
_(1000).times(function () {
var color = chance.color({format: 'hex', grayscale: true});
expect(color).to.be.a('string');
expect(color).to.have.length(7);
expect(color).to.match(/#[a-z0-9]+/m);
expect(color.slice(1, 3)).to.equal(color.slice(3, 5));
expect(color.slice(1, 3)).to.equal(color.slice(5, 7));
});
it("can take and respect a domain", function () {
_(1000).times(function () {
url = chance.url({ domain: "socialradar.com" });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.split('socialradar.com')).to.have.length.above(1);
});
});
it("({format: 'shorthex'}) returns what looks a short hex color", function () {
_(1000).times(function () {
var color = chance.color({format: 'shorthex'});
expect(color).to.be.a('string');
expect(color).to.have.length(4);
expect(color).to.match(/#[a-z0-9]+/m);
});
it("can take and respect a domain prefix", function () {
_(1000).times(function () {
url = chance.url({ domain_prefix: "www" });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.split('www.')).to.have.length.above(1);
});
});
it("({format: 'shorthex', grayScale: true}) returns what looks a gray scale short hex color", function () {
_(1000).times(function () {
var color = chance.color({format: 'shorthex', grayscale: true});
expect(color).to.be.a('string');
expect(color).to.have.length(4);
expect(color).to.match(/#[a-z0-9]+/m);
expect(color.slice(1, 2)).to.equal(color.slice(2, 3));
expect(color.slice(1, 2)).to.equal(color.slice(3, 4));
});
it("can take and respect a path", function () {
_(1000).times(function () {
url = chance.url({ path: "images" });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.split('/images')).to.have.length.above(1);
});
it("({format: 'rgb'}) returns what looks a rgb color", function () {
_(1000).times(function () {
var color = chance.color({format: 'rgb'});
expect(color).to.be.a('string');
var matchColors = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
var match = matchColors.exec(color);
expect(match).to.have.length.of(4);
expect(match[1]).to.be.within(0, 255);
expect(match[2]).to.be.within(0, 255);
expect(match[3]).to.be.within(0, 255);
});
});
it("({format: 'rgb', grayScale: true}) returns what looks a gray scale rgb color", function () {
_(1000).times(function () {
var color = chance.color({format: 'rgb', grayscale: true});
expect(color).to.be.a('string');
var matchColors = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
var match = matchColors.exec(color);
expect(match).to.have.length.of(4);
expect(match[1]).to.be.within(0, 255);
expect(match[1]).to.equal(match[2]);
expect(match[1]).to.equal(match[3]);
});
});
it("({format: '0x'}) returns what looks a 0x color", function () {
_(1000).times(function () {
var color = chance.color({format: '0x'});
expect(color).to.be.a('string');
expect(color).to.have.length(8);
expect(color).to.match(/0x[a-z0-9]+/m);
});
});
});
describe("url()", function() {
it("returns a legit url", function () {
_(1000).times(function () {
url = chance.url();
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
});
it("can take and respect extensions", function () {
_(1000).times(function () {
url = chance.url({ extensions: ["html"] });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.indexOf('.html')).to.not.equal(-1);
});
it("can take and respect a domain", function () {
_(1000).times(function () {
url = chance.url({ domain: "socialradar.com" });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.split('socialradar.com')).to.have.length.above(1);
});
});
it("can take and respect a domain prefix", function () {
_(1000).times(function () {
url = chance.url({ domain_prefix: "www" });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.split('www.')).to.have.length.above(1);
});
});
it("can take and respect a path", function () {
_(1000).times(function () {
url = chance.url({ path: "images" });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.split('/images')).to.have.length.above(1);
});
});
it("can take and respect extensions", function () {
_(1000).times(function () {
url = chance.url({ extensions: ["html"] });
expect(url).to.be.a('string');
expect(url.split('.')).to.have.length.above(1);
expect(url.split('://')).to.have.length.above(1);
expect(url.indexOf('.html')).to.not.equal(-1);
});
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc