Skip to content

Commit 5e7feb2

Browse files
committed
added edge/corner case and invalid input tests and associated code for get ordinal number
1 parent b290716 commit 5e7feb2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Sprint-3/2-practice-tdd/get-ordinal-number.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
function getOrdinalNumber(num) {
2+
if (arguments.length !== 1) {
3+
throw new Error("Function requires exactly one argument");
4+
}
5+
if (typeof num !== "number" || isNaN(num)) {
6+
throw new Error("Input must be a number");
7+
}
8+
if (!Number.isFinite(num)){
9+
throw new Error("Input must be a finite number");
10+
}
11+
if (!Number.isInteger(num) || num < 0) {
12+
throw new Error("Input must be a non-negative integer");
13+
}
14+
15+
216
switch (num % 100) {
317
case 11:
418
case 12:

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,50 @@ test("should return '111th' for 111", () => {
5858

5959
test("should return '0th' for 0", () => {
6060
expect(getOrdinalNumber(0)).toEqual("0th");
61-
});
61+
});
62+
63+
// Extra test to check for correct number of arguments
64+
test("should throw an error if no arguments are provided", () => {
65+
expect(() => getOrdinalNumber()).toThrow("Function requires exactly one argument");
66+
});
67+
68+
test("should throw an error if more than one argument is provided", () => {
69+
expect(() => getOrdinalNumber(1, 2)).toThrow("Function requires exactly one argument");
70+
});
71+
72+
//Invalid input tests
73+
test("should throw an error if the argument is not a number", () => {
74+
expect(() => getOrdinalNumber("a")).toThrow("Input must be a number");
75+
});
76+
77+
test("should throw an error if the argument is a negative number", () => {
78+
expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer");
79+
});
80+
81+
test("should throw an error if the argument is a decimal", () => {
82+
expect(() => getOrdinalNumber(1.5)).toThrow("Input must be a non-negative integer");
83+
});
84+
85+
test("should throw an error if the argument is NaN", () => {
86+
expect(() => getOrdinalNumber(NaN)).toThrow("Input must be a number");
87+
});
88+
89+
test("should throw an error if the argument is Infinity", () => {
90+
expect(() => getOrdinalNumber(Infinity)).toThrow("Input must be a finite number");
91+
});
92+
93+
test("should throw an error if the argument is -Infinity", () => {
94+
expect(() => getOrdinalNumber(-Infinity)).toThrow("Input must be a finite number");
95+
});
96+
test("should throw an error if the argument is an object", () => {
97+
expect(() => getOrdinalNumber({})).toThrow("Input must be a number");
98+
});
99+
test("should throw an error if the argument is an array", () => {
100+
expect(() => getOrdinalNumber([])).toThrow("Input must be a number");
101+
});
102+
test("should throw an error if the argument is null", () => {
103+
expect(() => getOrdinalNumber(null)).toThrow("Input must be a number");
104+
});
105+
test("should throw an error if the argument is undefined", () => {
106+
expect(() => getOrdinalNumber(undefined)).toThrow("Input must be a number");
107+
});

0 commit comments

Comments
 (0)