1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/* tslint:disable:typedef */
// Disabled to allow quick template creation.
/* tslint:disable:newline-per-chained-call */
// Disabled to allow Chai- and Mocha-style test formatting.
/* tslint:disable:no-unused-expression */
// Disabled to allow `...to.be.true`.
import * as chai from 'chai';
chai.should();
import * as options from '../../src/matcher/options';
import { default as types } from '../../src/matcher/types';
// tslint:disable-next-line:no-require-imports
describe('Matching:', () => {
describe('Rough Matching', () => {
context('for literals', () => {
context('accepts matching', () => {
it('strings', () => {
options.match('a', 'a').should.be.true;
});
it('numbers', () => {
options.match(3, 3).should.be.true;
});
it('boolean values', () => {
options.match(true, true).should.be.true;
});
it('null to null', () => {
options.match(null, null).should.be.true;
});
});
context('rejects', () => {
it('different strings', () => {
options.match('x', 'y').should.be.false;
});
it('different numbers', () => {
options.match(20, 42).should.be.false;
});
it('different boolean values', () => {
options.match(true, false).should.be.false;
});
it('different types', () => {
options.match('55', 55).should.be.false;
});
it('null matched with non-null values', () => {
options.match(null, false).should.be.false;
});
it('non-null values matched with null', () => {
options.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;
});
});
context('rejects', () => {
it('values that do not succeed the checker function', () => {
options.match(types.boolean, 'true').should.be.false;
});
});
});
it('accepts basic things', () => {
const template = {
a: types.string
};
const matchee = {
a: 'yes'
};
options.match(template, matchee).should.be.true;
});
it('accepts objects with superfluous properties', () => {
const template = {
a: types.number
};
const matchee = {
a: 40,
b: 20
};
options.match(template, matchee).should.be.true;
});
it('rejects objects missing some properties', () => {
const template = {
a: types.boolean,
b: types.number,
c: types.number
};
const matchee = {
b: 5
};
options.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
};
const matchee = {
a: 4
};
options.match(template, matchee).should.be.true;
});
it('rejects empty objects for non-empty templates', () => {
const template = {
a: types.boolean
};
const matchee = {
};
options.match(template, matchee).should.be.false;
});
it('accepts empty objects for empty templates', () => {
const template = {
};
const matchee = {
};
options.match(template, matchee).should.be.true;
});
it('accepts nested objects that match', () => {
const template = {
a: types.number,
b: {
ba: types.string,
bb: types.string
},
c: types.boolean
};
const matchee = {
a: 432,
b: {
ba: 'baracus',
bb: 'ATEAM'
},
c: true,
x: false
};
options.match(template, matchee).should.be.true;
});
it('accepts literal matches', () => {
const template = {
a: 'literal'
};
const matchee = {
a: 'literal'
};
options.match(template, matchee).should.be.true;
});
it('rejects incorrect literal matches', () => {
const template = {
g: 99
};
const matchee = {
g: 80
};
options.match(template, matchee).should.be.false;
});
it('accepts optional matches', () => {
const template = {
a: options.optional(types.array),
b: options.optional(types.anything)
};
const matchee = {
b: []
};
options.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 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 */
options.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' },
private_metadata: {},
admin_metadata: {} } ];
const template = types.array({
project_name: types.string,
users: types.array({
username: types.string,
access_level: types.string
}),
public_metadata: types.anything,
private_metadata: options.optional(types.anything),
admin_metadata: options.optional(types.anything)
});
/* tslint:enable:align */
options.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 */
options.match(template)(matchee).should.be.true;
});
});
});
|