Skip to content

Commit eefb34c

Browse files
committed
mocha: more Util.ts unit tests
Signed-off-by: Dennis Francis <[email protected]> Change-Id: I18311f09a63e793fb58b4de44c0f6c58c69f0883
1 parent c0ba8e9 commit eefb34c

File tree

1 file changed

+156
-2
lines changed

1 file changed

+156
-2
lines changed

browser/mocha_tests/Util.test.ts

Lines changed: 156 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,165 @@ describe('Util static members tests', function () {
5151
});
5252

5353
it('decimal with 1 decimal places round', function () {
54-
assertFloat(Util.formatNum(5.35333333, 1), 5.3, 1e-5, '');
54+
assertFloat(Util.formatNum(5.35333333, 1), 5.4, 1e-5, '');
5555
});
5656

57-
it('decimal with 4 decimal places no-round', function () {
57+
it('decimal with 4 decimal places round', function () {
5858
assertFloat(Util.formatNum(5.30335333, 4), 5.3034, 1e-5, '');
5959
});
6060
});
61+
62+
describe('trimStart()', function () {
63+
it('whole string is prefix', function () {
64+
assert.strictEqual(Util.trimStart('ABC', 'ABC'), '');
65+
});
66+
67+
it('whole string shorter than prefix', function () {
68+
assert.strictEqual(Util.trimStart('ABC', 'ABCD'), 'ABC');
69+
});
70+
71+
it('No prefix', function () {
72+
assert.strictEqual(Util.trimStart('XYZ', 'ABCD'), 'XYZ');
73+
});
74+
75+
it('Multi prefix', function () {
76+
assert.strictEqual(Util.trimStart('ABCDABCDXYZ', 'ABCD'), 'ABCDXYZ');
77+
});
78+
});
79+
80+
describe('trimEnd()', function () {
81+
it('whole string is suffix', function () {
82+
assert.strictEqual(Util.trimEnd('ABC', 'ABC'), '');
83+
});
84+
85+
it('whole string shorter than suffix', function () {
86+
assert.strictEqual(Util.trimEnd('ABC', 'ABCD'), 'ABC');
87+
});
88+
89+
it('No suffix', function () {
90+
assert.strictEqual(Util.trimEnd('XYZ', 'ABCD'), 'XYZ');
91+
});
92+
93+
it('Multi suffix', function () {
94+
assert.strictEqual(Util.trimEnd('XYZABCDABCD', 'ABCD'), 'XYZABCD');
95+
});
96+
});
97+
98+
describe('trim()', function () {
99+
it('trim() with no prefix or suffix argument', function () {
100+
assert.strictEqual(Util.trim('\t \tCONTENT \t\t \t'), 'CONTENT');
101+
});
102+
103+
it('whole string is prefix', function () {
104+
assert.strictEqual(Util.trim('ABC', 'ABC'), '');
105+
});
106+
107+
it('whole string shorter than prefix', function () {
108+
assert.strictEqual(Util.trim('ABC', 'ABCD'), 'ABC');
109+
});
110+
111+
it('whole string is suffix', function () {
112+
assert.strictEqual(Util.trim('ABC', ' ', 'ABC'), '');
113+
});
114+
115+
it('whole string shorter than suffix', function () {
116+
assert.strictEqual(Util.trim('ABC', '', 'ABCD'), 'ABC');
117+
});
118+
119+
it('No prefix', function () {
120+
assert.strictEqual(Util.trim('XYZ', 'ABCD'), 'XYZ');
121+
});
122+
123+
it('No suffix', function () {
124+
assert.strictEqual(Util.trim('XYZ', '', 'ABCD'), 'XYZ');
125+
});
126+
127+
it('Multi prefix and suffix', function () {
128+
assert.strictEqual(Util.trim('ABCDABCDXYZABCDABCD', 'ABCD', 'ABCD'), 'ABCDXYZABCD');
129+
});
130+
131+
it('Overlapping prefix and suffix', function () {
132+
assert.strictEqual(Util.trim('ABCDAB', 'ABCD', 'CDAB'), 'AB');
133+
});
134+
});
135+
136+
describe('splitWords()', function () {
137+
it('split empty string', function () {
138+
assert.deepEqual(Util.splitWords(''), ['']);
139+
});
140+
141+
it('split string with white spaces', function () {
142+
assert.deepEqual(Util.splitWords(' \t \t\t '), ['']);
143+
});
144+
145+
it('split string with single word', function () {
146+
assert.deepEqual(Util.splitWords('ABC'), ['ABC']);
147+
});
148+
149+
it('split string with single word surrounded by multi white-spaces', function () {
150+
assert.deepEqual(Util.splitWords(' \t \t \t\t ABC\t \t\t \t'), ['ABC']);
151+
});
152+
153+
it('split string with two words', function () {
154+
assert.deepEqual(Util.splitWords(' \t \t \t\t ABC\t \t\t \tXYZ \t\t \t'), ['ABC', 'XYZ']);
155+
});
156+
});
157+
158+
describe('round()', function() {
159+
it('integer with no decimal places', function () {
160+
assertFloat(Util.round(5), 5, 1e-5, '');
161+
});
162+
163+
it('integer with 4 decimal places', function () {
164+
assertFloat(Util.round(5, 1e-4), 5, 1e-5, '');
165+
});
166+
167+
it('decimal with 1 decimal places no-round', function () {
168+
assertFloat(Util.round(5.30333333, 0.1), 5.3, 1e-5, '');
169+
});
170+
171+
it('decimal with 4 decimal places no-round', function () {
172+
assertFloat(Util.round(5.30333333, 0.0001), 5.3033, 1e-5, '');
173+
});
174+
175+
it('decimal with 1 decimal places round', function () {
176+
assertFloat(Util.round(5.35333333, 0.1), 5.4, 1e-5, '');
177+
});
178+
179+
it('decimal with 4 decimal places round', function () {
180+
assertFloat(Util.round(5.30335333, 0.0001), 5.3034, 1e-5, '');
181+
});
182+
});
183+
184+
describe('template()', function () {
185+
it('empty string', function () {
186+
assert.strictEqual(Util.template('', {}), '');
187+
});
188+
189+
it('no substitutions', function () {
190+
assert.strictEqual(Util.template('cool apps', {'cool': 32}), 'cool apps');
191+
});
192+
193+
it('one key one substitution', function () {
194+
assert.strictEqual(Util.template('cool { app } abcd', {'cool': 32, 'app': 'calc'}), 'cool calc abcd');
195+
});
196+
197+
it('one key two substitutions', function () {
198+
assert.strictEqual(Util.template('A {app } cool { app} abcd', {'cool': 32, 'app': 'calc'}), 'A calc cool calc abcd');
199+
});
200+
201+
it('two keys multiple substitutions', function () {
202+
assert.strictEqual(Util.template('A) { app1}, B) {app2 }, C) { app2}, D) { app1 } ', {'cool': 32, 'app': 'calc', 'app1': 'draw', 'app2': 'impress'}), 'A) draw, B) impress, C) impress, D) draw ');
203+
});
204+
205+
it('key function', function () {
206+
assert.strictEqual(Util.template('{fkey }, { key }', {
207+
'key': '1234',
208+
'fkey': function(data: any) {
209+
return data['key'] + '_999';
210+
},
211+
}), '1234_999, 1234');
212+
});
213+
});
214+
61215
});

0 commit comments

Comments
 (0)