/
assertName-test.ts
69 lines (58 loc) · 2.05 KB
/
assertName-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 { expect } from 'chai';
import { describe, it } from 'mocha';
import { assertName, assertEnumValueName } from '../assertName';
describe('assertName()', () => {
it('passthrough valid name', () => {
expect(assertName('_ValidName123')).to.equal('_ValidName123');
});
it('throws for non-strings', () => {
// @ts-expect-error
expect(() => assertName({})).to.throw('Expected name to be a string.');
});
it('throws on empty strings', () => {
expect(() => assertName('')).to.throw(
'Expected name to be a non-empty string.',
);
});
it('throws for names with invalid characters', () => {
expect(() => assertName('>--()-->')).to.throw(
'Names must only contain [_a-zA-Z0-9] but ">--()-->" does not.',
);
});
it('throws for names starting with invalid characters', () => {
expect(() => assertName('42MeaningsOfLife')).to.throw(
'Names must start with [_a-zA-Z] but "42MeaningsOfLife" does not.',
);
});
});
describe('assertEnumValueName()', () => {
it('passthrough valid name', () => {
expect(assertEnumValueName('_ValidName123')).to.equal('_ValidName123');
});
it('throws on empty strings', () => {
expect(() => assertEnumValueName('')).to.throw(
'Expected name to be a non-empty string.',
);
});
it('throws for names with invalid characters', () => {
expect(() => assertEnumValueName('>--()-->')).to.throw(
'Names must only contain [_a-zA-Z0-9] but ">--()-->" does not.',
);
});
it('throws for names starting with invalid characters', () => {
expect(() => assertEnumValueName('42MeaningsOfLife')).to.throw(
'Names must start with [_a-zA-Z] but "42MeaningsOfLife" does not.',
);
});
it('throws for restricted names', () => {
expect(() => assertEnumValueName('true')).to.throw(
'Enum values cannot be named: true',
);
expect(() => assertEnumValueName('false')).to.throw(
'Enum values cannot be named: false',
);
expect(() => assertEnumValueName('null')).to.throw(
'Enum values cannot be named: null',
);
});
});