-
Notifications
You must be signed in to change notification settings - Fork 1
/
linguini.test.ts
153 lines (126 loc) · 5.32 KB
/
linguini.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
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
import { assert, expect } from 'chai';
import path from 'path';
import { Linguini, LinguiniError, TypeMappers } from '../../src';
describe('Linguini', (): void => {
let folderPath = path.join(__dirname, './data');
let fileName = 'lang';
let linguini = new Linguini(folderPath, fileName);
describe('#constructor()', (): void => {
it('Custom common file', (): void => {
let linguini = new Linguini(folderPath, fileName, {
customCommonFile: path.join(
__dirname,
'./data/custom-common-file/custom.common.json'
),
});
let com = linguini.getCom('custom.description');
expect(com).to.equal('This is a custom common file.');
let line = linguini.get('intro.myGitHub', 'en', TypeMappers.String);
expect(line).to.equal('My GitHub is: https://github.com/KevinNovak');
});
it('Custom common file does not exist', (): void => {
let customCommonFile = path.join(
__dirname,
'./data/custom-common-file/does-not-exist.common.json'
);
function myFunction(): void {
new Linguini(folderPath, fileName, {
customCommonFile,
});
}
assert.throw(
myFunction,
LinguiniError,
`Custom common file does not exist: ${customCommonFile}`
);
});
});
describe('#get()', (): void => {
it('Regular expression', (): void => {
let regex = linguini.get('regexes.hello', 'en', TypeMappers.RegExp);
expect(regex.toString()).to.equal('/hello/i');
assert(regex.test('Hello World!'));
assert(!regex.test('Goodbye Cruel World!'));
});
it('Common reference', (): void => {
let line = linguini.get('intro.myGitHub', 'en', TypeMappers.String);
expect(line).to.equal('My GitHub is: https://github.com/KevinNovak');
});
it('Variable', (): void => {
let greetingOne = linguini.get('intro.greeting', 'en', TypeMappers.String, {
FIRST_NAME: 'Bruce',
LAST_NAME: 'Wayne',
});
expect(greetingOne).to.equal('Hello, nice to meet you Bruce Wayne!');
let greetingTwo = linguini.get('intro.greeting', 'en', TypeMappers.String, {
FIRST_NAME: 'Clark',
LAST_NAME: 'Kent',
});
expect(greetingTwo).to.equal('Hello, nice to meet you Clark Kent!');
});
});
describe('#getRaw()', (): void => {
it('Basic example', (): void => {
let json = linguini.getRaw('regexes.hello', 'en');
expect(json.pattern).to.equal('hello');
});
it('Variable', (): void => {
let greetingOne = linguini.getRaw('intro.greeting', 'en', {
FIRST_NAME: 'Bruce',
LAST_NAME: 'Wayne',
});
expect(greetingOne).to.equal('Hello, nice to meet you Bruce Wayne!');
let greetingTwo = linguini.getRaw('intro.greeting', 'en', {
FIRST_NAME: 'Clark',
LAST_NAME: 'Kent',
});
expect(greetingTwo).to.equal('Hello, nice to meet you Clark Kent!');
});
it('Lang file does not exist', (): void => {
function myFunction(): void {
linguini.getRaw('myCategory.myName', 'badLang');
}
assert.throw(myFunction, LinguiniError, 'Invalid language code: badLang');
});
it('Location does not exist', (): void => {
function myFunction(): void {
linguini.getRaw('badCategory.badName', 'en');
}
assert.throw(myFunction, LinguiniError, 'Invalid location: badCategory.badName');
});
});
describe('#getRef()', (): void => {
it('Basic example', (): void => {
let ref = linguini.getRef('aboutMe.favoriteColor', 'en');
expect(ref).to.equal('Blue');
});
it('Common reference', (): void => {
let ref = linguini.getRef('aboutMe.myTwitch', 'en');
expect(ref).to.equal('NovaKevin (https://www.twitch.tv/novakevin)');
});
it('Lang file does not exist', (): void => {
function myFunction(): void {
linguini.getRef('myCategory.myName', 'badLang');
}
assert.throw(myFunction, LinguiniError, 'Invalid language code: badLang');
});
it('Location does not exist', (): void => {
function myFunction(): void {
linguini.getRef('badCategory.badName', 'en');
}
assert.throw(myFunction, LinguiniError, 'Invalid location: badCategory.badName');
});
});
describe('#getCom()', (): void => {
it('Basic example', (): void => {
let com = linguini.getCom('links.github');
expect(com).to.equal('https://github.com/KevinNovak');
});
it('Location does not exist', (): void => {
function myFunction(): void {
linguini.getCom('badCategory.badName');
}
assert.throw(myFunction, LinguiniError, 'Invalid location: badCategory.badName');
});
});
});