@jdeighan/coffee-utils 4.1.35 → 5.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,258 +0,0 @@
1
- # UnitTester.coffee
2
-
3
- import test from 'ava'
4
-
5
- import {
6
- assert, undef, pass, error, croak,
7
- isString, isFunction, isInteger, isArray,
8
- } from '@jdeighan/coffee-utils'
9
- import {blockToArray} from '@jdeighan/coffee-utils/block'
10
- import {log, currentLogger, setLogger} from '@jdeighan/coffee-utils/log'
11
- import {debug, debugging, setDebugging} from '@jdeighan/coffee-utils/debug'
12
-
13
- # ---------------------------------------------------------------------------
14
-
15
- export class UnitTester
16
-
17
- constructor: (@file='unknown file') ->
18
- @hFound = {}
19
- @whichTest = 'deepEqual'
20
- @justshow = false
21
- @testing = true
22
- @maxLineNum = undef
23
-
24
- # ........................................................................
25
-
26
- initialize: () -> # override to do any initialization
27
-
28
- pass
29
-
30
- # ........................................................................
31
-
32
- justshow: (flag) ->
33
-
34
- @justshow = flag
35
- return
36
-
37
- # ........................................................................
38
-
39
- just_show: (flag) ->
40
-
41
- @justshow = flag
42
- return
43
-
44
- # ........................................................................
45
-
46
- setMaxLineNum: (n) ->
47
-
48
- @maxLineNum = n
49
- return
50
-
51
- # ........................................................................
52
-
53
- setWhichTest: (testName) ->
54
- @whichTest = testName
55
- return
56
-
57
- # ........................................................................
58
-
59
- truthy: (lineNum, input, expected=undef) ->
60
- @setWhichTest 'truthy'
61
- @test lineNum, input, expected
62
- return
63
-
64
- # ........................................................................
65
-
66
- falsy: (lineNum, input, expected=undef) ->
67
- @setWhichTest 'falsy'
68
- @test lineNum, input, expected
69
- return
70
-
71
- # ........................................................................
72
-
73
- equal: (lineNum, input, expected) ->
74
- @setWhichTest 'deepEqual'
75
- @test lineNum, input, expected
76
- return
77
-
78
- # ........................................................................
79
-
80
- notequal: (lineNum, input, expected) ->
81
- @setWhichTest 'notDeepEqual'
82
- @test lineNum, input, expected
83
- return
84
-
85
- # ........................................................................
86
-
87
- same: (lineNum, input, expected) ->
88
- @setWhichTest 'is'
89
- @test lineNum, input, expected
90
- return
91
-
92
- # ........................................................................
93
-
94
- different: (lineNum, input, expected) ->
95
- @setWhichTest 'not'
96
- @test lineNum, input, expected
97
- return
98
-
99
- # ........................................................................
100
-
101
- fails: (lineNum, func, expected) ->
102
-
103
- assert ! expected?, "UnitTester: fails doesn't allow expected"
104
- assert isFunction(func), "UnitTester: fails requires a function"
105
-
106
- # --- disable logging
107
- logger = currentLogger()
108
- setLogger (x) -> pass
109
- try
110
- func()
111
- ok = true
112
- catch err
113
- ok = false
114
- setLogger logger
115
- @setWhichTest 'falsy'
116
- @test lineNum, ok, expected
117
- return
118
-
119
- # ........................................................................
120
-
121
- succeeds: (lineNum, func, expected) ->
122
-
123
- assert ! expected?, "UnitTester: succeeds doesn't allow expected"
124
- assert isFunction(func), "UnitTester: succeeds requires a function"
125
- try
126
- func()
127
- ok = true
128
- catch err
129
- ok = false
130
- @setWhichTest 'truthy'
131
- @test lineNum, ok, expected
132
- return
133
-
134
- # ........................................................................
135
-
136
- same_list: (lineNum, list, expected) ->
137
- assert ! list? || isArray(list), "UnitTester: not an array"
138
- assert ! expected? || isArray(expected),
139
- "UnitTester: expected is not an array"
140
-
141
- @setWhichTest 'deepEqual'
142
- @test lineNum, list.sort(), expected.sort()
143
- return
144
-
145
- # ........................................................................
146
-
147
- not_same_list: (lineNum, list, expected) ->
148
- assert ! list? || isArray(list), "UnitTester: not an array"
149
- assert ! expected? || isArray(expected),
150
- "UnitTester: expected is not an array"
151
-
152
- @setWhichTest 'notDeepEqual'
153
- @test lineNum, list.sort(), expected.sort()
154
- return
155
-
156
- # ........................................................................
157
-
158
- normalize: (input) ->
159
-
160
- # --- Convert all whitespace to single space character
161
- # Remove empty lines
162
-
163
- if isString(input)
164
- lLines = for line in blockToArray(input)
165
- line = line.trim()
166
- line.replace(/\s+/g, ' ')
167
- lLines = lLines.filter (line) -> line != ''
168
- return lLines.join('\n')
169
- else
170
- return input
171
-
172
- # ........................................................................
173
-
174
- test: (lineNum, input, expected) ->
175
-
176
- assert isInteger(lineNum),
177
- "UnitTester.test(): arg 1 must be an integer"
178
-
179
- if process.env.TEST_LINE_NUMBER
180
- if Math.abs(lineNum) != parseInt(process.env.TEST_LINE_NUMBER)
181
- return
182
-
183
- @initialize()
184
- @lineNum = lineNum # set an object property
185
-
186
- if (lineNum < 0) && process.env.FINALTEST
187
- error "Negative line numbers not allowed in FINALTEST in #{@file}"
188
-
189
- if ! @testing || (@maxLineNum && (lineNum > @maxLineNum))
190
- return
191
-
192
- if lineNum < -100000
193
- setDebugging true
194
-
195
- lineNum = @getLineNum(lineNum) # corrects for duplicates
196
- errMsg = undef
197
- try
198
- got = @transformValue(input)
199
- if isString(got)
200
- got = @normalize(got)
201
- catch err
202
- errMsg = err.message || 'UNKNOWN ERROR'
203
- log "got ERROR in unit test: #{errMsg}"
204
-
205
- expected = @transformExpected(expected)
206
- if isString(expected)
207
- expected = @normalize(expected)
208
-
209
- if @justshow
210
- log "line #{lineNum}"
211
- if errMsg
212
- log "GOT ERROR #{errMsg}"
213
- else
214
- log got, "GOT:"
215
- log expected, "EXPECTED:"
216
- if lineNum < -100000
217
- setDebugging false
218
- return
219
-
220
- # --- We need to save this here because in the tests themselves,
221
- # 'this' won't be correct
222
- whichTest = @whichTest
223
-
224
- if lineNum < 0
225
- test.only "line #{lineNum}", (t) ->
226
- t[whichTest](got, expected)
227
- @testing = false
228
- else
229
- test "line #{lineNum}", (t) ->
230
- t[whichTest](got, expected)
231
- if lineNum < -100000
232
- setDebugging false
233
- return
234
-
235
- # ........................................................................
236
-
237
- transformValue: (input) ->
238
- return input
239
-
240
- # ........................................................................
241
-
242
- transformExpected: (input) ->
243
- return input
244
-
245
- # ........................................................................
246
-
247
- getLineNum: (lineNum) ->
248
-
249
- # --- patch lineNum to avoid duplicates
250
- while @hFound[lineNum]
251
- if lineNum < 0
252
- lineNum -= 1000
253
- else
254
- lineNum += 1000
255
- @hFound[lineNum] = true
256
- return lineNum
257
-
258
- # ---------------------------------------------------------------------------
package/src/UnitTester.js DELETED
@@ -1,278 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // UnitTester.coffee
3
- import test from 'ava';
4
-
5
- import {
6
- assert,
7
- undef,
8
- pass,
9
- error,
10
- croak,
11
- isString,
12
- isFunction,
13
- isInteger,
14
- isArray
15
- } from '@jdeighan/coffee-utils';
16
-
17
- import {
18
- blockToArray
19
- } from '@jdeighan/coffee-utils/block';
20
-
21
- import {
22
- log,
23
- currentLogger,
24
- setLogger
25
- } from '@jdeighan/coffee-utils/log';
26
-
27
- import {
28
- debug,
29
- debugging,
30
- setDebugging
31
- } from '@jdeighan/coffee-utils/debug';
32
-
33
- // ---------------------------------------------------------------------------
34
- export var UnitTester = class UnitTester {
35
- constructor(file = 'unknown file') {
36
- this.file = file;
37
- this.hFound = {};
38
- this.whichTest = 'deepEqual';
39
- this.justshow = false;
40
- this.testing = true;
41
- this.maxLineNum = undef;
42
- }
43
-
44
- // ........................................................................
45
- initialize() { // override to do any initialization
46
- return pass;
47
- }
48
-
49
- // ........................................................................
50
- justshow(flag) {
51
- this.justshow = flag;
52
- }
53
-
54
- // ........................................................................
55
- just_show(flag) {
56
- this.justshow = flag;
57
- }
58
-
59
- // ........................................................................
60
- setMaxLineNum(n) {
61
- this.maxLineNum = n;
62
- }
63
-
64
- // ........................................................................
65
- setWhichTest(testName) {
66
- this.whichTest = testName;
67
- }
68
-
69
- // ........................................................................
70
- truthy(lineNum, input, expected = undef) {
71
- this.setWhichTest('truthy');
72
- this.test(lineNum, input, expected);
73
- }
74
-
75
- // ........................................................................
76
- falsy(lineNum, input, expected = undef) {
77
- this.setWhichTest('falsy');
78
- this.test(lineNum, input, expected);
79
- }
80
-
81
- // ........................................................................
82
- equal(lineNum, input, expected) {
83
- this.setWhichTest('deepEqual');
84
- this.test(lineNum, input, expected);
85
- }
86
-
87
- // ........................................................................
88
- notequal(lineNum, input, expected) {
89
- this.setWhichTest('notDeepEqual');
90
- this.test(lineNum, input, expected);
91
- }
92
-
93
- // ........................................................................
94
- same(lineNum, input, expected) {
95
- this.setWhichTest('is');
96
- this.test(lineNum, input, expected);
97
- }
98
-
99
- // ........................................................................
100
- different(lineNum, input, expected) {
101
- this.setWhichTest('not');
102
- this.test(lineNum, input, expected);
103
- }
104
-
105
- // ........................................................................
106
- fails(lineNum, func, expected) {
107
- var err, logger, ok;
108
- assert(expected == null, "UnitTester: fails doesn't allow expected");
109
- assert(isFunction(func), "UnitTester: fails requires a function");
110
- // --- disable logging
111
- logger = currentLogger();
112
- setLogger(function(x) {
113
- return pass;
114
- });
115
- try {
116
- func();
117
- ok = true;
118
- } catch (error1) {
119
- err = error1;
120
- ok = false;
121
- }
122
- setLogger(logger);
123
- this.setWhichTest('falsy');
124
- this.test(lineNum, ok, expected);
125
- }
126
-
127
- // ........................................................................
128
- succeeds(lineNum, func, expected) {
129
- var err, ok;
130
- assert(expected == null, "UnitTester: succeeds doesn't allow expected");
131
- assert(isFunction(func), "UnitTester: succeeds requires a function");
132
- try {
133
- func();
134
- ok = true;
135
- } catch (error1) {
136
- err = error1;
137
- ok = false;
138
- }
139
- this.setWhichTest('truthy');
140
- this.test(lineNum, ok, expected);
141
- }
142
-
143
- // ........................................................................
144
- same_list(lineNum, list, expected) {
145
- assert((list == null) || isArray(list), "UnitTester: not an array");
146
- assert((expected == null) || isArray(expected), "UnitTester: expected is not an array");
147
- this.setWhichTest('deepEqual');
148
- this.test(lineNum, list.sort(), expected.sort());
149
- }
150
-
151
- // ........................................................................
152
- not_same_list(lineNum, list, expected) {
153
- assert((list == null) || isArray(list), "UnitTester: not an array");
154
- assert((expected == null) || isArray(expected), "UnitTester: expected is not an array");
155
- this.setWhichTest('notDeepEqual');
156
- this.test(lineNum, list.sort(), expected.sort());
157
- }
158
-
159
- // ........................................................................
160
- normalize(input) {
161
- var lLines, line;
162
- // --- Convert all whitespace to single space character
163
- // Remove empty lines
164
- if (isString(input)) {
165
- lLines = (function() {
166
- var i, len, ref, results;
167
- ref = blockToArray(input);
168
- results = [];
169
- for (i = 0, len = ref.length; i < len; i++) {
170
- line = ref[i];
171
- line = line.trim();
172
- results.push(line.replace(/\s+/g, ' '));
173
- }
174
- return results;
175
- })();
176
- lLines = lLines.filter(function(line) {
177
- return line !== '';
178
- });
179
- return lLines.join('\n');
180
- } else {
181
- return input;
182
- }
183
- }
184
-
185
- // ........................................................................
186
- test(lineNum, input, expected) {
187
- var err, errMsg, got, whichTest;
188
- assert(isInteger(lineNum), "UnitTester.test(): arg 1 must be an integer");
189
- if (process.env.TEST_LINE_NUMBER) {
190
- if (Math.abs(lineNum) !== parseInt(process.env.TEST_LINE_NUMBER)) {
191
- return;
192
- }
193
- }
194
- this.initialize();
195
- this.lineNum = lineNum; // set an object property
196
- if ((lineNum < 0) && process.env.FINALTEST) {
197
- error(`Negative line numbers not allowed in FINALTEST in ${this.file}`);
198
- }
199
- if (!this.testing || (this.maxLineNum && (lineNum > this.maxLineNum))) {
200
- return;
201
- }
202
- if (lineNum < -100000) {
203
- setDebugging(true);
204
- }
205
- lineNum = this.getLineNum(lineNum); // corrects for duplicates
206
- errMsg = undef;
207
- try {
208
- got = this.transformValue(input);
209
- if (isString(got)) {
210
- got = this.normalize(got);
211
- }
212
- } catch (error1) {
213
- err = error1;
214
- errMsg = err.message || 'UNKNOWN ERROR';
215
- log(`got ERROR in unit test: ${errMsg}`);
216
- }
217
- expected = this.transformExpected(expected);
218
- if (isString(expected)) {
219
- expected = this.normalize(expected);
220
- }
221
- if (this.justshow) {
222
- log(`line ${lineNum}`);
223
- if (errMsg) {
224
- log(`GOT ERROR ${errMsg}`);
225
- } else {
226
- log(got, "GOT:");
227
- }
228
- log(expected, "EXPECTED:");
229
- if (lineNum < -100000) {
230
- setDebugging(false);
231
- }
232
- return;
233
- }
234
- // --- We need to save this here because in the tests themselves,
235
- // 'this' won't be correct
236
- whichTest = this.whichTest;
237
- if (lineNum < 0) {
238
- test.only(`line ${lineNum}`, function(t) {
239
- return t[whichTest](got, expected);
240
- });
241
- this.testing = false;
242
- } else {
243
- test(`line ${lineNum}`, function(t) {
244
- return t[whichTest](got, expected);
245
- });
246
- }
247
- if (lineNum < -100000) {
248
- setDebugging(false);
249
- }
250
- }
251
-
252
- // ........................................................................
253
- transformValue(input) {
254
- return input;
255
- }
256
-
257
- // ........................................................................
258
- transformExpected(input) {
259
- return input;
260
- }
261
-
262
- // ........................................................................
263
- getLineNum(lineNum) {
264
- // --- patch lineNum to avoid duplicates
265
- while (this.hFound[lineNum]) {
266
- if (lineNum < 0) {
267
- lineNum -= 1000;
268
- } else {
269
- lineNum += 1000;
270
- }
271
- }
272
- this.hFound[lineNum] = true;
273
- return lineNum;
274
- }
275
-
276
- };
277
-
278
- // ---------------------------------------------------------------------------