UNPKG

2.48 kBJavaScriptView Raw
1var AssertionString = require('./AssertionString');
2
3module.exports = function createStandardErrorMessage(
4 output,
5 subject,
6 testDescription,
7 args,
8 options
9) {
10 if ( options === void 0 ) options = {};
11
12 var preamble = 'expected';
13
14 var subjectOutput = output.clone();
15 if (subject) {
16 subject.call(subjectOutput, subjectOutput);
17 }
18
19 var argsOutput = output.clone();
20 if (typeof args === 'function') {
21 args.call(argsOutput, argsOutput);
22 } else {
23 if (args.length > 0) {
24 var previousWasAssertion = false;
25 args.forEach(function (arg, index) {
26 var isAssertion =
27 arg && typeof arg === 'object' && arg instanceof AssertionString;
28 if (index > 0) {
29 if (!isAssertion && !previousWasAssertion) {
30 argsOutput.text(',');
31 }
32 argsOutput.sp();
33 }
34 if (isAssertion) {
35 argsOutput.error(arg.text);
36 } else {
37 arg.call(argsOutput, argsOutput);
38 }
39 previousWasAssertion = isAssertion;
40 });
41 }
42 }
43
44 var subjectSize = subjectOutput.size();
45 var argsSize = argsOutput.size();
46 var width =
47 preamble.length +
48 subjectSize.width +
49 argsSize.width +
50 testDescription.length;
51 var height = Math.max(subjectSize.height, argsSize.height);
52
53 if ('omitSubject' in output && output.omitSubject === options.subject) {
54 var matchTestDescription = /^(not )?to (.*)/.exec(testDescription);
55 if (matchTestDescription) {
56 output.error('should ');
57 if (matchTestDescription[1]) {
58 output.error('not ');
59 }
60 testDescription = matchTestDescription[2];
61 } else {
62 testDescription = "expected: " + testDescription;
63 }
64 } else if (
65 options.compact &&
66 options.compactSubject &&
67 (subjectSize.height > 1 || subjectSize.width > (options.compactWidth || 35))
68 ) {
69 output.error('expected').sp();
70 options.compactSubject.call(output, output);
71 output.sp();
72 } else {
73 output.error(preamble);
74 if (subjectSize.height > 1) {
75 output.nl();
76 } else {
77 output.sp();
78 }
79 output.append(subjectOutput);
80 if (
81 subjectSize.height > 1 ||
82 (height === 1 && width > output.preferredWidth)
83 ) {
84 output.nl();
85 } else {
86 output.sp();
87 }
88 }
89
90 output.error(testDescription);
91
92 if (argsSize.height > 1) {
93 output.nl();
94 } else if (argsSize.width > 0) {
95 output.sp();
96 }
97
98 output.append(argsOutput);
99
100 return output;
101};