Skip to content

Commit d737bd1

Browse files
Complete Sprint 3 implementations and tests
1 parent 29ef74a commit d737bd1

9 files changed

Lines changed: 353 additions & 8 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,41 @@ test("Classifies right angles", () => {
1010
const right = getAngleType(90);
1111
assert.equal(right, "Right angle");
1212
});
13+
14+
test("classifies acute angles", () => {
15+
assert.equal(getAngleType(1), "Acute angle");
16+
assert.equal(getAngleType(45), "Acute angle");
17+
assert.equal(getAngleType(89), "Acute angle");
18+
assert.equal(getAngleType(36), "Acute angle");
19+
assert.equal(getAngleType(20), "Acute angle");
20+
21+
});
22+
23+
test("classifies obtuse angles", () => {
24+
assert.equal(getAngleType(91), "Obtuse angle");
25+
assert.equal(getAngleType(105), "Obtuse angle");
26+
assert.equal(getAngleType(179), "Obtuse angle");
27+
assert.equal(getAngleType(125), "Obtuse angle");
28+
assert.equal(getAngleType(110), "Obtuse angle");
29+
});
30+
31+
test("classifies straight angles", () => {
32+
const straight = getAngleType(180);
33+
assert.equal(straight, "Straight angle");
34+
});
35+
36+
test("classifies reflex angles", () => {
37+
assert.equal(getAngleType(181), "Reflex angle");
38+
assert.equal(getAngleType(220), "Reflex angle");
39+
assert.equal(getAngleType(289), "Reflex angle");
40+
assert.equal(getAngleType(300), "Reflex angle");
41+
assert.equal(getAngleType(349), "Reflex angle");
42+
});
43+
44+
test("classifies invalid angles", () => {
45+
assert.equal(getAngleType(-5), "Invalid angle");
46+
assert.equal(getAngleType(0), "Invalid angle");
47+
assert.equal(getAngleType(360), "Invalid angle");
48+
assert.equal(getAngleType(400), "Invalid angle");
49+
assert.equal(getAngleType(420), "Invalid angle");
50+
});

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,75 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
66
// TODO: Write tests to cover all cases.
77
// What combinations of numerators and denominators should you test?
88

9-
test("Basic proper fraction", () => {
9+
test("Returns true for a basic proper fraction", () => {
1010
// Example: 1/2 is a proper fraction
1111
assert.equal(isProperFraction(1, 2), true);
1212
});
13+
14+
test("Returns true for 2/8", () => {
15+
assert.equal(isProperFraction(2, 8), true);
16+
});
17+
18+
test("Returns true when the numerator is zero and denominator is positive", () => {
19+
assert.equal(isProperFraction(0, 1), true);
20+
});
21+
22+
test("Returns true for 5/8", () => {
23+
assert.equal(isProperFraction(5, 8), true);
24+
});
25+
26+
test("Returns true for 8/9", () => {
27+
assert.equal(isProperFraction(8, 9), true);
28+
});
29+
30+
test("Returns true for another proper fraction with a smaller numerator", () => {
31+
assert.equal(isProperFraction(2, 4), true);
32+
});
33+
34+
test("Returns true when both numerator and denominator is negative", () => {
35+
assert.equal(isProperFraction(-1, -2), true);
36+
});
37+
38+
test("Returns true when the numerator is positive denominator is negative", () => {
39+
assert.equal(isProperFraction(1, -2), true);
40+
});
41+
42+
test("Returns true when the numerator is negative and the denominator is positive", () => {
43+
assert.equal(isProperFraction(-1, 2), true);
44+
});
45+
46+
test("Returns false when the numerator is greater than the denominator", () => {
47+
assert.equal(isProperFraction(9, 8), false);
48+
});
49+
50+
test("Returns false when numerator is negative and denominator is zero", () => {
51+
assert.equal(isProperFraction(-1, 0), false);
52+
});
53+
54+
test("Returns false for 4/2", () => {
55+
assert.equal(isProperFraction(4, 2), false);
56+
});
57+
58+
test("Returns false for 5/3", () => {
59+
assert.equal(isProperFraction(5, 3), false);
60+
});
61+
62+
test("Returns false for 6/4", () => {
63+
assert.equal(isProperFraction(6, 4), false);
64+
});
65+
66+
test("Returns false for 7/1", () => {
67+
assert.equal(isProperFraction(7, 1), false);
68+
});
69+
70+
test("Returns false for 15/14", () => {
71+
assert.equal(isProperFraction(15, 14), false);
72+
});
73+
74+
test("Returns false when numerator equals denominator", () => {
75+
assert.equal(isProperFraction(4, 4), false);
76+
});
77+
78+
test("Returns false when numerator is positive and denominator is zero", () => {
79+
assert.equal(isProperFraction(1,0), false);
80+
})

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

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

12+
test("Ace has a value of 11", () => {
13+
assert.equal(getCardValue("A♠"), 11);
14+
});
15+
16+
test("Two has a value of 2", () => {
17+
assert.equal(getCardValue("2♥"), 2);
18+
});
19+
20+
test("Ten has a value of 10", () => {
21+
assert.equal(getCardValue("10♥"), 10);
22+
});
23+
24+
test("Jack has a value of 10", () => {
25+
assert.equal(getCardValue("J♣"), 10);
26+
});
27+
28+
test("Queen has a value of 10", () => {
29+
assert.equal(getCardValue("Q♦"), 10);
30+
});
31+
32+
test("King has a value of 10", () => {
33+
assert.equal(getCardValue("K♦"), 10);
34+
});
35+
1236
test("Arbitrary non-card string", () => {
1337
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
1438
});
1539

16-
// TODO: What other invalid card cases can you think of?
40+
test("Invalid rank throws an error", () => {
41+
assert.throws(() => getCardValue("Z♠"), /Expected a number followed by a suit, but got "Z♠"/, "Expected clear error");
42+
});
43+
44+
test("Invalid suit throws an error", () => {
45+
assert.throws(() => getCardValue("9X"), /Expected a number followed by a suit, but got "9X"/, "Expected clear error");
46+
});
47+
48+
test("Card without a suit throws an error", () => {
49+
assert.throws(() => getCardValue("9"), /Expected a number followed by a suit, but got "9"/, "Expected clear error");
50+
});
51+
52+
test("Card without a rank throws an error", () => {
53+
assert.throws(() => getCardValue("♠"), /Expected a number followed by a suit, but got "♠"/, "Expected clear error");
54+
});
55+
56+
test("Card without a number greater than 10 throws an error", () => {
57+
assert.throws(() => getCardValue("15♠"), /Expected a number followed by a suit, but got "15♠"/, "Expected clear error");
58+
});
59+
60+
61+
// TODO: What other invalid card cases can you think of?

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

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

1919
export function getAngleType(angle) {
20-
// TODO: Implement this function
20+
if (angle === 90) {
21+
return "Right angle";
22+
}
23+
24+
if (angle > 0 && angle < 90) {
25+
return "Acute 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+
if (angle > 180 && angle < 360) {
36+
return "Reflex angle";
37+
}
38+
39+
return "Invalid angle";
2140
}

‎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 Math.abs(numerator) < Math.abs(denominator)
1818
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@
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+
2627
export function getCardValue(card) {
27-
// TODO: Implement this function
28-
}
28+
const rank = card.slice(0, -1);
29+
const suit = card.slice(-1);
30+
const validSuits = ["♠", "♥", "♦", "♣"];
31+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
32+
33+
if (!validSuits.includes(suit)) {
34+
throw new Error (`Expected a number followed by a suit, but got "${card}"`);
35+
}
36+
37+
if (!validRanks.includes(rank)) {
38+
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
39+
}
40+
41+
if (rank === "A") {
42+
return 11;
43+
}
44+
45+
if (rank === "J" || rank === "Q" || rank === "K") {
46+
return 10;
47+
}
48+
49+
return Number(rank);
50+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,38 @@ 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 the exact right-angle boundary
17+
expect(getAngleType(90)).toEqual("Right angle");
18+
});
19+
1520
// Case 3: Obtuse angles
21+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
22+
// Test various obtuse angles, including boundary cases
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(120)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
26+
});
27+
1628
// Case 4: Straight angle
29+
test(`should return "Straight angle" when (angle === 180)`, () => {
30+
// Test the exact straight-angle boundary
31+
expect(getAngleType(180)).toEqual("Straight angle");
32+
});
33+
1734
// Case 5: Reflex angles
35+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
36+
// Test various reflex angles, including boundary cases
37+
expect(getAngleType(181)).toEqual("Reflex angle");
38+
expect(getAngleType(220)).toEqual("Reflex angle");
39+
expect(getAngleType(359)).toEqual("Reflex angle");
40+
});
41+
1842
// Case 6: Invalid angles
43+
test(`should return "Invalid angle" when (angles outside of valid range)`, () => {
44+
// Test various invalid angles, including boundary cases
45+
expect(getAngleType(0)).toEqual("Invalid angle");
46+
expect(getAngleType(360)).toEqual("Invalid angle");
47+
expect(getAngleType(-5)).toEqual("Invalid angle");
48+
expect(getAngleType(400)).toEqual("Invalid angle");
49+
});

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,80 @@ 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: numerator is zero
6+
// test(`should return false when denominator is zero`, () => {
7+
// expect(isProperFraction(1, 0)).toEqual(false);
8+
// });
9+
10+
// Special case: denominator is zero
611
test(`should return false when denominator is zero`, () => {
712
expect(isProperFraction(1, 0)).toEqual(false);
813
});
14+
15+
test(`should return false when numerator equals the denominator`, () => {
16+
expect(isProperFraction(4, 4)).toEqual(false);
17+
});
18+
19+
test(`should return false if 15/14`, () => {
20+
expect(isProperFraction(15, 14)).toEqual(false);
21+
});
22+
23+
test(`should return false if 7/1`, () => {
24+
expect(isProperFraction(7, 1)).toEqual(false);
25+
});
26+
27+
test(`should return false if 6/4`, () => {
28+
expect(isProperFraction(6, 4)).toEqual(false);
29+
});
30+
31+
test(`should return false if 5/3`, () => {
32+
expect(isProperFraction(5, 3)).toEqual(false);
33+
});
34+
35+
test(`should return false if 4/2`, () => {
36+
expect(isProperFraction(4, 2)).toEqual(false);
37+
});
38+
39+
test(`should return false if numerator is negative and denominator is zero`, () => {
40+
expect(isProperFraction(-1, 0)).toEqual(false);
41+
});
42+
43+
test(`should return false when the numerator is greater than the denominator`, () => {
44+
expect(isProperFraction(9, 8)).toEqual(false);
45+
});
46+
47+
test(`should return true if numerator is negative and denominator is positive`, () => {
48+
expect(isProperFraction(-1, 2)).toEqual(true);
49+
});
50+
51+
test(`should return true if numerator is positive and denominator is negative`, () => {
52+
expect(isProperFraction(1, -2)).toEqual(true);
53+
});
54+
55+
test(`should return true if both numerator and denominator is negative`, () => {
56+
expect(isProperFraction(-1, -2)).toEqual(true);
57+
});
58+
59+
test(`should return true for another proper fraction with a smaller numerator`, () => {
60+
expect(isProperFraction(2, 4)).toEqual(true);
61+
});
62+
63+
test(`should return true if 8/9`, () => {
64+
expect(isProperFraction(8, 9)).toEqual(true);
65+
});
66+
67+
test(`should return true if 5/8`, () => {
68+
expect(isProperFraction(5, 8)).toEqual(true);
69+
});
70+
71+
test(`should return true if numerator is zero and the denominator is positive`, () => {
72+
expect(isProperFraction(0, 1)).toEqual(true);
73+
});
74+
75+
test(`should return true 2/8`, () => {
76+
expect(isProperFraction(2, 8)).toEqual(true);
77+
});
78+
79+
test(`should return true for a basic proper fraction`, () => {
80+
expect(isProperFraction(1, 2)).toEqual(true);
81+
});

0 commit comments

Comments
 (0)