neatjson 0.6 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bea172fb8129d16c9334bd5ffdce64be0841fea2
4
- data.tar.gz: 09cb444f5bc31b69a08a89e82a45484865c3d5ab
3
+ metadata.gz: 25e68572d39dc687907b501288dee898cd91b0fe
4
+ data.tar.gz: 945e91700e9d4a0494efa3bf21f38a257580bcdb
5
5
  SHA512:
6
- metadata.gz: 85b9b72bd66a09a29aa5f299d6febce537d103cef6ca023552004730a227f6db65ef05fa5c3a94d81d827f8bb31b4b242166fd4f2f5ef892fb185527bd45609b
7
- data.tar.gz: 6fa5bce4bdffda217798aa340b0c273e0abb9d6d628049e1351eac01d52cc0aa77243ec59febaf7e9618e82563d3c06872b9d3dd1392ce6b48d92b197183c855
6
+ metadata.gz: 7eabc205eb0b9f291ff8d06d717b38c06abbaa43030811e09bd080db2fe5adb7c2e76df76f33e36c5db30b4c6cbb63fdb8cd5b59c1c59c8bcbf159a6b8d3e338
7
+ data.tar.gz: 642c88a6ca834738c8f095e3c17ede54570d07785f79a2a5c5e6a295a205e1580174a23ba724f6ddd41c62c392e6cff99198fcfc7e8d73173e03558cc3e139cf
data/README.md CHANGED
@@ -1,190 +1,194 @@
1
- # NeatJSON
2
-
3
- [![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
4
- [![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
5
-
6
- Pretty-print your JSON in Ruby with more power than is provided by `JSON.pretty_generate`. In particular, like Ruby's `pp` (pretty print), NeatJSON will keep objects on one line if they fit, but break them over multiple lines if needed.
7
-
8
- Here's an excerpt (from a much larger JSON):
9
-
10
- ~~~ json
11
- {
12
- "navigation.createroute.poi":[
13
- {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
14
- {"text":"Take me to the airport","params":{"poi":"airport"}},
15
- {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
16
- {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
17
- {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
18
- {
19
- "text":"Go to the Hilton by the Airport",
20
- "params":{"poi":"Hilton","location":"Airport"}
21
- },
22
- {
23
- "text":"Take me to the Fry's in Fresno",
24
- "params":{"poi":"Fry's","location":"Fresno"}
25
- }
26
- ],
27
- "navigation.eta":[
28
- {"text":"When will we get there?"},
29
- {"text":"When will I arrive?"},
30
- {"text":"What time will I get to the destination?"},
31
- {"text":"What time will I reach the destination?"},
32
- {"text":"What time will it be when I arrive?"}
33
- ]
34
- }
35
- ~~~
36
-
37
- ## Installation
38
-
39
- `gem install neatjson`
40
-
41
- ## Examples
42
-
43
- ~~~ ruby
44
- require 'neatjson'
45
-
46
- o = { b:42.005, a:[42,17], longer:true, str:"yes
47
- please" }
48
-
49
- puts JSON.neat_generate(o)
50
- #=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
51
-
52
- puts JSON.neat_generate(o,sorted:true)
53
- #=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
54
-
55
- puts JSON.neat_generate(o,sorted:true,padding:1,after_comma:1)
56
- #=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
57
-
58
- puts JSON.neat_generate(o,sorted:true,wrap:40)
59
- #=> {
60
- #=> "a":[42,17],
61
- #=> "b":42.005,
62
- #=> "longer":true,
63
- #=> "str":"yes\nplease"
64
- #=> }
65
-
66
- puts JSON.neat_generate(o,sorted:true,wrap:40,decimals:2)
67
- #=> {
68
- #=> "a":[42,17],
69
- #=> "b":42.01,
70
- #=> "longer":true,
71
- #=> "str":"yes\nplease"
72
- #=> }
73
-
74
- puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true)
75
- #=> {
76
- #=> "a" :[42,17],
77
- #=> "b" :42.005,
78
- #=> "longer":true,
79
- #=> "str" :"yes\nplease"
80
- #=> }
81
-
82
- puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1)
83
- #=> {
84
- #=> "a" : [42,17],
85
- #=> "b" : 42.005,
86
- #=> "longer" : true,
87
- #=> "str" : "yes\nplease"
88
- #=> }
89
-
90
- puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1,short:true)
91
- #=> {"a" : [42,17],
92
- #=> "b" : 42.005,
93
- #=> "longer" : true,
94
- #=> "str" : "yes\nplease"}
95
-
96
- a = [1,2,[3,4,[5]]]
97
- puts JSON.neat_generate(a)
98
- #=> [1,2,[3,4,[5]]]
99
-
100
- puts JSON.pretty_generate(a) # oof!
101
- #=> [
102
- #=> 1,
103
- #=> 2,
104
- #=> [
105
- #=> 3,
106
- #=> 4,
107
- #=> [
108
- #=> 5
109
- #=> ]
110
- #=> ]
111
- #=> ]
112
-
113
- puts JSON.neat_generate( a, wrap:true, short:true )
114
- #=> [1,
115
- #=> 2,
116
- #=> [3,
117
- #=> 4,
118
- #=> [5]]]
119
- ~~~
120
-
121
- ## Options
122
- You may pass any of the following option symbols to `neat_generate`:
123
-
124
- * `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
125
- * `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
126
- * `:short` — Keep the output 'short' when wrapping? This puts opening brackets on the same line as the first value, and closing brackets on the same line as the last. Default: `false`
127
- * _This causes the `:indent` option to be ignored, instead basing indentation on array and object padding._
128
- * `:sorted` — Sort the keys for objects to be in alphabetical order? Default: `false`
129
- * `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
130
- * `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
131
- * `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
132
- * `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
133
- * `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
134
- * `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
135
- * `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
136
- * `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
137
- * `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
138
- * `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
139
- * `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
140
- * `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
141
- * `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
142
- * `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
143
- * `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
144
-
145
-
146
- ## License & Contact
147
-
148
- NeatJSON is copyright ©2015 by Gavin Kistner and is released under
149
- the [MIT License](http://www.opensource.org/licenses/mit-license.php).
150
- See the LICENSE.txt file for more details.
151
-
152
- For bugs or feature requests please open [issues on GitHub][1].
153
- For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
154
-
155
- ## TODO (aka Known Limitations)
156
-
157
- * Figure out the best way to play with custom objects that use `to_json` for their representation.
158
- * Detect circular references.
159
- * Possibly allow illegal JSON values like `NaN` or `Infinity`.
160
- * Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
161
-
162
- ## HISTORY
163
-
164
- * **v0.6** - April 26th, 2015
165
- * Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
166
-
167
- * **v0.5** - April 19th, 2015
168
- * Do not format integers (or floats that equal their integer) using `decimals` option.
169
- * Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
170
- * Add (Node-based) testing for the JavaScript version.
171
-
172
- * **v0.4** - April 18th, 2015
173
- * Add JavaScript version with online runner.
174
-
175
- * **v0.3.2** - April 16th, 2015
176
- * Force YARD to use Markdown for documentation.
177
-
178
- * **v0.3.1** - April 16th, 2015
179
- * Remove some debugging code accidentally left in.
180
-
181
- * **v0.3** - April 16th, 2015
182
- * Fix another bug with `short:true` and wrapping array values inside objects.
183
-
184
- * **v0.2** - April 16th, 2015
185
- * Fix bug with `short:true` and wrapping values inside objects.
186
-
187
- * **v0.1** - April 15th, 2015
188
- * Initial release.
189
-
190
- [1]: https://github.com/Phrogz/NeatJSON/issues
1
+ # NeatJSON
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
4
+ [![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
5
+
6
+ Pretty-print your JSON in Ruby with more power than is provided by `JSON.pretty_generate`. In particular, like Ruby's `pp` (pretty print), NeatJSON will keep objects on one line if they fit, but break them over multiple lines if needed.
7
+
8
+ Here's an excerpt (from a much larger JSON):
9
+
10
+ ~~~ json
11
+ {
12
+ "navigation.createroute.poi":[
13
+ {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
14
+ {"text":"Take me to the airport","params":{"poi":"airport"}},
15
+ {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
16
+ {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
17
+ {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
18
+ {
19
+ "text":"Go to the Hilton by the Airport",
20
+ "params":{"poi":"Hilton","location":"Airport"}
21
+ },
22
+ {
23
+ "text":"Take me to the Fry's in Fresno",
24
+ "params":{"poi":"Fry's","location":"Fresno"}
25
+ }
26
+ ],
27
+ "navigation.eta":[
28
+ {"text":"When will we get there?"},
29
+ {"text":"When will I arrive?"},
30
+ {"text":"What time will I get to the destination?"},
31
+ {"text":"What time will I reach the destination?"},
32
+ {"text":"What time will it be when I arrive?"}
33
+ ]
34
+ }
35
+ ~~~
36
+
37
+ ## Installation
38
+
39
+ `gem install neatjson`
40
+
41
+ ## Examples
42
+
43
+ ~~~ ruby
44
+ require 'neatjson'
45
+
46
+ o = { b:42.005, a:[42,17], longer:true, str:"yes
47
+ please" }
48
+
49
+ puts JSON.neat_generate(o)
50
+ #=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
51
+
52
+ puts JSON.neat_generate(o,sorted:true)
53
+ #=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
54
+
55
+ puts JSON.neat_generate(o,sorted:true,padding:1,after_comma:1)
56
+ #=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
57
+
58
+ puts JSON.neat_generate(o,sorted:true,wrap:40)
59
+ #=> {
60
+ #=> "a":[42,17],
61
+ #=> "b":42.005,
62
+ #=> "longer":true,
63
+ #=> "str":"yes\nplease"
64
+ #=> }
65
+
66
+ puts JSON.neat_generate(o,sorted:true,wrap:40,decimals:2)
67
+ #=> {
68
+ #=> "a":[42,17],
69
+ #=> "b":42.01,
70
+ #=> "longer":true,
71
+ #=> "str":"yes\nplease"
72
+ #=> }
73
+
74
+ puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true)
75
+ #=> {
76
+ #=> "a" :[42,17],
77
+ #=> "b" :42.005,
78
+ #=> "longer":true,
79
+ #=> "str" :"yes\nplease"
80
+ #=> }
81
+
82
+ puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1)
83
+ #=> {
84
+ #=> "a" : [42,17],
85
+ #=> "b" : 42.005,
86
+ #=> "longer" : true,
87
+ #=> "str" : "yes\nplease"
88
+ #=> }
89
+
90
+ puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1,short:true)
91
+ #=> {"a" : [42,17],
92
+ #=> "b" : 42.005,
93
+ #=> "longer" : true,
94
+ #=> "str" : "yes\nplease"}
95
+
96
+ a = [1,2,[3,4,[5]]]
97
+ puts JSON.neat_generate(a)
98
+ #=> [1,2,[3,4,[5]]]
99
+
100
+ puts JSON.pretty_generate(a) # oof!
101
+ #=> [
102
+ #=> 1,
103
+ #=> 2,
104
+ #=> [
105
+ #=> 3,
106
+ #=> 4,
107
+ #=> [
108
+ #=> 5
109
+ #=> ]
110
+ #=> ]
111
+ #=> ]
112
+
113
+ puts JSON.neat_generate( a, wrap:true, short:true )
114
+ #=> [1,
115
+ #=> 2,
116
+ #=> [3,
117
+ #=> 4,
118
+ #=> [5]]]
119
+ ~~~
120
+
121
+ ## Options
122
+ You may pass any of the following option symbols to `neat_generate`:
123
+
124
+ * `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
125
+ * `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
126
+ * `:short` — Keep the output 'short' when wrapping? This puts opening brackets on the same line as the first value, and closing brackets on the same line as the last. Default: `false`
127
+ * _This causes the `:indent` option to be ignored, instead basing indentation on array and object padding._
128
+ * `:sorted` — Sort the keys for objects to be in alphabetical order? Default: `false`
129
+ * `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
130
+ * `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
131
+ * `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
132
+ * `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
133
+ * `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
134
+ * `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
135
+ * `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
136
+ * `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
137
+ * `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
138
+ * `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
139
+ * `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
140
+ * `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
141
+ * `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
142
+ * `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
143
+ * `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
144
+
145
+
146
+ ## License & Contact
147
+
148
+ NeatJSON is copyright ©2015 by Gavin Kistner and is released under
149
+ the [MIT License](http://www.opensource.org/licenses/mit-license.php).
150
+ See the LICENSE.txt file for more details.
151
+
152
+ For bugs or feature requests please open [issues on GitHub][1].
153
+ For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
154
+
155
+ ## TODO (aka Known Limitations)
156
+
157
+ * Figure out the best way to play with custom objects that use `to_json` for their representation.
158
+ * Detect circular references.
159
+ * Possibly allow illegal JSON values like `NaN` or `Infinity`.
160
+ * Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
161
+
162
+ ## HISTORY
163
+
164
+ * **v0.6.1** - October 12th, 2015
165
+ * Fix handling of nested empty objects and arrays. (Would cause a runtime error in many cases.)
166
+ * _This change causes empty arrays in a tight wrapping scenario to appear on a single line where they would previously take up three lines._
167
+
168
+ * **v0.6** - April 26th, 2015
169
+ * Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
170
+
171
+ * **v0.5** - April 19th, 2015
172
+ * Do not format integers (or floats that equal their integer) using `decimals` option.
173
+ * Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
174
+ * Add (Node-based) testing for the JavaScript version.
175
+
176
+ * **v0.4** - April 18th, 2015
177
+ * Add JavaScript version with online runner.
178
+
179
+ * **v0.3.2** - April 16th, 2015
180
+ * Force YARD to use Markdown for documentation.
181
+
182
+ * **v0.3.1** - April 16th, 2015
183
+ * Remove some debugging code accidentally left in.
184
+
185
+ * **v0.3** - April 16th, 2015
186
+ * Fix another bug with `short:true` and wrapping array values inside objects.
187
+
188
+ * **v0.2** - April 16th, 2015
189
+ * Fix bug with `short:true` and wrapping values inside objects.
190
+
191
+ * **v0.1** - April 15th, 2015
192
+ * Initial release.
193
+
194
+ [1]: https://github.com/Phrogz/NeatJSON/issues
data/lib/neatjson.rb CHANGED
@@ -66,6 +66,7 @@ module JSON
66
66
  end
67
67
 
68
68
  when Array
69
+ return "#{indent}[]" if o.empty?
69
70
  pieces = o.map{ |v| build[v,''] }
70
71
  one_line = "#{indent}[#{apad}#{pieces.join comma}#{apad}]"
71
72
  if !opts[:wrap] || (one_line.length <= opts[:wrap])
@@ -82,6 +83,7 @@ module JSON
82
83
  end
83
84
 
84
85
  when Hash
86
+ return "#{indent}{}" if o.empty?
85
87
  keyvals = o.map{ |k,v| [ k.to_s.inspect, build[v,''] ] }
86
88
  keyvals = keyvals.sort_by(&:first) if opts[:sorted]
87
89
  keyvals = keyvals.map{ |kv| kv.join(colon1) }.join(comma)
data/neatjson.gemspec CHANGED
@@ -1,17 +1,17 @@
1
- # encoding: UTF-8
2
- require 'date'
3
- Gem::Specification.new do |s|
4
- s.name = "neatjson"
5
- s.version = "0.6"
6
- s.date = Date.today.iso8601
7
- s.authors = ["Gavin Kistner"]
8
- s.email = "gavin@phrogz.net"
9
- s.homepage = "http://github.com/Phrogz/NeatJSON"
10
- s.summary = "Pretty, powerful, flexible JSON generation."
11
- s.description = "Generate JSON strings from Ruby objects with flexible formatting options. Key features: keep arrays and objects on a single line when they fit; format floats to specific precision; sort and align object keys; adjust whitespace padding of arrays and objects, inside and around commas and colons. JavaScript version included."
12
- s.license = "MIT license (MIT)"
13
- s.platform = Gem::Platform::RUBY
14
- s.require_paths = ['lib']
15
- s.files = Dir.glob("{lib,test,html}/**/*") + ['LICENSE.txt', 'README.md', '.yardopts', __FILE__]
16
- s.has_rdoc = 'yard'
17
- end
1
+ # encoding: UTF-8
2
+ require 'date'
3
+ Gem::Specification.new do |s|
4
+ s.name = "neatjson"
5
+ s.version = "0.6.1"
6
+ s.date = Date.today.iso8601
7
+ s.authors = ["Gavin Kistner"]
8
+ s.email = "gavin@phrogz.net"
9
+ s.homepage = "http://github.com/Phrogz/NeatJSON"
10
+ s.summary = "Pretty, powerful, flexible JSON generation."
11
+ s.description = "Generate JSON strings from Ruby objects with flexible formatting options. Key features: keep arrays and objects on a single line when they fit; format floats to specific precision; sort and align object keys; adjust whitespace padding of arrays and objects, inside and around commas and colons. JavaScript version included."
12
+ s.license = "MIT license (MIT)"
13
+ s.platform = Gem::Platform::RUBY
14
+ s.require_paths = ['lib']
15
+ s.files = Dir.glob("{lib,test,html}/**/*") + ['LICENSE.txt', 'README.md', '.yardopts', __FILE__]
16
+ s.has_rdoc = 'yard'
17
+ end
@@ -4,23 +4,28 @@ var count=0, pass=0;
4
4
  require('./tests.js').tests.forEach(function(valTest){
5
5
  var value = valTest.value;
6
6
  valTest.tests.forEach(function(test){
7
- var mesg = "neatJSON("+JSON.stringify(value);
8
- if (test.opts){
9
- var opts = {};
10
- for (var k in test.opts) opts[k] = test.opts[k];
11
- var json = neatJSON(value,opts);
12
- mesg += ","+JSON.stringify(test.opts);
13
- }else{
14
- var json = neatJSON(value);
15
- }
16
- var success = (test.json.constructor==RegExp) ? test.json.test(json) : json==test.json;
17
- if (success) pass+=1;
18
- else{
19
- console.log(mesg+")");
20
- console.log('EXPECTED');
21
- console.log(test.json);
22
- console.log('ACTUAL');
23
- console.log(json,"\n");
7
+ var cmd = "neatJSON("+JSON.stringify(value)+(test.opts?","+JSON.stringify(test.opts):'')+")";
8
+ try{
9
+ if (test.opts){
10
+ var opts = {};
11
+ for (var k in test.opts) opts[k] = test.opts[k];
12
+ var json = neatJSON(value,opts);
13
+ }else{
14
+ var json = neatJSON(value);
15
+ }
16
+ var success = (test.json.constructor==RegExp) ? test.json.test(json) : json==test.json;
17
+ if (success) pass+=1;
18
+ else{
19
+ console.log(cmd);
20
+ console.log('EXPECTED');
21
+ console.log(test.json);
22
+ console.log('ACTUAL');
23
+ console.log(json,"\n");
24
+ }
25
+ } catch (e){
26
+ console.log("Error running "+cmd);
27
+ console.log(e.stack);
28
+ console.log("")
24
29
  }
25
30
  count+=1;
26
31
  });
@@ -7,16 +7,17 @@ count = 0
7
7
  TESTS.each do |value_tests|
8
8
  val, tests = value_tests[:value], value_tests[:tests]
9
9
  tests.each do |test|
10
+ cmd = test[:opts] ? "JSON.neat_generate(#{val.inspect},#{test[:opts].inspect})" : "JSON.neat_generate(#{val.inspect})"
10
11
  begin
11
- count += 1
12
12
  json = test[:opts] ? JSON.neat_generate(val,test[:opts].dup) : JSON.neat_generate(val)
13
- mesg = test[:opts] ? "JSON.neat_generate(#{val.inspect},#{test[:opts].inspect})" : "JSON.neat_generate(#{val.inspect})"
14
- raise "#{mesg}\nEXPECTED:\n#{test[:json]}\nACTUAL:\n#{json}\n\n" unless test[:json]===json
13
+ raise "EXPECTED:\n#{test[:json]}\nACTUAL:\n#{json}" unless test[:json]===json
15
14
  pass += 1
16
15
  rescue StandardError => e
17
- puts e
16
+ puts "Error running #{cmd}", e, ""
18
17
  end
18
+ count += 1
19
19
  end
20
20
  end
21
21
  elapsed = Time.now-start
22
+ elapsed = 0.0001 if elapsed==0
22
23
  puts "%d/%d test#{:s if count!=1} passed in %.2fms (%d tests per second)" % [pass, count, elapsed*1000, count/elapsed]
data/test/tests.js CHANGED
@@ -1,135 +1,161 @@
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
- ]},
57
-
58
- {value:{b:1,a:2}, tests:[
59
- { json:'{"b":1,"a":2}' },
60
- { json:'{"a":2,"b":1}', opts:{sorted:true} },
61
- { json:'{"a":2, "b":1}', opts:{sorted:true,afterComma:1} },
62
- { json:'{"a" :2,"b" :1}', opts:{sorted:true,beforeColon:1} },
63
- { json:'{"a": 2,"b": 1}', opts:{sorted:true,afterColon:1} },
64
- { json:'{"a" : 2,"b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1} },
65
- { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1} },
66
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,padding:1} },
67
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:1,afterComma:1,objectPadding:1} },
68
- { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,arrayPadding:1} },
69
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:2,afterComma:1,padding:2} },
70
- { json:'{ "a":2, "b":1 }', opts:{sorted:true,afterComma:1,padding:2} },
71
- { json:'{"b": 1,"a": 2}', opts:{afterColon1:2} },
72
- { json:'{"b" : 1,"a" : 2}', opts:{aroundColon1:2} },
73
- { json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,aroundColon1:2} },
74
- { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColon:1} },
75
- { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColonN:1} },
76
- { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
77
- { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColon:1} },
78
- { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColonN:1} },
79
- { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,afterColon1:1} }
80
- ]},
81
-
82
- {value:{b:1,aaa:2,cc:3}, tests:[
83
- { json:"{\n \"b\":1,\n \"aaa\":2,\n \"cc\":3\n}", opts:{wrap:true} },
84
- { json:"{\n \"b\" :1,\n \"aaa\":2,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true} },
85
- { json:"{\"b\":1,\"aaa\":2,\"cc\":3}", opts:{aligned:true} },
86
- { json:"{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true,sorted:true} }
87
- ]},
88
-
89
- {value:{a:1}, tests:[
90
- { json:'{"a":1}' },
91
- { json:"{\n \"a\":1\n}", opts:{wrap:true} }
92
- ]},
93
-
94
- {value:{ b:17, a:42 }, tests:[
95
- { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} }
96
- ]},
97
-
98
- {value:[1,{a:2},3], tests:[
99
- { json:'[1,{"a":2},3]' },
100
- { json:'[ 1,{ "a":2 },3 ]', opts:{padding:1} },
101
- { json:'[ 1, { "a":2 }, 3 ]', opts:{padding:1,afterComma:1} },
102
- { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts:{wrap:true} },
103
- { json:"[\n 1,\n {\"a\":2},\n 3\n]", opts:{wrap:10} }
104
- ]},
105
-
106
- {value:[1,{a:2,b:3},4], tests:[
107
- { json:"[1,\n {\"a\":2,\n \"b\":3},\n 4]", opts:{wrap:0,short:true} },
108
- ]},
109
-
110
- {value:{a:1,b:[2,3,4],c:3}, tests:[
111
- { json:'{"a":1,"b":[2,3,4],"c":3}' },
112
- { json:"{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts:{wrap:10} },
113
- { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts:{wrap:true} }
114
- ]},
115
-
116
- {value:{hooo:42,whee:['yaaa','oooo','booy'],zoop:"whoop"}, tests:[
117
- { json:"{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts:{wrap:20,short:true} }
118
- ]},
119
-
120
- {value:{ a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }, tests:[
121
- { json:"{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts:{wrap:true,short:true} }
122
- ]},
123
-
124
- // {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
125
- // { json:'{ "a":1}' },
126
- // { json:'{ "a":1}', opts:{wrap:true} },
127
- // { json:'{"a":1}', opts:{indent:''} },
128
- // ]},
129
-
130
- // {value:Class.new{ def to_json(*a); JSON.neat_generate({a:1},*a); end }.new, tests:[
131
- // { json:'{"a":1}' },
132
- // { json:"{\n \"a\":1\n}", opts:{wrap:true} }
133
- // ]}
134
- ]
135
-
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
+ ]},
57
+
58
+ {value:{b:1,a:2}, tests:[
59
+ { json:'{"b":1,"a":2}' },
60
+ { json:'{"a":2,"b":1}', opts:{sorted:true} },
61
+ { json:'{"a":2, "b":1}', opts:{sorted:true,afterComma:1} },
62
+ { json:'{"a" :2,"b" :1}', opts:{sorted:true,beforeColon:1} },
63
+ { json:'{"a": 2,"b": 1}', opts:{sorted:true,afterColon:1} },
64
+ { json:'{"a" : 2,"b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1} },
65
+ { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1} },
66
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,padding:1} },
67
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:1,afterComma:1,objectPadding:1} },
68
+ { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,arrayPadding:1} },
69
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:2,afterComma:1,padding:2} },
70
+ { json:'{ "a":2, "b":1 }', opts:{sorted:true,afterComma:1,padding:2} },
71
+ { json:'{"b": 1,"a": 2}', opts:{afterColon1:2} },
72
+ { json:'{"b" : 1,"a" : 2}', opts:{aroundColon1:2} },
73
+ { json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,aroundColon1:2} },
74
+ { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColon:1} },
75
+ { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColonN:1} },
76
+ { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
77
+ { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColon:1} },
78
+ { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColonN:1} },
79
+ { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,afterColon1:1} }
80
+ ]},
81
+
82
+ {value:{b:1,aaa:2,cc:3}, tests:[
83
+ { json:"{\n \"b\":1,\n \"aaa\":2,\n \"cc\":3\n}", opts:{wrap:true} },
84
+ { json:"{\n \"b\" :1,\n \"aaa\":2,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true} },
85
+ { json:"{\"b\":1,\"aaa\":2,\"cc\":3}", opts:{aligned:true} },
86
+ { json:"{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true,sorted:true} }
87
+ ]},
88
+
89
+ {value:{a:1}, tests:[
90
+ { json:'{"a":1}' },
91
+ { json:"{\n \"a\":1\n}", opts:{wrap:true} }
92
+ ]},
93
+
94
+ {value:{ b:17, a:42 }, tests:[
95
+ { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} }
96
+ ]},
97
+
98
+ {value:[1,{a:2},3], tests:[
99
+ { json:'[1,{"a":2},3]' },
100
+ { json:'[ 1,{ "a":2 },3 ]', opts:{padding:1} },
101
+ { json:'[ 1, { "a":2 }, 3 ]', opts:{padding:1,afterComma:1} },
102
+ { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts:{wrap:true} },
103
+ { json:"[\n 1,\n {\"a\":2},\n 3\n]", opts:{wrap:10} }
104
+ ]},
105
+
106
+ {value:[1,{a:2,b:3},4], tests:[
107
+ { json:"[1,\n {\"a\":2,\n \"b\":3},\n 4]", opts:{wrap:0,short:true} },
108
+ ]},
109
+
110
+ {value:{a:1,b:[2,3,4],c:3}, tests:[
111
+ { json:'{"a":1,"b":[2,3,4],"c":3}' },
112
+ { json:"{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts:{wrap:10} },
113
+ { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts:{wrap:true} }
114
+ ]},
115
+
116
+ {value:{hooo:42,whee:['yaaa','oooo','booy'],zoop:"whoop"}, tests:[
117
+ { json:"{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts:{wrap:20,short:true} }
118
+ ]},
119
+
120
+ {value:{ a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }, tests:[
121
+ { json:"{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts:{wrap:true,short:true} }
122
+ ]},
123
+
124
+ {value:{"abcdefghij":[{"abcdefghijklmnop":{}}]}, tests:[
125
+ { json:'{"abcdefghij":[{"abcdefghijklmnop":{}}]}' },
126
+ { json:'{"abcdefghij" : [{"abcdefghijklmnop" : {}}]}', opts:{wrap:1, short:true, aroundColonN:1} },
127
+ ]},
128
+
129
+ {value:{"foo":{}}, tests:[
130
+ { json:'{"foo":{}}' },
131
+ { json:'{"foo":{}}', opts:{wrap:false} },
132
+ { json:'{\n "foo":{}\n}', opts:{wrap:5} },
133
+ { json:'{"foo":{}}', opts:{wrap:1, short:true} }
134
+ ]},
135
+
136
+ {value:["foo",{},"bar"], tests:[
137
+ { json:'[\n "foo",\n {},\n "bar"\n]', opts:{wrap:1} },
138
+ { json:'["foo",\n {},\n "bar"]', opts:{wrap:1, short:true} }
139
+ ]},
140
+
141
+ {value:["foo",[],"bar"], tests:[
142
+ { json:'[\n "foo",\n [],\n "bar"\n]', opts:{wrap:1} },
143
+ { json:'["foo",\n [],\n "bar"]', opts:{wrap:1, short:true} }
144
+ ]},
145
+
146
+ {value:["foo",[{},[{"foo":[]},42]],"bar"], tests:[
147
+ { json:'["foo",\n [{},\n [{"foo":[]},\n 42]],\n "bar"]', opts:{wrap:1, short:true} },
148
+ ]}
149
+
150
+ // {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
151
+ // { json:'{ "a":1}' },
152
+ // { json:'{ "a":1}', opts:{wrap:true} },
153
+ // { json:'{"a":1}', opts:{indent:''} },
154
+ // ]},
155
+
156
+ // {value:Class.new{ def to_json(*a); JSON.neat_generate({a:1},*a); end }.new, tests:[
157
+ // { json:'{"a":1}' },
158
+ // { json:"{\n \"a\":1\n}", opts:{wrap:true} }
159
+ // ]}
160
+ ]
161
+
data/test/tests.rb CHANGED
@@ -1,133 +1,160 @@
1
- TESTS = [
2
- {value:true, tests:[{json:"true" }]},
3
- {value:false, tests:[{json:"false" }]},
4
- {value:nil, tests:[{json:"null" }]},
5
- {value:5, tests:[
6
- {json:"5"},
7
- {json:"5", opts:{decimals:3}}
8
- ]},
9
- {value:5.0, tests:[
10
- {json:"5"},
11
- {json:"5", opts:{decimals:3}}
12
- ]},
13
- {value:5.0001, tests:[
14
- {json:"5.0001"},
15
- {json:"5.000", opts:{decimals:3}}
16
- ]},
17
- {value:4.2, tests:[
18
- {json:"4.2"},
19
- {json:"4", opts:{decimals:0}},
20
- {json:"4.20",opts:{decimals:2}},
21
- ]},
22
- {value:4.199, tests:[{json:"4.20", opts:{decimals:2}}]},
23
- {value:4.204, tests:[{json:"4.20", opts:{decimals:2}}]},
24
- {value:-1.9, tests:[{json:"-2", opts:{decimals:0}}]},
25
- {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
26
- {value:1e23, tests:[{json:/1(?:\.0+)?e\+23/i}]},
27
- {value:1e-9, tests:[{json:/1(?:\.0+)?e-0*9/i}]},
28
- {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
29
-
30
- {value:"foo", tests:[{json:"\"foo\""}]},
31
- {value: :foo, tests:[{json:"\"foo\""}]},
32
- {value:"foo\nbar", tests:[{json:"\"foo\\nbar\""}]},
33
-
34
- {value:[1,2,3,4,[5,6,7,[8,9,10],11,12]], tests:[
35
- { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]" },
36
- { json:"[\n 1,\n 2,\n 3,\n 4,\n [5,6,7,[8,9,10],11,12]\n]", opts:{wrap:30} },
37
- { 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} },
38
- { 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} },
39
- { 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"} },
40
- { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]", opts:{array_padding:0} },
41
- { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{array_padding:1} },
42
- { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{array_padding:2} },
43
- { json:"[1, 2, 3, 4, [5, 6, 7, [8, 9, 10], 11, 12]]", opts:{after_comma:1} },
44
- { json:"[ 1, 2, 3, 4, [ 5, 6, 7, [ 8, 9, 10 ], 11, 12 ] ]", opts:{after_comma:1,array_padding:1} },
45
- { 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} },
46
- { 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,after_comma: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,array_padding:1} },
48
- ]},
49
-
50
- {value:[1,2,3], tests:[
51
- { json:"[1,2,3]" },
52
- { json:"[1 ,2 ,3]", opts:{before_comma:1} },
53
- { json:"[1 , 2 , 3]", opts:{around_comma:1} }
54
- ]},
55
-
56
- {value:{b:1,a:2}, tests:[
57
- { json:'{"b":1,"a":2}' },
58
- { json:'{"a":2,"b":1}', opts:{sorted:true} },
59
- { json:'{"a":2, "b":1}', opts:{sorted:true,after_comma:1} },
60
- { json:'{"a" :2,"b" :1}', opts:{sorted:true,before_colon:1} },
61
- { json:'{"a": 2,"b": 1}', opts:{sorted:true,after_colon:1} },
62
- { json:'{"a" : 2,"b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1} },
63
- { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1} },
64
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1,padding:1} },
65
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,around_colon:1,after_comma:1,object_padding:1} },
66
- { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1,array_padding:1} },
67
- { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,around_colon:2,after_comma:1,padding:2} },
68
- { json:'{ "a":2, "b":1 }', opts:{sorted:true,after_comma:1,padding:2} },
69
- { json:'{"b": 1,"a": 2}', opts:{after_colon_1:2} },
70
- { json:'{"b" : 1,"a" : 2}', opts:{around_colon_1:2} },
71
- { json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,around_colon_1:2} },
72
- { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,after_colon:1} },
73
- { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,after_colon_n:1} },
74
- { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
75
- { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,after_colon:1} },
76
- { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,after_colon_n:1} },
77
- { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,after_colon_1:1} },
78
- ]},
79
-
80
- {value:{b:1,aaa:2,cc:3}, tests:[
81
- { json:"{\n \"b\":1,\n \"aaa\":2,\n \"cc\":3\n}", opts:{wrap:true} },
82
- { json:"{\n \"b\" :1,\n \"aaa\":2,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true} },
83
- { json:"{\"b\":1,\"aaa\":2,\"cc\":3}", opts:{aligned:true} },
84
- { json:"{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true,sorted:true} }
85
- ]},
86
-
87
- {value:{a:1}, tests:[
88
- { json:'{"a":1}' },
89
- { json:"{\n \"a\":1\n}", opts:{wrap:true} }
90
- ]},
91
-
92
- {value:{ b:17, a:42 }, tests:[
93
- { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} }
94
- ]},
95
-
96
- {value:[1,{a:2},3], tests:[
97
- { json:'[1,{"a":2},3]' },
98
- { json:'[ 1,{ "a":2 },3 ]', opts:{padding:1} },
99
- { json:'[ 1, { "a":2 }, 3 ]', opts:{padding:1,after_comma:1} },
100
- { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts:{wrap:true} },
101
- { json:"[\n 1,\n {\"a\":2},\n 3\n]", opts:{wrap:10} }
102
- ]},
103
-
104
- {value:[1,{a:2,b:3},4], tests:[
105
- { json:"[1,\n {\"a\":2,\n \"b\":3},\n 4]", opts:{wrap:0,short:true} },
106
- ]},
107
-
108
- {value:{a:1,b:[2,3,4],c:3}, tests:[
109
- { json:'{"a":1,"b":[2,3,4],"c":3}' },
110
- { json:"{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts:{wrap:10} },
111
- { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts:{wrap:true} }
112
- ]},
113
-
114
- {value:{hooo:42,whee:['yaaa','oooo','booy'],zoop:"whoop"}, tests:[
115
- { json:"{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts:{wrap:20,short:true} }
116
- ]},
117
-
118
- {value:{ a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }, tests:[
119
- { json:"{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts:{wrap:true,short:true} }
120
- ]},
121
-
122
- {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
123
- { json:'{ "a":1}' },
124
- { json:'{ "a":1}', opts:{wrap:true} },
125
- { json:'{"a":1}', opts:{indent:''} },
126
- ]},
127
-
128
- {value:Class.new{ def to_json(*a); JSON.neat_generate({a:1},*a); end }.new, tests:[
129
- { json:'{"a":1}' },
130
- { json:"{\n \"a\":1\n}", opts:{wrap:true} }
131
- ]}
132
- ]
133
-
1
+ TESTS = [
2
+ {value:true, tests:[{json:"true" }]},
3
+ {value:false, tests:[{json:"false" }]},
4
+ {value:nil, tests:[{json:"null" }]},
5
+ {value:5, tests:[
6
+ {json:"5"},
7
+ {json:"5", opts:{decimals:3}}
8
+ ]},
9
+ {value:5.0, tests:[
10
+ {json:"5"},
11
+ {json:"5", opts:{decimals:3}}
12
+ ]},
13
+ {value:5.0001, tests:[
14
+ {json:"5.0001"},
15
+ {json:"5.000", opts:{decimals:3}}
16
+ ]},
17
+ {value:4.2, tests:[
18
+ {json:"4.2"},
19
+ {json:"4", opts:{decimals:0}},
20
+ {json:"4.20",opts:{decimals:2}},
21
+ ]},
22
+ {value:4.199, tests:[{json:"4.20", opts:{decimals:2}}]},
23
+ {value:4.204, tests:[{json:"4.20", opts:{decimals:2}}]},
24
+ {value:-1.9, tests:[{json:"-2", opts:{decimals:0}}]},
25
+ {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
26
+ {value:1e23, tests:[{json:/1(?:\.0+)?e\+23/i}]},
27
+ {value:1e-9, tests:[{json:/1(?:\.0+)?e-0*9/i}]},
28
+ {value:-2.4, tests:[{json:"-2", opts:{decimals:0}}]},
29
+
30
+ {value:"foo", tests:[{json:"\"foo\""}]},
31
+ {value: :foo, tests:[{json:"\"foo\""}]},
32
+ {value:"foo\nbar", tests:[{json:"\"foo\\nbar\""}]},
33
+
34
+ {value:[1,2,3,4,[5,6,7,[8,9,10],11,12]], tests:[
35
+ { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]" },
36
+ { json:"[\n 1,\n 2,\n 3,\n 4,\n [5,6,7,[8,9,10],11,12]\n]", opts:{wrap:30} },
37
+ { 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} },
38
+ { 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} },
39
+ { 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"} },
40
+ { json:"[1,2,3,4,[5,6,7,[8,9,10],11,12]]", opts:{array_padding:0} },
41
+ { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{array_padding:1} },
42
+ { json:"[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts:{array_padding:2} },
43
+ { json:"[1, 2, 3, 4, [5, 6, 7, [8, 9, 10], 11, 12]]", opts:{after_comma:1} },
44
+ { json:"[ 1, 2, 3, 4, [ 5, 6, 7, [ 8, 9, 10 ], 11, 12 ] ]", opts:{after_comma:1,array_padding:1} },
45
+ { 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} },
46
+ { 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,after_comma: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,array_padding:1} },
48
+ ]},
49
+
50
+ {value:[1,2,3], tests:[
51
+ { json:"[1,2,3]" },
52
+ { json:"[1 ,2 ,3]", opts:{before_comma:1} },
53
+ { json:"[1 , 2 , 3]", opts:{around_comma:1} }
54
+ ]},
55
+
56
+ {value:{b:1,a:2}, tests:[
57
+ { json:'{"b":1,"a":2}' },
58
+ { json:'{"a":2,"b":1}', opts:{sorted:true} },
59
+ { json:'{"a":2, "b":1}', opts:{sorted:true,after_comma:1} },
60
+ { json:'{"a" :2,"b" :1}', opts:{sorted:true,before_colon:1} },
61
+ { json:'{"a": 2,"b": 1}', opts:{sorted:true,after_colon:1} },
62
+ { json:'{"a" : 2,"b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1} },
63
+ { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1} },
64
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1,padding:1} },
65
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,around_colon:1,after_comma:1,object_padding:1} },
66
+ { json:'{"a" : 2, "b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1,array_padding:1} },
67
+ { json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,around_colon:2,after_comma:1,padding:2} },
68
+ { json:'{ "a":2, "b":1 }', opts:{sorted:true,after_comma:1,padding:2} },
69
+ { json:'{"b": 1,"a": 2}', opts:{after_colon_1:2} },
70
+ { json:'{"b" : 1,"a" : 2}', opts:{around_colon_1:2} },
71
+ { json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,around_colon_1:2} },
72
+ { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,after_colon:1} },
73
+ { json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,after_colon_n:1} },
74
+ { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
75
+ { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,after_colon:1} },
76
+ { json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,after_colon_n:1} },
77
+ { json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,after_colon_1:1} },
78
+ ]},
79
+
80
+ {value:{b:1,aaa:2,cc:3}, tests:[
81
+ { json:"{\n \"b\":1,\n \"aaa\":2,\n \"cc\":3\n}", opts:{wrap:true} },
82
+ { json:"{\n \"b\" :1,\n \"aaa\":2,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true} },
83
+ { json:"{\"b\":1,\"aaa\":2,\"cc\":3}", opts:{aligned:true} },
84
+ { json:"{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts:{wrap:true,aligned:true,sorted:true} }
85
+ ]},
86
+
87
+ {value:{a:1}, tests:[
88
+ { json:'{"a":1}' },
89
+ { json:"{\n \"a\":1\n}", opts:{wrap:true} }
90
+ ]},
91
+
92
+ {value:{ b:17, a:42 }, tests:[
93
+ { json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} }
94
+ ]},
95
+
96
+ {value:[1,{a:2},3], tests:[
97
+ { json:'[1,{"a":2},3]' },
98
+ { json:'[ 1,{ "a":2 },3 ]', opts:{padding:1} },
99
+ { json:'[ 1, { "a":2 }, 3 ]', opts:{padding:1,after_comma:1} },
100
+ { json:"[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts:{wrap:true} },
101
+ { json:"[\n 1,\n {\"a\":2},\n 3\n]", opts:{wrap:10} }
102
+ ]},
103
+
104
+ {value:[1,{a:2,b:3},4], tests:[
105
+ { json:"[1,\n {\"a\":2,\n \"b\":3},\n 4]", opts:{wrap:0,short:true} },
106
+ ]},
107
+
108
+ {value:{a:1,b:[2,3,4],c:3}, tests:[
109
+ { json:'{"a":1,"b":[2,3,4],"c":3}' },
110
+ { json:"{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts:{wrap:10} },
111
+ { json:"{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts:{wrap:true} }
112
+ ]},
113
+
114
+ {value:{hooo:42,whee:['yaaa','oooo','booy'],zoop:"whoop"}, tests:[
115
+ { json:"{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts:{wrap:20,short:true} }
116
+ ]},
117
+
118
+ {value:{ a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }, tests:[
119
+ { json:"{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts:{wrap:true,short:true} }
120
+ ]},
121
+
122
+ {value:{"abcdefghij"=>[{"abcdefghijklmnop"=>{}}]}, tests:[
123
+ { json:"{\"abcdefghij\":[{\"abcdefghijklmnop\":{}}]}" },
124
+ { json:"{\"abcdefghij\" : [{\"abcdefghijklmnop\" : {}}]}", opts:{wrap:1, short:true, around_colon_n:1} },
125
+ ]},
126
+
127
+ {value:{foo:{}}, tests:[
128
+ { json:"{\"foo\":{}}" },
129
+ { json:"{\"foo\":{}}", opts:{wrap:false} },
130
+ { json:"{\n \"foo\":{}\n}", opts:{wrap:5} },
131
+ { json:"{\"foo\":{}}", opts:{wrap:1, short:true} }
132
+ ]},
133
+
134
+ {value:["foo",{},"bar"], tests:[
135
+ { json:"[\n \"foo\",\n {},\n \"bar\"\n]", opts:{wrap:1} },
136
+ { json:"[\"foo\",\n {},\n \"bar\"]", opts:{wrap:1, short:true} }
137
+ ]},
138
+
139
+ {value:["foo",[],"bar"], tests:[
140
+ { json:"[\n \"foo\",\n [],\n \"bar\"\n]", opts:{wrap:1} },
141
+ { json:"[\"foo\",\n [],\n \"bar\"]", opts:{wrap:1, short:true} }
142
+ ]},
143
+
144
+ {value:["foo",[{},[{foo:[]},42]],"bar"], tests:[
145
+ { json:"[\"foo\",\n [{},\n [{\"foo\":[]},\n 42]],\n \"bar\"]", opts:{wrap:1, short:true} },
146
+ ]},
147
+
148
+ {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
149
+ { json:'{ "a":1}' },
150
+ { json:'{ "a":1}', opts:{wrap:true} },
151
+ { json:'{"a":1}', opts:{indent:''} },
152
+ ]},
153
+
154
+ {value:Class.new{ def to_json(*a); JSON.neat_generate({a:1},*a); end }.new, tests:[
155
+ { json:'{"a":1}' },
156
+ { json:"{\n \"a\":1\n}", opts:{wrap:true} }
157
+ ]}
158
+
159
+ ]
160
+
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.6'
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Kistner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-26 00:00:00.000000000 Z
11
+ date: 2015-10-12 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
@@ -48,8 +48,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  requirements: []
50
50
  rubyforge_project:
51
- rubygems_version: 2.4.6
51
+ rubygems_version: 2.2.3
52
52
  signing_key:
53
53
  specification_version: 4
54
54
  summary: Pretty, powerful, flexible JSON generation.
55
55
  test_files: []
56
+ has_rdoc: yard