-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphabeticOrUnderscoreOrHyphenString.test.ts
69 lines (62 loc) · 1.98 KB
/
AlphabeticOrUnderscoreOrHyphenString.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
57
58
59
60
61
62
63
64
65
66
67
68
69
import { assertEquals, assertThrows } from '@std/assert';
import { AlphabeticOrUnderscoreOrHyphenString } from './AlphabeticOrUnderscoreOrHyphenString.ts';
Deno.test('AlphabeticOrUnderscoreOrHyphenString - validates letters, underscores, and hyphens', () =>
{
const validInputs = [
'Hello',
'WORLD',
'snake_case',
'kebab-case',
'mixed_snake-kebab',
'SCREAMING_SNAKE-KEBAB',
'abc',
'ABC',
'_leading',
'-leading',
'trailing_',
'trailing-',
'_',
'-',
'__double__underscore__',
'--double--hyphen--',
];
for (const input of validInputs)
{
const result = AlphabeticOrUnderscoreOrHyphenString.try(input);
assertEquals(result, input);
}
});
Deno.test('AlphabeticOrUnderscoreOrHyphenString - rejects invalid inputs', () =>
{
const invalidInputs = [
'Hello123', // numbers
'Hello!', // special characters
'Hello World', // spaces
'', // empty string
'123', // only numbers
'.abc', // periods
'dot.case', // periods
];
for (const input of invalidInputs)
{
const result = AlphabeticOrUnderscoreOrHyphenString.try(input);
assertEquals(result, undefined);
}
});
Deno.test('AlphabeticOrUnderscoreOrHyphenString - assert throws with descriptive message', () =>
{
assertThrows(
() => AlphabeticOrUnderscoreOrHyphenString.assert('Hello.World123!'),
Error,
'Invalid string did not pass validation: Supplied value "Hello.World123!" is not valid for validator "AlphabeticOrUnderscoreOrHyphenString" (must contain only letters (A-Z, a-z), underscores, or hyphens).',
);
});
Deno.test('AlphabeticOrUnderscoreOrHyphenString - type safety', () =>
{
// @ts-expect-error Type 'string' is not assignable to type 'AlphabeticOrUnderscoreOrHyphenString'
const _invalid: AlphabeticOrUnderscoreOrHyphenString = 'Hello.World';
const valid: AlphabeticOrUnderscoreOrHyphenString = AlphabeticOrUnderscoreOrHyphenString.assert(
'Hello-World_Example',
);
assertEquals(valid, 'Hello-World_Example');
});