-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (116 loc) · 3.41 KB
/
index.js
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
/**
* dependencies
*/
const tape = require('tape')
const manner = require('manner-test')
const mixin = require('deepmix')
const clone = require('clone-deep')
/**
* Expose test runner.
*/
module.exports = service
/**
* Run manner service agains json object describing
* the expected ouput for given inputs.
*
* @param {Object} service
* @param {Object} json
* @param {Object} schema
* @param {boolean} invertResults // when true, use notDeepEqual instead of deepEqual
* @api public
*/
function service(service, json, schema = {}, invertResults = false) {
return new Promise((resolve, reject) => {
tape.onFinish(resolve)
if (typeof service === 'string') {
service = require(service)
try {
schema = require(service + '/schema.js')
} catch (e) {
console.log(e)
}
}
const test = manner(service, schema)
Object.keys(json).map(key => {
const method = json[key]
Object.keys(method).map(route => {
var cases = method[route]
var cb = test[key]
if (cases instanceof Array) {
cases.map(obj => testCase(cb, route, obj, invertResults))
} else {
Object.keys(cases).map(identifier => testCase(cb, route, cases[identifier], invertResults))
}
})
})
})
}
/**
* Run test case.
*
* @param {Function} method
* @param {String} route
* @param {Object} obj
* @param {boolean} invertResults // when true, use notDeepEqual instead of deepEqual
* @api private
*/
function testCase (method, route, obj, invertResults) {
const result = obj.result
const {
status,
multiline,
payload
} = result
tape(obj.description, assert => {
const assertDeepEqual = (...args) => invertResults ? assert.notDeepEqual(...args) : assert.deepEqual(...args)
const assertEqual = (...args) => invertResults ? assert.notEqual(...args) : assert.equal(...args)
assert.plan(plan(status, payload))
method(route, obj.query || {}, obj.body || {}).then(response => {
var returned = response.payload
if (status != null) assertEqual(response.status, status)
if (payload != null) {
if (typeof payload === 'function') {
assertEqual(payload(returned), true)
} else {
if (multiline) returned = parse(returned)
const failureMessage = actual => process.env.VERBOSE_TAPE
? `Expected ${invertResults ? 'not ' : '' }to find ${JSON.stringify(payload, null, 2)}\n\nAs a subset of ${JSON.stringify(actual, null, 2)}`
: undefined
if (typeof payload === 'object') {
if (returned instanceof Buffer) returned = JSON.parse(returned)
assertDeepEqual(returned, mixin(clone(returned), clone(payload)), failureMessage(returned))
} else {
assertDeepEqual(returned, payload, failureMessage(returned))
}
}
}
})
})
}
/**
* Parse multiline json delimited by line break.
*
* @param {String} response
* @return {Array}
* @api private
*/
function parse (json) {
const arr = json.toString().split('\n')
arr.pop()
return arr.map(org => JSON.parse(org))
}
/**
* Plan the number or tests based
* on the number of arguments.
*
* @return {Number}
* @api private
*/
function plan () {
var nb = 0
for (var l = arguments.length; l--;) {
if (arguments[l] != null) nb++
}
return nb
}
service.test = tape