Skip to content

Commit e736380

Browse files
added test cases for utils
1 parent ccb6032 commit e736380

File tree

2 files changed

+244
-7
lines changed

2 files changed

+244
-7
lines changed

test/unit-tests.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/util.test.js

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
const assert = require('assert');
2+
const {
3+
truncateString,
4+
updateAndParseCompoundParams,
5+
getParameterisedTemplate,
6+
escapeHTML,
7+
categoriesToArray
8+
} = require("../src/utils");
9+
10+
describe('truncateString', () => {
11+
it('should return the original string if its length is less than or equal to the specified length', () => {
12+
const result = truncateString('Hello', 10);
13+
assert.strictEqual(result, 'Hello');
14+
});
15+
16+
it('should truncate the string and add "..." if the string is longer than the specified length', () => {
17+
const result = truncateString('Hello, world!', 5);
18+
assert.strictEqual(result, 'Hello...');
19+
});
20+
21+
it('should handle strings with leading and trailing spaces correctly', () => {
22+
const result = truncateString(' Hello, world! ', 5);
23+
assert.strictEqual(result, 'Hello...');
24+
});
25+
26+
it('should return an empty string if an empty string is provided', () => {
27+
const result = truncateString('', 5);
28+
assert.strictEqual(result, '');
29+
});
30+
31+
it('should return "..." if the specified length is zero', () => {
32+
const result = truncateString('Hello', 0);
33+
assert.strictEqual(result, '...');
34+
});
35+
36+
it('should return "H..." if the specified length is 1 after trimming leading spaces', () => {
37+
const result = truncateString(' Hello', 1);
38+
assert.strictEqual(result, 'H...');
39+
});
40+
41+
it('should correctly handle multi-byte characters', () => {
42+
const result = truncateString('こんにちは世界', 5);
43+
assert.strictEqual(result, 'こんにちは...');
44+
});
45+
});
46+
47+
describe('updateAndParseCompoundParams', () => {
48+
it('should update the object with the correct key-value pair and return the source name when the source has compound parameters', () => {
49+
const obj = {};
50+
const result = updateAndParseCompoundParams('stackoverflow/Comment by $author/', obj);
51+
assert.strictEqual(result, 'stackoverflow');
52+
assert.deepStrictEqual(obj, {'stackoverflow': 'Comment by $author'});
53+
});
54+
55+
it('should return the original source name if the source does not contain exactly 3 parts', () => {
56+
const obj = {};
57+
const result = updateAndParseCompoundParams('github', obj);
58+
assert.strictEqual(result, 'github');
59+
assert.deepStrictEqual(obj, {}); // Ensure obj is not modified
60+
});
61+
62+
it('should handle cases where the source has only 2 parts and return the original source', () => {
63+
const obj = {};
64+
const result = updateAndParseCompoundParams('github/Issue', obj);
65+
assert.strictEqual(result, 'github/Issue');
66+
assert.deepStrictEqual(obj, {}); // Ensure obj is not modified
67+
});
68+
69+
it('should handle cases where the source has more than 3 parts and return the original source', () => {
70+
const obj = {};
71+
const result = updateAndParseCompoundParams('stackoverflow/Comment by $author/extra/part', obj);
72+
assert.strictEqual(result, 'stackoverflow/Comment by $author/extra/part');
73+
assert.deepStrictEqual(obj, {}); // Ensure obj is not modified
74+
});
75+
76+
it('should handle an empty string as the source and not update the object', () => {
77+
const obj = {};
78+
const result = updateAndParseCompoundParams('', obj);
79+
assert.strictEqual(result, '');
80+
assert.deepStrictEqual(obj, {}); // Ensure obj is not modified
81+
});
82+
83+
it('should handle cases where the object is pre-populated', () => {
84+
const obj = {existingKey: 'existingValue'};
85+
const result = updateAndParseCompoundParams('stackoverflow/Comment by $author/', obj);
86+
assert.strictEqual(result, 'stackoverflow');
87+
assert.deepStrictEqual(obj, {existingKey: 'existingValue', 'stackoverflow': 'Comment by $author'});
88+
});
89+
90+
});
91+
92+
describe('getParameterisedTemplate', () => {
93+
it('should return an array of parameters when the template contains a valid key with parameters', () => {
94+
const template = '$randomEmoji(💯,🔥,💫,🚀,🌮) $emojiKey(💯,🔥,💫)';
95+
const result = getParameterisedTemplate(template, 'randomEmoji');
96+
assert.deepStrictEqual(result, ['💯', '🔥', '💫', '🚀', '🌮']);
97+
});
98+
99+
it('should return an array of parameters when the template contains another valid key with parameters', () => {
100+
const template = '$randomEmoji(💯,🔥,💫,🚀,🌮) $emojiKey(💯,🔥,💫)';
101+
const result = getParameterisedTemplate(template, 'emojiKey');
102+
assert.deepStrictEqual(result, ['💯', '🔥', '💫']);
103+
});
104+
105+
it('should return null if the key does not exist in the template', () => {
106+
const template = '$randomEmoji(💯,🔥,💫,🚀,🌮) $emojiKey(💯,🔥,💫)';
107+
const result = getParameterisedTemplate(template, 'missingKey');
108+
assert.strictEqual(result, null);
109+
});
110+
111+
it('should return null if the key exists but is not followed by an opening parenthesis', () => {
112+
const template = '$randomEmoji💯,🔥,💫,🚀,🌮 $emojiKey(💯,🔥,💫)';
113+
const result = getParameterisedTemplate(template, 'randomEmoji');
114+
assert.strictEqual(result, null);
115+
});
116+
117+
it('should handle templates with multiple occurrences of the same key', () => {
118+
const template = '$randomEmoji(💯,🔥) and again $randomEmoji(💫,🚀)';
119+
const result = getParameterisedTemplate(template, 'randomEmoji');
120+
assert.deepStrictEqual(result, ['💯', '🔥']); // Only the first occurrence should be processed
121+
});
122+
123+
it('should correctly parse keys with spaces around parameters', () => {
124+
const template = '$randomEmoji( 💯 , 🔥 , 💫 ) $emojiKey( 💯,🔥 ,💫)';
125+
const result = getParameterisedTemplate(template, 'randomEmoji');
126+
assert.deepStrictEqual(result, ['💯', '🔥', '💫']); // Spaces should be trimmed
127+
});
128+
129+
it('should return null if the template is empty', () => {
130+
const template = '';
131+
const result = getParameterisedTemplate(template, 'randomEmoji');
132+
assert.strictEqual(result, null);
133+
});
134+
135+
it('should return null if the key is empty', () => {
136+
const template = '$randomEmoji(💯,🔥,💫,🚀,🌮)';
137+
const result = getParameterisedTemplate(template, '');
138+
assert.strictEqual(result, null);
139+
});
140+
});
141+
142+
describe('escapeHTML', () => {
143+
it('should escape the ampersand character (&)', () => {
144+
const result = escapeHTML('Rock & Roll');
145+
assert.strictEqual(result, 'Rock & Roll');
146+
});
147+
148+
it('should escape the less-than character (<)', () => {
149+
const result = escapeHTML('5 < 10');
150+
assert.strictEqual(result, '5 &lt; 10');
151+
});
152+
153+
it('should escape the greater-than character (>)', () => {
154+
const result = escapeHTML('10 > 5');
155+
assert.strictEqual(result, '10 &gt; 5');
156+
});
157+
158+
it('should escape the single quote character (\')', () => {
159+
const result = escapeHTML("It's a test");
160+
assert.strictEqual(result, 'It&#39;s a test');
161+
});
162+
163+
it('should escape the double quote character (")', () => {
164+
const result = escapeHTML('She said "Hello"');
165+
assert.strictEqual(result, 'She said &quot;Hello&quot;');
166+
});
167+
168+
it('should escape the opening parenthesis character (()', () => {
169+
const result = escapeHTML('3 * (2 + 1)');
170+
assert.strictEqual(result, '3 * &lpar;2 + 1&rpar;');
171+
});
172+
173+
it('should escape the closing parenthesis character ())', () => {
174+
const result = escapeHTML('Math.PI = 3.14');
175+
assert.strictEqual(result, 'Math.PI = 3.14');
176+
});
177+
178+
it('should escape multiple conflicting characters', () => {
179+
const result = escapeHTML('if (a < b) { return "yes"; } else { return "no"; }');
180+
assert.strictEqual(result, 'if &lpar;a &lt; b&rpar; { return &quot;yes&quot;; } else { return &quot;no&quot;; }');
181+
});
182+
183+
it('should return the original string if there are no conflicting characters', () => {
184+
const result = escapeHTML('No special characters');
185+
assert.strictEqual(result, 'No special characters');
186+
});
187+
188+
it('should return an empty string if the input is an empty string', () => {
189+
const result = escapeHTML('');
190+
assert.strictEqual(result, '');
191+
});
192+
});
193+
194+
describe('categoriesToArray', () => {
195+
it('should return an array of strings when the input is an array of strings', () => {
196+
const categories = ['C#', 'Controller'];
197+
const result = categoriesToArray(categories);
198+
assert.deepStrictEqual(result, ['C#', 'Controller']);
199+
});
200+
it('should return an array of strings when the input is an array of CategoryObj objects', () => {
201+
const categories = [{_: 'C#'}, {_: 'Controller'}];
202+
const result = categoriesToArray(categories);
203+
assert.deepStrictEqual(result, ['C#', 'Controller']);
204+
});
205+
206+
it('should return an array of strings when the input is a mixed array of strings and CategoryObj objects', () => {
207+
const categories = ['Programming', {_: 'C#'}, {_: 'Controller'}, 'Tech'];
208+
const result = categoriesToArray(categories);
209+
assert.deepStrictEqual(result, ['Programming', 'C#', 'Controller', 'Tech']);
210+
});
211+
212+
it('should return an empty array when the input is an empty array', () => {
213+
const categories = [];
214+
const result = categoriesToArray(categories);
215+
assert.deepStrictEqual(result, []);
216+
});
217+
218+
it('should return an empty array when the input is not an array', () => {
219+
const categories = null;
220+
const result = categoriesToArray(categories);
221+
assert.deepStrictEqual(result, []);
222+
});
223+
224+
it('should ignore objects without the "_" property', () => {
225+
const categories = [{_: 'C#'}, {domain: 'http://example.com'}];
226+
const result = categoriesToArray(categories);
227+
assert.deepStrictEqual(result, ['C#']);
228+
});
229+
230+
it('should return an empty array when the input array contains only objects without the "_" property', () => {
231+
const categories = [{domain: 'http://example.com'}, {domain: 'http://example.org'}];
232+
const result = categoriesToArray(categories);
233+
assert.deepStrictEqual(result, []);
234+
});
235+
236+
it('should handle a mix of valid and invalid objects', () => {
237+
const categories = ['Programming', {_: 'C#'}, {domain: 'http://example.com'}, 'Tech'];
238+
const result = categoriesToArray(categories);
239+
assert.deepStrictEqual(result, ['Programming', 'C#', 'Tech']);
240+
});
241+
242+
});
243+
244+

0 commit comments

Comments
 (0)