/
usage.js
176 lines (139 loc) · 6.33 KB
/
usage.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
/*jslint indent: 2, maxlen: 80, node: true */
/* -*- tab-width: 2 -*- */
'use strict';
var D = require('lib-demo-util-160404')(module), tmp = {};
D.expect.verbose = true;
D.hrPath.fileAliases[module.filename] = '<usage demo>';
tmp.hi = 'hello';
D.result = tmp.hi.substr(0, 4);
D.expect('===', 'hell'); //= `+ string "hell"`
D.expect('strlen', 4); //= `+ strlen 4`
D.expect('!==', 'help'); //= `+ string "hell" ≠ string "help"`
D.expect('fails=', 0); //= `+ expect.failCnt = 0`
D.chap('Expect violation of an expectation:');
D.expect('===', 'hel');
//= `! string "hell"`
//= '≠ string "hel"'
//= "☛ (<usage demo>:17:3)"
//= ""
D.expect('fails=', 1); //= `+ expect.failCnt = 1`
D.expect('reset_fails', 1); //= `+ expect.failCnt = 1, reset.`
D.expect('fails=', 0); //= `+ expect.failCnt = 0`
D.chap('UTF-8 vs. UCS-2:');
D.result = '😇'; // U+01F607 smiling face with halo
D.expect('===', String.fromCodePoint(0x01F607)); //= `+ string "😇"`
D.expect('!==', '\u01F607'); //= `+ string "😇" ≠ string "Ƕ07"`
D.expect('===', '\uD83D\uDE07'); //= `+ string "😇"`
D.expect('strlen', D.result.length); //= `+ strlen 2`
D.chap('Data containers:');
D.result = Array.from(tmp.hi);
D.expect('like', ['h', 'e', 'l', 'l', 'o']);
//= `+ array ["h", "e", "l", "l", "o"]`
D.annot('^-- whether our human-readable description of the data looks the ' +
'same'); // ^-- for test detection, the "+" has to be on ends of lines.
D.expect('type', 'array');
//= `+ array ["h", "e", "l", "l", "o"] ∈ {array}`
D.expect('strlen', [1, 1, 1, 1, 1]); //= `+ strlen [1, 1, 1, 1, 1]`
D.result = { str: ['abc', 'def'], num: [123, -456] };
D.expect('type', 'Object');
//= `+ Object {"str": ["abc", "def"], "num": [123, -456]} ∈ {Object}`
D.result = function noop() { return 'I contain source code!'; };
D.expect('type', 'function');
//= `+ function noop() ∈ {function}`
D.result = 'ints -> zero: -2 -1 -0 | 0 | +0 +1 +2 | 1 2 345 6789';
D.expect('type', 'string');
//=0 `+ string "ints -> zero: 0 0 0 | 0 | 0 0 0 | 0 0 0 0" ∈ {string}`
D.result = 'floats -> zero: -2.2 -1.1 -0.5 -0.0 | 0.000 | +0.5 +1.1 +2.2';
D.expect('type', 'string');
//=0 `+ string "floats -> zero: 0 0 0 0 | 0 | 0 0 0" ∈ {string}`
D.result = 'exp nums -> zero: -2.2e-2 -1.1E-1 -0e0 | 0.0E0 | 1.1e+1 +2.2e2';
D.expect('type', 'string');
//=0 `+ string "exp nums -> zero: 0 0 0 | 0 | 0 0" ∈ {string}`
D.chap('RegExp matching:');
tmp.rgx = {
httpErrorTemplate: /^(\d+) ([\S\s]+)$|$:$2 (HTTP $1) <- $0/,
};
D.result = '404 Not Found';
D.expect('regexp', /^\d+/); //= `+ string … → "404"`
D.expect('regexp', /\S+/g); //= `+ string … → ["404", "Not", "Found"]`
D.expect('regexp', tmp.rgx.httpErrorTemplate);
//= `+ string … → "Not Found (HTTP 404) <- 404 Not Found"`
D.chap('Synchronous runtime errors:');
function failWhale(why) {
console.log('failWhale() gonna fail:', why);
throw new Error(why);
}
D.result = D.catch(failWhale)('502 Bad Gateway');
//= `failWhale() gonna fail: 502 Bad Gateway`
D.expect('type', 'error'); //= `+ error 502 Bad Gateway ∈ {error}`
D.expect('error', '502 Bad Gateway'); //= `+ error 502 Bad Gateway`
D.expect('error', /^\d+/); //= `+ error … → "502"`
D.expect('error', /\S+/g); //= `+ error … → ["502", "Bad", "Gateway"]`
D.expect('error', tmp.rgx.httpErrorTemplate);
//= `+ error … → "Bad Gateway (HTTP 502) <- 502 Bad Gateway"`
D.annot();
D.result = D.err2str(failWhale)('410 Gone');
//= `failWhale() gonna fail: 410 Gone`
D.expect('type', 'string'); //= `+ string "Error: 410 Gone" ∈ {string}`
D.expect('===', 'Error: 410 Gone'); //= `+ string "Error: 410 Gone"`
D.expect('regexp', /: \d+/); //= `+ string … → ": 410"`
D.expect('regexp', /\S+/g); //= `+ string … → ["Error:", "410", "Gone"]`
D.expect('regexp', tmp.rgx.httpErrorTemplate);
//= `+ string … → false` // because there's "Error: " in front of "410"
D.chap('Custom assertions:');
D.result = 42;
D.expect('override', { where: 'beaver' });
// ^-- for the next expectation, use custom stack hint.
D.expect((D.result < 0), 'negative');
//= `! number 42`
//= `↯ disproven: negative`
//= /beaver|$:drunken $0/
//= ``
D.expect('reset_fails', 1); //= `+ expect.failCnt = 1, reset.`
D.expect((D.result > 0), 'positive'); //= `+ number 42 ✔ positive`
D.chap('Output annotations in simple comment lines are ignored:');
// D.chap('ignored chapter'); //= `? not-ignored chapter`
D.result = 'ignored expect';
// D.expect('===', 'whatever'); //= `? not-ignored expect`
// D.annot('ignored annotation'); //= `? not-ignored annotation`
D.chap('Smart display of certain characters: (using univeil)');
D.annot('some control characters:');
D.result = decodeURIComponent('%00%04%07%08%09%0A%0C%0D%1B');
D.expect('type', 'string');
//= `+ string "\u0000\u0004\u0007\b\t\n\f\r\u001b" ∈ {string}`
D.annot('ASCII DEL:');
D.result = '\x7F';
D.expect('type', 'string');
//= `+ string "\u007F" ∈ {string}`
D.annot('D.jsonify = univeil(JSON.stringify(…)):');
D.result = D.jsonify('\x7F');
D.expect('strlen', 8); //= `+ strlen 8`
D.result = D.result.charCodeAt(1);
D.expect('===', 0x5C); //= `+ number 92`
D.annot('compare native JSON.stringify():');
D.result = JSON.stringify('\x7F');
D.expect('strlen', 3); //= `+ strlen 3`
D.result = D.result.charCodeAt(1);
D.expect('===', 0x7F); //= `+ number 127`
D.chap('Utility functions:');
function vowels(sel, opt) { return D.getProp.these(vowels.lower, sel, opt); }
vowels.lower = 'aeiou'.match(/\S/g);
D.result = vowels(Object);
D.expect('like', vowels.lower); //= `+ array ["a", "e", "i", "o", "u"]`
D.result = vowels(Array);
D.expect('like', vowels.lower.join('')); //= `+ string "aeiou"`
D.result = vowels(Array, '+');
D.expect('like', vowels.lower.join('+')); //= `+ string "a+e+i+o+u"`
D.result = vowels(2);
D.expect('===', 'i'); //= `+ string "i"`
D.result = vowels('342');
D.expect('===', 'oui'); //= `+ string "oui"`
D.result = D.repeat(['a', 'b', 'c', {}], 2);
D.expect('like', ['a', 'b', 'c', {}, 'a', 'b', 'c', {}]);
//= `+ array ["a", "b", "c", {}, "a", "b", "c", {}]`
D.result[1] = 1;
D.result[3].o = 0;
D.result[5] = 5;
D.expect('like', ['a', 1, 'c', {o: 0}, 'a', 5, 'c', {o: 0}]);
//= `+ array ["a", 1, "c", {"o": 0}, "a", 5, "c", {"o": 0}]`
D.ok(); //= "+OK all usage tests passed."