-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
180 lines (168 loc) · 4.91 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
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
const assert = require('assert');
class CucumberAssert {
/**
* Call an actual "equals" assertion of the assert lib of node
* See http://nodejs.org/api/assert.html for details
*
* @param method The method to be called
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
callActualEqualAssert(method, actual, expected, message) {
return new Promise((resolve, reject) => {
try {
assert[method](actual, expected, message);
resolve();
} catch(e) {
reject(new Error(e.message));
}
});
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message
*
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
equal(actual, expected, message) {
return this.callActualEqualAssert('equal', actual, expected, message);
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_notequal_actual_expected_message
*
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
notEqual(actual, expected, message) {
return this.callActualEqualAssert('notEqual', actual, expected, message);
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message
*
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
deepEqual(actual, expected, message) {
return this.callActualEqualAssert('deepEqual', actual, expected, message);
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_notdeepequal_actual_expected_message
*
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
notDeepEqual(actual, expected, message) {
return this.callActualEqualAssert('notDeepEqual', actual, expected, message);
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message
*
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
strictEqual(actual, expected, message) {
return this.callActualEqualAssert('strictEqual', actual, expected, message);
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message
*
* @param actual The actual value
* @param expected The expected value
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
notStrictEqual(actual, expected, message) {
return this.callActualEqualAssert('notStrictEqual', actual, expected, message);
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_throws_block_error_message
*
* @param block The function to be executed
* @param [error] The expected error (optional)
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
throws(block, error, message) {
return new Promise((resolve, reject) => {
try {
assert.throws(block, error, message);
resolve();
} catch(e) {
// For some reason with assert.throws, etc. the exception does not use the message provided
message = message || e.message;
reject(new Error(message));
}
});
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_doesnotthrow_block_message
*
* @param block The function to be executed
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
doesNotThrow(block, message) {
return new Promise((resolve, reject) => {
try {
assert.doesNotThrow(block, message);
resolve();
} catch(e) {
// For some reason with assert.doesNotThrow the exception message is undefined. Use a custom one
// if no message is provided
message = message || 'Caught exception where there was supposed to be none.';
reject(new Error(message));
}
});
}
/**
* Wrapper for http://nodejs.org/api/assert.html#assert_assert_iferror_value
*
* @param value The value to be tested
* @param [message] The error message (optional)
*
* @returns {Promise}
*/
ifError(value, message) {
return new Promise((resolve, reject) => {
try {
assert.ifError(value);
resolve();
} catch(e) {
message = message || 'Expected value to be false, true provided.';
reject(new Error(message));
}
});
}
/**
* Provide a convinient way to use Promise.all, otherwise you would always have to do
* Promise.all(...).then(() => callback())
*
* @param promises
* @returns {Promise}
*/
all(promises) {
return Promise.all(promises).then(() => {});
}
}
module.exports = new CucumberAssert();