123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- var assert = require('assert');
- var validate = require('../lib/validate');
- var validateStrict = require('../lib/validate-strict');
- var coerce = require('../lib/coerce');
- describe('Acceptance: simple coercion, validation, and strict validation', function() {
- describe('.coerce()', function() {
- describe('to string', function() {
- it('should coerce undefined to base type', function() {
- assert.strictEqual(coerce('string', undefined), '');
- });
- it('should coerce to base type when it gets: null', function (){
- assert.strictEqual(coerce('string', null), '');
- });
- it('should coerce to base type when it gets: NaN', function (){
- assert.strictEqual(coerce('string', NaN), '');
- });
- it('should coerce to base type when it gets: Infinity', function (){
- assert.strictEqual(coerce('string', Infinity), '');
- });
- it('should coerce to base type when it gets: -Infinity', function (){
- assert.strictEqual(coerce('string', -Infinity), '');
- });
- it('should not touch arbitrary string', function() {
- assert.strictEqual(coerce('string', 'foo'), 'foo');
- });
- it('should not touch empty string', function() {
- assert.strictEqual(coerce('string', ''), '');
- });
- it('should not touch integerish string', function() {
- assert.strictEqual(coerce('string', '2382'), '2382');
- });
- it('should not touch negative integerish string', function() {
- assert.strictEqual(coerce('string', '-2382'), '-2382');
- });
- it('should not touch negative zeroish string', function() {
- assert.strictEqual(coerce('string', '0'), '0');
- });
- it('should not touch decimalish string', function() {
- assert.strictEqual(coerce('string', '1.325'), '1.325');
- });
- it('should not touch negative decimalish string', function() {
- assert.strictEqual(coerce('string', '-1.325'), '-1.325');
- });
- it('should coerce numbers to strings', function() {
- assert.strictEqual(coerce('string', 2382), '2382');
- assert.strictEqual(coerce('string', -2382), '-2382');
- assert.strictEqual(coerce('string', 0), '0');
- assert.strictEqual(coerce('string', 1.325), '1.325');
- assert.strictEqual(coerce('string', -1.325), '-1.325');
- });
- });
- describe('to number', function (){
- it('should coerce undefined to base type', function() {
- assert.strictEqual(coerce('number', undefined), 0);
- });
- it('should coerce to base type when it gets: null', function (){
- assert.strictEqual(coerce('number', null), 0);
- });
- it('should coerce to base type when it gets: NaN', function (){
- assert.strictEqual(coerce('number', NaN), 0);
- });
- it('should coerce to base type when it gets: Infinity', function (){
- assert.strictEqual(coerce('number', Infinity), 0);
- });
- it('should coerce to base type when it gets: -Infinity', function (){
- assert.strictEqual(coerce('number', -Infinity), 0);
- });
- it('should not touch positive integer', function (){
- assert.strictEqual(coerce('number', 3), 3);
- });
- it('should not touch negative integer', function (){
- assert.strictEqual(coerce('number', -3), -3);
- });
- it('should not touch negative decimal', function (){
- assert.strictEqual(coerce('number', -3.2), -3.2);
- });
- it('should not touch zero', function (){
- assert.strictEqual(coerce('number', 0), 0);
- });
- it('should coerce "3.25" to 3.25', function() {
- assert.strictEqual(coerce('number', '3.25'), 3.25);
- });
- it('should coerce "-3.25" to -3.25', function() {
- assert.strictEqual(coerce('number', '-3.25'), -3.25);
- });
- it('should coerce "0" to 0', function() {
- assert.strictEqual(coerce('number', '0'), 0);
- });
- });
- describe('to boolean', function (){
- it('should coerce undefined to base type', function() {
- assert.strictEqual(coerce('boolean', undefined), false);
- });
- it('should coerce to base type when it gets: null', function (){
- assert.strictEqual(coerce('boolean', null), false);
- });
- it('should coerce to base type when it gets: NaN', function (){
- assert.strictEqual(coerce('boolean', NaN), false);
- });
- it('should coerce to base type when it gets: Infinity', function (){
- assert.strictEqual(coerce('boolean', Infinity), false);
- });
- it('should coerce to base type when it gets: -Infinity', function (){
- assert.strictEqual(coerce('boolean', Infinity), false);
- });
- it('should not touch true', function() {
- assert.strictEqual(coerce('boolean', true), true);
- });
- it('should not touch false', function() {
- assert.strictEqual(coerce('boolean', false), false);
- });
- it('should coerce "true" to true', function() {
- assert.strictEqual(coerce('boolean', 'true'), true);
- });
- it('should coerce "false" to false', function() {
- assert.strictEqual(coerce('boolean', 'false'), false);
- });
- });
- });
- describe('.validateStrict()', function() {
- describe('to string', function() {
- it('should fail on null', function (){
- assert.throws(function (){
- validateStrict('string', null);
- });
- });
- it('should fail on NaN', function (){
- assert.throws(function (){
- validateStrict('string', NaN);
- });
- });
- it('should fail on Infinity', function (){
- assert.throws(function (){
- validateStrict('string', Infinity);
- });
- });
- it('should fail on -Infinity', function (){
- assert.throws(function (){
- validateStrict('string', -Infinity);
- });
- });
- it('should choke on `undefined`', function() {
- assert.throws(function (){
- validateStrict('string', undefined);
- });
- });
- it('should not touch arbitrary string', function() {
- assert.strictEqual(validate('string', 'foo'), 'foo');
- });
- it('should not touch empty string', function() {
- assert.strictEqual(validate('string', ''), '');
- });
- it('should not touch integerish string', function() {
- assert.strictEqual(validate('string', '2382'), '2382');
- });
- it('should not touch negative integerish string', function() {
- assert.strictEqual(validate('string', '-2382'), '-2382');
- });
- it('should not touch negative zeroish string', function() {
- assert.strictEqual(validate('string', '0'), '0');
- });
- it('should not touch decimalish string', function() {
- assert.strictEqual(validate('string', '1.325'), '1.325');
- });
- it('should not touch negative decimalish string', function() {
- assert.strictEqual(validate('string', '-1.325'), '-1.325');
- });
- it('should choke on numbers', function() {
- assert.throws(function (){
- validateStrict('string', undefined);
- });
- assert.throws(function (){
- validateStrict('string', 2382);
- });
- assert.throws(function (){
- validateStrict('string', -2382);
- });
- assert.throws(function (){
- validateStrict('string', 0);
- });
- assert.throws(function (){
- validateStrict('string', 1.325);
- });
- assert.throws(function (){
- validateStrict('string', -1.325);
- });
- });
- });
- describe('to number', function (){
- it('should fail on null', function (){
- assert.throws(function (){
- validateStrict('number', null);
- });
- });
- it('should fail on NaN', function (){
- assert.throws(function (){
- validateStrict('number', NaN);
- });
- });
- it('should fail on Infinity', function (){
- assert.throws(function (){
- validateStrict('number', Infinity);
- });
- });
- it('should fail on -Infinity', function (){
- assert.throws(function (){
- validateStrict('number', -Infinity);
- });
- });
- it('should choke on `undefined`', function() {
- assert.throws(function (){
- validateStrict('number', undefined);
- });
- });
- it('should not touch positive integer', function (){
- assert.strictEqual(validate('number', 3), 3);
- });
- it('should not touch negative integer', function (){
- assert.strictEqual(validate('number', -3), -3);
- });
- it('should not touch negative decimal', function (){
- assert.strictEqual(validate('number', -3.2), -3.2);
- });
- it('should not touch zero', function (){
- assert.strictEqual(validate('number', 0), 0);
- });
- it('should choke on "3.25"', function() {
- assert.throws(function(){
- validateStrict('number', '3.25');
- });
- });
- it('should choke on "-3.25"', function() {
- assert.throws(function(){
- validateStrict('number', '-3.25');
- });
- });
- it('should choke on "0"', function() {
- assert.throws(function(){
- validateStrict('number', '0');
- });
- });
- });
- describe('to boolean', function (){
- it('should fail on null', function (){
- assert.throws(function (){
- validateStrict('boolean', null);
- });
- });
- it('should fail on NaN', function (){
- assert.throws(function (){
- validateStrict('boolean', NaN);
- });
- });
- it('should fail on Infinity', function (){
- assert.throws(function (){
- validateStrict('boolean', Infinity);
- });
- });
- it('should fail on -Infinity', function (){
- assert.throws(function (){
- validateStrict('boolean', -Infinity);
- });
- });
- it('should choke on `undefined`', function() {
- assert.throws(function (){
- validateStrict('boolean', undefined);
- });
- });
- it('should not touch true', function() {
- assert.strictEqual(validate('boolean', true), true);
- });
- it('should not touch false', function() {
- assert.strictEqual(validate('boolean', false), false);
- });
- it('should choke on "true"', function() {
- assert.throws(function (){
- validateStrict('boolean', 'true');
- });
- });
- it('should choke on "false"', function() {
- assert.throws(function (){
- validateStrict('boolean', 'false');
- });
- });
- });
- });
- });
|