ts-fortress 2.0.4 → 2.1.0
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.
- package/README.md +56 -1
- package/dist/record/record.d.mts +2 -0
- package/dist/record/record.d.mts.map +1 -1
- package/dist/record/record.mjs +19 -3
- package/dist/record/record.mjs.map +1 -1
- package/package.json +1 -1
- package/src/array/array-of-length.test.mts +16 -1
- package/src/array/array.test.mts +16 -1
- package/src/array/non-empty-array.test.mts +16 -1
- package/src/array/tuple.test.mts +18 -1
- package/src/compose/intersection.test.mts +43 -1
- package/src/compose/merge-records.test.mts +16 -1
- package/src/compose/union.test.mts +16 -1
- package/src/enum/enum.test.mts +16 -1
- package/src/enum/uint-range.test.mts +14 -1
- package/src/primitives/null.test.mts +170 -0
- package/src/primitives/number.test.mts +284 -0
- package/src/primitives/string.test.mts +341 -0
- package/src/primitives/undefined.test.mts +179 -0
- package/src/record/key-value-record.test.mts +27 -1
- package/src/record/keyof.test.mts +12 -0
- package/src/record/partial.test.mts +18 -0
- package/src/record/record-allow-excess-properties.test.mts +180 -0
- package/src/record/record-strict-composition-simple.test.mts +134 -0
- package/src/record/record-strict-composition.test.mts +403 -0
- package/src/record/record.mts +52 -25
- package/src/record/record.test.mts +71 -1
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { expectType, Result } from 'ts-data-forge';
|
|
2
|
+
import { type TypeOf } from '../type.mjs';
|
|
3
|
+
import { number, numberLiteral } from './number.mjs';
|
|
4
|
+
|
|
5
|
+
describe('number', () => {
|
|
6
|
+
describe('default value', () => {
|
|
7
|
+
test('without explicit default value', () => {
|
|
8
|
+
const numDefault = number();
|
|
9
|
+
expect(numDefault.defaultValue).toBe(0);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('with explicit default value', () => {
|
|
13
|
+
const numCustom = number(42);
|
|
14
|
+
expect(numCustom.defaultValue).toBe(42);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const num = number(0);
|
|
19
|
+
type Num = TypeOf<typeof num>;
|
|
20
|
+
|
|
21
|
+
expectType<Num, number>('=');
|
|
22
|
+
expectType<typeof num.defaultValue, Num>('=');
|
|
23
|
+
|
|
24
|
+
describe('is', () => {
|
|
25
|
+
test('truthy cases', () => {
|
|
26
|
+
const values = [
|
|
27
|
+
0,
|
|
28
|
+
42,
|
|
29
|
+
-42,
|
|
30
|
+
3.14,
|
|
31
|
+
-3.14,
|
|
32
|
+
Number.POSITIVE_INFINITY,
|
|
33
|
+
Number.NEGATIVE_INFINITY,
|
|
34
|
+
Number.NaN,
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
for (const value of values) {
|
|
38
|
+
const v: unknown = value;
|
|
39
|
+
|
|
40
|
+
if (num.is(v)) {
|
|
41
|
+
expectType<typeof v, Num>('=');
|
|
42
|
+
} else {
|
|
43
|
+
expectType<typeof v, unknown>('=');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
expect(num.is(v)).toBe(true);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('falsy cases', () => {
|
|
51
|
+
const values = [
|
|
52
|
+
'42',
|
|
53
|
+
'0',
|
|
54
|
+
true,
|
|
55
|
+
false,
|
|
56
|
+
null,
|
|
57
|
+
undefined,
|
|
58
|
+
{},
|
|
59
|
+
[],
|
|
60
|
+
Symbol('test'),
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
for (const value of values) {
|
|
64
|
+
const v: unknown = value;
|
|
65
|
+
|
|
66
|
+
if (num.is(v)) {
|
|
67
|
+
expectType<typeof v, Num>('=');
|
|
68
|
+
} else {
|
|
69
|
+
expectType<typeof v, unknown>('=');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
expect(num.is(v)).toBe(false);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('assertIs', () => {
|
|
78
|
+
const assertIs: (a: unknown) => asserts a is number = num.assertIs;
|
|
79
|
+
|
|
80
|
+
test('valid number', () => {
|
|
81
|
+
const value: unknown = 42;
|
|
82
|
+
expect(() => {
|
|
83
|
+
assertIs(value);
|
|
84
|
+
}).not.toThrow();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('invalid value throws', () => {
|
|
88
|
+
const value: unknown = '42';
|
|
89
|
+
expect(() => {
|
|
90
|
+
assertIs(value);
|
|
91
|
+
}).toThrow(/Expected number/u);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('cast', () => {
|
|
96
|
+
test('valid number returns as is', () => {
|
|
97
|
+
const value: unknown = 42;
|
|
98
|
+
const result = num.cast(value);
|
|
99
|
+
expect(result).toBe(42);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('invalid value throws error', () => {
|
|
103
|
+
const value: unknown = 'not a number';
|
|
104
|
+
expect(() => num.cast(value)).toThrow('Expected number, got string');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('throws error with type mismatch', () => {
|
|
108
|
+
const numWithDefault = number(100);
|
|
109
|
+
const value: unknown = 'not a number';
|
|
110
|
+
expect(() => numWithDefault.cast(value)).toThrow(
|
|
111
|
+
'Expected number, got string',
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('fill', () => {
|
|
117
|
+
test('valid number returns as is', () => {
|
|
118
|
+
const value: unknown = 42;
|
|
119
|
+
const result = num.fill(value);
|
|
120
|
+
expect(result).toBe(42);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('undefined returns default', () => {
|
|
124
|
+
const value: unknown = undefined;
|
|
125
|
+
const result = num.fill(value);
|
|
126
|
+
expect(result).toBe(0);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('null returns default', () => {
|
|
130
|
+
const value: unknown = null;
|
|
131
|
+
const result = num.fill(value);
|
|
132
|
+
expect(result).toBe(0);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('invalid value returns default', () => {
|
|
136
|
+
const value: unknown = 'not a number';
|
|
137
|
+
const result = num.fill(value);
|
|
138
|
+
expect(result).toBe(0);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('uses custom default value for invalid', () => {
|
|
142
|
+
const numWithDefault = number(100);
|
|
143
|
+
const value: unknown = 'not a number';
|
|
144
|
+
const result = numWithDefault.fill(value);
|
|
145
|
+
expect(result).toBe(100);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('validate', () => {
|
|
150
|
+
test('valid number', () => {
|
|
151
|
+
const value: unknown = 42;
|
|
152
|
+
const result = num.validate(value);
|
|
153
|
+
|
|
154
|
+
expect(Result.isOk(result)).toBe(true);
|
|
155
|
+
if (Result.isOk(result)) {
|
|
156
|
+
expect(result.value).toBe(42);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('invalid value', () => {
|
|
161
|
+
const value: unknown = 'not a number';
|
|
162
|
+
const result = num.validate(value);
|
|
163
|
+
|
|
164
|
+
expect(Result.isErr(result)).toBe(true);
|
|
165
|
+
if (Result.isErr(result)) {
|
|
166
|
+
expect(result.value).toStrictEqual([
|
|
167
|
+
{
|
|
168
|
+
path: [],
|
|
169
|
+
actualValue: 'not a number',
|
|
170
|
+
expectedType: 'number',
|
|
171
|
+
typeName: 'number',
|
|
172
|
+
message: undefined,
|
|
173
|
+
},
|
|
174
|
+
]);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('numberLiteral', () => {
|
|
181
|
+
const literal42 = numberLiteral(42);
|
|
182
|
+
type Literal42 = TypeOf<typeof literal42>;
|
|
183
|
+
|
|
184
|
+
expectType<Literal42, 42>('=');
|
|
185
|
+
expectType<typeof literal42.defaultValue, Literal42>('=');
|
|
186
|
+
|
|
187
|
+
describe('default value', () => {
|
|
188
|
+
test('uses literal as default', () => {
|
|
189
|
+
expect(literal42.defaultValue).toBe(42);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('is', () => {
|
|
194
|
+
test('exact match returns true', () => {
|
|
195
|
+
const value: unknown = 42;
|
|
196
|
+
|
|
197
|
+
if (literal42.is(value)) {
|
|
198
|
+
expectType<typeof value, Literal42>('=');
|
|
199
|
+
} else {
|
|
200
|
+
expectType<typeof value, unknown>('=');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
expect(literal42.is(value)).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('different number returns false', () => {
|
|
207
|
+
const value: unknown = 43;
|
|
208
|
+
|
|
209
|
+
if (literal42.is(value)) {
|
|
210
|
+
expectType<typeof value, Literal42>('=');
|
|
211
|
+
} else {
|
|
212
|
+
expectType<typeof value, unknown>('=');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
expect(literal42.is(value)).toBe(false);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('non-number returns false', () => {
|
|
219
|
+
const value: unknown = '42';
|
|
220
|
+
|
|
221
|
+
expect(literal42.is(value)).toBe(false);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe('special number literals', () => {
|
|
226
|
+
test('zero literal', () => {
|
|
227
|
+
const zero = numberLiteral(0);
|
|
228
|
+
type Zero = TypeOf<typeof zero>;
|
|
229
|
+
expectType<Zero, 0>('=');
|
|
230
|
+
|
|
231
|
+
expect(zero.is(0)).toBe(true);
|
|
232
|
+
expect(zero.is(-0)).toBe(true); // JavaScript treats 0 and -0 as equal
|
|
233
|
+
expect(zero.is(1)).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test('negative literal', () => {
|
|
237
|
+
const negative = numberLiteral(-100);
|
|
238
|
+
type Negative = TypeOf<typeof negative>;
|
|
239
|
+
expectType<Negative, -100>('=');
|
|
240
|
+
|
|
241
|
+
expect(negative.is(-100)).toBe(true);
|
|
242
|
+
expect(negative.is(100)).toBe(false);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('decimal literal', () => {
|
|
246
|
+
const pi = numberLiteral(3.14);
|
|
247
|
+
type Pi = TypeOf<typeof pi>;
|
|
248
|
+
expectType<Pi, 3.14>('=');
|
|
249
|
+
|
|
250
|
+
expect(pi.is(3.14)).toBe(true);
|
|
251
|
+
expect(pi.is(3.141)).toBe(false);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe('validate', () => {
|
|
256
|
+
test('exact match', () => {
|
|
257
|
+
const value: unknown = 42;
|
|
258
|
+
const result = literal42.validate(value);
|
|
259
|
+
|
|
260
|
+
expect(Result.isOk(result)).toBe(true);
|
|
261
|
+
if (Result.isOk(result)) {
|
|
262
|
+
expect(result.value).toBe(42);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test('different value', () => {
|
|
267
|
+
const value: unknown = 43;
|
|
268
|
+
const result = literal42.validate(value);
|
|
269
|
+
|
|
270
|
+
expect(Result.isErr(result)).toBe(true);
|
|
271
|
+
if (Result.isErr(result)) {
|
|
272
|
+
expect(result.value).toStrictEqual([
|
|
273
|
+
{
|
|
274
|
+
path: [],
|
|
275
|
+
actualValue: 43,
|
|
276
|
+
expectedType: 'numberLiteral(42)',
|
|
277
|
+
typeName: 'numberLiteral(42)',
|
|
278
|
+
message: undefined,
|
|
279
|
+
},
|
|
280
|
+
]);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
});
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { expectType, Result } from 'ts-data-forge';
|
|
2
|
+
import { type TypeOf } from '../type.mjs';
|
|
3
|
+
import { string, stringLiteral } from './string.mjs';
|
|
4
|
+
|
|
5
|
+
describe('string', () => {
|
|
6
|
+
describe('default value', () => {
|
|
7
|
+
test('without explicit default value', () => {
|
|
8
|
+
const strDefault = string();
|
|
9
|
+
expect(strDefault.defaultValue).toBe('');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('with explicit default value', () => {
|
|
13
|
+
const strCustom = string('hello');
|
|
14
|
+
expect(strCustom.defaultValue).toBe('hello');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const str = string('');
|
|
19
|
+
type Str = TypeOf<typeof str>;
|
|
20
|
+
|
|
21
|
+
expectType<Str, string>('=');
|
|
22
|
+
expectType<typeof str.defaultValue, Str>('=');
|
|
23
|
+
|
|
24
|
+
describe('is', () => {
|
|
25
|
+
test('truthy cases', () => {
|
|
26
|
+
const values = [
|
|
27
|
+
'',
|
|
28
|
+
'hello',
|
|
29
|
+
'world',
|
|
30
|
+
'123',
|
|
31
|
+
'true',
|
|
32
|
+
'false',
|
|
33
|
+
' ',
|
|
34
|
+
'\n',
|
|
35
|
+
'\t',
|
|
36
|
+
'🎉',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
for (const value of values) {
|
|
40
|
+
const v: unknown = value;
|
|
41
|
+
|
|
42
|
+
if (str.is(v)) {
|
|
43
|
+
expectType<typeof v, Str>('=');
|
|
44
|
+
} else {
|
|
45
|
+
expectType<typeof v, unknown>('=');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
expect(str.is(v)).toBe(true);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('falsy cases', () => {
|
|
53
|
+
const values = [
|
|
54
|
+
42,
|
|
55
|
+
0,
|
|
56
|
+
true,
|
|
57
|
+
false,
|
|
58
|
+
null,
|
|
59
|
+
undefined,
|
|
60
|
+
{},
|
|
61
|
+
[],
|
|
62
|
+
Symbol('test'),
|
|
63
|
+
Number.NaN,
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
for (const value of values) {
|
|
67
|
+
const v: unknown = value;
|
|
68
|
+
|
|
69
|
+
if (str.is(v)) {
|
|
70
|
+
expectType<typeof v, Str>('=');
|
|
71
|
+
} else {
|
|
72
|
+
expectType<typeof v, unknown>('=');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
expect(str.is(v)).toBe(false);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('assertIs', () => {
|
|
81
|
+
const assertIs: (a: unknown) => asserts a is string = str.assertIs;
|
|
82
|
+
|
|
83
|
+
test('valid string', () => {
|
|
84
|
+
const value: unknown = 'hello';
|
|
85
|
+
expect(() => {
|
|
86
|
+
assertIs(value);
|
|
87
|
+
}).not.toThrow();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('invalid value throws', () => {
|
|
91
|
+
const value: unknown = 42;
|
|
92
|
+
expect(() => {
|
|
93
|
+
assertIs(value);
|
|
94
|
+
}).toThrow(/Expected string/u);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('cast', () => {
|
|
99
|
+
test('valid string returns as is', () => {
|
|
100
|
+
const value: unknown = 'hello';
|
|
101
|
+
const result = str.cast(value);
|
|
102
|
+
expect(result).toBe('hello');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('invalid value throws error', () => {
|
|
106
|
+
const value: unknown = 42;
|
|
107
|
+
expect(() => str.cast(value)).toThrow('Expected string, got number');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('throws error with type mismatch', () => {
|
|
111
|
+
const strWithDefault = string('default');
|
|
112
|
+
const value: unknown = 42;
|
|
113
|
+
expect(() => strWithDefault.cast(value)).toThrow(
|
|
114
|
+
'Expected string, got number',
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('fill', () => {
|
|
120
|
+
test('valid string returns as is', () => {
|
|
121
|
+
const value: unknown = 'hello';
|
|
122
|
+
const result = str.fill(value);
|
|
123
|
+
expect(result).toBe('hello');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('undefined returns default', () => {
|
|
127
|
+
const value: unknown = undefined;
|
|
128
|
+
const result = str.fill(value);
|
|
129
|
+
expect(result).toBe('');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('null returns default', () => {
|
|
133
|
+
const value: unknown = null;
|
|
134
|
+
const result = str.fill(value);
|
|
135
|
+
expect(result).toBe('');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('invalid value returns default', () => {
|
|
139
|
+
const value: unknown = 42;
|
|
140
|
+
const result = str.fill(value);
|
|
141
|
+
expect(result).toBe('');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('uses custom default value for invalid', () => {
|
|
145
|
+
const strWithDefault = string('default');
|
|
146
|
+
const value: unknown = 42;
|
|
147
|
+
const result = strWithDefault.fill(value);
|
|
148
|
+
expect(result).toBe('default');
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe('validate', () => {
|
|
153
|
+
test('valid string', () => {
|
|
154
|
+
const value: unknown = 'hello';
|
|
155
|
+
const result = str.validate(value);
|
|
156
|
+
|
|
157
|
+
expect(Result.isOk(result)).toBe(true);
|
|
158
|
+
if (Result.isOk(result)) {
|
|
159
|
+
expect(result.value).toBe('hello');
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('empty string is valid', () => {
|
|
164
|
+
const value: unknown = '';
|
|
165
|
+
const result = str.validate(value);
|
|
166
|
+
|
|
167
|
+
expect(Result.isOk(result)).toBe(true);
|
|
168
|
+
if (Result.isOk(result)) {
|
|
169
|
+
expect(result.value).toBe('');
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('invalid value', () => {
|
|
174
|
+
const value: unknown = 42;
|
|
175
|
+
const result = str.validate(value);
|
|
176
|
+
|
|
177
|
+
expect(Result.isErr(result)).toBe(true);
|
|
178
|
+
if (Result.isErr(result)) {
|
|
179
|
+
expect(result.value).toStrictEqual([
|
|
180
|
+
{
|
|
181
|
+
path: [],
|
|
182
|
+
actualValue: 42,
|
|
183
|
+
expectedType: 'string',
|
|
184
|
+
typeName: 'string',
|
|
185
|
+
message: undefined,
|
|
186
|
+
},
|
|
187
|
+
]);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('stringLiteral', () => {
|
|
194
|
+
const hello = stringLiteral('hello');
|
|
195
|
+
type Hello = TypeOf<typeof hello>;
|
|
196
|
+
|
|
197
|
+
expectType<Hello, 'hello'>('=');
|
|
198
|
+
expectType<typeof hello.defaultValue, Hello>('=');
|
|
199
|
+
|
|
200
|
+
describe('default value', () => {
|
|
201
|
+
test('uses literal as default', () => {
|
|
202
|
+
expect(hello.defaultValue).toBe('hello');
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe('is', () => {
|
|
207
|
+
test('exact match returns true', () => {
|
|
208
|
+
const value: unknown = 'hello';
|
|
209
|
+
|
|
210
|
+
if (hello.is(value)) {
|
|
211
|
+
expectType<typeof value, Hello>('=');
|
|
212
|
+
} else {
|
|
213
|
+
expectType<typeof value, unknown>('=');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
expect(hello.is(value)).toBe(true);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('different string returns false', () => {
|
|
220
|
+
const value: unknown = 'world';
|
|
221
|
+
|
|
222
|
+
if (hello.is(value)) {
|
|
223
|
+
expectType<typeof value, Hello>('=');
|
|
224
|
+
} else {
|
|
225
|
+
expectType<typeof value, unknown>('=');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
expect(hello.is(value)).toBe(false);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('non-string returns false', () => {
|
|
232
|
+
const value: unknown = 42;
|
|
233
|
+
|
|
234
|
+
expect(hello.is(value)).toBe(false);
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe('special string literals', () => {
|
|
239
|
+
test('empty string literal', () => {
|
|
240
|
+
const empty = stringLiteral('');
|
|
241
|
+
type Empty = TypeOf<typeof empty>;
|
|
242
|
+
expectType<Empty, ''>('=');
|
|
243
|
+
|
|
244
|
+
expect(empty.is('')).toBe(true);
|
|
245
|
+
expect(empty.is(' ')).toBe(false);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test('whitespace literal', () => {
|
|
249
|
+
const whitespace = stringLiteral(' ');
|
|
250
|
+
type Whitespace = TypeOf<typeof whitespace>;
|
|
251
|
+
expectType<Whitespace, ' '>('=');
|
|
252
|
+
|
|
253
|
+
expect(whitespace.is(' ')).toBe(true);
|
|
254
|
+
expect(whitespace.is(' ')).toBe(false);
|
|
255
|
+
expect(whitespace.is(' ')).toBe(false);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test('emoji literal', () => {
|
|
259
|
+
const emoji = stringLiteral('🎉');
|
|
260
|
+
type Emoji = TypeOf<typeof emoji>;
|
|
261
|
+
expectType<Emoji, '🎉'>('=');
|
|
262
|
+
|
|
263
|
+
expect(emoji.is('🎉')).toBe(true);
|
|
264
|
+
expect(emoji.is('🎊')).toBe(false);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('multiline literal', () => {
|
|
268
|
+
const multiline = stringLiteral('line1\nline2');
|
|
269
|
+
type Multiline = TypeOf<typeof multiline>;
|
|
270
|
+
expectType<Multiline, 'line1\nline2'>('=');
|
|
271
|
+
|
|
272
|
+
expect(multiline.is('line1\nline2')).toBe(true);
|
|
273
|
+
expect(multiline.is(String.raw`line1\nline2`)).toBe(false);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe('validate', () => {
|
|
278
|
+
test('exact match', () => {
|
|
279
|
+
const value: unknown = 'hello';
|
|
280
|
+
const result = hello.validate(value);
|
|
281
|
+
|
|
282
|
+
expect(Result.isOk(result)).toBe(true);
|
|
283
|
+
if (Result.isOk(result)) {
|
|
284
|
+
expect(result.value).toBe('hello');
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test('different value', () => {
|
|
289
|
+
const value: unknown = 'world';
|
|
290
|
+
const result = hello.validate(value);
|
|
291
|
+
|
|
292
|
+
expect(Result.isErr(result)).toBe(true);
|
|
293
|
+
if (Result.isErr(result)) {
|
|
294
|
+
expect(result.value).toStrictEqual([
|
|
295
|
+
{
|
|
296
|
+
path: [],
|
|
297
|
+
actualValue: 'world',
|
|
298
|
+
expectedType: 'stringLiteral(hello)',
|
|
299
|
+
typeName: 'stringLiteral(hello)',
|
|
300
|
+
message: undefined,
|
|
301
|
+
},
|
|
302
|
+
]);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
describe('cast', () => {
|
|
308
|
+
test('valid literal returns as is', () => {
|
|
309
|
+
const value: unknown = 'hello';
|
|
310
|
+
const result = hello.cast(value);
|
|
311
|
+
expect(result).toBe('hello');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('invalid value throws error', () => {
|
|
315
|
+
const value: unknown = 'world';
|
|
316
|
+
expect(() => hello.cast(value)).toThrow(
|
|
317
|
+
'Expected stringLiteral(hello), got string',
|
|
318
|
+
);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('fill', () => {
|
|
323
|
+
test('valid literal returns as is', () => {
|
|
324
|
+
const value: unknown = 'hello';
|
|
325
|
+
const result = hello.fill(value);
|
|
326
|
+
expect(result).toBe('hello');
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test('undefined returns default (the literal itself)', () => {
|
|
330
|
+
const value: unknown = undefined;
|
|
331
|
+
const result = hello.fill(value);
|
|
332
|
+
expect(result).toBe('hello');
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('different value returns default', () => {
|
|
336
|
+
const value: unknown = 'world';
|
|
337
|
+
const result = hello.fill(value);
|
|
338
|
+
expect(result).toBe('hello');
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
});
|