-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit.ts
157 lines (138 loc) · 4.38 KB
/
unit.ts
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
import { StringifyJsonOutput } from ".";
import { Output, Json, Effect, UnkeyedStore } from "@shanzhai/interfaces";
describe(`StringifyJsonOutput`, () => {
const unkeyedStore: UnkeyedStore<unknown> = {
type: `unkeyedStore`,
name: `Test Unkeyed Store`,
get: jasmine.createSpy(`unkeyedStore.get`).and.callFake(fail),
set: jasmine.createSpy(`unkeyedStore.set`).and.callFake(fail),
};
const outputEffectA: Effect = {
type: `unkeyedStoreSet`,
unkeyedStore,
};
const outputEffectB: Effect = {
type: `unkeyedStoreSet`,
unkeyedStore,
};
const outputEffectC: Effect = {
type: `unkeyedStoreSet`,
unkeyedStore,
};
describe(`on construction`, () => {
let outputSet: jasmine.Spy;
let output: Output<string>;
let stringifyJsonOutput: StringifyJsonOutput;
beforeAll(() => {
outputSet = jasmine.createSpy(`outputSet`);
output = {
set: outputSet,
effects: [outputEffectA, outputEffectB, outputEffectC],
};
stringifyJsonOutput = new StringifyJsonOutput(output);
});
it(`exposes the output's effects`, () => {
expect(stringifyJsonOutput.effects).toEqual([
outputEffectA,
outputEffectB,
outputEffectC,
]);
});
it(`exposes the output`, () => {
expect(stringifyJsonOutput.output).toBe(output);
});
it(`does not write to the output`, () => {
expect(outputSet).not.toHaveBeenCalled();
});
});
describe(`on execution`, () => {
const scenario = (
description: string,
inputJson: Json,
outputJson: string
): void => {
describe(description, () => {
let outputSet: jasmine.Spy;
let output: Output<string>;
let stringifyJsonOutput: StringifyJsonOutput;
beforeAll(async () => {
outputSet = jasmine.createSpy(`outputSet`).and.resolveTo();
output = {
set: outputSet,
effects: [outputEffectA, outputEffectB, outputEffectC],
};
stringifyJsonOutput = new StringifyJsonOutput(output);
await stringifyJsonOutput.set(inputJson);
});
it(`continues to expose the output's effects`, () => {
expect(stringifyJsonOutput.effects).toEqual([
outputEffectA,
outputEffectB,
outputEffectC,
]);
});
it(`continues to expose the output`, () => {
expect(stringifyJsonOutput.output).toBe(output);
});
it(`writes once to the output`, () => {
expect(outputSet).toHaveBeenCalledTimes(1);
});
it(`writes the stringified JSON`, () => {
expect(outputSet).toHaveBeenCalledWith(outputJson);
});
});
};
scenario(`null`, null, `null`);
scenario(`false`, false, `false`);
scenario(`true`, true, `true`);
scenario(`empty string`, ``, `""`);
scenario(
`non-empty string`,
`Test "Non-Empty" String`,
`"Test \\"Non-Empty\\" String"`
);
scenario(`zero`, 0, `0`);
scenario(`positive integer`, 358, `358`);
scenario(`negative integer`, -358, `-358`);
scenario(`positive float`, 3.58, `3.58`);
scenario(`negative float`, -3.58, `-3.58`);
scenario(`empty array`, [], `[]`);
scenario(`one-item array`, [false], `[false]`);
scenario(`two-item array`, [false, 358], `[false,358]`);
scenario(`three-item array`, [false, 358, true], `[false,358,true]`);
scenario(`empty object`, {}, `{}`);
scenario(
`one-item object`,
{ "Test Key B": false },
`{"Test Key B":false}`
);
scenario(
`two-item object`,
{ "Test Key B": false, "Test Key A": null },
`{"Test Key A":null,"Test Key B":false}`
);
scenario(
`three-item object`,
{ "Test Key B": false, "Test Key A": null, "Test Key C": true },
`{"Test Key A":null,"Test Key B":false,"Test Key C":true}`
);
scenario(
`complex`,
{
"Test Root B": [
null,
{
"Test Nested D": false,
"Test Nested A": true,
'Test "Nested" B': `Test "Nested" String`,
"Test Nested C": undefined,
},
[{}],
-70,
],
"Test Root A": 36.5,
} as unknown as Json,
`{"Test Root A":36.5,"Test Root B":[null,{"Test \\"Nested\\" B":"Test \\"Nested\\" String","Test Nested A":true,"Test Nested D":false},[{}],-70]}`
);
});
});