Skip to content

Commit

Permalink
Add "Null" and "Undefined" specifications (and tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryaningl3 committed Jan 27, 2022
1 parent 367f671 commit fede4e4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .releases/4.18.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**New Features**

* Added a `Changes` specification.
* Added an `Exists` specification.
* Added a `Null` specification.
* Added an `Undefined` specification.
31 changes: 31 additions & 0 deletions specifications/Null.js
Original file line number Diff line number Diff line change
@@ -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;
})();


31 changes: 31 additions & 0 deletions specifications/Undefined.js
Original file line number Diff line number Diff line change
@@ -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;
})();


31 changes: 31 additions & 0 deletions test/specs/specifications/NullSpec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
32 changes: 32 additions & 0 deletions test/specs/specifications/UndefinedSpec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit fede4e4

Please sign in to comment.