-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain_test.js
62 lines (54 loc) · 2.17 KB
/
main_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const {describe, it} = require('node:test');
const assert = require('assert');
const { Calculator } = require('./main');
// TODO: write your tests here
describe('Calculator', () => {
describe('exp', () => {
const errorCases = [
{ input: null, expectedError: 'unsupported operand type' },
{ input: 'abc', expectedError: 'unsupported operand type' },
{ input: 1000, expectedError: 'overflow' }
];
const successCases = [
{ input: 0, expected: 1 },
{ input: 1, expected: Math.E },
{ input: -1, expected: 1/Math.E }
];
errorCases.forEach(({ input, expectedError }) => {
it(`exp(${JSON.stringify(input)}) should throw ${expectedError}`, () => {
const calc = new Calculator();
assert.throws(() => calc.exp(input), { message: expectedError });
});
});
successCases.forEach(({ input, expected }) => {
it(`exp(${input}) should return ${expected}`, () => {
const calc = new Calculator();
assert.strictEqual(calc.exp(input), expected);
});
});
});
describe('log', () => {
const errorCases = [
{ input: undefined, expectedError: 'unsupported operand type' },
{ input: 0, expectedError: 'math domain error (1)' },
{ input: -5, expectedError: 'math domain error (2)' }
];
const successCases = [
{ input: 1, expected: 0 },
{ input: Math.E, expected: 1 },
{ input: 10, expected: Math.log(10) }
];
errorCases.forEach(({ input, expectedError }) => {
it(`log(${JSON.stringify(input)}) should throw ${expectedError}`, () => {
const calc = new Calculator();
assert.throws(() => calc.log(input), { message: expectedError });
});
});
successCases.forEach(({ input, expected }) => {
it(`log(${input}) should return ${expected}`, () => {
const calc = new Calculator();
assert.strictEqual(calc.log(input), expected);
});
});
});
});