neatjson 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,28 +1,28 @@
1
- require_relative '../lib/neatjson'
2
- require_relative './tests'
3
-
4
- pass = 0
5
- count = 0
6
- $perftesting = %w[-p --perfest].include?(ARGV.first)
7
- $large = JSON.parse(IO.read(File.expand_path('../large.json',__FILE__))) if $perftesting
8
- start = Time.now
9
- TESTS.each do |value_tests|
10
- val, tests = value_tests[:value], value_tests[:tests]
11
- tests.each do |test|
12
- cmd = "JSON.neat_generate(#{val.inspect},#{test[:opts].inspect})"
13
- begin
14
- json = test[:opts] ? JSON.neat_generate(val,test[:opts].dup) : JSON.neat_generate(val)
15
- raise "EXPECTED:\n#{test[:json]}\nACTUAL:\n#{json}" unless test[:json]===json
16
- pass += 1
17
- rescue StandardError => e
18
- puts "Failure running #{cmd}", e, ""
19
- end
20
- count += 1
21
- if $perftesting
22
- JSON.neat_generate($large,test[:opts]) rescue puts "Error serializing $large with #{test[:opts].inspect}"
23
- end
24
- end
25
- end
26
- elapsed = Time.now-start
27
- elapsed = 0.0001 if elapsed==0
28
- puts "%d/%d test#{:s if count!=1} passed in %.2fms (%d tests per second)" % [pass, count, elapsed*1000, count/elapsed]
1
+ require_relative '../lib/neatjson'
2
+ require_relative './tests'
3
+
4
+ pass = 0
5
+ count = 0
6
+ $perftesting = %w[-p --perfest].include?(ARGV.first)
7
+ $large = JSON.parse(IO.read(File.expand_path('../large.json',__FILE__))) if $perftesting
8
+ start = Time.now
9
+ TESTS.each do |value_tests|
10
+ val, tests = value_tests[:value], value_tests[:tests]
11
+ tests.each do |test|
12
+ cmd = "JSON.neat_generate(#{val.inspect},#{test[:opts].inspect})"
13
+ begin
14
+ json = test[:opts] ? JSON.neat_generate(val,test[:opts].dup) : JSON.neat_generate(val)
15
+ raise "EXPECTED:\n#{test[:json]}\nACTUAL:\n#{json}" unless test[:json]===json
16
+ pass += 1
17
+ rescue StandardError => e
18
+ puts "Failure running #{cmd}", e, ""
19
+ end
20
+ count += 1
21
+ if $perftesting
22
+ JSON.neat_generate($large,test[:opts]) rescue puts "Error serializing $large with #{test[:opts].inspect}"
23
+ end
24
+ end
25
+ end
26
+ elapsed = Time.now-start
27
+ elapsed = 0.0001 if elapsed==0
28
+ puts "%d/%d test#{:s if count!=1} passed in %.2fms (%d tests per second)" % [pass, count, elapsed*1000, count/elapsed]
@@ -1,183 +1,183 @@
1
- exports.tests = [
2
- {value:true, tests:[{json:"true" }]},
3
- {value:false, tests:[{json:"false" }]},
4
- // {value:nil, tests:[{json:"null" }]},
5
- {value:null, tests:[{json:"null" }]},
6
- {value:undefined,tests:[{json:"null" }]},
7
- {value:5, tests:[
8
- {json:"5"},
9
- {json:"5", opts:{decimals:3}},
10
- ]},
11
- {value:5.0, tests:[
12
- {json:"5"},
13
- {json:"5", opts:{decimals:3}},
14
- ]},
15
- {value:5.0001, tests:[
16
- {json:"5.0001"},
17
- {json:"5.000", opts:{decimals:3}},
18
- ]},
19
- {value:4.2, tests:[
20
- {json:"4.2"},
21
- {json:"4", opts:{decimals:0}},
22
- {json:"4.20",opts:{decimals:2}},
23
- ]},
24
- {value:4.199, tests:[{json:"4.20", opts:{decimals:2}}]},
25
- {value:4.204, tests:[{json:"4.20", opts:{decimals:2}}]},
26
- {value:-1.9, tests:[{json:"-2", opts:{decimals:0}}]},
27
- {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
28
- {value:1e23, tests:[{json:/1(?:\.0+)?e\+23/i}]},
29
- {value:1e-9, tests:[{json:/1(?:\.0+)?e-0*9/i}]},
30
- {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
31
-
32
- {value:"foo", tests:[{json:"\"foo\""}]},
33
- // {value: :foo, tests:[{json:"\"foo\""}]},
34
- {value:"foo\nbar", tests:[{json:"\"foo\\nbar\""}]},
35
-
36
- {value:[1,2,3,4,[5,6,7,[8,9,10],11,12]], tests:[
37
- { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]" },
38
- { json:"[\n 1,\n 2,\n 3,\n 4,\n [5,6,7,[8,9,10],11,12]\n]", opts:{wrap:30} },
39
- { json:"[\n 1,\n 2,\n 3,\n 4,\n [\n 5,\n 6,\n 7,\n [8,9,10],\n 11,\n 12\n ]\n]", opts:{wrap:20} },
40
- { json:"[\n 1,\n 2,\n 3,\n 4,\n [\n 5,\n 6,\n 7,\n [\n 8,\n 9,\n 10\n ],\n 11,\n 12\n ]\n]", opts:{wrap:true} },
41
- { json:"[\n\t1,\n\t2,\n\t3,\n\t4,\n\t[\n\t\t5,\n\t\t6,\n\t\t7,\n\t\t[\n\t\t\t8,\n\t\t\t9,\n\t\t\t10\n\t\t],\n\t\t11,\n\t\t12\n\t]\n]", opts:{wrap:true,indent:"\t"} },
42
- { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]", opts:{arrayPadding:0} },
43
- { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{arrayPadding:1} },
44
- { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{arrayPadding:2} },
45
- { json:"[1, 2, 3, 4, [5, 6, 7, [8, 9, 10], 11, 12]]", opts:{afterComma:1} },
46
- { json:"[ 1, 2, 3, 4, [ 5, 6, 7, [ 8, 9, 10 ], 11, 12 ] ]", opts:{afterComma:1,arrayPadding:1} },
47
- { json:"[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", opts:{short:true,wrap:true} },
48
- { json:"[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", opts:{short:true,wrap:true,afterComma:1} },
49
- { json:"[ 1,\n 2,\n 3,\n 4,\n [ 5,\n 6,\n 7,\n [ 8,\n 9,\n 10 ],\n 11,\n 12 ] ]", opts:{short:true,wrap:true,arrayPadding:1} },
50
- ]},
51
-
52
- {value:[1,2,3], tests:[
53
- { json:"[1,2,3]" },
54
- { json:"[1 ,2 ,3]", opts:{beforeComma:1} },
55
- { json:"[1 , 2 , 3]", opts:{aroundComma:1} },
56
- { json:"[\n\t1,\n\t2,\n\t3\n]", opts:{wrap:true,indent:"\t"} },
57
- { json:"[\n\t1,\n\t2,\n\t3\n\t]", opts:{wrap:true,indent:"\t",indentLast:true} },
58
- ]},
59
-
60
- {value:{b:1,a:2}, tests:[
61
- { json:'{"b":1,"a":2}' },
62
- { json:'{"a":2,"b":1}', opts:{sorted:true} },
63
- { json:'{"a":2,"b":1}', opts:{sort:true} },
64
- { json:'{"a":2, "b":1}', opts:{sorted:true,afterComma:1} },
65
- { json:'{"a" :2,"b" :1}', opts:{sorted:true,beforeColon:1} },
66
- { json:'{"a": 2,"b": 1}', opts:{sorted:true,afterColon:1} },
67
- { json:'{"a" : 2,"b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1} },
68
- { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1} },
69
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,padding:1} },
70
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:1,afterComma:1,objectPadding:1} },
71
- { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,arrayPadding:1} },
72
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:2,afterComma:1,padding:2} },
73
- { json:'{ "a":2, "b":1 }', opts:{sorted:true,afterComma:1,padding:2} },
74
- { json:'{"b": 1,"a": 2}', opts:{afterColon1:2} },
75
- { json:'{"b" : 1,"a" : 2}', opts:{aroundColon1:2} },
76
- { json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,aroundColon1:2} },
77
- { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColon:1} },
78
- { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColonN:1} },
79
- { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
80
- { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColon:1} },
81
- { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColonN:1} },
82
- { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,afterColon1:1} },
83
- ]},
84
-
85
- {value:{b:1,aaa:2,cc:3}, tests:[
86
- { json:"{\n \"b\":1,\n \"aaa\":2,\n \"cc\":3\n}", opts:{wrap:true} },
87
- { json:"{\n \"b\" :1,\n \"aaa\":2,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true} },
88
- { json:"{\"b\":1,\"aaa\":2,\"cc\":3}", opts:{aligned:true} },
89
- { json:"{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true,sorted:true} },
90
- ]},
91
-
92
- {value:{a:1}, tests:[
93
- { json:'{"a":1}' },
94
- { json:"{\n \"a\":1\n}", opts:{wrap:true} },
95
- { json:"{\n \"a\":1\n }", opts:{wrap:true, indentLast:true} },
96
- { json:"{\n \"a\":1\n }", opts:{wrap:true, indentLast:true, indent:" " } },
97
- ]},
98
-
99
- {value:{ b:17, a:42 }, tests:[
100
- { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} },
101
- { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sort:true, short:true} },
102
- { json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sorted:true} },
103
- { json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sort:true} },
104
- { json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k){ return k } } },
105
- { json:"{\"b\":17,\"a\":42}", opts:{wrap:false,sort:function(k,v){ return v } } },
106
- { json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k,v){ return -v } } },
107
- { json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k,v,o){ return v==o.a ? 0 : 1 } } },
108
- { json:"{\n\"b\":17,\n\"a\":42\n}", opts:{wrap:1,indent:"",sort:function(k){ return k=="a" ? 1 : 0 } } },
109
- { json:"{\n\"a\":42,\n\"b\":17\n}", opts:{wrap:1,indent:"",sort:function(k){ return k=="a" ? 0 : 1 } } },
110
- ]},
111
-
112
- {value:[1,{a:2},3], tests:[
113
- { json:'[1,{"a":2},3]' },
114
- { json:'[ 1,{ "a":2 },3 ]', opts:{padding:1} },
115
- { json:'[ 1, { "a":2 }, 3 ]', opts:{padding:1,afterComma:1} },
116
- { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts:{wrap:true} },
117
- { json:"[\n 1,\n {\"a\":2},\n 3\n]", opts:{wrap:10} },
118
- { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n ]", opts:{wrap:true,indentLast:true} },
119
- ]},
120
-
121
- {value:[1,{a:2,b:3},4], tests:[
122
- { json:"[1,\n {\"a\":2,\n \"b\":3},\n 4]", opts:{wrap:0,short:true} },
123
- ]},
124
-
125
- {value:{a:1,b:[2,3,4],c:3}, tests:[
126
- { json:'{"a":1,"b":[2,3,4],"c":3}' },
127
- { json:"{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts:{wrap:10} },
128
- { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts:{wrap:true} },
129
- { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n }", opts:{wrap:true,indentLast:true} },
130
- ]},
131
-
132
- {value:{hooo:42,whee:['yaaa','oooo','booy'],zoop:"whoop"}, tests:[
133
- { json:"{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts:{wrap:20,short:true} },
134
- ]},
135
-
136
- {value:{ a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }, tests:[
137
- { json:"{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts:{wrap:true,short:true} },
138
- ]},
139
-
140
- {value:{"abcdefghij":[{"abcdefghijklmnop":{}}]}, tests:[
141
- { json:'{"abcdefghij":[{"abcdefghijklmnop":{}}]}' },
142
- { json:'{"abcdefghij" : [{"abcdefghijklmnop" : {}}]}', opts:{wrap:1, short:true, aroundColonN:1} },
143
- ]},
144
-
145
- {value:{"foo":{}}, tests:[
146
- { json:'{"foo":{}}' },
147
- { json:'{"foo":{}}', opts:{wrap:false} },
148
- { json:'{\n "foo":{}\n}', opts:{wrap:5} },
149
- { json:'{"foo":{}}', opts:{wrap:1, short:true} },
150
- ]},
151
-
152
- {value:["foo",{},"bar"], tests:[
153
- { json:'[\n "foo",\n {},\n "bar"\n]', opts:{wrap:1} },
154
- { json:'["foo",\n {},\n "bar"]', opts:{wrap:1, short:true} },
155
- ]},
156
-
157
- {value:["foo",[],"bar"], tests:[
158
- { json:'[\n "foo",\n [],\n "bar"\n]', opts:{wrap:1} },
159
- { json:'["foo",\n [],\n "bar"]', opts:{wrap:1, short:true} },
160
- ]},
161
-
162
- {value:["foo",[{},[{"foo":[]},42]],"bar"], tests:[
163
- { json:'["foo",\n [{},\n [{"foo":[]},\n 42]],\n "bar"]', opts:{wrap:1, short:true} },
164
- ]},
165
-
166
- {value:{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:1}}}}}}}}}}}}}, tests:[
167
- { json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:false} },
168
- { json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:1,short:true} },
169
- { json:"{\n \"a\":{\n \"b\":{\n \"c\":{\n \"d\":{\n \"e\":{\n \"f\":{\n \"g\":{\n \"h\":{\n \"i\":{\n \"j\":{\n \"k\":{\n \"l\":{\n \"m\":1\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}", opts:{wrap:1} },
170
- ]},
171
-
172
- // {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
173
- // { json:'{ "a":1}' },
174
- // { json:'{ "a":1}', opts:{wrap:true} },
175
- // { json:'{"a":1}', opts:{indent:''} },
176
- // ]},
177
-
178
- // {value:Class.new{ def to_json(*a); JSON.neat_generate({a:1},*a); end }.new, tests:[
179
- // { json:'{"a":1}' },
180
- // { json:"{\n \"a\":1\n}", opts:{wrap:true} }
181
- // ]}
182
- ]
183
-
1
+ exports.tests = [
2
+ {value:true, tests:[{json:"true" }]},
3
+ {value:false, tests:[{json:"false" }]},
4
+ // {value:nil, tests:[{json:"null" }]},
5
+ {value:null, tests:[{json:"null" }]},
6
+ {value:undefined,tests:[{json:"null" }]},
7
+ {value:5, tests:[
8
+ {json:"5"},
9
+ {json:"5", opts:{decimals:3}},
10
+ ]},
11
+ {value:5.0, tests:[
12
+ {json:"5"},
13
+ {json:"5", opts:{decimals:3}},
14
+ ]},
15
+ {value:5.0001, tests:[
16
+ {json:"5.0001"},
17
+ {json:"5.000", opts:{decimals:3}},
18
+ ]},
19
+ {value:4.2, tests:[
20
+ {json:"4.2"},
21
+ {json:"4", opts:{decimals:0}},
22
+ {json:"4.20",opts:{decimals:2}},
23
+ ]},
24
+ {value:4.199, tests:[{json:"4.20", opts:{decimals:2}}]},
25
+ {value:4.204, tests:[{json:"4.20", opts:{decimals:2}}]},
26
+ {value:-1.9, tests:[{json:"-2", opts:{decimals:0}}]},
27
+ {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
28
+ {value:1e23, tests:[{json:/1(?:\.0+)?e\+23/i}]},
29
+ {value:1e-9, tests:[{json:/1(?:\.0+)?e-0*9/i}]},
30
+ {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
31
+
32
+ {value:"foo", tests:[{json:"\"foo\""}]},
33
+ // {value: :foo, tests:[{json:"\"foo\""}]},
34
+ {value:"foo\nbar", tests:[{json:"\"foo\\nbar\""}]},
35
+
36
+ {value:[1,2,3,4,[5,6,7,[8,9,10],11,12]], tests:[
37
+ { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]" },
38
+ { json:"[\n 1,\n 2,\n 3,\n 4,\n [5,6,7,[8,9,10],11,12]\n]", opts:{wrap:30} },
39
+ { json:"[\n 1,\n 2,\n 3,\n 4,\n [\n 5,\n 6,\n 7,\n [8,9,10],\n 11,\n 12\n ]\n]", opts:{wrap:20} },
40
+ { json:"[\n 1,\n 2,\n 3,\n 4,\n [\n 5,\n 6,\n 7,\n [\n 8,\n 9,\n 10\n ],\n 11,\n 12\n ]\n]", opts:{wrap:true} },
41
+ { json:"[\n\t1,\n\t2,\n\t3,\n\t4,\n\t[\n\t\t5,\n\t\t6,\n\t\t7,\n\t\t[\n\t\t\t8,\n\t\t\t9,\n\t\t\t10\n\t\t],\n\t\t11,\n\t\t12\n\t]\n]", opts:{wrap:true,indent:"\t"} },
42
+ { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]", opts:{arrayPadding:0} },
43
+ { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{arrayPadding:1} },
44
+ { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{arrayPadding:2} },
45
+ { json:"[1, 2, 3, 4, [5, 6, 7, [8, 9, 10], 11, 12]]", opts:{afterComma:1} },
46
+ { json:"[ 1, 2, 3, 4, [ 5, 6, 7, [ 8, 9, 10 ], 11, 12 ] ]", opts:{afterComma:1,arrayPadding:1} },
47
+ { json:"[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", opts:{short:true,wrap:true} },
48
+ { json:"[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", opts:{short:true,wrap:true,afterComma:1} },
49
+ { json:"[ 1,\n 2,\n 3,\n 4,\n [ 5,\n 6,\n 7,\n [ 8,\n 9,\n 10 ],\n 11,\n 12 ] ]", opts:{short:true,wrap:true,arrayPadding:1} },
50
+ ]},
51
+
52
+ {value:[1,2,3], tests:[
53
+ { json:"[1,2,3]" },
54
+ { json:"[1 ,2 ,3]", opts:{beforeComma:1} },
55
+ { json:"[1 , 2 , 3]", opts:{aroundComma:1} },
56
+ { json:"[\n\t1,\n\t2,\n\t3\n]", opts:{wrap:true,indent:"\t"} },
57
+ { json:"[\n\t1,\n\t2,\n\t3\n\t]", opts:{wrap:true,indent:"\t",indentLast:true} },
58
+ ]},
59
+
60
+ {value:{b:1,a:2}, tests:[
61
+ { json:'{"b":1,"a":2}' },
62
+ { json:'{"a":2,"b":1}', opts:{sorted:true} },
63
+ { json:'{"a":2,"b":1}', opts:{sort:true} },
64
+ { json:'{"a":2, "b":1}', opts:{sorted:true,afterComma:1} },
65
+ { json:'{"a" :2,"b" :1}', opts:{sorted:true,beforeColon:1} },
66
+ { json:'{"a": 2,"b": 1}', opts:{sorted:true,afterColon:1} },
67
+ { json:'{"a" : 2,"b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1} },
68
+ { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1} },
69
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,padding:1} },
70
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:1,afterComma:1,objectPadding:1} },
71
+ { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,arrayPadding:1} },
72
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:2,afterComma:1,padding:2} },
73
+ { json:'{ "a":2, "b":1 }', opts:{sorted:true,afterComma:1,padding:2} },
74
+ { json:'{"b": 1,"a": 2}', opts:{afterColon1:2} },
75
+ { json:'{"b" : 1,"a" : 2}', opts:{aroundColon1:2} },
76
+ { json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,aroundColon1:2} },
77
+ { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColon:1} },
78
+ { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColonN:1} },
79
+ { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
80
+ { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColon:1} },
81
+ { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColonN:1} },
82
+ { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,afterColon1:1} },
83
+ ]},
84
+
85
+ {value:{b:1,aaa:2,cc:3}, tests:[
86
+ { json:"{\n \"b\":1,\n \"aaa\":2,\n \"cc\":3\n}", opts:{wrap:true} },
87
+ { json:"{\n \"b\" :1,\n \"aaa\":2,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true} },
88
+ { json:"{\"b\":1,\"aaa\":2,\"cc\":3}", opts:{aligned:true} },
89
+ { json:"{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true,sorted:true} },
90
+ ]},
91
+
92
+ {value:{a:1}, tests:[
93
+ { json:'{"a":1}' },
94
+ { json:"{\n \"a\":1\n}", opts:{wrap:true} },
95
+ { json:"{\n \"a\":1\n }", opts:{wrap:true, indentLast:true} },
96
+ { json:"{\n \"a\":1\n }", opts:{wrap:true, indentLast:true, indent:" " } },
97
+ ]},
98
+
99
+ {value:{ b:17, a:42 }, tests:[
100
+ { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} },
101
+ { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sort:true, short:true} },
102
+ { json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sorted:true} },
103
+ { json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sort:true} },
104
+ { json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k){ return k } } },
105
+ { json:"{\"b\":17,\"a\":42}", opts:{wrap:false,sort:function(k,v){ return v } } },
106
+ { json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k,v){ return -v } } },
107
+ { json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k,v,o){ return v==o.a ? 0 : 1 } } },
108
+ { json:"{\n\"b\":17,\n\"a\":42\n}", opts:{wrap:1,indent:"",sort:function(k){ return k=="a" ? 1 : 0 } } },
109
+ { json:"{\n\"a\":42,\n\"b\":17\n}", opts:{wrap:1,indent:"",sort:function(k){ return k=="a" ? 0 : 1 } } },
110
+ ]},
111
+
112
+ {value:[1,{a:2},3], tests:[
113
+ { json:'[1,{"a":2},3]' },
114
+ { json:'[ 1,{ "a":2 },3 ]', opts:{padding:1} },
115
+ { json:'[ 1, { "a":2 }, 3 ]', opts:{padding:1,afterComma:1} },
116
+ { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts:{wrap:true} },
117
+ { json:"[\n 1,\n {\"a\":2},\n 3\n]", opts:{wrap:10} },
118
+ { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n ]", opts:{wrap:true,indentLast:true} },
119
+ ]},
120
+
121
+ {value:[1,{a:2,b:3},4], tests:[
122
+ { json:"[1,\n {\"a\":2,\n \"b\":3},\n 4]", opts:{wrap:0,short:true} },
123
+ ]},
124
+
125
+ {value:{a:1,b:[2,3,4],c:3}, tests:[
126
+ { json:'{"a":1,"b":[2,3,4],"c":3}' },
127
+ { json:"{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts:{wrap:10} },
128
+ { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts:{wrap:true} },
129
+ { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n }", opts:{wrap:true,indentLast:true} },
130
+ ]},
131
+
132
+ {value:{hooo:42,whee:['yaaa','oooo','booy'],zoop:"whoop"}, tests:[
133
+ { json:"{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts:{wrap:20,short:true} },
134
+ ]},
135
+
136
+ {value:{ a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }, tests:[
137
+ { json:"{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts:{wrap:true,short:true} },
138
+ ]},
139
+
140
+ {value:{"abcdefghij":[{"abcdefghijklmnop":{}}]}, tests:[
141
+ { json:'{"abcdefghij":[{"abcdefghijklmnop":{}}]}' },
142
+ { json:'{"abcdefghij" : [{"abcdefghijklmnop" : {}}]}', opts:{wrap:1, short:true, aroundColonN:1} },
143
+ ]},
144
+
145
+ {value:{"foo":{}}, tests:[
146
+ { json:'{"foo":{}}' },
147
+ { json:'{"foo":{}}', opts:{wrap:false} },
148
+ { json:'{\n "foo":{}\n}', opts:{wrap:5} },
149
+ { json:'{"foo":{}}', opts:{wrap:1, short:true} },
150
+ ]},
151
+
152
+ {value:["foo",{},"bar"], tests:[
153
+ { json:'[\n "foo",\n {},\n "bar"\n]', opts:{wrap:1} },
154
+ { json:'["foo",\n {},\n "bar"]', opts:{wrap:1, short:true} },
155
+ ]},
156
+
157
+ {value:["foo",[],"bar"], tests:[
158
+ { json:'[\n "foo",\n [],\n "bar"\n]', opts:{wrap:1} },
159
+ { json:'["foo",\n [],\n "bar"]', opts:{wrap:1, short:true} },
160
+ ]},
161
+
162
+ {value:["foo",[{},[{"foo":[]},42]],"bar"], tests:[
163
+ { json:'["foo",\n [{},\n [{"foo":[]},\n 42]],\n "bar"]', opts:{wrap:1, short:true} },
164
+ ]},
165
+
166
+ {value:{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:1}}}}}}}}}}}}}, tests:[
167
+ { json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:false} },
168
+ { json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:1,short:true} },
169
+ { json:"{\n \"a\":{\n \"b\":{\n \"c\":{\n \"d\":{\n \"e\":{\n \"f\":{\n \"g\":{\n \"h\":{\n \"i\":{\n \"j\":{\n \"k\":{\n \"l\":{\n \"m\":1\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}", opts:{wrap:1} },
170
+ ]},
171
+
172
+ // {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
173
+ // { json:'{ "a":1}' },
174
+ // { json:'{ "a":1}', opts:{wrap:true} },
175
+ // { json:'{"a":1}', opts:{indent:''} },
176
+ // ]},
177
+
178
+ // {value:Class.new{ def to_json(*a); JSON.neat_generate({a:1},*a); end }.new, tests:[
179
+ // { json:'{"a":1}' },
180
+ // { json:"{\n \"a\":1\n}", opts:{wrap:true} }
181
+ // ]}
182
+ ]
183
+
@@ -174,6 +174,15 @@ TESTS = [
174
174
  { json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:false} },
175
175
  { json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:1,short:true} },
176
176
  { json:"{\n \"a\":{\n \"b\":{\n \"c\":{\n \"d\":{\n \"e\":{\n \"f\":{\n \"g\":{\n \"h\":{\n \"i\":{\n \"j\":{\n \"k\":{\n \"l\":{\n \"m\":1\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}", opts:{wrap:1} },
177
- ]}
177
+ ]},
178
+
179
+ # Issue #27
180
+ {value:{'b'=>2, a:1}, tests:[
181
+ {json:'{"b":2,"a":1}', opts:{wrap:false}},
182
+ {json:'{"a":1,"b":2}', opts:{wrap:false, sort:->(k){k.to_s}}},
183
+ {json:'{"a":1,"b":2}', opts:{wrap:false, sort:->(k,v){k.to_s}}},
184
+ {json:'{"a":1,"b":2}', opts:{wrap:false, sort:->(k,v,o){k.to_s}}},
185
+ {json:'{"a":1,"b":2}', opts:{wrap:false, sort:true}},
186
+ ]},
178
187
  ]
179
188
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neatjson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Kistner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-20 00:00:00.000000000 Z
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Generate JSON strings from Ruby objects with flexible formatting options.
14
14
  Key features: keep arrays and objects on a single line when they fit; format floats
@@ -31,7 +31,7 @@ files:
31
31
  - test/tests.rb
32
32
  homepage: http://github.com/Phrogz/NeatJSON
33
33
  licenses:
34
- - MIT license (MIT)
34
+ - MIT
35
35
  metadata: {}
36
36
  post_install_message:
37
37
  rdoc_options: []
@@ -49,9 +49,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  version: '0'
50
50
  requirements: []
51
51
  rubyforge_project:
52
- rubygems_version: 2.4.6
52
+ rubygems_version: 2.5.2
53
53
  signing_key:
54
54
  specification_version: 4
55
55
  summary: Pretty, powerful, flexible JSON generation.
56
56
  test_files: []
57
- has_rdoc: yard