Browse code
feat(tests): start experimenting with tests that can generate docs
Edward Langley authored on 22/03/2022 02:39:38
Showing 3 changed files
Showing 3 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,147 @@ |
1 |
+var t = await import("./t/index_tests.js"); |
|
2 |
+ |
|
3 |
+const when = (expect) => (thing) => ({ |
|
4 |
+ isCalledWith(...arglists) { |
|
5 |
+ const call = []; |
|
6 |
+ for (const args of arglists) { |
|
7 |
+ call.push(args); |
|
8 |
+ } |
|
9 |
+ |
|
10 |
+ return { |
|
11 |
+ get expect() { |
|
12 |
+ return { |
|
13 |
+ toEqual(w) { |
|
14 |
+ console.log( |
|
15 |
+ `\t\t${thing.name}${call |
|
16 |
+ .map((v) => `(${v.map((w) => JSON.stringify(w)).join(", ")})`) |
|
17 |
+ .join("")} returns ${JSON.stringify(w)}` |
|
18 |
+ ); |
|
19 |
+ }, |
|
20 |
+ }; |
|
21 |
+ }, |
|
22 |
+ }; |
|
23 |
+ }, |
|
24 |
+}); |
|
25 |
+ |
|
26 |
+function describe(desc, cb) { |
|
27 |
+ console.log(desc); |
|
28 |
+ cb(); |
|
29 |
+} |
|
30 |
+ |
|
31 |
+function test(desc, cb) { |
|
32 |
+ console.log("\t", desc); |
|
33 |
+ cb(); |
|
34 |
+} |
|
35 |
+ |
|
36 |
+function expect(v, context) { |
|
37 |
+ v = v.toString(); |
|
38 |
+ |
|
39 |
+ return { |
|
40 |
+ toHaveLength(length) { |
|
41 |
+ console.log("\t\t", v, "has length", length, "with context", context); |
|
42 |
+ }, |
|
43 |
+ toEqual(w) { |
|
44 |
+ console.log("\t\t", v, "equals", w, "with context", context); |
|
45 |
+ }, |
|
46 |
+ toHaveProperty(prop, value) { |
|
47 |
+ console.log( |
|
48 |
+ "\t\t", |
|
49 |
+ v, |
|
50 |
+ "has property", |
|
51 |
+ prop, |
|
52 |
+ "with value", |
|
53 |
+ value, |
|
54 |
+ "with context", |
|
55 |
+ context |
|
56 |
+ ); |
|
57 |
+ }, |
|
58 |
+ toBeTruthy() { |
|
59 |
+ console.log("\t\t", v, "returns a truthy value"); |
|
60 |
+ }, |
|
61 |
+ toBeFalsy() { |
|
62 |
+ console.log("\t\t", v, "returns a falsy value"); |
|
63 |
+ }, |
|
64 |
+ }; |
|
65 |
+} |
|
66 |
+ |
|
67 |
+t.tests({ expect, test, describe, when: when(expect) }); |
|
68 |
+// iota |
|
69 |
+// returns the right number of items |
|
70 |
+// uut.iota(items) has length 0 |
|
71 |
+// uut.iota(items) has length 1 |
|
72 |
+// uut.iota(items) has length 2 |
|
73 |
+// uut.iota(items) has length 3 |
|
74 |
+// uut.iota(items) has length 4 |
|
75 |
+// uut.iota(items) has length 5 |
|
76 |
+// uut.iota(items) has length 6 |
|
77 |
+// uut.iota(items) has length 7 |
|
78 |
+// uut.iota(items) has length 8 |
|
79 |
+// uut.iota(items) has length 9 |
|
80 |
+// returns the right number of items, independently of start |
|
81 |
+// uut.iota(5, start) has length 5 |
|
82 |
+// uut.iota(5, start) has length 5 |
|
83 |
+// uut.iota(5, start) has length 5 |
|
84 |
+// uut.iota(5, start) has length 5 |
|
85 |
+// uut.iota(5, start) has length 5 |
|
86 |
+// uut.iota(5, start) has length 5 |
|
87 |
+// uut.iota(5, start) has length 5 |
|
88 |
+// uut.iota(5, start) has length 5 |
|
89 |
+// uut.iota(5, start) has length 5 |
|
90 |
+// uut.iota(5, start) has length 5 |
|
91 |
+// uut.iota(5, 2.3) has length 5 |
|
92 |
+// returns the right start, if count > 0 |
|
93 |
+// uut.iota(0, 5) equals [] |
|
94 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
95 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
96 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
97 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
98 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
99 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
100 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
101 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
102 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
103 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
104 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
105 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
106 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
107 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
108 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
109 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
110 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
111 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
112 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
113 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
114 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
115 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
116 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
117 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
118 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
119 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
120 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
121 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
122 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
123 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
124 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
125 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
126 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
127 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
128 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
129 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
130 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
131 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
132 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
133 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
134 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
135 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
136 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
137 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
138 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
139 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
140 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
141 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
|
142 |
+// uut.iota(items, 5) has property 0 with value 5 |
|
143 |
+// uut.iota(items, -5) has property 0 with value -5 |
|
144 |
+// uut.iota(items, 2.5) has property 0 with value 2.5 |
|
145 |
+// uut.iota(items, NaN) has property 0 with value NaN |
|
146 |
+// uut.iota(items, Infinity) has property 0 with value Infinity |
|
147 |
+// uut.iota(items, -Infinity) has property 0 with value -Infinity |
... | ... |
@@ -1,6 +1,24 @@ |
1 | 1 |
import { describe, test, expect } from "@jest/globals"; |
2 | 2 |
import { tests } from "./index_tests"; |
3 | 3 |
|
4 |
+const when = (expect) => (thing) => ({ |
|
5 |
+ isCalledWith(...arglists) { |
|
6 |
+ let result = thing; |
|
7 |
+ for (const args of arglists) { |
|
8 |
+ result = result(...args); |
|
9 |
+ } |
|
10 |
+ return { |
|
11 |
+ get expect() { |
|
12 |
+ return expect(() => result); |
|
13 |
+ }, |
|
14 |
+ }; |
|
15 |
+ }, |
|
16 |
+}); |
|
17 |
+ |
|
18 |
+const _expect = function (cb, context, ...args) { |
|
19 |
+ return expect(cb(context), ...args); |
|
20 |
+}; |
|
21 |
+ |
|
4 | 22 |
tests({ |
5 | 23 |
describe, |
6 | 24 |
test(desc, cb, ...args) { |
... | ... |
@@ -13,7 +31,6 @@ tests({ |
13 | 31 |
...args |
14 | 32 |
); |
15 | 33 |
}, |
16 |
- expect(cb, context, ...args) { |
|
17 |
- return expect(cb(context), ...args); |
|
18 |
- }, |
|
34 |
+ expect: _expect, |
|
35 |
+ when: when(_expect), |
|
19 | 36 |
}); |
... | ... |
@@ -252,12 +252,18 @@ export function pickTests({ describe, test, expect }) { |
252 | 252 |
"September" "October" "November" "December")) |
253 | 253 |
*/ |
254 | 254 |
|
255 |
-function compress_runsTest({ describe, test, expect }) { |
|
255 |
+function compress_runsTest({ describe, test, expect, when }) { |
|
256 | 256 |
describe("compress_runs", () => { |
257 | 257 |
test("works", () => { |
258 |
- expect(() => uut.compress_runs()([1, 1, 2, 3, 3, 4, 5, 5])).toEqual([ |
|
259 |
- 1, 2, 3, 4, 5, |
|
260 |
- ]); |
|
258 |
+ when(uut.compress_runs) |
|
259 |
+ .isCalledWith([], [[1, 1, 2, 3, 3, 4, 5, 5]]) |
|
260 |
+ .expect.toEqual([1, 2, 3, 4, 5]); |
|
261 |
+ |
|
262 |
+ when(uut.compress_runs) |
|
263 |
+ .isCalledWith([], [[1, 2, 3, 4, 5]]) |
|
264 |
+ .expect.toEqual([1, 2, 3, 4, 5]); |
|
265 |
+ |
|
266 |
+ when(uut.compress_runs).isCalledWith([], [[]]).expect.toEqual([]); |
|
261 | 267 |
}); |
262 | 268 |
}); |
263 | 269 |
} |
... | ... |
@@ -325,7 +331,7 @@ function composedTests({ describe, test, expect }) { |
325 | 331 |
} |
326 | 332 |
|
327 | 333 |
/** |
328 |
- * @param {{ expect: any; test: any; describe: any; }} context |
|
334 |
+ * @param {{ expect: any; test: any; describe: any; when: any; }} context |
|
329 | 335 |
*/ |
330 | 336 |
export function tests(context) { |
331 | 337 |
[ |