Skip to content

Commit d7f2621

Browse files
committed
Uploaded implement and rewrite tests coursework
1 parent 29ef74a commit d7f2621

9 files changed

Lines changed: 234 additions & 6 deletions

File tree

‎implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,38 @@ test("Classifies right angles", () => {
1010
const right = getAngleType(90);
1111
assert.equal(right, "Right angle");
1212
});
13+
14+
test("Classifies acute angles", () => {
15+
const acute = getAngleType(15);
16+
assert.equal(acute, "Acute angle");
17+
});
18+
19+
test("Classifies obtuse angles", () => {
20+
const obtuse = getAngleType(179);
21+
assert.equal(obtuse, "Obtuse angle");
22+
});
23+
24+
test("Classifies straight angles", () => {
25+
const straight = getAngleType(180);
26+
assert.equal(straight, "Straight angle");
27+
});
28+
29+
test("Classifies reflex angles", () => {
30+
const reflex = getAngleType(359);
31+
assert.equal(reflex, "Reflex angle");
32+
});
33+
34+
test("Classifies negative angles as invalid angles", () => {
35+
const invalid = getAngleType(-180);
36+
assert.equal(invalid, "Invalid angle");
37+
});
38+
39+
test("Classifies invalid angles", () => {
40+
const invalid = getAngleType(361);
41+
assert.equal(invalid, "Invalid angle");
42+
});
43+
44+
test("Classifies invalid angles", () => {
45+
const invalid = getAngleType(0);
46+
assert.equal(invalid, "Invalid angle");
47+
});

‎implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,23 @@ test("Basic proper fraction", () => {
1010
// Example: 1/2 is a proper fraction
1111
assert.equal(isProperFraction(1, 2), true);
1212
});
13+
14+
test("Basic proper fraction", () => {
15+
assert.equal(isProperFraction(1, 2), true);
16+
})
17+
18+
test("Basic proper fraction", () => {
19+
assert.equal(isProperFraction(2, 2), false);
20+
})
21+
22+
test("Basic proper fraction", () => {
23+
assert.equal(isProperFraction(3, 2), false);
24+
})
25+
26+
test("Basic proper fraction", () => {
27+
assert.equal(isProperFraction(0, 0), false);
28+
})
29+
30+
test("Basic proper fraction", () => {
31+
assert.equal(isProperFraction(-1, 1), true);
32+
})

‎implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,48 @@ test("Valid single-digit card", () => {
99
assert.equal(getCardValue("9♠"), 9);
1010
});
1111

12+
test("Valid single-digit card", () => {
13+
assert.equal(getCardValue("A♠"), 11);
14+
});
15+
16+
test("Valid single-digit card", () => {
17+
assert.equal(getCardValue("7♦"), 7);
18+
});
19+
20+
test("Valid Valid numeric vale", () => {
21+
assert.equal(getCardValue("J♣"), 10);
22+
});
23+
24+
test("Valid numeric vale", () => {
25+
assert.equal(getCardValue("K♣"), 10);
26+
});
27+
28+
test("Valid numeric vale", () => {
29+
assert.equal(getCardValue("Q♦"), 10);
30+
});
31+
1232
test("Arbitrary non-card string", () => {
13-
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
33+
assert.throws(
34+
() => getCardValue("Invalid"),
35+
/The card you entered is invalid\. Please enter a valid card\./,
36+
"Expected a clear error for an invalid card"
37+
);
1438
});
1539

1640
// TODO: What other invalid card cases can you think of?
41+
42+
test("Invalid string", () => {
43+
assert.throws(() => getCardValue("11♠"), /The card you entered is invalid/);
44+
});
45+
46+
test("Invalid string", () => {
47+
assert.throws(() => getCardValue("1♥"), /The card you entered is invalid/);
48+
});
49+
50+
test("Invalid string", () => {
51+
assert.throws(() => getCardValue("JX"), /The card you entered is invalid/);
52+
});
53+
54+
test("Invalid string", () => {
55+
assert.throws(() => getCardValue("♠A"), /The card you entered is invalid/);
56+
});

‎implement-and-rewrite-tests/implement/1-get-angle-type.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,25 @@
1717
// execute the code to ensure all tests pass.
1818

1919
export function getAngleType(angle) {
20-
// TODO: Implement this function
20+
if (angle > 0 && angle < 90) {
21+
return "Acute angle";
22+
}
23+
24+
if (angle === 90) {
25+
return "Right angle";
26+
}
27+
28+
if (angle > 90 && angle < 180) {
29+
return "Obtuse angle";
30+
}
31+
32+
if (angle === 180) {
33+
return "Straight angle";
34+
}
35+
36+
if (angle > 180 && angle < 360) {
37+
return "Reflex angle";
38+
}
39+
40+
return "Invalid angle";
2141
}

‎implement-and-rewrite-tests/implement/2-is-proper-fraction.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
// execute the code to ensure all tests pass.
1515

1616
export function isProperFraction(numerator, denominator) {
17-
// TODO: Implement this function
17+
return numerator < denominator;
1818
}

‎implement-and-rewrite-tests/implement/3-get-card-value.js‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,47 @@
2323
// After you have implemented the function, write tests to cover all the cases, and
2424
// execute the code to ensure all tests pass.
2525

26+
//const cardRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
27+
// const emoji = ["♠", "♥", "♦", "♣"];
28+
29+
const cardRanks = [
30+
"A",
31+
"2",
32+
"3",
33+
"4",
34+
"5",
35+
"6",
36+
"7",
37+
"8",
38+
"9",
39+
"10",
40+
"J",
41+
"Q",
42+
"K",
43+
];
44+
const suits = ["♠", "♥", "♦", "♣"];
45+
2646
export function getCardValue(card) {
27-
// TODO: Implement this function
47+
if (typeof card !== "string" || card.length < 2) {
48+
throw new Error(
49+
"The card you entered is invalid. Please enter a valid card."
50+
);
51+
}
52+
53+
const rank = card.slice(0, -1);
54+
const suit = card.slice(-1);
55+
56+
if (!cardRanks.includes(rank) || !suits.includes(suit)) {
57+
throw new Error(
58+
"The card you entered is invalid. Please enter a valid card."
59+
);
60+
}
61+
62+
if (rank === "A") {
63+
return 11;
64+
} else if (["J", "Q", "K"].includes(rank)) {
65+
return 10;
66+
} else {
67+
return Number(rank);
68+
}
2869
}

‎implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,39 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1212
});
1313

1414
// Case 2: Right angle
15+
test(`should return "Right angle" when (angle === 90)`, () => {
16+
// Test various right angles, including boundary cases
17+
expect(getAngleType(90)).toEqual("Right angle");
18+
});
19+
1520
// Case 3: Obtuse angles
21+
test(`should return "Obtuse angle" when (angle > 90 && angle < 180)`, () => {
22+
// Test various obtuse angles, including boundary cases
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(179)).toEqual("Obtuse angle");
25+
expect(getAngleType(169)).toEqual("Obtuse angle");
26+
expect(getAngleType(101)).toEqual("Obtuse angle");
27+
});
28+
1629
// Case 4: Straight angle
30+
test(`should return "Straight angle" when (angle === 180)`, () => {
31+
// Test various straight angles, including boundary cases
32+
expect(getAngleType(180)).toEqual("Straight angle");
33+
});
34+
1735
// Case 5: Reflex angles
36+
test(`should return "Reflex angle" when (angle > 180 && angle < 360)`, () => {
37+
// Test various reflex angles, including boundary cases
38+
expect(getAngleType(181)).toEqual("Reflex angle");
39+
expect(getAngleType(359)).toEqual("Reflex angle");
40+
expect(getAngleType(305)).toEqual("Reflex angle");
41+
expect(getAngleType(200)).toEqual("Reflex angle");
42+
});
43+
1844
// Case 6: Invalid angles
45+
test(`should return "Invalid angle" when (angle === Invalid)`, () => {
46+
// Test various invalid angles, including boundary cases
47+
expect(getAngleType(361)).toEqual("Invalid angle");
48+
expect(getAngleType(0)).toEqual("Invalid angle");
49+
expect(getAngleType(-181)).toEqual("Invalid angle");
50+
});

‎implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,27 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
22

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

5-
// Special case: numerator is zero
5+
// Special case: denominator is zero
66
test(`should return false when denominator is zero`, () => {
77
expect(isProperFraction(1, 0)).toEqual(false);
88
});
9+
10+
// Special case: numerator is One
11+
test(`should return true when denominator is two`, () => {
12+
expect(isProperFraction(1, 2)).toEqual(true);
13+
});
14+
15+
// Special case: numerator is two
16+
test(`should return false when denominator is 2`, () => {
17+
expect(isProperFraction(2, 2)).toEqual(false);
18+
});
19+
20+
// Special case: numerator is negative number
21+
test(`should return true when denominator is 1`, () => {
22+
expect(isProperFraction(-1, 1)).toEqual(false);
23+
});
24+
25+
// Special case: numerator is zero
26+
test(`should return false when denominator is zero`, () => {
27+
expect(isProperFraction(0, 0)).toEqual(false);
28+
});

‎implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,27 @@ test(`Should return 11 when given an ace card`, () => {
1212
// Face Cards (J, Q, K)
1313
// Invalid Cards
1414

15+
// Case 2: Number Cards (2-10)
16+
test(`Should return the numerical value when given a number card`, () => {
17+
expect(getCardValue("2♠")).toEqual(2);
18+
expect(getCardValue("5♥")).toEqual(5);
19+
expect(getCardValue("10♦")).toEqual(10);
20+
});
21+
22+
// Case 3: Face Cards (J, Q, K)
23+
test(`Should return 10 when given a face card`, () => {
24+
expect(getCardValue("J♣")).toEqual(10);
25+
expect(getCardValue("Q♦")).toEqual(10);
26+
expect(getCardValue("K♠")).toEqual(10);
27+
});
28+
29+
// Case 4: Invalid Cards (["11♠", "JX", "♠A"])
30+
test(`Should throw an error when given an invalid card`, () => {
31+
expect(() => getCardValue("11♠")).toThrow();
32+
expect(() => getCardValue("JX")).toThrow();
33+
expect(() => getCardValue("♠A")).toThrow();
34+
});
35+
1536
// To learn how to test whether a function throws an error as expected in Jest,
1637
// please refer to the Jest documentation:
1738
// https://jestjs.io/docs/expect#tothrowerror
18-

0 commit comments

Comments
 (0)