Skip to content

Commit 54a5617

Browse files
committed
Testing: ordinal numbers
1 parent 483e8fd commit 54a5617

File tree

1 file changed

+115
-20
lines changed

1 file changed

+115
-20
lines changed
Lines changed: 115 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,123 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber.
32

4-
// Continue testing and implementing getOrdinalNumber for additional cases.
5-
// Write your tests using Jest — remember to run your tests often for continual feedback.
6-
7-
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
8-
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
9-
// Instead of writing tests for individual numbers, consider grouping all possible input values
10-
// into meaningful categories. Then, select representative samples from each category to test.
11-
// This approach improves coverage and makes our tests easier to maintain.
12-
13-
// Case 1: Numbers ending with 1 (but not 11)
14-
// When the number ends with 1, except those ending with 11,
15-
// Then the function should return a string by appending "st" to the number.
3+
// Case 1: Numbers ending with 1 (but not in 11)
164
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
5+
const numbers = [1, 21, 131];
6+
7+
for (const number of numbers) {
8+
expect(getOrdinalNumber(number)).toEqual(`${number}st`);
9+
}
2010
});
2111

12+
// Case 2: Numbers ending with 11
2213
test("should append 'th' for numbers ending with 11", () => {
23-
expect(getOrdinalNumber(11)).toEqual("11th");
24-
expect(getOrdinalNumber(11)).toEqual("11th");
25-
expect(getOrdinalNumber(700011)).toEqual("700011th");
14+
const numbers = [11, 111, 700011];
15+
16+
for (const number of numbers) {
17+
expect(getOrdinalNumber(number)).toEqual(`${number}th`);
18+
}
19+
});
20+
21+
// Case 3: Numbers ending with 2 (but not in 12)
22+
test("should append 'nd' for numbers ending in 2", () => {
23+
const numbers = [2, 22, 32, 502];
24+
25+
for (const number of numbers) {
26+
expect(getOrdinalNumber(number)).toEqual(`${number}nd`);
27+
}
2628
});
2729

28-
test();
30+
// Case 4: Numbers ending with 12
31+
test("should append 'th' for numbers ending in 12", () => {
32+
const numbers = [12, 212, 312, 5012];
33+
34+
for (const number of numbers) {
35+
expect(getOrdinalNumber(number)).toEqual(`${number}th`);
36+
}
37+
});
38+
39+
// Case 5: Numbers ending in 3 (but not in 13)
40+
test("should append 'rd' for numbers ending in 3 (but not 13)", () => {
41+
const numbers = [3, 23, 33, 1063];
42+
43+
for (const number of numbers) {
44+
expect(getOrdinalNumber(number)).toEqual(`${number}rd`);
45+
}
46+
});
47+
48+
// Case 6: Numbers ending in 13
49+
test("should append 'th' for numbers ending in 13", () => {
50+
const numbers = [13, 113, 613, 713];
51+
52+
for (const number of numbers) {
53+
expect(getOrdinalNumber(number)).toEqual(`${number}th`);
54+
}
55+
});
56+
57+
// Case 7: Numbers ending in 4-10
58+
test("should append 'th' for numbers ending in 4-10", () => {
59+
const lastDigits = [4, 5, 6, 7, 8, 9, 10];
60+
const bases = [0, 10, 50, 90, 100];
61+
62+
for (const lastDigit of lastDigits) {
63+
for (const base of bases) {
64+
const finalNumber = base + lastDigit;
65+
expect(getOrdinalNumber(finalNumber)).toEqual(`${finalNumber}th`);
66+
}
67+
}
68+
});
69+
70+
// Case 8: Negative numbers
71+
test("should return ordinal number for negative integer input", () => {
72+
const ordinals = [
73+
{ a: -2, b: "-2nd" },
74+
{ a: -1, b: "-1st" },
75+
{ a: -3, b: "-3rd" },
76+
{ a: -4, b: "-4th" },
77+
{ a: -11, b: "-11th" },
78+
{ a: -12, b: "-12th" },
79+
{ a: -13, b: "-13th" },
80+
];
81+
82+
for (const { a, b } of ordinals) {
83+
expect(getOrdinalNumber(a)).toEqual(b);
84+
}
85+
});
86+
87+
// Case 9: Decimal representations of integers
88+
test("should return ordinal number for integer in decimal form", () => {
89+
const ordinals = [
90+
{ a: 1.0, b: "1st" },
91+
{ a: 2.0, b: "2nd" },
92+
{ a: 3.0, b: "3rd" },
93+
{ a: 4.0, b: "4th" },
94+
{ a: -11.0, b: "-11th" },
95+
{ a: -12.0, b: "-12th" },
96+
{ a: -13.0, b: "-13th" },
97+
];
98+
99+
for (const { a, b } of ordinals) {
100+
expect(getOrdinalNumber(a)).toEqual(b);
101+
}
102+
});
103+
104+
// Case 10: Invalid entries
105+
test("should throw error for invalid inputs", () => {
106+
const invalidEntries = [
107+
"one",
108+
"",
109+
[1],
110+
null,
111+
undefined,
112+
9.5,
113+
8.00001,
114+
{},
115+
true,
116+
];
117+
118+
for (const entry of invalidEntries) {
119+
expect(() => {
120+
getOrdinalNumber(entry);
121+
}).toThrow();
122+
}
123+
});

0 commit comments

Comments
 (0)