Skip to content

Commit 0078e0b

Browse files
authored
Update main_test.js
1 parent 3c6b2c7 commit 0078e0b

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: 6, expected: Math.log(6) },
13+
{ data: 5, expected: Math.log(5) },
14+
{ data: 2, expected: Math.log(2) },
15+
{ data: 'guava', 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 (1)" },
19+
{ data: -1, expected: Error, message: "math domain error (2)" },
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: 4, expected: Math.exp(4) },
42+
{ data: 0, expected: Math.exp(0) },
43+
{ data: -2, expected: Math.exp(-2) },
44+
{ data: 'guava666', 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)