UNPKG

3 kBJavaScriptView Raw
1'use strict'
2
3var should = require('should'),
4 ObjectID = require('mongodb').ObjectID,
5 types = [String, Number, Boolean, Object, Array, Date, RegExp, ObjectID]
6
7/**
8 * @param {*} actual
9 * @param {*} expected
10 * @param {boolean} strict
11 * @param {string[]} [ignoredKeys=[]] only used if strict is true and only useful in the root level
12 * @param {string} [path] used internally
13 * @throws if invalid. The exception has a 'path' field with the path name that caused the error
14 */
15module.exports = function (actual, expected, strict, ignoredKeys, path) {
16 var key, subpath
17
18 try {
19 if (types.indexOf(expected) !== -1) {
20 // Simple type check
21 should(actual).be.instanceof(expected)
22 } else if (Array.isArray(expected)) {
23 // Check every array element
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 // Hash map
35 should(actual).be.an.Object
36 for (key in expected) {
37 if (typeof expected[key] === 'function' && types.indexOf(expected[key]) === -1) {
38 // Skip functions, like toJSON
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 // Simple value check
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 * @param {Array} actual
67 * @param {Array} expected
68 * @param {boolean} isOrdered
69 * @param {boolean} strict
70 * @param {Array<string>} ignoredKeys
71 * @param {string} path
72 * @throws if invalid. The exception has a 'path' field with the path name that caused the error
73 */
74function checkArray(actual, expected, isOrdered, strict, ignoredKeys, path) {
75 if (isOrdered) {
76 // Simple case: compare expected[i] with actual[i]
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 // Ignore these errors, we'll check next elements
99 }
100 }
101
102 if (j === actual.length) {
103 throw new Error('Unordered array mismatch')
104 }
105 })
106}
\No newline at end of file