-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
52 lines (44 loc) · 1.77 KB
/
test.js
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
const assert = require("assert");
require("./index");
describe("Testing splitWidth() prototype", function() {
it("should split a string into substrings of specified width", function() {
const str = "abcdefghij";
const result = str.splitWidth(2);
assert.deepStrictEqual(result, ["ab", "cd", "ef", "gh", "ij"]);
});
it("should handle strings where length is not a multiple of width", function() {
const str = "abcdefgh";
const result = str.splitWidth(3);
assert.deepStrictEqual(result, ["abc", "def", "gh"]);
});
it("should return the entire string if width is equal to the string length", function() {
const str = "abcdef";
const result = str.splitWidth(6);
assert.deepStrictEqual(result, ["abcdef"]);
});
it("should return single characters if width is 1", function() {
const str = "abcdef";
const result = str.splitWidth(1);
assert.deepStrictEqual(result, ["a", "b", "c", "d", "e", "f"]);
});
it("should handle strings with special characters", function() {
const str = "a!@#b$%^c";
const result = str.splitWidth(2);
assert.deepStrictEqual(result, ["a!", "@#", "b$", "%^", "c"]);
});
it("should throw an error if width is less than or equal to 0", function() {
const str = "abcdef";
assert.throws(() => str.splitWidth(0), /Width must be greater than 0/);
assert.throws(() => str.splitWidth(-1), /Width must be greater than 0/);
});
it("should return an empty array if the string is empty", function() {
const str = "";
const result = str.splitWidth(3);
assert.deepStrictEqual(result, []);
});
it("should handle strings with spaces", function() {
const str = "abc def ghi";
const result = str.splitWidth(4);
assert.deepStrictEqual(result, ["abc ", "def ", "ghi"]);
});
});