@@ -2,4 +2,59 @@ const { describe, it } = require('node:test');
2
2
const assert = require ( 'assert' ) ;
3
3
const { Calculator } = require ( './main' ) ;
4
4
5
- // TODO: write your tests here
5
+ describe ( 'Calculator' , ( ) => {
6
+ const calculator = new Calculator ( ) ;
7
+
8
+ describe ( 'exp function' , ( ) => {
9
+ it ( 'calculates the exponential of a number' , async ( ) => {
10
+ assert . strictEqual ( calculator . exp ( 1 ) , Math . exp ( 1 ) ) ;
11
+ } ) ;
12
+
13
+ it ( 'throws error on non-finite input' , async ( ) => {
14
+ assert . throws ( ( ) => calculator . exp ( 'a' ) , {
15
+ name : 'Error' ,
16
+ message : 'unsupported operand type'
17
+ } ) ;
18
+ assert . throws ( ( ) => calculator . exp ( Infinity ) , {
19
+ name : 'Error' ,
20
+ message : 'unsupported operand type'
21
+ } ) ;
22
+ } ) ;
23
+
24
+ it ( 'handles overflow' , async ( ) => {
25
+ assert . throws ( ( ) => calculator . exp ( 1000 ) , {
26
+ name : 'Error' ,
27
+ message : 'overflow'
28
+ } ) ;
29
+ } ) ;
30
+ } ) ;
31
+
32
+ describe ( 'log function' , ( ) => {
33
+ it ( 'calculates the logarithm of a number' , async ( ) => {
34
+ assert . strictEqual ( calculator . log ( Math . E ) , Math . log ( Math . E ) ) ;
35
+ } ) ;
36
+
37
+ it ( 'throws error on non-finite input' , async ( ) => {
38
+ assert . throws ( ( ) => calculator . log ( 'a' ) , {
39
+ name : 'Error' ,
40
+ message : 'unsupported operand type'
41
+ } ) ;
42
+ assert . throws ( ( ) => calculator . log ( - 1 ) , {
43
+ name : 'Error' ,
44
+ message : 'math domain error (1)'
45
+ } ) ;
46
+ } ) ;
47
+
48
+ it ( 'handles domain errors' , async ( ) => {
49
+ assert . throws ( ( ) => calculator . log ( 0 ) , {
50
+ name : 'Error' ,
51
+ message : 'math domain error (1)'
52
+ } ) ;
53
+ assert . throws ( ( ) => calculator . log ( null ) , {
54
+ name : 'Error' ,
55
+ message : 'unsupported operand type'
56
+ } ) ;
57
+ } ) ;
58
+ } ) ;
59
+ } ) ;
60
+
0 commit comments