-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstdio.js
88 lines (72 loc) · 2.29 KB
/
stdio.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
"use strict";
const util = require("./util");
module.exports = stdioAssertions;
/**
* Assertions for the program's stdio
*/
function stdioAssertions (chai) {
const { assert, Assertion } = chai;
["stdout", "stderr", "output"].forEach((stdio) => {
assert[stdio] = (cli, expected, message) => {
let assertion = new Assertion(cli);
if (expected instanceof RegExp) {
match.call(assertion, expected, message);
}
else {
assertion.to.have[stdio](expected, message);
}
};
Assertion.addChainableMethod(stdio, stdioAssertion, stdioChainingBehavior);
/**
* Assets that the program's stdio matches the expected regular expression
*
* @param {RegExp} expected
* @param {string} [message]
* @this Assertion
*/
function match (expected, message) {
let { cli } = util.flags(this, { message });
this.assert(
expected.test(cli[stdio]),
`expected "${cli}" ${stdio} to match #{exp}`,
`expected "${cli}" ${stdio} not to match #{exp}`,
expected,
cli[stdio]
);
}
/**
* When used in a chain, the object being asserted is changed to the program's stdio,
* so that further assertions can be performed on it.
*
* @this Assertion
*/
function stdioChainingBehavior () {
let { cli } = util.flags(this);
// Make sure the property is valid
assert.isObject(cli, "Invalid CLI object");
// Change the object being asserted to the stdio property
util.flags(this, { cli, object: cli[stdio] });
}
/**
* Asserts that the program's stdio equals the expected string
*
* @param {string} expected
* @param {string} [message]
* @this Assertion
*/
function stdioAssertion (expected, message) {
let { cli, object: actual } = util.flags(this, { message });
assert.isString(expected, message);
this.assert(
actual === expected,
`expected "${cli}" ${stdio} to equal #{exp}`,
`expected "${cli}" ${stdio} not to equal #{exp}`,
expected,
actual
);
// Change the object being asserted back to the CLI object.
// This allows chains such as `should.have.stdout("foo").and.exitCode(0)`
util.flags(this, { object: cli });
}
});
}