1
- var jsonLogic = require ( "../dist/jsonLogic.js" ) ;
2
1
var http = require ( "http" ) ;
3
2
var fs = require ( "fs" ) ;
3
+ var jsonLogic = require ( "../dist/jsonLogic.js" ) ;
4
4
5
5
var download = function ( url , dest , cb ) {
6
6
var file = fs . createWriteStream ( dest ) ;
@@ -15,52 +15,57 @@ var download = function(url, dest, cb) {
15
15
} ) ;
16
16
} ;
17
17
18
- var remote_or_cache = function ( remote_url , local_file , description , runner ) {
19
- var parse_and_iterate = function ( local_file , description , runner ) {
20
- fs . readFile ( local_file , "utf8" , function ( error , body ) {
21
- var tests ;
22
- try {
23
- tests = JSON . parse ( body ) ;
24
- } catch ( e ) {
25
- throw new Error ( "Trouble parsing " + description + ": " + e . message ) ;
26
- }
27
-
28
- // Remove comments
29
- tests = tests . filter ( function ( test ) { return typeof test !== 'string' ; } ) ;
30
-
31
- console . log ( "Including " + tests . length + " " + description ) ;
32
-
33
- QUnit . test ( description , function ( assert ) {
34
- tests . map ( runner ) ;
18
+ var remote_or_cache = function ( remote_url , local_file , description , runner ) {
19
+ QUnit . test ( 'Load and run remote tests form: ' + remote_url , function ( assert ) {
20
+ assert . expect ( 0 ) ;
21
+ // Only waiting on the request() is async
22
+ var done = assert . async ( ) ;
23
+
24
+ var parse_and_iterate = function ( local_file , description , runner ) {
25
+ fs . readFile ( local_file , "utf8" , function ( error , body ) {
26
+ var tests ;
27
+ try {
28
+ tests = JSON . parse ( body ) ;
29
+ } catch ( e ) {
30
+ throw new Error ( "Trouble parsing " + description + ": " + e . message ) ;
31
+ }
32
+
33
+ // Remove comments
34
+ tests = tests . filter ( function ( test ) {
35
+ return typeof test !== 'string' ;
36
+ } ) ;
37
+
38
+ console . log ( "Including " + tests . length + " " + description ) ;
39
+
40
+ QUnit . test ( description , function ( assert ) {
41
+ tests . map ( function ( test ) {
42
+ runner ( test , assert ) ;
43
+ } ) ;
44
+ } ) ;
45
+
46
+ done ( ) ;
35
47
} ) ;
36
-
37
- start ( ) ;
38
- } ) ;
39
-
40
- } ;
41
-
42
- // Only waiting on the request() is async
43
- stop ( ) ;
44
-
45
- fs . stat ( local_file , function ( err , stats ) {
46
- if ( err ) {
47
- console . log ( "Downloading " + description + " from JsonLogic.com" ) ;
48
- download ( remote_url , local_file , function ( ) {
48
+ } ;
49
+
50
+ fs . stat ( local_file , function ( err , stats ) {
51
+ if ( err ) {
52
+ console . log ( "Downloading " + description + " from JsonLogic.com" ) ;
53
+ download ( remote_url , local_file , function ( ) {
54
+ parse_and_iterate ( local_file , description , runner ) ;
55
+ } ) ;
56
+ } else {
57
+ console . log ( "Using cached " + description ) ;
49
58
parse_and_iterate ( local_file , description , runner ) ;
50
- } ) ;
51
- } else {
52
- console . log ( "Using cached " + description ) ;
53
- parse_and_iterate ( local_file , description , runner ) ;
54
- }
59
+ }
60
+ } ) ;
55
61
} ) ;
56
-
57
62
} ;
58
63
59
64
remote_or_cache (
60
65
"http://jsonlogic.com/tests.json" ,
61
66
"tests/tests.json" ,
62
67
"applies() tests" ,
63
- function ( test ) {
68
+ function ( test , assert ) {
64
69
var rule = test [ 0 ] ;
65
70
var data = test [ 1 ] ;
66
71
var expected = test [ 2 ] ;
@@ -69,8 +74,8 @@ remote_or_cache(
69
74
jsonLogic . apply ( rule , data ) ,
70
75
expected ,
71
76
"jsonLogic.apply(" + JSON . stringify ( rule ) + "," +
72
- JSON . stringify ( data ) + ") === " +
73
- JSON . stringify ( expected )
77
+ JSON . stringify ( data ) + ") === " +
78
+ JSON . stringify ( expected )
74
79
) ;
75
80
}
76
81
) ;
@@ -79,7 +84,7 @@ remote_or_cache(
79
84
"http://jsonlogic.com/rule_like.json" ,
80
85
"tests/rule_like.json" ,
81
86
"rule_like() tests" ,
82
- function ( test ) {
87
+ function ( test , assert ) {
83
88
var rule = test [ 0 ] ;
84
89
var pattern = test [ 1 ] ;
85
90
var expected = test [ 2 ] ;
@@ -88,17 +93,12 @@ remote_or_cache(
88
93
jsonLogic . rule_like ( rule , pattern ) ,
89
94
expected ,
90
95
"jsonLogic.rule_like(" + JSON . stringify ( rule ) + "," +
91
- JSON . stringify ( pattern ) + ") === " +
92
- JSON . stringify ( expected )
96
+ JSON . stringify ( pattern ) + ") === " +
97
+ JSON . stringify ( expected )
93
98
) ;
94
99
}
95
100
) ;
96
101
97
-
98
-
99
-
100
-
101
-
102
102
QUnit . test ( "Bad operator" , function ( assert ) {
103
103
assert . throws (
104
104
function ( ) {
@@ -108,11 +108,13 @@ QUnit.test( "Bad operator", function( assert ) {
108
108
) ;
109
109
} ) ;
110
110
111
-
112
111
QUnit . test ( "logging" , function ( assert ) {
113
112
var last_console ;
113
+ var log = console . log ;
114
114
console . log = function ( logged ) {
115
115
last_console = logged ;
116
+
117
+ log ( logged ) ;
116
118
} ;
117
119
assert . equal ( jsonLogic . apply ( { "log" : [ 1 ] } ) , 1 ) ;
118
120
assert . equal ( last_console , 1 ) ;
@@ -172,7 +174,7 @@ QUnit.test( "Expanding functionality with add_operator", function( assert) {
172
174
42
173
175
) ;
174
176
175
- //Remove operation:
177
+ // Remove operation:
176
178
jsonLogic . rm_operation ( "times" ) ;
177
179
178
180
assert . throws (
@@ -193,9 +195,6 @@ QUnit.test( "Expanding functionality with add_operator", function( assert) {
193
195
) ,
194
196
42
195
197
) ;
196
-
197
-
198
-
199
198
} ) ;
200
199
201
200
QUnit . test ( "Expanding functionality with method" , function ( assert ) {
@@ -232,7 +231,7 @@ QUnit.test( "Expanding functionality with method", function( assert) {
232
231
} ) ;
233
232
234
233
235
- QUnit . test ( "Control structures don't eval depth-first" , function ( assert ) {
234
+ QUnit . test ( "Control structures don't eval depth-first" , function ( assert ) {
236
235
// Depth-first recursion was wasteful but not harmful until we added custom operations that could have side-effects.
237
236
238
237
// If operations run the condition, if truthy, it runs and returns that consequent.
0 commit comments