aboutsummaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authorTom Harley2018-04-17 17:39:56 +0100
committerTom Harley2018-04-17 17:39:56 +0100
commitfb54545484732f149223a63d06eff4c7d98b8410 (patch)
treed7ed162d8a3b661368cfd2665c7d35a68857de85 /test/unit
parentMerge branch 'trh/gets' of gitlab.cs.st-andrews.ac.uk:cs3099group-be-4/projec... (diff)
downloadmedicine-fb54545484732f149223a63d06eff4c7d98b8410.tar.gz
medicine-fb54545484732f149223a63d06eff4c7d98b8410.zip

Fix tests for matcher, and minor import edits

Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/options.unit.test.ts162
1 files changed, 87 insertions, 75 deletions
diff --git a/test/unit/options.unit.test.ts b/test/unit/options.unit.test.ts
index fd88ca6..a442c38 100644
--- a/test/unit/options.unit.test.ts
+++ b/test/unit/options.unit.test.ts
@@ -10,8 +10,9 @@
import * as chai from 'chai';
chai.should();
-import * as options from '../../src/matcher/options';
-import { default as types } from '../../src/matcher/types';
+import { anything, boolean, not_present, number, string
+} from '../../src/matcher';
+import { array, match, optional } from '../../src/matcher/options';
// tslint:disable-next-line:no-require-imports
@@ -20,115 +21,115 @@ describe('Matching:', () => {
context('for literals', () => {
context('accepts matching', () => {
it('strings', () => {
- options.match('a', 'a').should.be.true;
+ match('a', 'a').should.be.true;
});
it('numbers', () => {
- options.match(3, 3).should.be.true;
+ match(3, 3).should.be.true;
});
it('boolean values', () => {
- options.match(true, true).should.be.true;
+ match(true, true).should.be.true;
});
it('null to null', () => {
- options.match(null, null).should.be.true;
+ match(null, null).should.be.true;
});
});
context('rejects', () => {
it('different strings', () => {
- options.match('x', 'y').should.be.false;
+ match('x', 'y').should.be.false;
});
it('different numbers', () => {
- options.match(20, 42).should.be.false;
+ match(20, 42).should.be.false;
});
it('different boolean values', () => {
- options.match(true, false).should.be.false;
+ match(true, false).should.be.false;
});
it('different types', () => {
- options.match('55', 55).should.be.false;
+ match('55', 55).should.be.false;
});
it('null matched with non-null values', () => {
- options.match(null, false).should.be.false;
+ match(null, false).should.be.false;
});
it('non-null values matched with null', () => {
- options.match(0, null).should.be.false;
+ match(0, null).should.be.false;
});
});
});
context('for checker functions', () => {
context('accepts', () => {
it('values that succeed the checker function', () => {
- options.match(types.number, 9).should.be.true;
+ match(number, 9).should.be.true;
});
});
context('rejects', () => {
it('values that do not succeed the checker function', () => {
- options.match(types.boolean, 'true').should.be.false;
+ match(boolean, 'true').should.be.false;
});
});
});
it('accepts basic things', () => {
const template = {
- a: types.string
+ a: string
};
const matchee = {
a: 'yes'
};
- options.match(template, matchee).should.be.true;
+ match(template, matchee).should.be.true;
});
it('accepts objects with superfluous properties', () => {
const template = {
- a: types.number
+ a: number
};
const matchee = {
a: 40,
b: 20
};
- options.match(template, matchee).should.be.true;
+ match(template, matchee).should.be.true;
});
it('rejects objects missing some properties', () => {
const template = {
- a: types.boolean,
- b: types.number,
- c: types.number
+ a: boolean,
+ b: number,
+ c: number
};
const matchee = {
b: 5
};
- options.match(template, matchee).should.be.false;
+ match(template, matchee).should.be.false;
});
it('accepts objects that omit not_present properties', () => {
const template = {
- a: types.number,
- b: types.not_present,
- c: types.not_present
+ a: number,
+ b: not_present,
+ c: not_present
};
const matchee = {
a: 4
};
- options.match(template, matchee).should.be.true;
+ match(template, matchee).should.be.true;
});
it('rejects empty objects for non-empty templates', () => {
const template = {
- a: types.boolean
+ a: boolean
};
const matchee = {
};
- options.match(template, matchee).should.be.false;
+ match(template, matchee).should.be.false;
});
it('accepts empty objects for empty templates', () => {
const template = {
};
const matchee = {
};
- options.match(template, matchee).should.be.true;
+ match(template, matchee).should.be.true;
});
it('accepts nested objects that match', () => {
const template = {
- a: types.number,
+ a: number,
b: {
- ba: types.string,
- bb: types.string
+ ba: string,
+ bb: string
},
- c: types.boolean
+ c: boolean
};
const matchee = {
a: 432,
@@ -139,7 +140,7 @@ describe('Matching:', () => {
c: true,
x: false
};
- options.match(template, matchee).should.be.true;
+ match(template, matchee).should.be.true;
});
it('accepts literal matches', () => {
const template = {
@@ -148,7 +149,7 @@ describe('Matching:', () => {
const matchee = {
a: 'literal'
};
- options.match(template, matchee).should.be.true;
+ match(template, matchee).should.be.true;
});
it('rejects incorrect literal matches', () => {
const template = {
@@ -157,65 +158,76 @@ describe('Matching:', () => {
const matchee = {
g: 80
};
- options.match(template, matchee).should.be.false;
+ match(template, matchee).should.be.false;
});
it('accepts optional matches', () => {
const template = {
- a: options.optional(types.array),
- b: options.optional(types.anything)
+ a: optional(number),
+ b: optional(anything)
};
const matchee = {
b: []
};
- options.match(template)(matchee).should.be.true;
+ match(template)(matchee).should.be.true;
});
it('big bad unit test', () => {
- /* tslint:disable:align */
- const matchee = { status: 'success',
- data: [ { privilege: 'admin', description: 'null', internal: false },
- { privilege: 'logging',
- description: 'Allows Post to Log',
- internal: false } ] };
+ const matchee = {
+ status: 'success',
+ data: [
+ {
+ privilege: 'admin',
+ description: 'null',
+ internal: false
+ },
+ {
+ privilege: 'logging',
+ description: 'Allows Post to Log',
+ internal: false
+ }
+ ]
+ };
const template = {
- status: types.string,
- data: options.optional(types.anything),
- error: options.optional(types.string),
- error_description: options.optional(types.string),
- user_message: options.optional(types.string),
- error_data: options.optional(types.anything)
- };
- /* tslint:enable:align */
+ status: string,
+ data: optional(anything),
+ error: optional(string),
+ error_description: optional(string),
+ user_message: optional(string),
+ error_data: optional(anything)
+ };
- options.match(template)(matchee).should.be.true;
+ match(template)(matchee).should.be.true;
});
it('bigger badder unit test', () => {
- /* tslint:disable:align */
- const matchee = [ { project_name: 'mocky',
- users: [{ username: 'uname', access_level: 'admin'}],
- public_metadata: { creation_date: '2018-04-14T17:13:44.291Z' },
+ const matchee = [{
+ project_name: 'mocky',
+ users: [{
+ username: 'uname',
+ access_level: 'admin'
+ }],
+ public_metadata: {
+ creation_date: '2018-04-14T17:13:44.291Z'
+ },
private_metadata: {},
- admin_metadata: {} } ];
- const template = types.array({
- project_name: types.string,
- users: types.array({
- username: types.string,
- access_level: types.string
+ admin_metadata: {}
+ }];
+ const template = array({
+ project_name: string,
+ users: array({
+ username: string,
+ access_level: string
}),
- public_metadata: types.anything,
- private_metadata: options.optional(types.anything),
- admin_metadata: options.optional(types.anything)
+ public_metadata: anything,
+ private_metadata: optional(anything),
+ admin_metadata: optional(anything)
});
- /* tslint:enable:align */
- options.match(template)(matchee).should.be.true;
+ match(template)(matchee).should.be.true;
});
- it.only('Matching Arrays', () => {
- /* tslint:disable:align */
- const matchee: any = ['ss'];
- const template: any = types.array;
- /* tslint:enable:align */
+ it('Matching Arrays', () => {
+ const matchee: string[] = ['ss'];
+ const template = array(string);
- options.match(template)(matchee).should.be.true;
+ match(template)(matchee).should.be.true;
});
});
});