@@ -3,3 +3,70 @@ const assert = require('assert');
3
3
const { Calculator } = require ( './main' ) ;
4
4
5
5
// TODO: write your tests here
6
+
7
+ describe ( 'Calculator' , function ( ) {
8
+ const calculator = new Calculator ( ) ;
9
+
10
+ // 定義針對 exp 功能的測試組合
11
+ describe ( '#exp()' , function ( ) {
12
+ // 針對預期正確的參數測試
13
+ const testCasesExp = [
14
+ { input : 1.618 , expected : Math . exp ( 1.618 ) } , // 黃金分割率
15
+ { input : 0 , expected : Math . exp ( 0 ) } ,
16
+ { input : - 1 , expected : Math . exp ( - 1 ) }
17
+ ] ;
18
+
19
+ // for 進行測試
20
+ testCasesExp . forEach ( ( { input, expected } ) => {
21
+ it ( `should correctly calculate exp(${ input } )` , function ( ) {
22
+ assert . strictEqual ( calculator . exp ( input ) , expected ) ;
23
+ } ) ;
24
+ } ) ;
25
+
26
+ // 用 RE 針對預期錯誤的參數測試
27
+ const errorCasesExp = [
28
+ { input : 'string' , message : / u n s u p p o r t e d o p e r a n d t y p e / } ,
29
+ { input : Infinity , message : / u n s u p p o r t e d o p e r a n d t y p e / } ,
30
+ { input : 123456789 , message : / o v e r f l o w / }
31
+ ] ;
32
+
33
+ // for 進行測試
34
+ errorCasesExp . forEach ( ( { input, message } ) => {
35
+ it ( `should throw error for exp(${ input } )` , function ( ) {
36
+ assert . throws ( ( ) => calculator . exp ( input ) , message ) ;
37
+ } ) ;
38
+ } ) ;
39
+ } ) ;
40
+
41
+ // 定義針對 log 功能的測試組合
42
+ describe ( '#log()' , function ( ) {
43
+ // 針對預期正確的參數測試
44
+ const testCasesLog = [
45
+ { input : Math . E , expected : 1 } ,
46
+ { input : 1 , expected : Math . log ( 1 ) } ,
47
+ { input : 3.141592653 , expected : Math . log ( 3.141592653 ) } // π
48
+ ] ;
49
+
50
+ // for 進行測試
51
+ testCasesLog . forEach ( ( { input, expected } ) => {
52
+ it ( `should correctly calculate log(${ input } )` , function ( ) {
53
+ assert . strictEqual ( calculator . log ( input ) , expected ) ;
54
+ } ) ;
55
+ } ) ;
56
+
57
+ // 用 RE 針對預期錯誤的參數測試
58
+ const errorCasesLog = [
59
+ { input : 'string' , message : / u n s u p p o r t e d o p e r a n d t y p e / } ,
60
+ { input : 0 , message : / m a t h d o m a i n e r r o r \( 1 \) / } ,
61
+ { input : - 1 , message : / m a t h d o m a i n e r r o r \( 2 \) / }
62
+ ] ;
63
+
64
+ // for 進行測試
65
+ errorCasesLog . forEach ( ( { input, message } ) => {
66
+ it ( `should throw error for log(${ input } )` , function ( ) {
67
+ assert . throws ( ( ) => calculator . log ( input ) , message ) ;
68
+ } ) ;
69
+ } ) ;
70
+ } ) ;
71
+ } ) ;
72
+
0 commit comments