@@ -65,7 +65,7 @@ describe('Arithmetic', () => {
6565 } ) ;
6666
6767 describe ( 'unary minus (for eg, -1)' , ( ) => {
68- it ( 'should handle multiplying by a negative' , ( ) => {
68+ it ( 'should handle dividing by a negative' , ( ) => {
6969 const result = runArithmetic ( '4 / - 2' ) ;
7070 expect ( result ) . toEqual ( 4 / - 2 ) ;
7171 } ) ;
@@ -75,7 +75,7 @@ describe('Arithmetic', () => {
7575 expect ( result ) . toEqual ( 4 * - 2 * - 2 ) ;
7676 } ) ;
7777
78- it ( 'should handle multiplying by a double negative' , ( ) => {
78+ it ( 'should handle dividing by a double negative' , ( ) => {
7979 const result = runArithmetic ( '4 / - - 2' ) ;
8080 expect ( result ) . toEqual ( 4 / 2 ) ;
8181 } ) ;
@@ -94,6 +94,44 @@ describe('Arithmetic', () => {
9494 const result = runArithmetic ( '1+1 - - 5' ) ;
9595 expect ( result ) . toEqual ( 1 + 1 - - 5 ) ;
9696 } ) ;
97+
98+ it ( 'should handle negating a function call' , ( ) => {
99+ const result = runArithmetic ( '1 - -max(1, 2)' ) ;
100+ expect ( result ) . toEqual ( 1 - - Math . max ( 1 , 2 ) ) ;
101+ } ) ;
102+ } ) ;
103+
104+ describe ( 'max function' , ( ) => {
105+ const { max } = Math ;
106+ it ( 'should handle max with no arguments' , ( ) => {
107+ const result = runArithmetic ( 'max()' ) ;
108+ expect ( result ) . toEqual ( - Infinity ) ;
109+ } ) ;
110+
111+ it ( 'should handle max of one number' , ( ) => {
112+ const result = runArithmetic ( 'max(15)' ) ;
113+ expect ( result ) . toEqual ( max ( 15 ) ) ;
114+ } ) ;
115+
116+ it ( 'should handle max of two numbers' , ( ) => {
117+ const result = runArithmetic ( 'max(15, 20)' ) ;
118+ expect ( result ) . toEqual ( max ( 15 , 20 ) ) ;
119+ } ) ;
120+
121+ it ( 'should handle a more complex expression' , ( ) => {
122+ const result = runArithmetic ( 'max(1, 3 - 2, -100) / 2' ) ;
123+ expect ( result ) . toEqual ( max ( 1 , 3 - 2 , - 100 ) / 2 ) ;
124+ } ) ;
125+
126+ it ( 'should handle a yet more complex expression' , ( ) => {
127+ const result = runArithmetic ( 'max(max(), (-max(15, 3 -2, -100) / 2 + 1 - max(3/2)) /2, -10)' ) ;
128+ expect ( result ) . toEqual ( max ( - Infinity , ( - max ( 15 , 3 - 2 , - 100 ) / 2 + 1 - max ( 3 / 2 ) ) / 2 , - 10 ) ) ;
129+ } ) ;
130+
131+ it ( 'should be caps insensitive' , ( ) => {
132+ const result = runArithmetic ( 'maX(15)' ) ;
133+ expect ( result ) . toEqual ( max ( 15 ) ) ;
134+ } ) ;
97135 } ) ;
98136
99137 describe ( 'substituting values' , ( ) => {
@@ -103,6 +141,9 @@ describe('Arithmetic', () => {
103141 pi : 3.14159 ,
104142 sins : 7 ,
105143 negative : - 5 ,
144+ theLetter_A : 65 ,
145+ theLetter_a : 97 ,
146+ max : 100 ,
106147 } ;
107148
108149 it ( 'should handle simple value substitution' , ( ) => {
@@ -115,10 +156,25 @@ describe('Arithmetic', () => {
115156 expect ( result ) . toEqual ( 10 - 5 ) ;
116157 } ) ;
117158
159+ it ( 'should handle substituting based on capitalization' , ( ) => {
160+ const result = runArithmetic ( 'theLetter_A - theLetter_a' , VALUES ) ;
161+ expect ( result ) . toEqual ( 65 - 97 ) ;
162+ } ) ;
163+
118164 it ( 'should handle a more complicated case' , ( ) => {
119165 const result = runArithmetic ( '-eyes * (sins - pi * 3) / negative + (sins + 1)' , VALUES ) ;
120166 expect ( result ) . toEqual ( ( - 2 * ( 7 - 3.14159 * 3 ) ) / - 5 + ( 7 + 1 ) ) ;
121167 } ) ;
168+
169+ it ( 'should handle substitution of a value which is a function name' , ( ) => {
170+ const result = runArithmetic ( 'fingers + max' , VALUES ) ;
171+ expect ( result ) . toEqual ( 10 + 100 ) ;
172+ } ) ;
173+
174+ it ( 'should be able to substitute values into a function with the same name' , ( ) => {
175+ const result = runArithmetic ( 'max(max, eyes)' , VALUES ) ;
176+ expect ( result ) . toEqual ( Math . max ( 100 , 10 ) ) ;
177+ } ) ;
122178 } ) ;
123179
124180 describe ( 'errors' , ( ) => {
@@ -135,6 +191,25 @@ describe('Arithmetic', () => {
135191 expect ( ( ) => runArithmetic ( '4 * 1 + 2)' ) ) . toThrow ( ) ;
136192 } ) ;
137193
194+ it ( 'should fail on incorrect function call' , ( ) => {
195+ expect ( ( ) => runArithmetic ( 'max(' ) ) . toThrow ( ) ;
196+ expect ( ( ) => runArithmetic ( 'max())' ) ) . toThrow ( ) ;
197+ expect ( ( ) => runArithmetic ( 'max(()' ) ) . toThrow ( ) ;
198+ expect ( ( ) => runArithmetic ( 'max((1, 2), 3)' ) ) . toThrow ( ) ;
199+ expect ( ( ) => runArithmetic ( 'max(3, (1, 2))' ) ) . toThrow ( ) ;
200+ expect ( ( ) => runArithmetic ( 'max(, 3)' ) ) . toThrow ( ) ;
201+ expect ( ( ) => runArithmetic ( 'max(3, )' ) ) . toThrow ( ) ;
202+ expect ( ( ) => runArithmetic ( 'max(3,,2)' ) ) . toThrow ( ) ;
203+ expect ( ( ) => runArithmetic ( 'max(3-,2)' ) ) . toThrow ( ) ;
204+ } ) ;
205+
206+ it ( 'should fail on incorrect comma usage' , ( ) => {
207+ expect ( ( ) => runArithmetic ( '1 + , 1' ) ) . toThrow ( ) ;
208+ expect ( ( ) => runArithmetic ( '1 , 1' ) ) . toThrow ( ) ;
209+ expect ( ( ) => runArithmetic ( '1 + (1, 1)' ) ) . toThrow ( ) ;
210+ expect ( ( ) => runArithmetic ( ',' ) ) . toThrow ( ) ;
211+ } ) ;
212+
138213 it ( 'should fail if a substitution is not numeric' , ( ) => {
139214 expect ( ( ) => runArithmetic ( 'check + 1' , { check : 'check' } ) ) . toThrow ( ) ;
140215 expect ( ( ) => runArithmetic ( 'check + 1' , { check : '+' } ) ) . toThrow ( ) ;
0 commit comments