1 | 'use strict'
|
2 |
|
3 | var should = require('should'),
|
4 | ObjectID = require('mongodb').ObjectID,
|
5 | types = [String, Number, Boolean, Object, Array, Date, RegExp, ObjectID]
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = function (actual, expected, strict, ignoredKeys, path) {
|
16 | var key, subpath
|
17 |
|
18 | try {
|
19 | if (types.indexOf(expected) !== -1) {
|
20 |
|
21 | should(actual).be.instanceof(expected)
|
22 | } else if (Array.isArray(expected)) {
|
23 |
|
24 | should(actual).be.an.Array
|
25 | if (strict) {
|
26 | should(actual).have.length(expected.length)
|
27 | } else {
|
28 | should(actual).have.property('length').above(expected.length - 1)
|
29 | }
|
30 | checkArray(actual, expected, expected.isOrdered, strict, ignoredKeys, path)
|
31 | } else if (expected &&
|
32 | typeof expected === 'object' &&
|
33 | (expected.constructor === Object || Object.getPrototypeOf(expected) === null)) {
|
34 |
|
35 | should(actual).be.an.Object
|
36 | for (key in expected) {
|
37 | if (typeof expected[key] === 'function' && types.indexOf(expected[key]) === -1) {
|
38 |
|
39 | continue
|
40 | }
|
41 | should(actual).have.property(key)
|
42 | subpath = path ? path + '.' + key : key
|
43 | module.exports(actual[key], expected[key], strict, [], subpath)
|
44 | }
|
45 | if (strict) {
|
46 | ignoredKeys = ignoredKeys || []
|
47 | for (key in actual) {
|
48 | if (ignoredKeys.indexOf(key) === -1) {
|
49 | should(expected).have.property(key)
|
50 | }
|
51 | }
|
52 | }
|
53 | } else {
|
54 |
|
55 | should(actual).be.eql(expected)
|
56 | }
|
57 | } catch (e) {
|
58 | if (!e.path) {
|
59 | e.path = path
|
60 | }
|
61 | throw e
|
62 | }
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function checkArray(actual, expected, isOrdered, strict, ignoredKeys, path) {
|
75 | if (isOrdered) {
|
76 |
|
77 | expected.forEach(function (each, i) {
|
78 | module.exports(actual[i], each, strict, [], path ? path + '.' + i : i)
|
79 | })
|
80 | return
|
81 | }
|
82 |
|
83 | var visited = actual.map(function () {
|
84 | return false
|
85 | })
|
86 | expected.forEach(function (eachExpected) {
|
87 | var j
|
88 | for (j = 0; j < actual.length; j++) {
|
89 | if (visited[j]) {
|
90 | continue
|
91 | }
|
92 |
|
93 | try {
|
94 | module.exports(actual[j], eachExpected, strict, [], path ? path + '.' + j : j)
|
95 | visited[j] = true
|
96 | break
|
97 | } catch (e) {
|
98 |
|
99 | }
|
100 | }
|
101 |
|
102 | if (j === actual.length) {
|
103 | throw new Error('Unordered array mismatch')
|
104 | }
|
105 | })
|
106 | } |
\ | No newline at end of file |