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 @@ -15,7 +15,19 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle == 90) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here you're using == but JavaScript also has a === operator. Do you know the difference? Why have you chosen to use == here? When would === make more sense?

This applies in many places in your PR - please take a look at them all!

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as above

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.
Expand All @@ -31,7 +43,10 @@ function assertEquals(actualOutput, 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");
assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(185), "Reflex angle");
assertEquals(getAngleType(365), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (isNaN(numerator) && isNaN(denominator)) {
return false;
}

if (numerator < denominator) {
return true;
}
return false;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -26,8 +33,11 @@ function assertEquals(actualOutput, 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(2, 2), false);
assertEquals(isProperFraction(3, 2), false);
assertEquals(isProperFraction(4312, 5000), true);
assertEquals(isProperFraction("Invalid", "data"), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,31 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const lastChar = card.length - 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The name of this variable suggests it contains a character. But it actually contains an index of a character. Can you think of a name that could make this more clear?

const rank = card.slice(0, lastChar);
const suit = card.slice(lastChar);

if (rank === "10") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works, but I'm curious - why did you decide to handle 10 specially here?

What would happen if someone passed the string "10Q" to this function? What would you want to happen? What other edge cases may you want to test for in your tests?

return 10;
}

if (card.length > 2) {
throw new Error("Invalid input length");
}

if (!(suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣")) {
throw new Error("Invalid suit ", suit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's often useful to include in an error message what the invalid data was. Imagine you're are running a big long programme, and aren't sure exactly what data is being processed - the more context we can give in errors the better.

}

if (rank == "A") {
return 11;
} else if (rank == "J" || rank == "Q" || rank == "K") {
return 10;
} else if (!isNaN(rank)) {
return Number(rank);
} else {
throw new Error("Invalid card");
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,36 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle == 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse" when (90 < angle < 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(140)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when (angle == 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(220)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle"`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType("Test data")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(2, 2)).toEqual(false);
expect(isProperFraction(3, 2)).toEqual(false);
expect(isProperFraction(3, 4)).toEqual(true);
expect(isProperFraction(4312, 5000)).toEqual(true);
expect(isProperFraction("invalid", "data")).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ const getCardValue = require("../implement/3-get-card-value");
// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(() => getCardValue("AA♠")).toThrow(Error);
});

// Case 2
test(`Should return 10 when given an jack, queen, king`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
expect(() => getCardValue("K🤣")).toThrow(Error);
expect(() => getCardValue("KKW🤣")).toThrow(Error);
});

// Case 3
test(`Should return number when given a number`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♥")).toEqual(10);
expect(() => getCardValue("423🤣")).toThrow(Error);
});

test("Should return error on invalid input", () => {
expect(() => getCardValue(null)).toThrow(Error);
expect(() => getCardValue(undefined)).toThrow(Error);
expect(() => getCardValue(12398)).toThrow(Error);
expect(() => getCardValue(NaN)).toThrow(Error);
});

// Suggestion: Group the remaining test data into these categories:
Expand All @@ -17,4 +41,3 @@ test(`Should return 11 when given an ace card`, () => {
// 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

25 changes: 14 additions & 11 deletions Sprint-3/1-implement-and-rewrite-tests/testing-guide.md

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please revert the changes to this file, they're not relevant for this exercise.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ Input ──▶ Function ──▶ Output
```

A function

- Takes **input** (via **arguments**)
- Does some work
- Produces **one output** (via a **return value**)

Example:

```
Expand All @@ -19,17 +20,18 @@ sum(2, 3) → 5

Important idea: the same input should produce the same output.


## 2. Testing Means Predicting

Testing means:
> If I give this input, what output should I get?
Testing means:

> If I give this input, what output should I get?

## 3. Choosing Good Test Values

### Step 1: Determining the space of possible inputs

Ask:

- What type of value is expected?
- What values make sense?
- If they are numbers:
Expand All @@ -39,7 +41,7 @@ Ask:
- What are their length and patterns?
- What values would not make sense?

### Step 2: Choosing Good Test Values
### Step 2: Choosing Good Test Values

#### Normal Cases

Expand All @@ -48,10 +50,9 @@ These confirm that the function works in normal use.
- What does a typical, ordinary input look like?
- Are there multiple ordinary groups of inputs? e.g. for an age checking function, maybe there are "adults" and "children" as expected ordinary groups of inputs.


#### Boundary Cases

Test values exactly at, just inside, and just outside defined ranges.
Test values exactly at, just inside, and just outside defined ranges.
These values are where logic breaks most often.

#### Consider All Outcomes
Expand All @@ -64,6 +65,7 @@ Every outcome must be reached by at least one test.
#### Crossing the Edges and Invalid Values

This tests how the function behaves when assumptions are violated.

- What happens when input is outside of the expected range?
- What happens when input is not of the expected type?
- What happens when input is not in the expected format?
Expand All @@ -73,18 +75,19 @@ This tests how the function behaves when assumptions are violated.
### 1. Using `console.assert()`

```javascript
// Report a failure only when the first argument is false
console.assert( sum(4, 6) === 10, "Expected 4 + 6 to equal 10" );
// Report a failure only when the first argument is false
console.assert(sum(4, 6) === 10, "Expected 4 + 6 to equal 10");
```

It is simpler than using `if-else` and requires no setup.

### 2. Jest Testing Framework

```javascript
describe("Description for this case used for grouping")
test("Should correctly return the sum of two positive numbers", () => {
expect( sum(4, 6) ).toEqual(10);
... // Can test multiple samples
... // Can test multiple samples
});

```
Expand Down
Loading