Skip to content

Commit 2beffba

Browse files
committed
Added edge case and invalid input tests and updated function to pass
1 parent 5e7feb2 commit 2beffba

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
function repeat(str, count) {
2-
if (count < 0) {
3-
return "Count must be a non-negative integer"
2+
if ( arguments.length !== 2) {
3+
return "Function requires exactly 2 arguments";
4+
}
5+
if (typeof str !== "string") {
6+
return "First argument must be a string";
7+
}
8+
if (!Number.isInteger(count) || count < 0) {
9+
return "Second argument must be a non-negative integer";
410
}
511
return str.repeat(count);
612
}

Sprint-3/2-practice-tdd/repeat.test.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,56 @@ test("should return empty string if count is 0", () => {
4646
test("should return error message for negative count", () => {
4747
const str = "hello";
4848
const count = -2;
49-
expect(repeat(str, count)).toEqual("Count must be a non-negative integer");
50-
});
49+
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
50+
});
51+
52+
// invalid input tests
53+
test("should return error message for non-integer count", () => {
54+
const str = "hello";
55+
const count = 2.5;
56+
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
57+
});
58+
59+
test("should return error message for non-string input", () => {
60+
const str = 123;
61+
const count = 3;
62+
expect(repeat(str, count)).toEqual("First argument must be a string");
63+
});
64+
65+
test("should return error message for non-string input with invalid count", () => {
66+
const str = { text: "hello" };
67+
const count = -2;
68+
expect(repeat(str, count)).toEqual("First argument must be a string");
69+
});
70+
71+
test("should return error message for string input with non number count", () => {
72+
const str = "hello";
73+
const count = "3";
74+
const count2 = [];
75+
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
76+
expect(repeat(str, count2)).toEqual("Second argument must be a non-negative integer");
77+
});
78+
79+
test("should return error message for string input with NaN count", () => {
80+
const str = "hello";
81+
const count = NaN;
82+
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
83+
});
84+
85+
test("should return error message for string input with null count", () => {
86+
const str = "hello";
87+
const count = null;
88+
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
89+
});
90+
91+
test("should return error message for string input with undefined count", () => {
92+
const str = "hello";
93+
const count = undefined;
94+
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
95+
});
96+
97+
test('should have the correct amount of arguments', () => {
98+
expect(repeat('hello')).toEqual("Function requires exactly 2 arguments");
99+
expect(repeat("hello", 3, 3)).toEqual("Function requires exactly 2 arguments");
100+
})
101+

0 commit comments

Comments
 (0)