Skip to content

Commit 41893ad

Browse files
committed
narrowed down to scope of repeat to only handle a string and a positive integer
1 parent fd74934 commit 41893ad

File tree

2 files changed

+115
-46
lines changed

2 files changed

+115
-46
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
function repeat(str, count) {
2-
if (typeof str !== "string" && typeof str !== "number") {
3-
throw new Error("Input should be a string");
2+
if (typeof str !== "string") {
3+
throw new Error("Input str should be a string in format 'Hello'");
44
}
5-
if (Number(count) < 0 && Number.isInteger(Number(count))) {
5+
if (!Number.isInteger(count) || count <= 0) {
66
throw new Error("Count should be a positive integer number");
77
}
8-
if (typeof count !== "number" && typeof count !== "string") {
9-
throw new Error("Count should be a positive integer number");
10-
}
11-
return String(str).repeat(Number(count));
8+
return str.repeat(count);
129
}
1310

1411
module.exports = repeat;

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

Lines changed: 111 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ test("should return the string 1 time when count is 1", () => {
3030
// case: Handle Count of 0:
3131
// Given a target string str and a count equal to 0,
3232
// When the repeat function is called with these inputs,
33-
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34-
test("should return an empty string when count is 0", () => {
33+
// Then it should throw an error or return an appropriate error message, as repeating a string zero times is not valid.
34+
test("should throw an error when count is 0", () => {
3535
const str = "hello";
3636
const count = 0;
37-
const repeatedStr = repeat(str, count);
38-
expect(repeatedStr).toEqual("");
37+
expect(() => repeat(str, count)).toThrow(
38+
"Count should be a positive integer number"
39+
);
3940
});
4041

4142
// case: Negative Count:
@@ -50,37 +51,73 @@ test("should throw an error when count is negative", () => {
5051
);
5152
});
5253

53-
// case: str is not a string:
54-
// Given a non-string input for str (e.g., a number) and a positive integer count,
54+
// case: count is a decimal number:
55+
// Given a target string str and a decimal number for count,
5556
// When the repeat function is called with these inputs,
56-
// Then it should convert the non-string str to a string and repeat it count times, returning the appropriately repeated string.
57-
test("should convert non-string str to string and repeat it count times", () => {
58-
const str = 123;
59-
const count = 2;
60-
const repeatedStr = repeat(str, count);
61-
expect(repeatedStr).toEqual("123123");
57+
// Then it should throw an error or return an appropriate error message, indicating that the count should be a positive integer number.
58+
test("should throw an error when count is a decimal number", () => {
59+
const str = "hello";
60+
const count = 2.5;
61+
expect(() => repeat(str, count)).toThrow(
62+
"Count should be a positive integer number"
63+
);
6264
});
6365

64-
// case: count is not a number:
65-
// Given a target string str and a non-numeric input for count (e.g., a string that can be converted to a number),
66+
// case: count is not an integer number:
67+
// Given a target string str and a non-integer input for count (e.g., a string or an array),
6668
// When the repeat function is called with these inputs,
67-
// Then it should convert the non-numeric count to a number and repeat the str that many times, returning the appropriately repeated string.
68-
test("should convert non-numeric count to number and repeat the string that many times", () => {
69+
// Then it should throw an error or return an appropriate error message, indicating that the count should be a positive integer number.
70+
test("should throw an error when count is not a number", () => {
6971
const str = "hello";
70-
const count = "3";
71-
const repeatedStr = repeat(str, count);
72-
expect(repeatedStr).toEqual("hellohellohello");
72+
const count = "abc";
73+
expect(() => repeat(str, count)).toThrow(
74+
"Count should be a positive integer number"
75+
);
7376
});
7477

75-
// case: count is a decimal number:
76-
// Given a target string str and a decimal number for count,
77-
// When the repeat function is called with these inputs,
78-
// Then it should round down the count to the nearest integer and repeat the str that many times, returning the appropriately repeated string.
79-
test("should round down decimal count to nearest integer and repeat the string that many times", () => {
78+
// case: count is an array:
79+
test("should throw an error when count is an array", () => {
8080
const str = "hello";
81-
const count = 3.7;
82-
const repeatedStr = repeat(str, count);
83-
expect(repeatedStr).toEqual("hellohellohello");
81+
const count = [2];
82+
expect(() => repeat(str, count)).toThrow(
83+
"Count should be a positive integer number"
84+
);
85+
});
86+
87+
// case: count is an object:
88+
test("should throw an error when count is an object", () => {
89+
const str = "hello";
90+
const count = { num: 2 };
91+
expect(() => repeat(str, count)).toThrow(
92+
"Count should be a positive integer number"
93+
);
94+
});
95+
96+
// case: count is null:
97+
test("should throw an error when count is null", () => {
98+
const str = "hello";
99+
const count = null;
100+
expect(() => repeat(str, count)).toThrow(
101+
"Count should be a positive integer number"
102+
);
103+
});
104+
105+
// case: count is a boolean:
106+
test("should throw an error when count is a boolean", () => {
107+
const str = "hello";
108+
const count = true;
109+
expect(() => repeat(str, count)).toThrow(
110+
"Count should be a positive integer number"
111+
);
112+
});
113+
114+
// case: count is undefined:
115+
test("should throw an error when count is undefined", () => {
116+
const str = "hello";
117+
const count = undefined;
118+
expect(() => repeat(str, count)).toThrow(
119+
"Count should be a positive integer number"
120+
);
84121
});
85122

86123
// case: str is an empty string:
@@ -94,21 +131,56 @@ test("should return an empty string when str is an empty string", () => {
94131
expect(repeatedStr).toEqual("");
95132
});
96133

97-
// case: invalid str and count types:
98-
// Given invalid types for both str (e.g., an object) and count (e.g., an array),
99-
// When the repeat function is called with these inputs,
100-
// Then it should throw an error or return an appropriate error message, indicating that the input types are not supported.
101-
test("should throw an error when str is invalid type", () => {
134+
// case: str is not a string eg: number:
135+
test("should throw an error when str is a number", () => {
136+
const str = 123;
137+
const count = 2;
138+
expect(() => repeat(str, count)).toThrow(
139+
"Input str should be a string in format 'Hello'"
140+
);
141+
});
142+
143+
// case: str is an array:
144+
test("should throw an error when str is an array", () => {
145+
const str = ["hello"];
146+
const count = 2;
147+
expect(() => repeat(str, count)).toThrow(
148+
"Input str should be a string in format 'Hello'"
149+
);
150+
});
151+
152+
// case: str is an object:
153+
test("should throw an error when str is an object", () => {
102154
const str = { text: "hello" };
103-
const count = 3;
104-
expect(() => repeat(str, count)).toThrow("Input should be a string");
155+
const count = 2;
156+
expect(() => repeat(str, count)).toThrow(
157+
"Input str should be a string in format 'Hello'"
158+
);
105159
});
106160

107-
// case: for throw error
108-
test("should throw an error when count is an array", () => {
109-
const str = "hello";
110-
const count = [2];
161+
// case: str is null:
162+
test("should throw an error when str is null", () => {
163+
const str = null;
164+
const count = 2;
111165
expect(() => repeat(str, count)).toThrow(
112-
"Count should be a positive integer number"
166+
"Input str should be a string in format 'Hello'"
167+
);
168+
});
169+
170+
// case: str is a boolean:
171+
test("should throw an error when str is a boolean", () => {
172+
const str = true;
173+
const count = 2;
174+
expect(() => repeat(str, count)).toThrow(
175+
"Input str should be a string in format 'Hello'"
176+
);
177+
});
178+
179+
// case: str is undefined:
180+
test("should throw an error when str is undefined", () => {
181+
const str = undefined;
182+
const count = 2;
183+
expect(() => repeat(str, count)).toThrow(
184+
"Input str should be a string in format 'Hello'"
113185
);
114186
});

0 commit comments

Comments
 (0)