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
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import {getAngleType} from "../implement/1-get-angle-type.js";
import assert from "node:assert";
import test from "node:test";

import { getAngleType } from "../implement/1-get-angle-type.js";
test("corrctly get angle type", function(){
assert.equal(getAngleType(75), "Acute angle");
});

test("corrctly get angle type", function(){
assert.equal(getAngleType(90), "Right angle");
});

test("corrctly get angle type", function(){
assert.equal(getAngleType(120), "Obtuse angle");
});

test("corrctly get angle type", function(){
assert.equal(getAngleType(180), "Straight angle");
});

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
test("corrctly get angle type", function(){
assert.equal(getAngleType(210), "Reflex angle");
});

test("corrctly get angle type", function(){
assert.equal(getAngleType(400), "Invalid angle");
});
Comment on lines +25 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be two boundary cases for invalid angle. Can you think of them? Please add them back.


test("corrctly get angle type", function(){
assert.equal(getAngleType(0), "Invalid angle");
});

test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
test("corrctly get angle type", function(){
assert.equal(getAngleType(360), "Invalid angle");
});

test("corrctly get angle type", function(){
assert.equal(getAngleType(-70), "Invalid angle");
});

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
// What combinations of numerators and denominators should you test?

test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

test("Basic proper fraction", () => {
assert.equal(isProperFraction(2, 1), false);
});

test("Basic proper fraction", () => {
assert.equal(isProperFraction(1, 0), false);
});

test("Basic proper fraction", () => {
assert.equal(isProperFraction(0, 1), true);
});

test("Basic proper fraction", () => {
assert.equal(isProperFraction(1, 1), false);
});

test("Basic proper fraction", () => {
assert.equal(isProperFraction(-1, 2), true);
});
Comment on lines +29 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good coverage for positive numbers. However, there should be more test cases for negative numbers. Please add them. Thank you.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For positive number cases, you have:

  • numerator smaller than denominator
  • numerator equal to denominator
  • numerator greater than denominator

For zero cases, you have:

  • numerator is zero
  • denominator is zero

For mixed of positive and negative numbers, you have (-1, 2) and (1, -2) which means there are two combinations still not tested yet;
and for negative number cases, you just have (-1, -2) which also means one test is still missing.

You are almost there, so just add them back. Please let me know here in the comment if you cannot think of them. Thank you.


test("Basic proper fraction", () => {
assert.equal(isProperFraction(1, -2), true);
});

test("Basic proper fraction", () => {
assert.equal(isProperFraction(-1, -2), true);
});



Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,66 @@ test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
});

test("Valid ten", () => {
assert.equal(getCardValue("10♥"), 10);
});

test("Ace returns 11", () => {
assert.equal(getCardValue("A♦"), 11);
});

test("Face cards return 10", () => {
assert.equal(getCardValue("J♣"), 10);
assert.equal(getCardValue("Q♠"), 10);
assert.equal(getCardValue("K♥"), 10);
});

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
assert.throws(
() => getCardValue("invalid"),
/Expected a number followed by a suit, but got "invalid"/,
"Expected clear error"
);
});

test("Missing suit throws", () => {
assert.throws(() => getCardValue("A"), /Expected a number followed by a suit/);
});


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What other invalid card cases can you think of? Please add them. Thank you.


test("Empty string throws", () => {
assert.throws(
() => getCardValue(""),
/Expected a number followed by a suit/
);
});

test("Suit only throws", () => {
assert.throws(
() => getCardValue("♠"),
/Expected a number followed by a suit/
);
});

test("Invalid rank throws", () => {
assert.throws(
() => getCardValue("1♠"),
/Expected a number followed by a suit/
);
});

test("Invalid suit throws", () => {
assert.throws(
() => getCardValue("9X"),
/Expected a number followed by a suit/
);
});

test("Rank with no suit character throws", () => {
assert.throws(
() => getCardValue("10"),
/Expected a number followed by a suit/
);
});
18 changes: 14 additions & 4 deletions implement-and-rewrite-tests/implement/1-get-angle-type.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While this is correct, I find it may be a bit hard to read. Sometimes using else if may look cleaner.

Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Implement a function getAngleType
//
function getAngleType(angle) {
return angle > 0 && angle < 90
? "Acute angle"
: angle === 90
? "Right angle"
: angle > 90 && angle < 180
? "Obtuse angle"
: angle === 180
? "Straight angle"
: angle > 180 && angle < 360
? "Reflex angle"
: "Invalid angle";
}
// 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:
Expand All @@ -16,6 +28,4 @@
// 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
}
export { getAngleType }
10 changes: 8 additions & 2 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
// 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) {
if (denominator === 0) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}


export { isProperFraction }
31 changes: 29 additions & 2 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@
// 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) {
const suit = card.slice(-1);
const rank = card.slice(0, -1);
const suits = ["♠", "♥", "♦", "♣"];
const values = {
A: 11,
J: 10,
Q: 10,
K: 10,
"10": 10,
"9": 9,
"8": 8,
"7": 7,
"6": 6,
"5": 5,
"4": 4,
"3": 3,
"2": 2,
};

if (!suits.includes(suit) || values[rank] === undefined) {
throw new Error(
`Expected a number followed by a suit, but got "${card}"`
);
}

return values[rank];
}

export { getCardValue }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good coverage of test cases.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when angle is 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles outside (0, 360)`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be more test cases for negative numbers. Please add them. Thank you.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test(`should return true for proper fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(0, 5)).toEqual(true);
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-1, -2)).toEqual(true);
});

test(`should return false for improper fractions`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(1, 1)).toEqual(false);
expect(isProperFraction(5, 3)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,45 @@ test(`Should return 11 when given an ace card`, () => {
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

test(`Should return numeric value for number cards`, () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("3♠")).toEqual(3);
expect(getCardValue("4♣")).toEqual(4);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("6♦")).toEqual(6);
expect(getCardValue("7♠")).toEqual(7);
expect(getCardValue("8♣")).toEqual(8);
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("10♦")).toEqual(10);
});

test(`Should return 10 for face cards`, () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});
Comment on lines +19 to +35

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be one more special case for valid card. Please add it. Thank you.


// Invalid cards
test(`should throw for invalid cards`, () => {
expect(() => getCardValue("invalid")).toThrow(
/Expected a number followed by a suit, but got "invalid"/
);
expect(() => getCardValue("A")).toThrow(
/Expected a number followed by a suit/
);
expect(() => getCardValue("")).toThrow(
/Expected a number followed by a suit/
);
expect(() => getCardValue("♠")).toThrow(
/Expected a number followed by a suit/
);
expect(() => getCardValue("1♠")).toThrow(
/Expected a number followed by a suit/
);
expect(() => getCardValue("9X")).toThrow(
/Expected a number followed by a suit/
);
expect(() => getCardValue("10")).toThrow(
/Expected a number followed by a suit/
);
});
Loading