Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Implement a function getAngleType
//
// Don't forget to write tests in implement-tests-with-node-test.
//
// When given an angle in degrees, it should return a string indicating the type of angle:
// - "Acute angle" for angles greater than 0° and less than 90°
// - "Right angle" for exactly 90°
Expand All @@ -16,6 +14,54 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
function getAngleType(angle) {
if (angle > 0 && angle < 90) {
return `Acute angle`;
} else if (angle === 90) {
return `Right angle`;
} else if (angle > 90 && angle < 180) {
return `Obtuse angle`;
} else if (angle === 180) {
return `Straight angle`;
} else if (angle > 180 && angle < 360) {
return `Reflex angle`;
} else {
return `Invalid 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;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const acute = getAngleType(78);
assertEquals(acute, "Acute angle");

const obtuse = getAngleType(102);
assertEquals(obtuse, "Obtuse angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const reflex = getAngleType(250);
assertEquals(reflex, "Reflex angle");

const invalid = getAngleType(420);
assertEquals(invalid, "Invalid angle");

const zero = getAngleType(0);
assertEquals(zero, "Invalid angle");
38 changes: 32 additions & 6 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Implement a function isProperFraction,
//
// Don't forget to write tests in implement-tests-with-node-test.
//
// When given two numbers, a numerator and a denominator, it should return true if
// when given two numbers, a numerator and a denominator, it should return true if
// the given numbers form a proper fraction, and false otherwise.

// Assumption: The parameters are valid numbers (not NaN or Infinity).
Expand All @@ -13,6 +10,35 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
function isProperFraction(numerator, denominator) {
// Non-unit fractions have a numerator that is more than 1, but less than the denominator.
//Unit fractions all have a numerator of 1.

let validFraction =
Math.abs(numerator) < Math.abs(denominator) && Math.abs(numerator) > 0;
return validFraction;
}

// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(1, 5), true);
assertEquals(isProperFraction(0, 4), false);
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(6, 5), false);
assertEquals(isProperFraction(3, 5), true);
assertEquals(isProperFraction(1, 0), false);
82 changes: 78 additions & 4 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// Don't forget to write tests in implement-tests-with-node-test.

// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.

Expand All @@ -23,6 +21,82 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

export function getCardValue(card) {
// TODO: Implement this function
function getCardValue(card) {
let regex = /^([2-9]|10|[AJQK])[♠♥♣♦]$/;
const valid = regex.test(card);
if (!valid) {
throw new Error("Invalid card");
}
let rank = card.slice(0, -1);

if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
rank = Number(rank);
return rank;
}
}

// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getCardValue;

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("K♠"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);

// Handling invalid cards
try {
getCardValue("invalid");

// 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 🎉");
}

// What other invalid card cases can you think of?

try {
getCardValue(3);
console.error("Error was not thrown for number 3");
} catch (e) {
console.log("Error thrown for invalid format");
}

try {
getCardValue("11K♠");
console.error("Error format too long");
} catch (e) {
console.log("Error thrown for too many characters");
}

try {
getCardValue(-1);
console.error("Error for negative number");
} catch (e) {
console.log("Error thrown for negative number");
}

try {
getCardValue("22♥");
console.error("Error for string 22♥");
} catch (e) {
console.log("Error thrown for string 22♥");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getAngleType } from "../implement/1-get-angle-type.js";
// This statement loads the getAngleType function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.
Expand All @@ -8,11 +10,37 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(77)).toEqual("Acute angle");
expect(getAngleType(24)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
test(`should return "Right angle" when(angle === 90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (angle > 90 && angle <180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(133)).toEqual("Obtuse angle");
expect(getAngleType(111)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when (angle ===180)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (angle > 180 && angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
expect(getAngleType(199)).toEqual("Reflex angle");
expect(getAngleType(244)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angle"`, () => {
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType(2332)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType("-222r")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import { isProperFraction } from "../implement/2-is-proper-fraction.js";
// This statement loads the isProperFraction function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const isProperFraction = require("../implement/2-is-proper-fraction");

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

// Special case: numerator is zero

test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test(`should return true when input (1, 2)`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});
test(`should return false when (numerator = 0)`, () => {
expect(isProperFraction(0, 4)).toEqual(false);
});
test(`should return false when (numerator===denominator)`, () => {
expect(isProperFraction(5, 5)).toEqual(false);
});
test(`should return false when (numerator>denominator)`, () => {
expect(isProperFraction(6, 5)).toEqual(false);
});
test(`should return false when (numerator ===0 && denominator === 0)`, () => {
expect(isProperFraction(0, 0)).toEqual(false);
});
test(`should return true when numerator is negative number`, () => {
expect(isProperFraction(-4, 8)).toEqual(true);
});
test(`should return false when numerator and denominator have equal absolute values `, () => {
expect(isProperFraction(-5, 5)).toEqual(false);
});
test(`should return true when denominator is negative `, () => {
expect(isProperFraction(3, -5)).toEqual(true);
});
test(`should return false when numerator is zero and denominator is negative zero`, () => {
expect(isProperFraction(0, -0)).toEqual(false);
});
test(`should return true when numerator and denominator are negative`, () => {
expect(isProperFraction(-3, -5)).toEqual(true);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getCardValue } from "../implement/3-get-card-value.js";
// This statement loads the getCardValue function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getCardValue = require("../implement/3-get-card-value");

// TODO: Write tests in Jest syntax to cover all possible outcomes.

Expand All @@ -9,10 +11,36 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
let suits = ["♠", "♥", "♣", "♦"];
for (let number = 2; number <= 10; number++) {
for (let suit of suits) {
test(`should return card value as number`, () => {
expect(getCardValue(`${number}${suit}`)).toEqual(number);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the effort gone to test these thoroughly, but one thing to be careful of is if you make your tests so complicated that the tests themselves need testing. As a general rule, it's better to pick out specific cases that test the general input and the edge cases rather than needing to create loops and data structures to test every single possible input.

Do you have nay thoughts on the approach you used here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I wanted to check every possible number and suit to make sure they all worked correctly. I figured that using a loop would be the most efficient approach, but I can see how testing a few specific cases would make testing simpler. Thank you for the feedback.

});
}
}
// Face Cards (J, Q, K)

let chars = ["J", "Q", "K"];

for (let char of chars) {
for (let suit of suits) {
test(`should return card value as 10`, () => {
expect(getCardValue(`${char}${suit}`)).toEqual(10);
});
}
}
// Invalid Cards

let invalidCards = ["22♥", "11K♠", -1, 3, "11^♠"];
for (let card of invalidCards) {
test(`should return "Invalid card"`, () => {
expect(() => {
getCardValue(card);
}).toThrow();
});
}

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

Loading