test-phony.coffee | |
---|---|
A Test Suite for Phony | |
| |
This (mocha) test script both demonstrates and validates the use of Phony. | |
The tests are written using the should.js assertion framework. | should = require('should') |
Setup | |
This test script can be run in a handful of different contexts, so lets configure things accordingly. | |
| HOME_DIR = "#{__dirname}/.." |
| IS_COFFEE = process.argv[0].indexOf('coffee') >= 0 |
| IS_INSTRUMENTED = (require('path')).existsSync("#{HOME_DIR}/lib-cov") |
| LIB_DIR = if IS_INSTRUMENTED then "#{HOME_DIR}/lib-cov" else "#{HOME_DIR}/lib" |
| FILE_SUFFIX = if IS_COFFEE then '.coffee' else '.js' |
| PHONY = "#{LIB_DIR}/phony#{FILE_SUFFIX}" |
Tests | describe "Phony", -> |
To use Phony, we simply | @phony = require(PHONY).make_phony() |
Phony can be used to generate fake data of several different types. | |
Generating Phony Names | it "generates phony names", (done)-> |
Phony can generate realistic-looking person names. | |
By default these are randomly generated from the most popular names in the United States. | |
| phony.name().should.match /^([A-Z][a-z]+)+ ([A-Z]\'?[a-z]+)+$/ |
You can also generate just first names and last names in isolation: | phony.first_name().should.match /^([A-Z][a-z]+)+$/
phony.surname().should.match /^([A-Z]\'?[a-z]+)+$/ |
Or male or female names specifically: | phony.male_name().should.match /^([A-Z][a-z]+)+ ([A-Z]\'?[a-z]+)+$/
phony.female_name().should.match /^([A-Z][a-z]+)+ ([A-Z]\'?[a-z]+)+$/
phony.male_first_name().should.match /^([A-Z][a-z]+)+$/
phony.female_first_name().should.match /^([A-Z][a-z]+)+$/ |
By default, the | phony.name().should.be.a 'string' |
But you can also obtain an | pair = phony.name(return:'array')
pair.should.be.an.instanceof Array
pair[0].should.match /^([A-Z][a-z]+)+$/
pair[1].should.match /^([A-Z](\'[A-Z])?[a-z]+)+$/ |
Or a map ( | map = phony.name(return:'map')
map.should.be.an.instanceof Object
map.first_name.should.match /^([A-Z][a-z]+)+$/
map.surname.should.match /^([A-Z](\'[A-Z])?[a-z]+)+$/ |
This also works for | phony.male_name(return:'string').should.be.a 'string'
phony.male_name(return:'array').should.be.an.instanceof Array
phony.male_name(return:'map').should.be.an.instanceof Object
phony.male_name(return:'object').should.be.an.instanceof Object
phony.female_name(return:'string').should.be.a 'string'
phony.female_name(return:'array').should.be.an.instanceof Array
phony.female_name(return:'map').should.be.an.instanceof Object
phony.female_name(return:'object').should.be.an.instanceof Object
done() |
Generating Phony (Physical) Addresses | it "generates phony physical addresses", (done)-> |
Phony can generate physical (postal) addresses. | |
Currently these are rather US-centric, but the infrastructure is in place to support non-US addresses as well. | |
| phony.city(country:'us').should.be.a 'string'
phony.city(country:'us').should.match /^([A-Z][a-z]+)( [A-Z][a-z]+)*$/ |
| phony.state(country:'us').should.be.a 'string'
phony.state(country:'us').should.match /^[A-Z][A-Z]$/ |
| phony.postal_code(country:'us').should.be.a 'string'
phony.postal_code(country:'us').should.match /^[0-9][0-9][0-9][0-9][0-9]$/ |
| phony.zip_code().should.be.a 'string'
phony.zip_code().should.match /^[0-9][0-9][0-9][0-9][0-9]$/ |
| phony.street().should.be.a 'string'
phony.street().should.match /^(([A-Z]|[0-9]+)[a-z]*)( (([A-Z]|[0-9]+)[a-z]*)*)*$/ |
| phony.street_address().should.be.a 'string'
phony.street_address().should.match /^[0-9]+ (([A-Z]|[0-9]+)[a-z]*)( (([A-Z]|[0-9]+)[a-z]*)*)*$/ |
| phony.city_state(country:'us').should.be.a 'string'
phony.city_state(country:'us').should.match /^([A-Z][a-z]+)( [A-Z][a-z]+)* [A-Z][A-Z]$/ |
By default, | phony.city_state(return:'string').should.be.a 'string'
phony.city_state(return:'array').should.be.an.instanceof Array
phony.city_state(return:'map').should.be.an.instanceof Object
phony.city_state(return:'object').should.be.an.instanceof Object |
| phony.city_state_zip(country:'us').should.be.a 'string'
phony.city_state_zip(country:'us').should.match /^([A-Z][a-z]+)( [A-Z][a-z]+)* [A-Z][A-Z] [0-9][0-9][0-9][0-9][0-9]$/ |
By default, | phony.city_state_zip(return:'string').should.be.a 'string'
phony.city_state_zip(return:'array').should.be.an.instanceof Array
phony.city_state_zip(return:'map').should.be.an.instanceof Object
phony.city_state_zip(return:'object').should.be.an.instanceof Object
done() |
Generating Phony Text Snippets | it "generates phony text", (done)-> |
Phony can generate random text. | |
| phony.letter().should.be.a 'string'
phony.letter().should.match /^[a-z]$/ |
| phony.letters(5).should.be.a 'string'
phony.letters(5).should.match /^[a-z][a-z][a-z][a-z][a-z]$/ |
By default | phony.letters(3).length.should.equal 3 |
But one can also specify an aribtrary delimiter: | phony.letters(5,delimiter:'-').should.match /^[a-z]-[a-z]-[a-z]-[a-z]-[a-z]$/
phony.letters(5,delimiter:' ').should.match /^[a-z] [a-z] [a-z] [a-z] [a-z]$/
phony.letters(5,delimiter:', ').should.match /^[a-z], [a-z], [a-z], [a-z], [a-z]$/ |
| phony.lorem_word().should.be.a 'string'
phony.lorem_word().should.match /^[a-z]+$/ |
| phony.lorem_words(3).should.be.a 'string'
phony.lorem_words(3).should.match /^[a-z]+ [a-z]+ [a-z]+$/ |
| phony.lorem_sentence().should.be.a 'string'
phony.lorem_sentence().should.match /^[A-Z][a-z]+(,? [a-z]+)+\.$/ |
| phony.lorem_sentences(2).should.be.a 'string'
phony.lorem_sentences(2).should.match /^[A-Z][a-z]+(,? [a-z]+)+\. [A-Z][a-z]+(,? [a-z]+)+\.$/ |
| phony.lorem_paragraph().should.be.a 'string' |
| phony.lorem_paragraphs(2).should.be.a 'string' |
By default, the | phony.lorem_word(return:'array').should.be.an.instanceof Array
phony.lorem_word(return:'array').length.should.equal 1
phony.lorem_words(5,return:'array').should.be.an.instanceof Array
phony.lorem_words(5,return:'array').length.should.equal 5 |
| phony.lorem_title().should.be.a 'string'
phony.lorem_title().should.match /^[A-Z][a-z]+( [A-Z][a-z]+)*$/
done() |
Generating Phony (Virtual) Addresses | it "generates phony virtual addresses", (done)-> |
Phony can generate Internet addresses of various types. | |
| phony.domain_name().should.be.a 'string'
phony.domain_name().should.match /^[a-z]+(\.[a-z]+)+$/ |
| phony.host_name().should.be.a 'string'
phony.host_name().should.match /^[a-z]+(\.[a-z]+)+$/ |
| phony.username().should.be.a 'string'
phony.username().should.match /^[a-z]([a-z-.]*)[a-z]+$/ |
| phony.email_address().should.be.a 'string'
phony.email_address().should.match /^[a-z]([a-z-.]*)[a-z]+@[a-z]+(\.[a-z]+)+$/ |
| phony.uri().should.be.a 'string'
phony.uri().should.match /^https?:\/\/[a-z0-9\.\-]+(:[0-9]+)?\/([a-z0-9\.\-\/]*)$/
done()
|