-
-
Notifications
You must be signed in to change notification settings - Fork 389
London | 26-ITP-May | Edina Kurdi | Sprint 3 | Implement and rewrite tests #1468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
281f53b
f8431cf
7c5d0b4
0d21142
dec4845
5a195ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,13 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getAngleType(angle) { | ||
| // TODO: Implement this function | ||
| if (angle <= 0 || angle >= 360) return "Invalid angle"; | ||
| if (angle < 90) return "Acute angle"; | ||
| if (angle === 90) return "Right angle"; | ||
| if (angle < 180) return "Obtuse angle"; | ||
| if (angle === 180) return "Straight angle"; | ||
| if (angle < 360) return "Reflex angle"; | ||
| } | ||
|
|
||
| // The line below allows us to load the getAngleType function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getAngleType; | ||
|
|
@@ -35,3 +39,47 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Example: Identify Right Angles | ||
| const right = getAngleType(90); | ||
| assertEquals(right, "Right angle"); | ||
|
|
||
| const straight = getAngleType(180); | ||
| assertEquals(straight, "Straight angle"); | ||
|
|
||
| const acute_upper_boundary = getAngleType(89); | ||
| assertEquals(acute_upper_boundary, "Acute angle"); | ||
|
|
||
| const acute_lower_boundary = getAngleType(1); | ||
| assertEquals(acute_lower_boundary, "Acute angle"); | ||
|
|
||
| const invalid_negative = getAngleType(-1); | ||
| assertEquals(invalid_negative, "Invalid angle"); | ||
|
|
||
| const invalid_zero = getAngleType(0); | ||
| assertEquals(invalid_zero, "Invalid angle"); | ||
|
|
||
| const invalid_full = getAngleType(360); | ||
| assertEquals(invalid_full, "Invalid angle"); | ||
|
|
||
| const invalid_big = getAngleType(361); | ||
| assertEquals(invalid_big, "Invalid angle"); | ||
|
|
||
| const obtuse_lower_boundary = getAngleType(91); | ||
| assertEquals(obtuse_lower_boundary, "Obtuse angle"); | ||
|
|
||
| const obtuse_upper_boundary = getAngleType(179); | ||
| assertEquals(obtuse_upper_boundary, "Obtuse angle"); | ||
|
|
||
| const reflex_lower_boundary = getAngleType(181); | ||
| assertEquals(reflex_lower_boundary, "Reflex angle"); | ||
|
|
||
| const reflex_upper_boundary = getAngleType(359); | ||
| assertEquals(reflex_upper_boundary, "Reflex angle"); | ||
|
|
||
| //decimal test cases | ||
|
|
||
| const acute_decimal = getAngleType(89.9); | ||
| assertEquals(acute_decimal, "Acute angle"); | ||
|
|
||
| const obtuse_decimal = getAngleType(179.9); | ||
| assertEquals(obtuse_decimal, "Obtuse angle"); | ||
|
|
||
| const reflex_decimal = getAngleType(180.1); | ||
| assertEquals(reflex_decimal, "Reflex angle"); | ||
|
Comment on lines
+78
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch to also test decimal values |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,10 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| if (denominator === 0 || numerator === 0) return false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can the numerator not be 0? |
||
| if (!(denominator % 1 === 0 && numerator % 1 === 0)) return false; | ||
| if (Math.abs(numerator) < Math.abs(denominator)) return true; | ||
| else return false; | ||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
|
|
@@ -28,6 +31,25 @@ function assertEquals(actualOutput, targetOutput) { | |
|
|
||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
|
|
||
| // definition is |numerator| < |denominator| so negative values need to be tested as well; also 0 for either - wouldn't be a fraction (or a valid fraction if its the den); also when the num = den | ||
| // Example: 1/2 is a proper fraction | ||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(-3, 4), true); | ||
| assertEquals(isProperFraction(3, -4), true); | ||
|
|
||
| //numerator or denominator is 0 | ||
| assertEquals(isProperFraction(0, 2), false); | ||
| assertEquals(isProperFraction(2, 0), false); | ||
|
|
||
| //result is a whole number | ||
| assertEquals(isProperFraction(-2, 2), false); | ||
| assertEquals(isProperFraction(-3, -3), false); | ||
| assertEquals(isProperFraction(3, 3), false); | ||
|
|
||
| //numerator or denominator is a decimal | ||
| assertEquals(isProperFraction(1.2, 3), false); | ||
| assertEquals(isProperFraction(1, 1.2), false); | ||
|
|
||
| //numerator or denominator is negative | ||
| assertEquals(isProperFraction(-4, 5), true); | ||
| assertEquals(isProperFraction(-5, 4), false); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,26 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const suits = ["♠", "♥", "♦", "♣"]; | ||
| if (card[0] === "A" && suits.includes(card[1]) && card.length === 2) | ||
| return 11; | ||
| if ( | ||
| (card[0] === "J" || card[0] === "Q" || card[0] === "K") && | ||
| suits.includes(card[1]) && | ||
| card.length === 2 | ||
| ) | ||
| return 10; | ||
| if ( | ||
| ["2", "3", "4", "5", "6", "7", "8", "9"].includes(card[0]) && | ||
| suits.includes(card[1]) && | ||
| card.length === 2 | ||
| ) | ||
| return Number(card[0]); | ||
| if (card.slice(0, 2) == "10" && suits.includes(card[2]) && card.length === 3) | ||
| return 10; | ||
| throw new Error( | ||
| 'Invalid input: Expected rank followed by a suit symbol. For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦"' | ||
|
Comment on lines
+26
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the code hard to read. I like the allowed suits definition. Can you do something similar with the numbers. How can you determine suit and rank of a card. (A different way then access by index might be simpler) |
||
| ); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -40,6 +59,11 @@ function assertEquals(actualOutput, targetOutput) { | |
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("K♣"), 10); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("2♦"), 2); | ||
| assertEquals(getCardValue("7♦"), 7); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
|
|
@@ -52,3 +76,62 @@ try { | |
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| // for instance "1♦" where it starts with 1 but its not 10 | ||
| // where it includes the correct answer but there are added characters maybe space(s) like "9♠ " | ||
| //where its the opposite way : "♠9" | ||
| //where it starts with a valid number but has otherwise more digits eg: "9♠♠" or "99♠" | ||
| //where there is a different symbol from the 4 given such as ❤️ | ||
|
|
||
| try { | ||
| getCardValue("1♥"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("9♠ "); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("♠9"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("99♠"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("9♠♠"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("9❤️"); | ||
|
Comment on lines
+95
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work. You found a lot of error cases |
||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,37 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { | |
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| test('should return "Right angle" when (angle = 90)', () => { | ||
| // Test right angle | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| // Case 3: Obtuse angles | ||
| test('should return "Obtuse angle" when (90 < angle < 180', () => { | ||
| // Test various obtuse angles, including boundary cases | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(100)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| // Case 4: Straight angle | ||
| test('should return "Straight angle" when (angle = 180)', () => { | ||
| // Test straight angle | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
|
|
||
| // Case 5: Reflex angles | ||
| test('should return "Reflex angle" when (180 < angle < 360', () => { | ||
| // Test various reflex angles, including boundary cases | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| expect(getAngleType(300)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| // Case 6: Invalid angles | ||
| test('Should return "Invalid angle" when the input is invalid', () => { | ||
| expect(getAngleType(-1)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(361)).toEqual("Invalid angle"); | ||
|
Comment on lines
+19
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well done on testing all the boundaries |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NNice touch using the variable names to explain the test case. This makes it easier to understand