From fede4e4fb24b85a46e9b44a9dc521c3287a7a301 Mon Sep 17 00:00:00 2001 From: Bryan Ingle Date: Thu, 27 Jan 2022 11:43:30 -0700 Subject: [PATCH] Add "Null" and "Undefined" specifications (and tests) --- .releases/4.18.0.md | 3 +- specifications/Null.js | 31 +++++++++++++++++++++ specifications/Undefined.js | 31 +++++++++++++++++++++ test/specs/specifications/NullSpec.js | 31 +++++++++++++++++++++ test/specs/specifications/UndefinedSpec.js | 32 ++++++++++++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 specifications/Null.js create mode 100644 specifications/Undefined.js create mode 100644 test/specs/specifications/NullSpec.js create mode 100644 test/specs/specifications/UndefinedSpec.js diff --git a/.releases/4.18.0.md b/.releases/4.18.0.md index c8eb04d..1bd63e3 100644 --- a/.releases/4.18.0.md +++ b/.releases/4.18.0.md @@ -1,4 +1,5 @@ **New Features** * Added a `Changes` specification. -* Added an `Exists` specification. \ No newline at end of file +* Added a `Null` specification. +* Added an `Undefined` specification. \ No newline at end of file diff --git a/specifications/Null.js b/specifications/Null.js new file mode 100644 index 0000000..b9637bf --- /dev/null +++ b/specifications/Null.js @@ -0,0 +1,31 @@ +const is = require('./../lang/is'); + +const Specification = require('./Specification'); + +module.exports = (() => { + 'use strict'; + + /** + * A {@link Specification} that passes when a data item is a null value. + * + * @public + * @extends {Specification} + */ + class Null extends Specification { + constructor() { + super(); + } + + _evaluate(data) { + return is.null(data); + } + + toString() { + return '[Null]'; + } + } + + return Null; +})(); + + diff --git a/specifications/Undefined.js b/specifications/Undefined.js new file mode 100644 index 0000000..6775812 --- /dev/null +++ b/specifications/Undefined.js @@ -0,0 +1,31 @@ +const is = require('./../lang/is'); + +const Specification = require('./Specification'); + +module.exports = (() => { + 'use strict'; + + /** + * A {@link Specification} that passes when a data item is an undefined value. + * + * @public + * @extends {Specification} + */ + class Undefined extends Specification { + constructor() { + super(); + } + + _evaluate(data) { + return is.undefined(data); + } + + toString() { + return '[Undefined]'; + } + } + + return Undefined; +})(); + + diff --git a/test/specs/specifications/NullSpec.js b/test/specs/specifications/NullSpec.js new file mode 100644 index 0000000..573d56c --- /dev/null +++ b/test/specs/specifications/NullSpec.js @@ -0,0 +1,31 @@ +const Null = require('./../../../specifications/Null'); + +describe('When a Null specification is constructed', () => { + 'use strict'; + + let specification; + + beforeEach(() => { + specification = new Null(); + }); + + it('should pass a null value', () => { + expect(specification.evaluate(null)).toEqual(true); + }); + + it('should not pass an implicit undefined value', () => { + expect(specification.evaluate()).toEqual(false); + }); + + it('should not pass an explicit undefined value', () => { + expect(specification.evaluate(undefined)).toEqual(false); + }); + + it('should not pass a zero value', () => { + expect(specification.evaluate(0)).toEqual(false); + }); + + it('should not pass an empty-length string value', () => { + expect(specification.evaluate('')).toEqual(false); + }); +}); \ No newline at end of file diff --git a/test/specs/specifications/UndefinedSpec.js b/test/specs/specifications/UndefinedSpec.js new file mode 100644 index 0000000..d64dc3c --- /dev/null +++ b/test/specs/specifications/UndefinedSpec.js @@ -0,0 +1,32 @@ +const Undefined = require('./../../../specifications/Undefined'); + +describe('When a Undefined specification is constructed', () => { + 'use strict'; + + let specification; + + beforeEach(() => { + specification = new Undefined(); + }); + + it('should not pass a null value', () => { + expect(specification.evaluate(null)).toEqual(false); + }); + + it('should pass an implicit undefined value', () => { + expect(specification.evaluate()).toEqual(true); + }); + + it('should pass an explicit undefined value', () => { + expect(specification.evaluate(undefined)).toEqual(true); + }); + + + it('should not pass a zero value', () => { + expect(specification.evaluate(0)).toEqual(false); + }); + + it('should not pass an empty-length string value', () => { + expect(specification.evaluate('')).toEqual(false); + }); +}); \ No newline at end of file