Skip to content

Commit 91ea752

Browse files
authored
Merge pull request #213 from kevin860104/lab3
[LAB3] 512558003
2 parents ebc4210 + 12d59dc commit 91ea752

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

lab3/main_test.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,63 @@ const { describe, it } = require('node:test');
22
const assert = require('assert');
33
const { Calculator } = require('./main');
44

5-
// TODO: write your tests here
5+
describe("Calculator Test", () => {
6+
const calculator = new Calculator();
7+
8+
const logTestSuites = [
9+
{
10+
operation: "log",
11+
cases: [
12+
{ data: 9, expected: 2.1972245773362196 },
13+
{ data: 6, expected: 1.791759469228055 },
14+
{ data: 1, expected: 0 },
15+
{ data: 'kevin', expected: Error, message: "unsupported operand type" },
16+
{ data: true, expected: Error, message: "unsupported operand type" },
17+
{ data: Infinity, expected: Error, message: "unsupported operand type" },
18+
{ data: 0, expected: Error, message: "math domain error (5)" },
19+
{ data: -1, expected: Error, message: "math domain error (3)" },
20+
]
21+
}
22+
];
23+
24+
logTestSuites.forEach(({ operation, cases }) => {
25+
it(`Calculator.${operation}() Test`, () => {
26+
cases.forEach(({ data: param, expected: expectedOutput, message: msg }) => {
27+
if (expectedOutput === Error) {
28+
assert.throws(() => calculator[operation](param), expectedOutput, msg);
29+
} else {
30+
const result = calculator[operation](param);
31+
assert.strictEqual(result, expectedOutput, msg);
32+
}
33+
});
34+
});
35+
});
36+
37+
const expTestSuites = [
38+
{
39+
operation: "exp",
40+
cases: [
41+
{ data: 8, expected: 2980.9579870417283 },
42+
{ data: 0, expected: 1 },
43+
{ data: -3, expected: 0.049787068367863944 },
44+
{ data: 'kkk', expected: Error, message: "unsupported operand type" },
45+
{ data: true, expected: Error, message: "unsupported operand type" },
46+
{ data: Infinity, expected: Error, message: "unsupported operand type" },
47+
{ data: Number.MAX_VALUE, expected: Error, message: "overflow" },
48+
]
49+
}
50+
];
51+
52+
expTestSuites.forEach(({ operation, cases }) => {
53+
it(`Calculator.${operation}() Test`, () => {
54+
cases.forEach(({ data: param, expected: expectedOutput, message: msg }) => {
55+
if (expectedOutput === Error) {
56+
assert.throws(() => calculator[operation](param), expectedOutput, msg);
57+
} else {
58+
const result = calculator[operation](param);
59+
assert.strictEqual(result, expectedOutput, msg);
60+
}
61+
});
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)