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
Expand Up @@ -5,8 +5,36 @@ import { getAngleType } from "../implement/1-get-angle-type.js";

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
test("Invalid value", () => {
assert.throws(
() => {
getAngleType("shirin");
},
{ message: "Input must be a number" }
);
});

test("Acute angle for angles greater than 0° and less than 90°", () => {
const message = getAngleType(70);
assert.equal(message, "Acute angle");
});

test("Right angle", () => {
const message = getAngleType(90);
assert.equal(message, "Right angle");
});

test("Obtuse angle for angles greater than 90° and less than 180°", () => {
const message = getAngleType(110);
assert.equal(message, "Obtuse angle");
});

test("Straight angle for exactly 180°", () => {
const message = getAngleType(180);
assert.equal(message, "Straight angle");
});

test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
test("Reflex angle for angles greater than 180° and less than 360°", () => {
const message = getAngleType(260);
assert.equal(message, "Reflex angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,50 @@ test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

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

test("Equal numerator and denominator", () => {
// Example: equal number
assert.equal(isProperFraction(5, 5), false);
});

test("zero numerator", () => {
// Example: zero numerator is a proper fraction?
assert.equal(isProperFraction(0, 6), true);
});

test("Negative numerator", () => {
// Example: negative numerator is a proper fraction?
assert.equal(isProperFraction(-3, 8), true);
assert.equal(isProperFraction(-9, 4), false);
});

test("Negative denominator", () => {
// Example: negative denominator is a proper fraction?
assert.equal(isProperFraction(2, -5), true);
});

test("Both negative", () => {
// Example: Both negative is a proper fraction?
assert.equal(isProperFraction(-3, -5), true);
assert.equal(isProperFraction(-5, -2), false);
});

test("Zero denominator", () => {
// Example: Zero denominator is a proper fraction?
assert.equal(isProperFraction(7, 0), false);
});

test("Zero numerator and negative denominator", () => {
// Example: Zero numerator and negative denominator is a proper fraction?
assert.equal(isProperFraction(0, -7), true);
});

test("Both Zero", () => {
// Example: Both Zero is a proper fraction?
assert.equal(isProperFraction(0, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,64 @@ test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
});

test("Valid double-digit card", () => {
assert.equal(getCardValue("10♠"), 10);
});
test("Ace card", () => {
assert.equal(getCardValue("A♠"), 11);
assert.equal(getCardValue("A♣"), 11);
assert.equal(getCardValue("A♦"), 11);
});

test("Face card", () => {
assert.equal(getCardValue("J♠"), 10);
});

test("card is not a string", () => {
assert.throws(
() => {
getCardValue(9);
},
{ message: "Input must be a String" }
);
});

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"
);

assert.throws(
() => getCardValue("1♠"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("9X"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("11♠"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("extra"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("08♠"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);
});

// TODO: What other invalid card cases can you think of?
25 changes: 24 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +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:
Expand All @@ -18,4 +18,27 @@

export function getAngleType(angle) {
// TODO: Implement this function
if (typeof angle != `number` || Number.isNaN(angle)) {
throw new Error("Input must be a number");
}

if (angle >= 0 && angle < 90) {
return "Acute angle";
}

if (angle === 90) {
return "Right angle";
}

if (angle > 90 && angle < 180) {
return "Obtuse angle";
}

if (angle === 180) {
return "Straight angle";
}

if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}
4 changes: 4 additions & 0 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}
24 changes: 24 additions & 0 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,28 @@

export function getCardValue(card) {
// TODO: Implement this function

if (typeof card != `string`) {
throw new Error("Input must be a String");
}
const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
const rank = card.slice(0, -1);
const suit = card.slice(-1);

if (suit != "♠" && suit != "♥" && suit != "♦" && suit != "♣") {
throw new Error("Expected a number followed by a suit, but got invalid");
}

if (validRanks.includes(rank)) {
return Number(rank);
}

if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}
throw new Error("Expected a number followed by a suit, but got invalid");
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAngleType } from "../implement/1-get-angle-type.js";

import { test, expect } from "@jest/globals";
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

Expand All @@ -12,7 +12,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle = 90)`, () => {
// Test various Right angles
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various Obtuse angle, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(145)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Obtuse angle" when (angle = 180)`, () => {
// Test various Straight angle
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various Obtuse angle, including boundary cases
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 angles" when angle is not number`, () => {
// Test various Obtuse angle, including boundary cases
expect(() => getAngleType(" ")).toThrow("Input must be a number");
expect(() => getAngleType("Shirin")).toThrow("Input must be a number");
expect(() => getAngleType("*123")).toThrow("Input must be a number");
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,44 @@ 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 when numerator is smaller than denominator `, () => {
expect(isProperFraction(3, 5)).toEqual(true);
});

test(`should return false when numerator is bigger than denominator `, () => {
expect(isProperFraction(6, 2)).toEqual(false);
});

test(`should return true when numerator is zero`, () => {
expect(isProperFraction(0, 2)).toEqual(true);
});

test(`should return true when numerator is negative ans smaller`, () => {
expect(isProperFraction(-4, 6)).toEqual(true);
});

test(`should return false when numerator is negative ans bigger`, () => {
expect(isProperFraction(-8, 6)).toEqual(false);
});

test(`should return true when denominator is negative and bigger`, () => {
expect(isProperFraction(2, -5)).toEqual(true);
});

test(`should return false when denominator is negative ans smaller`, () => {
expect(isProperFraction(4, -3)).toEqual(false);
});

test(`should return correct answer when both is negative`, () => {
expect(isProperFraction(-4, -3)).toEqual(false);
expect(isProperFraction(-1, -3)).toEqual(true);
});

test(`should return true answer when Zero numerator and negative denominator`, () => {
expect(isProperFraction(0, -3)).toEqual(true);
});

test(`should return false when both are zero`, () => {
expect(isProperFraction(0, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return number for Valid digit card`, () => {
expect(getCardValue("3♠")).toEqual(3);
});
// Face Cards (J, Q, K)
test(`Should return 10 when given an Face card`, () => {
expect(getCardValue("K♠")).toEqual(10);
});
// Invalid Cards
test(`should return "Input must be a String" when card is not string type`, () => {
expect(() => getCardValue(7)).toThrow("Input must be a String");
});

test(`should return "Expected a number followed by a suit, but got invalid" when card is not string type`, () => {
expect(() => getCardValue("1♠")).toThrow(
"Expected a number followed by a suit, but got invalid"
);
});

test(`should return "Expected a number followed by a suit, but got invalid" when card is not string type`, () => {
expect(() => getCardValue("9X")).toThrow(
"Expected a number followed by a suit, but got invalid"
);
});
// 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