diff --git a/lib/ext/bool.js b/lib/ext/bool.js index 500a113..391340b 100644 --- a/lib/ext/bool.js +++ b/lib/ext/bool.js @@ -68,4 +68,45 @@ export default function(should, Assertion) { this.assert(this.obj); }); + + /** + * Assert given object is truthy according javascript type conversions. + * + * @name truthy + * @memberOf Assertion + * @category assertion bool + * @example + * + * (true).should.be.truthy(); + * ''.should.not.be.truthy(); + * should(null).not.be.truthy(); + * should(void 0).not.be.truthy(); + * + * (10).should.be.truthy(); + * (0).should.not.be.truthy(); + */ + Assertion.alias("ok", "truthy"); + + + /** + * Assert given object is falsy according javascript type conversions. + * + * @name falsy + * @memberOf Assertion + * @category assertion bool + * @example + * + * (false).should.be.falsy(); + * ''.should.be.falsy(); + * should(null).be.falsy(); + * should(void 0).be.falsy(); + * + * (10).should.not.be.falsy(); + * (0).should.be.falsy(); + */ + Assertion.add('falsy', function() { + this.assertZeroArguments(arguments); + this.params = { operator: 'to be falsy' }; + this.assert(!this.obj); + }); } diff --git a/should.d.ts b/should.d.ts index 47678f8..aa72aac 100644 --- a/should.d.ts +++ b/should.d.ts @@ -80,6 +80,8 @@ declare namespace should { False(message?: string): this; ok(): this; + truthy(): this; + falsy(): this; //chain an: this; diff --git a/test.ts b/test.ts index 256196f..b9a89c5 100644 --- a/test.ts +++ b/test.ts @@ -66,6 +66,12 @@ false.should.not.be.ok(); "".should.not.be.ok(); (0).should.not.be.ok(); +"wee".should.be.truthy(); +should(-2).be.truthy(); + +(0).should.be.falsy(); +should(null).be.falsy(); + true.should.be.true(); true.should.be.True(); "1".should.not.be.true(); diff --git a/test/ext/bool.test.js b/test/ext/bool.test.js index c4b2486..e6ef9d6 100644 --- a/test/ext/bool.test.js +++ b/test/ext/bool.test.js @@ -50,5 +50,51 @@ describe("bool", function() { err(function() { "test".should.not.be.ok(); }, "expected 'test' not to be truthy (false negative fail)"); + + err(function() { + "hello".should.be.ok(true); + }, "This assertion does not expect any arguments. You may need to check your code"); + }); + + it("test truthy", function() { + true.should.be.truthy(); + false.should.not.be.truthy(); + (1).should.be.truthy(); + (0).should.not.be.truthy(); + ({}).should.be.truthy(); + (function() {}).should.be.truthy(); + + err(function() { + "".should.be.truthy(); + }, "expected '' to be truthy"); + + err(function() { + "test".should.not.be.truthy(); + }, "expected 'test' not to be truthy (false negative fail)"); + + err(function() { + "hello".should.be.truthy(true); + }, "This assertion does not expect any arguments. You may need to check your code"); + }); + + it("test falsy", function() { + true.should.not.be.falsy(); + false.should.be.falsy(); + (1).should.not.be.falsy(); + (0).should.be.falsy(); + ({}).should.not.be.falsy(); + (function() {}).should.not.be.falsy(); + + err(function() { + "".should.not.be.falsy(); + }, "expected '' not to be falsy (false negative fail)"); + + err(function() { + "test".should.be.falsy(); + }, "expected 'test' to be falsy"); + + err(function() { + "".should.be.falsy(false); + }, "This assertion does not expect any arguments. You may need to check your code"); }); });