-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphabeticString.test.ts
56 lines (49 loc) · 1.4 KB
/
AlphabeticString.test.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
import { assertEquals, assertThrows } from '@std/assert';
import { AlphabeticString } from './AlphabeticString.ts';
Deno.test('AlphabeticString - validates mixed-case letters', () =>
{
const validInputs = [
'Hello',
'WORLD',
'CamelCase',
'abc',
'ABC',
'Z',
];
for (const input of validInputs)
{
const result = AlphabeticString.try(input);
assertEquals(result, input);
}
});
Deno.test('AlphabeticString - rejects invalid inputs', () =>
{
const invalidInputs = [
'Hello123', // numbers
'Hello!', // special characters
'Hello World', // spaces
'', // empty string
'123', // only numbers
'_abc', // underscores
];
for (const input of invalidInputs)
{
const result = AlphabeticString.try(input);
assertEquals(result, undefined);
}
});
Deno.test('AlphabeticString - assert throws with descriptive message', () =>
{
assertThrows(
() => AlphabeticString.assert('Hello123!'),
Error,
'Invalid string did not pass validation: Supplied value "Hello123!" is not valid for validator "AlphabeticString" (must contain only letters (A-Z, a-z)).',
);
});
Deno.test('AlphabeticString - type safety', () =>
{
// @ts-expect-error Type 'string' is not assignable to type 'AlphabeticString'
const _invalid: AlphabeticString = 'Hello123';
const valid: AlphabeticString = AlphabeticString.assert('Hello');
assertEquals(valid, 'Hello');
});