|
1 |
| -const {test,mock} = require('node:test'); |
| 1 | +const { describe, test } = require('node:test'); |
2 | 2 | const assert = require('assert');
|
3 |
| -const fs = require('fs') |
4 |
| -mock.method(fs, 'readFile', (file, options, callback) => { |
5 |
| - callback(null, 'sam\njack\njim'); |
| 3 | +const { Calculator } = require('./main'); |
| 4 | + |
| 5 | +describe('Calculator', () => { |
| 6 | + const calc = new Calculator(); |
| 7 | + test('exp', () => { |
| 8 | + assert.strictEqual(calc.exp(0),1); |
| 9 | + assert.throws(() => calc.exp('hello'), Error, 'unsupported operand type'); |
| 10 | + assert.throws(() => calc.exp(10000), Error, 'overflow'); |
| 11 | + }); |
| 12 | + |
| 13 | + test('exp', () => { |
| 14 | + assert.strictEqual(calc.log(1),0); |
| 15 | + assert.throws(() => calc.log(-1), Error, 'math domain error (2)'); |
| 16 | + assert.throws(() => calc.log(0), Error, 'math domain error (1)'); |
| 17 | + assert.throws(() => calc.log('a'), Error, 'unsupported operand type'); |
| 18 | + }); |
6 | 19 | });
|
7 |
| -const { Application, MailSystem } = require('./main'); |
8 |
| - |
9 |
| - |
10 |
| - |
11 |
| -test('write', () => { |
12 |
| - const mailSystem = new MailSystem() |
13 |
| - assert.strictEqual(mailSystem.write('sam'),'Congrats, sam!') |
14 |
| - assert.strictEqual(mailSystem.write(123),'Congrats, 123!') |
15 |
| - assert.strictEqual(mailSystem.write(null),'Congrats, null!') |
16 |
| - |
17 |
| -}); |
18 |
| - |
19 |
| - |
20 |
| -test('send',()=>{ |
21 |
| - const mailSystem = new MailSystem() |
22 |
| - mock.method(Math, 'random', () => 0.9); |
23 |
| - assert.strictEqual(mailSystem.send('sam','hello world'),true) |
24 |
| - mock.method(Math, 'random', () => 0.1); |
25 |
| - assert.strictEqual(mailSystem.send('sam','hello world'),false) |
26 |
| -}) |
27 |
| - |
28 |
| - |
29 |
| -test('getNames', async () => { |
30 |
| - const app = new Application(); |
31 |
| - const names = await app.getNames(); |
32 |
| - assert.deepStrictEqual(names, [['sam', 'jack','jim'],[]]); |
33 |
| -}); |
34 |
| - |
35 |
| - |
36 |
| -test('getRandomPerson',async ()=>{ |
37 |
| - const app = new Application(); |
38 |
| - await app.getNames(); |
39 |
| - mock.method(Math,'random',()=>0.5) |
40 |
| - const person = app.getRandomPerson() |
41 |
| - assert.strictEqual(person,app.people[Math.floor(0.5*app.people.length)]) |
42 |
| -}) |
43 |
| - |
44 |
| - |
45 |
| - |
46 |
| -test('selectNextPerson', async ()=>{ |
47 |
| - const app = new Application(); |
48 |
| - await app.getNames(); |
49 |
| - app.selected = ['sam'] |
50 |
| - let i = 0 |
51 |
| - mock.method(app, 'getRandomPerson', () => app.people[i++]); |
52 |
| - assert.strictEqual(app.selectNextPerson(),'jack') |
53 |
| - assert.strictEqual(app.selectNextPerson(),'jim') |
54 |
| - assert.strictEqual(app.selectNextPerson(),null) |
55 |
| -}) |
56 |
| - |
57 |
| - |
58 |
| - |
59 |
| -test('notifySelected',async ()=>{ |
60 |
| - const app = new Application(); |
61 |
| - const [names] = await app.getNames(); |
62 |
| - app.selected = names.slice(); |
63 |
| - app.mailSystem.send = mock.fn(app.mailSystem.send); |
64 |
| - app.mailSystem.write = mock.fn(app.mailSystem.write); |
65 |
| - app.notifySelected(); |
66 |
| - assert.strictEqual(app.mailSystem.send.mock.calls.length, names.length); |
67 |
| - assert.strictEqual(app.mailSystem.write.mock.calls.length, names.length); |
68 |
| -}) |
0 commit comments