@@ -165,3 +165,83 @@ QUnit.test( "Expanding functionality with method", function( assert) {
165
165
assert . equal ( a . count , 42 ) ; //Happy state change
166
166
167
167
} ) ;
168
+
169
+
170
+ QUnit . test ( "Control structures don't use depth-first computation" , function ( assert ) {
171
+ //Depth-first recursion was wasteful but not harmful until we added custom operations that could have side-effects.
172
+
173
+ //If operations run the condition, if truthy, it runs and returns that consequent.
174
+ //Consequents of falsy conditions should not run.
175
+ //After one truthy condition, no other condition should run
176
+ var conditions = [ ] ;
177
+ var consequents = [ ] ;
178
+ jsonLogic . add_operation ( "push.if" , function ( v ) { conditions . push ( v ) ; return v ; } ) ;
179
+ jsonLogic . add_operation ( "push.then" , function ( v ) { consequents . push ( v ) ; return v ; } ) ;
180
+ jsonLogic . add_operation ( "push.else" , function ( v ) { consequents . push ( v ) ; return v ; } ) ;
181
+
182
+ jsonLogic . apply ( { "if" :[
183
+ { "push.if" : [ true ] } ,
184
+ { "push.then" :[ "first" ] } ,
185
+ { "push.if" : [ false ] } ,
186
+ { "push.then" :[ "second" ] } ,
187
+ { "push.else" :[ "third" ] }
188
+ ] } ) ;
189
+ assert . deepEqual ( conditions , [ true ] ) ;
190
+ assert . deepEqual ( consequents , [ "first" ] ) ;
191
+
192
+ conditions = [ ] ;
193
+ consequents = [ ] ;
194
+ jsonLogic . apply ( { "if" :[
195
+ { "push.if" : [ false ] } ,
196
+ { "push.then" :[ "first" ] } ,
197
+ { "push.if" : [ true ] } ,
198
+ { "push.then" :[ "second" ] } ,
199
+ { "push.else" :[ "third" ] }
200
+ ] } ) ;
201
+ assert . deepEqual ( conditions , [ false , true ] ) ;
202
+ assert . deepEqual ( consequents , [ "second" ] ) ;
203
+
204
+ conditions = [ ] ;
205
+ consequents = [ ] ;
206
+ jsonLogic . apply ( { "if" :[
207
+ { "push.if" : [ false ] } ,
208
+ { "push.then" :[ "first" ] } ,
209
+ { "push.if" : [ false ] } ,
210
+ { "push.then" :[ "second" ] } ,
211
+ { "push.else" :[ "third" ] }
212
+ ] } ) ;
213
+ assert . deepEqual ( conditions , [ false , false ] ) ;
214
+ assert . deepEqual ( consequents , [ "third" ] ) ;
215
+
216
+
217
+ jsonLogic . add_operation ( "push" , function ( arg ) { i . push ( arg ) ; return arg ; } ) ;
218
+ var i = [ ] ;
219
+
220
+ i = [ ] ;
221
+ jsonLogic . apply ( { "and" :[ { "push" :[ false ] } , { "push" :[ false ] } ] } ) ;
222
+ assert . deepEqual ( i , [ false ] ) ;
223
+ i = [ ] ;
224
+ jsonLogic . apply ( { "and" :[ { "push" :[ false ] } , { "push" :[ true ] } ] } ) ;
225
+ assert . deepEqual ( i , [ false ] ) ;
226
+ i = [ ] ;
227
+ jsonLogic . apply ( { "and" :[ { "push" :[ true ] } , { "push" :[ false ] } ] } ) ;
228
+ assert . deepEqual ( i , [ true , false ] ) ;
229
+ i = [ ] ;
230
+ jsonLogic . apply ( { "and" :[ { "push" :[ true ] } , { "push" :[ true ] } ] } ) ;
231
+ assert . deepEqual ( i , [ true , true ] ) ;
232
+
233
+
234
+ i = [ ] ;
235
+ jsonLogic . apply ( { "or" :[ { "push" :[ false ] } , { "push" :[ false ] } ] } ) ;
236
+ assert . deepEqual ( i , [ false , false ] ) ;
237
+ i = [ ] ;
238
+ jsonLogic . apply ( { "or" :[ { "push" :[ false ] } , { "push" :[ true ] } ] } ) ;
239
+ assert . deepEqual ( i , [ false , true ] ) ;
240
+ i = [ ] ;
241
+ jsonLogic . apply ( { "or" :[ { "push" :[ true ] } , { "push" :[ false ] } ] } ) ;
242
+ assert . deepEqual ( i , [ true ] ) ;
243
+ i = [ ] ;
244
+ jsonLogic . apply ( { "or" :[ { "push" :[ true ] } , { "push" :[ true ] } ] } ) ;
245
+ assert . deepEqual ( i , [ true ] ) ;
246
+
247
+ } ) ;
0 commit comments