Asserter value()

value() behavior

value() has all assertions.

var assertions = rawAssertions.call(this, undefined);
      for(var method in assertions){
        test.value(test.value(undefined)[method]).exists();
      }
      // ensures the test method
      test.exception(function(){
        test.value(test.value(undefined)['foobar_not_exist']).exists();
      });

Assertions of value()

is(expected).

test
  .value({fluent: 'is awesome', deep: [0, 1]})
    .is({fluent: 'is awesome', deep: [0, 1]})
  .exception(function(){
    test.value({fluent: 'is awesome', deep: [0, 1]})
      .is({fluent: 'is awesome', deep: [0, 2]});
  })
;

isNot(expected).

test
  .value({fluent: 'is awesome', deep: [0, 1]})
    .isNot({fluent: 'is awesome', deep: [0, '1']})
  .exception(function(){
    test.value({fluent: 'is awesome', deep: [0, 1]})
      .isNot({fluent: 'is awesome', deep: [0, 1]});
  })
;

isIdenticalTo(expected).

test
  .value(1)
    .isIdenticalTo(1)
  .exception(function(){
    test.value(1).isIdenticalTo(2);
  })
;

isNotIdenticalTo(expected).

test
  .value('1')
    .isNotIdenticalTo(1)
  .exception(function(){
    test.value(1).isNotIdenticalTo(1);
  })
;

isEqualTo(expected).

test
  .value('1')
    .isEqualTo(1)
  .exception(function(){
    test.value(1).isEqualTo(1.1);
  })
;

isNotEqualTo(expected).

test
  .value('foobar')
    .isNotEqualTo([])
  .exception(function(){
    test.value('foobar').isNotEqualTo('foobar');
  })
;

match(expected).

test
  .value('foobar')
    .match(/[fo]+bar$/)
  .value(['a', 'b', 'c'])
    .match(/[a-z]/)
  .value(10)
    .match(10)
  .exception(function(){
    test.value('foobar').match('whoops');
  })
;

notMatch(expected).

test
  .value('foobar')
    .notMatch(/[foo]+bazzz$/)
  .value(['a', 'b', 'c'])
    .notMatch(/[d-z]/)
  .value(10)
    .notMatch(8)
  .exception(function(){
    test.value('foobar').notMatch('foobar');
  })
;

matchEach(expected).

test
  .value([10, 11, 12])
    .matchEach(function(it) {
      return it >= 10;
    })
  .exception(function(){
    // error if one or several does not match
    test.value([10, 11, 12]).matchEach(function(it) {
      return it >= 11;
    });
  })
;

notMatchEach(expected).

test
  .value([10, 11, 12])
    .notMatchEach(function(it) {
      return it >= 13;
    })
  .exception(function(){
    // error all match
    test.value([10, 11, 12]).notMatchEach(function(it) {
      return it >= 11;
    });
  })
;

isValid(expected).

test
  .value(42)
    .isValid(function(actual) {
      return actual === 42;
    })
  .exception(function(){
    test.value(42).isValid(function(actual) {
      return actual === 'expected value';
    });
  })
;

isNotValid(expected).

test
  .value(42)
    .isNotValid(function(actual) {
      return actual === 44;
    })
  .exception(function(){
    test.value(42).isNotValid(function(actual) {
      return actual === 42;
    });
  })
;

isType(expected).

test
  .value('foobar').isType('string')
  .value(0).isType('number')
  .value(1).isType('number')
  .value(1.2).isType('number')
  .value('1').isType('string')
  .value({}).isType('object')
  .value(undefined).isType('undefined')
  .value(null).isType('object')
  .value(true).isType('boolean')
  .value(false).isType('boolean')
  .exception(function(){
    test.value('1').isType('number');
  })
;

isNotType(expected).

test
  .value({}).isNotType('string')
  .value('0').isNotType('number')
  .value('1').isNotType('number')
  .value('1.2').isNotType('number')
  .value(1).isNotType('string')
  .value(null).isNotType('undefined')
  .value(undefined).isNotType('object')
  .value('true').isNotType('boolean')
  .value('false').isNotType('boolean')
  .exception(function(){
    test.value('1').isNotType('string');
  })
;

isObject().

test
  .value({})
    .isObject()
  .exception(function(){
    test.value(function(){}).isObject();
  })
;

isArray().

test
  .value([])
    .isArray()
  .exception(function(){
    test.value({}).isArray();
  })
;

isFunction().

test
  .value(function(){})
    .isFunction()
  .exception(function(){
    test.value(new Date()).isFunction();
  })
;

isString().

test
  .value('foobar')
    .isString()
  .exception(function(){
    test.value(10.2).isString();
  })
;

isNumber().

test
  .value(42)
    .isNumber()
  .exception(function(){
    test.value(true).isNumber();
  })
;

isBool() - alias of isBoolean().

test
  .value(false)
    .isBool()
  .exception(function(){
    test.value(null).isBool();
  })
;

isBoolean().

test
  .value(true)
    .isBoolean()
  .exception(function(){
    test.value(undefined).isBoolean();
  })
;

isNull().

test
  .value(null)
    .isNull()
  .exception(function(){
    test.value(false).isNull();
  })
  .exception(function(){
    test.value(undefined).isNull();
  })
  .exception(function(){
    test.value(0).isNull();
  })
;

isUndefined().

test
  .value(undefined)
    .isUndefined()
  .exception(function(){
    test.value(null).isUndefined();
  })
;

isRegExp().

test
  .value(/[0-9]+/)
    .isRegExp()
  .exception(function(){
    test.value('/[0-9]+/').isRegExp();
  })
;

isNotRegExp().

test
  .value(new Date())
    .isNotRegExp()
  .exception(function(){
    test.value(/foo/).isNotRegExp();
  })
;

isDate().

test
  .value(new Date())
    .isDate()
  .exception(function(){
    test.value({month:5, year:2012, day:12}).isDate();
  })
;

isNotDate().

test
  .value(/[0-9]+/)
    .isNotDate()
  .exception(function(){
    test.value(new Date()).isNotDate();
  })
;

isArguments().

var fn = function(){
        test.value(arguments).isArguments();
      };
      fn(1, 2, 3);
      test.exception(function(){
        test.value({0: 'a'}).isArguments();
      });

isNotArguments().

var fn = function(){
        test
          .value([1, 2, 3])
            .isNotArguments()
          .value({0:1, 1:2, 2:3})
            .isNotArguments()
          .exception(function(){
            test.value(arguments).isNotArguments();
          })
        ;
      };
      fn(1, 2, 3);

isTrue().

test
  .value(true)
    .isTrue()
  .exception(function(){
    test.value(1).isTrue();
  })
;

isNotTrue().

test
        .value(false)
          .isNotTrue()
        .value(1)
          .isNotTrue()
        .value('1')
          .isNotTrue()
        .value('true')
          .isNotTrue()
        .exception(function(){
          test.value(true).isNotTrue();
        })
      ;

isTruthy().

test
  .value(true)
    .isTruthy()
  .value(1)
    .isTruthy()
  .value('ok')
    .isTruthy()
  .exception(function(){
    test.value('').isTruthy();
  })
;

isNotTruthy().

test
  .value(0)
    .isNotTruthy()
  .exception(function(){
    test.value('1').isNotTruthy();
  })
;

isFalse().

test
  .value(false)
    .isFalse()
  .exception(function(){
    test.value(null).isFalse();
  })
;

isNotFalse().

test
        .value(true)
          .isNotFalse()
        .value(0)
          .isNotFalse()
        .value('0')
          .isNotFalse()
        .value('false')
          .isNotFalse()
        .value(null)
          .isNotFalse()
        .value(undefined)
          .isNotFalse()
        .exception(function(){
          test.value(false).isNotFalse();
        })
      ;

isFalsy().

test
  .value(false)
    .isFalsy()
  .value(0)
    .isFalsy()
  .value('')
    .isFalsy()
  .value(null)
    .isFalsy()
  .value(undefined)
    .isFalsy()
  .exception(function(){
    test.value(1).isFalsy();
  })
;

isNotFalsy().

test
  .value(1)
    .isNotFalsy()
  .exception(function(){
    test.value(undefined).isNotFalsy();
  })
;

isEmpty().

test
  .value('')
    .isEmpty()
  .value([])
    .isEmpty()
  .value({})
    .isEmpty()
  .exception(function(){
    test.value(1).isEmpty();
  })
;

isNotEmpty().

test
  .value('a')
    .isNotEmpty()
  .exception(function(){
    test.value('').isNotEmpty();
  })
  .exception(function(){
    test.value({}).isNotEmpty();
  })
;

exists().

test
  .value('foobar')
    .exists()
  .exception(function(){
    test.value(null).exists();
  })
;

isError() - alias of throws(Error).

var trigger = function(){
        throw new Error('Whoops!');
      };
      test
        .value(trigger)
          .isError()
        .case('Test failure', function(){
          test
            .value(function(){
              test.value(function(){
                throw {name: 'error', message: 'Whoops'};
              })
              .isError();
            })
            .throws()
            .value(function(){
              test.value(function(){
                throw Error; // <= not instanciated
              })
              .isError();
            })
            .throws()
          ;
        })
      ;

throws([constructor], [expected]).

var indicator, trigger = function(){
        throw new Error("I'm a ninja !");
      };
      test
        .value(trigger)
          .throws()
          .throws("I'm a ninja !")
          .throws(/ninja/)
          .throws(Error)
          .throws(Error, "I'm a ninja !")
          .throws(Error, /ninja/)
          .throws(function(err) {
            if ((err instanceof Error) && /ninja/.test(err) ) {
              indicator = true; // just for test and example
              return true;
            }
          })
        // 'then' does nothing, it's just to make the test more expressive
        .then()
          .bool(indicator)
            .isTrue()
        .exception(function(){
          test.value(function(){
            return true;
          })
          .throws();
        })
      ;

hasLength(expected).

test
  .value([1, 2])
    .hasLength(2)
  .value('Hello Nico')
    .hasLength(10)
  .exception(function(){
    test.value('Hello Nico').hasLength(2);
  })
;

hasNotLength(expected).

test
  .value([1, 2])
    .hasNotLength(1)
  .value('Hello Nico')
    .hasNotLength(11)
  .exception(function(){
    test.value('Hello Nico').hasNotLength(10);
  })
;

isBetween(begin, end).

test
  .value(2)
    .isBetween(2, 4)
  .value(3)
    .isBetween(2, 4)
  .value(4)
    .isBetween(2, 4)
  .exception(function(){
    test.value(2).isBetween(4, 2);
  })
;

isNotBetween(begin, end).

test
  .value(1)
    .isNotBetween(2, 4)
  .value(5)
    .isNotBetween(2, 4)
  .exception(function(){
    test.value(2).isNotBetween(2, 2);
  })
;

isBefore(expected).

test
  .value(new Date(2010, 5, 20))
    .isBefore(new Date(2012, 2, 28))
  .exception(function(){
    test.value(new Date(2012, 2, 28))
      .isBefore(new Date(1982, 2, 17));
  })
;

isAfter(expected).

test
  .value(new Date(2012, 2, 28))
    .isAfter(new Date(2010, 5, 20))
  .exception(function(){
    test.value(new Date(2012, 2, 28))
      .isAfter(new Date(2014, 2, 28));
  })
;

isLessThan(expected).

test
  .value(1)
    .isLessThan(2)
  .exception(function(){
    test.value(1).isLessThan(0.98);
  })
;

isGreaterThan(expected).

test
  .value(2)
    .isGreaterThan(1)
  .exception(function(){
    test.value(1).isGreaterThan(1.00000001);
  })
;

isApprox(num, delta).

test
  .value(99.98)
    .isApprox(100, 0.1)
  .exception(function(){
    test.value(99.98)
      .isApprox(100, 0.01);
  })
;

isInfinite().

test
  .value(1/0)
    .isInfinite()
  .exception(function(){
    test.value(1.333333333333333333333)
      .isInfinite();
  })
;

isNotInfinite().

test
  .value(1.3333333333333333333333333)
    .isNotInfinite()
  .exception(function(){
    test.value(1/0)
      .isNotInfinite();
  })
;

isEnumerable(property).

test
  .value({prop: 'foobar'})
    .isEnumerable('prop')
  .exception(function(){
    test.value(function() {})
      .isEnumerable('call');
  })
;

isNotEnumerable(property).

test
  .value(function() {})
    .isNotEnumerable('call')
  .value(Object.create({}, {prop: {enumerable: 0}}))
    .isNotEnumerable('prop')
  .exception(function(){
    test.value({prop: 'foobar'})
      .isNotEnumerable('prop');
  })
;

isFrozen().

test
  .value(Object.freeze({}))
    .isFrozen()
  .exception(function(){
    test.value({})
      .isFrozen();
  })
;

isNotFrozen().

test
  .value({})
    .isNotFrozen()
  .exception(function(){
    test.value(Object.freeze({}))
      .isNotFrozen();
  })
;

isInstanceOf(expected).

test
  .value(new Date())
    .isInstanceOf(Date)
  .exception(function(){
    test.value(new Date())
      .isInstanceOf(Array);
  })
;

isNotInstanceOf(expected).

test
  .value(new Date())
    .isNotInstanceOf(RegExp)
  .exception(function(){
    test.value(new Date())
      .isNotInstanceOf(Object);
  })
;

hasProperty(property [, value]).

test
  .value({foo: 'bar'})
    .hasProperty('foo')
  .value({foo: 'bar'})
    .hasProperty('foo', 'bar')
  .exception(function(){
    test.value({})
      .hasProperty('foo');
  })
;

hasNotProperty(property [, value]).

test
  .value({foo: 'bar'})
    .hasNotProperty('bar')
  .value({foo: 'bar'})
    .hasNotProperty('foo', 'baz')
  .exception(function(){
    test.value({foo: 'bar'})
      .hasNotProperty('foo');
  })
;

hasOwnProperty(property [, value]).

test
  .value({foo: 'bar'})
    .hasOwnProperty('foo')
  .value({foo: 'bar'})
    .hasOwnProperty('foo', 'bar')
  .exception(function(){
    test.value(new RegExp('foo'))
      .hasOwnProperty('constructor');
  })
;

hasNotOwnProperty(property [, value]).

test
  .value({foo: 'bar'})
    .hasNotOwnProperty('bar')
  .value({foo: 'bar'})
    .hasNotOwnProperty('foo', 'baz')
  .exception(function(){
    test.value({foo: 'bar'})
      .hasNotOwnProperty('foo');
  })
;

hasProperties(properties).

var obj = {foo: 'bar', bar: 'huhu', other: 'vroom'};
      test
        .value(obj)
          .hasProperties(['other', 'bar', 'foo'])
        .exception(function(){
          test.value(obj)
            .hasProperties(['other', 'bar']);
        })
      ;

hasNotProperties(properties).

var obj = {foo: 'bar', bar: 'huhu', other: 'vroom'};
      test
        .value(obj)
          .hasNotProperties(['other', 'foo'])
        .exception(function(){
          test.value(obj)
            .hasNotProperties(['other', 'bar', 'foo']);
        })
      ;

hasOwnProperties(properties).

var obj = {foo: 'bar', bar: 'huhu', other: 'vroom'};
      test
        .value(obj)
          .hasOwnProperties(['other', 'bar', 'foo'])
        .exception(function(){
          test.value(obj)
            .hasOwnProperties(['other', 'bar']);
        })
      ;

hasKey(key [, value]).

test
  .value({foo: 'bar'})
    .hasKey('foo')
  .value({foo: 'bar'})
    .hasKey('foo', 'bar')
  .exception(function(){
    test.value({})
      .hasKey('foo');
  })
;

notHasKey(key [, value]).

test
  .value({foo: 'bar'})
    .notHasKey('bar')
  .value({foo: 'bar'})
    .notHasKey('foo', 'baz')
  .exception(function(){
    test.value({foo: 'bar'})
      .notHasKey('foo');
  })
;

hasKeys(keys).

var obj = {foo: 'bar', bar: 'huhu', other: 'vroom'};
      test
        .value(obj)
          .hasKeys(['other', 'bar', 'foo'])
        .exception(function(){
          test.value(obj)
            .hasKeys(['other', 'bar']);
        })
      ;

notHasKeys(keys).

var obj = {foo: 'bar', bar: 'huhu', other: 'vroom'};
      test
        .value(obj)
          .notHasKeys(['other', 'foo'])
        .exception(function(){
          test.value(obj)
            .notHasKeys(['other', 'bar', 'foo']);
        })
      ;

hasValue(expected).

test
  .value('Hello, Nico!')
    .hasValue('Nico')
  .value([1, 42, 3])
    .hasValue(42)
  .value({life: 42, love: 69})
    .hasValue(42)
  .exception(function(){
    test.value({life: 42, love: 69})
      .hasValue(6);
  })
;

notHasValue(expected).

test
  .value('Hello, Nico!')
    .notHasValue('Bye')
  .value([1, 42, 3])
    .notHasValue(4)
  .value({life: 42, love: 69})
    .notHasValue(4)
  .exception(function(){
    test.value({life: 42, love: 69})
      .notHasValue(69);
  })
;

hasValues(expected).

test
  .value([1, 42, 3])
    .hasValues([42, 3])
  .exception(function(){
    test.value([1, 42, 3])
      .hasValues([42, 3.01]);
  })
;

notHasValues(expected).

test
  .value([1, 42, 3])
    .notHasValues([4, 2])
  .exception(function(){
    test.value([1, 42, 3])
      .notHasValues([1, 42, 3]);
  })
;

contains(expected [, ...]).

test
  .value('hello boy')
    .contains('boy')
  .value('KISS principle : Keep it Simple, Stupid')
    .contains(['Simple'], ['principle'], [':'])
  .value([1,2,3])
    .contains([3])
  .value([1,2,3])
    .contains([1, 3])
  .value([1,2,3])
    .contains([3], [1, 3])
  .value({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }})
    .contains({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}})
  .value([1, 2, 3, { a: { b: { d: 12 }}}])
    .contains([2], [1, 2], [{ a: { b: {d: 12}}}])
  .value([[1],[2],[3]])
    .contains([[3]])
  .value([[1],[2],[3, 4]])
    .contains([[3]])
  .value([{a: 'a'}, {b: 'b', c: 'c'}])
    .contains([{a: 'a'}], [{b: 'b'}])
  .exception(function(){
    test.value([{a: 'a'}, {b: 'b', c: 'c'}])
    .contains([{a: 'a'}], [{b: 'c'}]);
  })
;

notContains(expected [, ...]).

test
  .value('hello boy')
    .notContains('bye')
  .value([[1],[2],[3, 4]])
    .notContains([[0]])
  .value([{a: 'a'}, {b: 'b', c: 'c'}])
    .notContains([{a: 'b'}], [{c: 'b'}])
  .exception(function(){
    test.value([{a: 'a'}, {b: 'b', c: 'c'}])
      .notContains([{a: 'a'}, {c: 'c'}]);
  })
;

startsWith(str).

test
  .value('foobar')
    .startsWith('foo')
  .exception(function(){
    test.value('Hello the world').startsWith('world');
  })
;

notStartsWith(str).

test
  .value('foobar')
    .notStartsWith('bar')
  .exception(function(){
    test.value('Hello the world').notStartsWith('Hello');
  })
;

endsWith(str).

test
  .value('foobar')
    .endsWith('bar')
  .exception(function(){
    test.value('Hello the world').endsWith('Hello');
  })
;

notEndsWith(str).

test
  .value('foobar')
    .notEndsWith('foo')
  .exception(function(){
    test.value('Hello the world').notEndsWith('world');
  })
;

hasHttpStatus(code).

var req = {
        headers: {
          'content-type': 'application/json',
        },
        statusCode: 200
      };
      test
        .value(req)
          .hasHttpStatus(200)
        .value(req)
          .hasProperty('statusCode', 200)
        .exception(function(){
          test.value(req).hasHttpStatus(500);
        })
      ;

notHasHttpStatus(code).

var req = {
        headers: {
          'content-type': 'application/json',
        },
        'statusCode': 200
      };
      test
        .value(req)
          .notHasHttpStatus(404)
        .value(req)
          .hasNotProperty('statusCode', 404)
        .exception(function(){
          test.value(req).notHasHttpStatus(200);
        })
      ;

hasHeader(field [, value]).

var req = {
        headers: {
          'content-type': 'application/json'
        }
      };
      test
        .exception(function(){
          test.value(req).hasHeader('charset');
        })
        .exception(function(){
          test.value(req).hasHeader('content-type', 'text/html');
        })
        .value(req)
          .hasHeader('content-type')
          .hasHeader('content-type', 'application/json')
      ;
      // or
      test
        .object(req.headers)
          .hasProperty('content-type')
        .string(req.headers['content-type'])
          .startsWith('application/json')
      ;
      // or
      test
        .string(req.headers['content-type'])
          .isNotEmpty()
        .string(req.headers['content-type'])
          .startsWith('application/json')
      ;

notHasHeader(field [, value]).

var req = {
        headers: {
          'content-type': 'application/json'
        }
      };
      test
        .exception(function(){
          test.value(req).notHasHeader('content-type');
        })
        .exception(function(){
          test.value(req).notHasHeader('content-type', 'application/json');
        })
        .value(req)
          .notHasHeader('charset')
          .notHasHeader('content-type', 'text/html')
        // other test cases
        .value({})
          .notHasHeader('charset')
          .notHasHeader('content-type', 'text/html')
        .value({headers: {}})
          .notHasHeader('charset')
          .notHasHeader('content-type', 'text/html')
      ;

hasHeaderJson().

var req = {
  headers: {
    'content-type': 'application/json'
  }
};
test.value(req).hasHeaderJson();
// or
test
  .string(req.headers['content-type'])
    .startsWith('application/json')
;
test
  .then(req.headers['content-type'] = 'application/json; charset=utf-8')
  .value(req).hasHeaderJson()
;
// or
test
  .string(req.headers['content-type'])
    .startsWith('application/json')
;
test
  .then(req.headers['content-type'] = 'text/html')
  .error(function(){
    test.value(req).hasHeaderJson();
  })
;

notHasHeaderJson().

var req = {
        headers: {
          'content-type': 'text/html'
        }
      };
      test
        .value(req)
          .notHasHeaderJson()
        .value({})
          .notHasHeaderJson()
      ;
      // or
      test
        .value(req)
          .notHasHeaderJson()
        .string(req.headers['content-type'])
          .notStartsWith('application/json')
        .value({})
          .notHasHeaderJson()
        .then(req.headers['content-type'] = 'application/json')
        .error(function(){
          test.value(req).notHasHeaderJson();
        })
      ;

hasHeaderHtml().

var req = {
        headers: {
          'content-type': 'text/html'
        }
      };
      test.value(req).hasHeaderHtml();
      // or
      test.string(req.headers['content-type']).startsWith('text/html');
      test
        .then(req.headers['content-type'] = 'text/html; charset=utf-8')
        .value(req).hasHeaderHtml()
      ;
      // or
      test.string(req.headers['content-type']).startsWith('text/html');
      test
        .then(req.headers['content-type'] = 'application/json')
        .error(function(){
          test.value(req).hasHeaderHtml();
        })
      ;

notHasHeaderHtml().

var req = {
        headers: {
          'content-type': 'application/json'
        }
      };
      test
        .value(req)
          .notHasHeaderHtml()
      // or
        .string(req.headers['content-type'])
          .notStartsWith('text/html')
        .then(req.headers['content-type'] = 'text/html')
        .error(function(){
          test.value(req).notHasHeaderHtml();
        })
      ;