1 | 'use strict';
|
2 |
|
3 | require('../lib/init');
|
4 |
|
5 | var assert = require('assert'),
|
6 | utils = require('../lib/utils')
|
7 | ;
|
8 |
|
9 | var func = function() { return; },
|
10 | Class = function() {},
|
11 | BadClass = function() {}
|
12 | ;
|
13 |
|
14 | Class.defineImplementedInterfaces(['interface']);
|
15 | BadClass.defineImplementedInterfaces(['badInterface']);
|
16 |
|
17 | var checkTypeTests = [
|
18 | {type: 'string', data: 'foo'},
|
19 | {type: 'number', data: 2},
|
20 | {type: 'boolean', data: true},
|
21 | {type: 'function', data: func},
|
22 | {type: 'date', data: new Date()},
|
23 | {type: 'error', data: new Error()},
|
24 | {type: 'array', data: ['foo', 2]},
|
25 | {type: 'object', data: {foo: '3', bar: 2}},
|
26 | {type: 'null', data: null},
|
27 | {type: 'undefined'},
|
28 | {type: 'mixed', data: {foo: 2, bar: ['foo', 4, {foo: 'bar'}]}},
|
29 | {type: 'string_array', data: ['foo', 'bar']},
|
30 | {type: 'number_array', data: [3]},
|
31 | {type: 'boolean_array', data: [false, true]},
|
32 | {type: 'function_array', data: [func, func]},
|
33 | {type: 'date_array', data: [new Date(), new Date()]},
|
34 | {type: 'error_array', data: [new Error(), new Error()]},
|
35 | {type: 'mixed_array', data: [func, 2, {foo: [1]}]},
|
36 | {type: 'number_array_array', data: [[1, 2], [3]]},
|
37 | {type: 'string_array_array', data: [['foo'], ['bar']]},
|
38 | {type: 'boolean_array_array', data: [[true, false], [true]]},
|
39 | {type: 'mixed_array_array', data: [[func, 2, {foo: [1]}], []]},
|
40 | {type: 'string_object', data: {foo: 'bar', bar: 'foo'}},
|
41 | {type: 'number_object', data: {foo: 2, bar: 1}},
|
42 | {type: 'boolean_object', data: {foo: true, bar: false}},
|
43 | {type: 'function_object', data: {foo: func, bar: func}},
|
44 | {type: 'date_object', data: {foo: new Date(), bar: new Date()}},
|
45 | {type: 'error_object', data: {foo: new Error(), bar: new Error()}},
|
46 | {type: 'mixed_object', data: {foo: 45, bar: {foo: 'bar'}}},
|
47 | {type: 'number_array_object', data: {foo: [2, 3, 4], bar: [5]}},
|
48 | {type: 'string_array_object', data: {foo: ['foo', 'bar'], bar: []}},
|
49 | {type: 'boolean_array_object', data: {foo: [true, false], bar: []}},
|
50 | {type: 'mixed_array_object', data: {foo: [1, 'bar'], bar: []}},
|
51 | {type: 'mixed', data: {foo: 2, bar: ['foo', 4, {foo: 'bar'}]}},
|
52 | {type: 'string_array|undefined', data: ['foo', 'bar'], expected: 'string_array'},
|
53 | {type: 'string_array|undefined', expected: 'undefined'},
|
54 | {type: 'interface', data: new Class()},
|
55 | {type: 'interface_array', data: [new Class(), new Class()]},
|
56 | {type: 'interface_array', data: []},
|
57 | {type: 'interface_object', data: {foo: new Class(), bar: new Class()}},
|
58 | {type: 'interface_object', data: {}},
|
59 | {type: 'string_array', data: []},
|
60 | {type: 'number', data: '2', interpret: 2},
|
61 | {type: 'boolean_array_object', data: {foo: [2, '3', false, 0], bar: ['0']}, interpret: {foo: [true, true, false, false], bar: [false]}}
|
62 | ]
|
63 | ;
|
64 |
|
65 | var checkTypeErrorTests = [
|
66 | {
|
67 | data: 1,
|
68 | type: 'string',
|
69 | expected: /The expected value is a "string"; a "number" given instead./
|
70 | },
|
71 | {
|
72 | data: 'bar',
|
73 | type: 'number',
|
74 | name: 'foo1',
|
75 | expected: /The expected value for "foo1" is a "number"; a "string" given instead./
|
76 | },
|
77 | {
|
78 | data: 1,
|
79 | type: 'boolean',
|
80 | name: 'foo2',
|
81 | expected: /The expected value for "foo2" is a "boolean"; a "number" given instead./
|
82 | },
|
83 | {
|
84 | data: 1,
|
85 | type: 'function',
|
86 | name: 'foo3',
|
87 | expected: /The expected value for "foo3" is a "function"; a "number" given instead./
|
88 | },
|
89 | {
|
90 | data: 1,
|
91 | type: 'string_array',
|
92 | name: 'foo4',
|
93 | expected: /The expected value for "foo4" is a "string_array"; a "number" given instead./
|
94 | },
|
95 | {
|
96 | data: ['foo', 'bar'],
|
97 | type: 'number_array',
|
98 | name: 'foo5',
|
99 | expected: /The expected value for "foo5" is a "number_array"; a "string_array" given instead./
|
100 | },
|
101 | {
|
102 | data: 1,
|
103 | type: 'string_object',
|
104 | name: 'foo6',
|
105 | expected: /The expected value for "foo6" is a "string_object"; a "number" given instead./
|
106 | },
|
107 | {
|
108 | data: {foo: 1},
|
109 | type: 'boolean_object',
|
110 | name: 'foo7',
|
111 | expected: /The expected value for "foo7" is a "boolean_object"; a "number_object" given instead./
|
112 | },
|
113 | {
|
114 | data: 1,
|
115 | type: 'mixed_object',
|
116 | name: 'foo8',
|
117 | expected: /The expected value for "foo8" is a "mixed_object"; a "number" given instead./
|
118 | },
|
119 | {
|
120 | data: {bar: 1},
|
121 | type: 'number_array_object',
|
122 | name: 'foo9',
|
123 | expected: /The expected value for "foo9" is a "number_array_object"; a "number_object" given instead./
|
124 | },
|
125 | {
|
126 | data: {bar: [1]},
|
127 | type: 'string_array_object',
|
128 | name: 'foo10',
|
129 | expected: /The expected value for "foo10" is a "string_array_object"; a "number_array_object" given instead./
|
130 | },
|
131 | {
|
132 | data: 1,
|
133 | type: 'mixed_array_object',
|
134 | name: 'foo11',
|
135 | expected: /The expected value for "foo11" is a "mixed_array_object"; a "number" given instead./
|
136 | },
|
137 | {
|
138 | data: {},
|
139 | type: 'array',
|
140 | name: 'foo12',
|
141 | expected: /The expected value for "foo12" is an "array"; an "object" given instead./
|
142 | },
|
143 | {
|
144 | data: [],
|
145 | type: 'object',
|
146 | name: 'foo13',
|
147 | expected: /The expected value for "foo13" is an "object"; an "array" given instead./
|
148 | },,
|
149 | {
|
150 | data: [],
|
151 | type: 'object',
|
152 | name: 'foo14',
|
153 | expected: /The expected value for "foo14" is an "object"; an "array" given instead./
|
154 | },
|
155 | {
|
156 | data: [],
|
157 | type: 'interface',
|
158 | name: 'foo15',
|
159 | expected: /The expected value for "foo15" is an "instance of `interface`"; an "array" given instead./
|
160 | },
|
161 | {
|
162 | data: new BadClass(),
|
163 | type: 'interface',
|
164 | name: 'foo16',
|
165 | expected: /The expected value for "foo16" is an "instance of `interface`"; an "instance of \[`badInterface`\]" given instead./
|
166 | },
|
167 | {
|
168 | data: true,
|
169 | type: 'number|string',
|
170 | name: 'foo17',
|
171 | expected: /The expected value for "foo17" is a "number" or a "string"; a "boolean" given instead./
|
172 | },
|
173 | {
|
174 | data: 2,
|
175 | type: 'interface_array',
|
176 | name: 'foo18',
|
177 | expected: /The expected value for "foo18" is an "array of instance of `interface` values"; a "number" given instead./
|
178 | },
|
179 | {
|
180 | data: [new Class()],
|
181 | type: 'interface_object',
|
182 | name: 'foo19',
|
183 | expected: /The expected value for "foo19" is an "object of instance of `interface` properties"; an "object_array" given instead./
|
184 | },
|
185 | {
|
186 | data: {foo: new Class(), bar: new BadClass()},
|
187 | type: 'interface_object',
|
188 | name: 'foo20',
|
189 | expected: /The expected value for "foo20" is an "object of instance of `interface` properties"; an "object_object" given instead./
|
190 | },
|
191 | {
|
192 | data: 3,
|
193 | type: 'date',
|
194 | name: 'foo21',
|
195 | expected: /The expected value for "foo21" is a "date"; a "number" given instead./
|
196 | },
|
197 | {
|
198 | data: 'foo',
|
199 | type: 'error',
|
200 | name: 'foo22',
|
201 | expected: /The expected value for "foo22" is an "error"; a "string" given instead./
|
202 | }
|
203 | ]
|
204 | ;
|
205 |
|
206 | var getTypeTests = [
|
207 | {value: 1, expected: 'number'},
|
208 | {value: '2', expected: 'string'},
|
209 | {value: null, expected: 'null'},
|
210 | {expected: 'undefined'},
|
211 | {value: [1, 2], expected: 'number_array'},
|
212 | {value: ['2', 1], expected: 'mixed_array'},
|
213 | {value: {foo: 1, bar: 2}, expected: 'number_object'},
|
214 | {value: {foo: '2', bar: 1}, expected: 'mixed_object'},
|
215 | {value: [[1],[2]], expected: 'number_array_array'},
|
216 | {value: [[1],['2']], expected: 'mixed_array_array'},
|
217 | {value: [{foo: 1}, {bar: 2}], expected: 'number_object_array'},
|
218 | {value: [{foo: '2'}, {bar: 1}], expected: 'mixed_object_array'},
|
219 | {value: {foo: [1], bar: [2]}, expected: 'number_array_object'},
|
220 | {value: {foo: ['2'], bar: [1]}, expected: 'mixed_array_object'},
|
221 | {value: {foo: {foo: 1}, bar: {bar: 2}}, expected: 'number_object_object'},
|
222 | {value: {foo: {foo: '2'}, bar: {bar: 1}}, expected: 'mixed_object_object'},
|
223 | {value: {foo: {foo: [1]}, bar: {bar: [2]}}, expected: 'mixed_object_object'},
|
224 | {value: [], expected: 'array'},
|
225 | {value: {}, expected: 'object'},
|
226 | {value: {foo: [1, 3], bar: 2}, expected: 'mixed_object'},
|
227 | {value: {foo: [1, 3], bar: ['2']}, expected: 'mixed_array_object'},
|
228 | {value: {foo: [1, 3], bar: []}, expected: 'number_array_object'}
|
229 | ]
|
230 | ;
|
231 |
|
232 | describe('Object', function() {
|
233 | checkTypeTests.forEach(function(test) {
|
234 | it('method "checkType" should return the type if the value has one of the given types', function() {
|
235 | var data = utils.clone(test.data),
|
236 | results = Object.checkType(
|
237 | data,
|
238 | test.type,
|
239 | undefined !== test.interpret ? true : false
|
240 | )
|
241 | ;
|
242 |
|
243 | assert.equal(results.matchedType, test.expected ? test.expected : test.type);
|
244 |
|
245 | assert.deepEqual(data, test.data);
|
246 |
|
247 | if (undefined !== test.interpret) {
|
248 | assert.deepEqual(results.interpretedValue, test.interpret);
|
249 | }
|
250 | })
|
251 | })
|
252 |
|
253 | checkTypeErrorTests.forEach(function(test) {
|
254 | it('method "checkType" should throw an error if the value has not one of the given types', function() {
|
255 | assert.throws(
|
256 | function() {
|
257 | Object.checkType(
|
258 | test.data,
|
259 | test.type,
|
260 | test.interpret,
|
261 | test.name
|
262 | );
|
263 | },
|
264 | test.expected
|
265 | );
|
266 | })
|
267 | });
|
268 |
|
269 | getTypeTests.forEach(function(test) {
|
270 | it('method "getType" should return the type of a value', function() {
|
271 | var type = Object.getType(test.value);
|
272 |
|
273 | assert.equal(type, test.expected);
|
274 | })
|
275 | })
|
276 |
|
277 | describe('method "checkDependencies"', function() {
|
278 | var A = function() {};
|
279 | var B = function() {};
|
280 |
|
281 | it('should succeed to check dependencies', function() {
|
282 | A.defineDependency('b', 'main:b');
|
283 |
|
284 | B.defineImplementedInterfaces(['main:b']);
|
285 |
|
286 | var a = new A(),
|
287 | b = new B()
|
288 | ;
|
289 | a.b = b;
|
290 |
|
291 | a.__metadata = A.__metadata;
|
292 | b.__metadata = B.__metadata;
|
293 |
|
294 | Object.checkDependencies(a);
|
295 | })
|
296 |
|
297 | it('should succeed to check dependencies with provided dependencies', function() {
|
298 | A.defineDependency('b', 'main:b', 'main.c');
|
299 |
|
300 | B.prototype.providedType = 'main.c';
|
301 |
|
302 | var a = new A(),
|
303 | b = new B()
|
304 | ;
|
305 | a.b = b;
|
306 |
|
307 | a.__metadata = A.__metadata;
|
308 | b.__metadata = B.__metadata;
|
309 |
|
310 | Object.checkDependencies(a);
|
311 | })
|
312 |
|
313 | it('should throw an error if the type of the dependency is not the expected one', function() {
|
314 | assert.throws(
|
315 | function() {
|
316 | A.defineDependency('b', 'main:c');
|
317 |
|
318 | var a = new A(),
|
319 | b = new B()
|
320 | ;
|
321 | a.b = b;
|
322 |
|
323 | a.__metadata = A.__metadata;
|
324 | a.__metadata.id = 'a';
|
325 | b.__metadata = B.__metadata;
|
326 |
|
327 | Object.checkDependencies(a);
|
328 | },
|
329 | /The object "a" expected an "instance of `main:c`" for its property "b"; an "instance of \[`main:b`\]" given instead./
|
330 | );
|
331 | })
|
332 |
|
333 | it('should throw an error if the type of the dependency is not the expected one', function() {
|
334 | assert.throws(
|
335 | function() {
|
336 | A.defineDependency('b', 'string', 'main:c');
|
337 |
|
338 | var a = new A();
|
339 | a.b = 'dumb';
|
340 |
|
341 | delete A.__metadata.id;
|
342 | a.__metadata = A.__metadata;
|
343 |
|
344 | Object.checkDependencies(a);
|
345 | },
|
346 | /The object expected a provider of "main:c" for its property "b"; a "string" given instead./
|
347 | );
|
348 | })
|
349 |
|
350 | it('should throw an error if the value has not one of the given types', function() {
|
351 | assert.throws(
|
352 | function() {
|
353 | A.defineDependency('b', 'main:b', 'main:c');
|
354 |
|
355 | B.prototype.providedType = null;
|
356 |
|
357 | var a = new A(),
|
358 | b = new B()
|
359 | ;
|
360 | a.b = b;
|
361 |
|
362 | a.__metadata = A.__metadata;
|
363 | b.__metadata = B.__metadata;
|
364 |
|
365 | Object.checkDependencies(a);
|
366 | },
|
367 | /The object expected a provider of "main:c" for its property "b"; an object with no "providedType" getter given instead./
|
368 | );
|
369 | })
|
370 |
|
371 | it('should throw an error if the value has not one of the given types', function() {
|
372 | assert.throws(
|
373 | function() {
|
374 | B.prototype.providedType = 'main:d';
|
375 |
|
376 | var a = new A(),
|
377 | b = new B()
|
378 | ;
|
379 | a.b = b;
|
380 |
|
381 | a.__metadata = A.__metadata;
|
382 | b.__metadata = B.__metadata;
|
383 |
|
384 | Object.checkDependencies(a);
|
385 | },
|
386 | /The object expected a provider of "main:c" for its property "b"; a provider of "main:d" given instead./
|
387 | );
|
388 | })
|
389 | })
|
390 | }) |
\ | No newline at end of file |