|
9 | 9 | end |
10 | 10 | require 'stringio' |
11 | 11 |
|
| 12 | +unless Array.method_defined?(:permutation) |
| 13 | + begin |
| 14 | + require 'enumerator' |
| 15 | + require 'permutation' |
| 16 | + class Array |
| 17 | + def permutation |
| 18 | + Permutation.for(self).to_enum.map { |x| x.project } |
| 19 | + end |
| 20 | + end |
| 21 | + rescue LoadError |
| 22 | + warn "Skipping permutation tests." |
| 23 | + end |
| 24 | +end |
| 25 | + |
12 | 26 | class TC_JSON < Test::Unit::TestCase |
13 | 27 | include JSON |
14 | 28 |
|
@@ -94,30 +108,24 @@ def test_parse_simple_objects |
94 | 108 | assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) |
95 | 109 | end |
96 | 110 |
|
97 | | - begin |
98 | | - require 'permutation' |
| 111 | + if Array.method_defined?(:permutation) |
99 | 112 | def test_parse_more_complex_arrays |
100 | 113 | a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] |
101 | | - perms = Permutation.for a |
102 | | - perms.each do |perm| |
103 | | - orig_ary = perm.project |
104 | | - json = pretty_generate(orig_ary) |
105 | | - assert_equal orig_ary, parse(json) |
| 114 | + a.permutation.each do |perm| |
| 115 | + json = pretty_generate(perm) |
| 116 | + assert_equal perm, parse(json) |
106 | 117 | end |
107 | 118 | end |
108 | 119 |
|
109 | 120 | def test_parse_complex_objects |
110 | 121 | a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] |
111 | | - perms = Permutation.for a |
112 | | - perms.each do |perm| |
| 122 | + a.permutation.each do |perm| |
113 | 123 | s = "a" |
114 | | - orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } |
| 124 | + orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } |
115 | 125 | json = pretty_generate(orig_obj) |
116 | 126 | assert_equal orig_obj, parse(json) |
117 | 127 | end |
118 | 128 | end |
119 | | - rescue LoadError |
120 | | - warn "Skipping permutation tests." |
121 | 129 | end |
122 | 130 |
|
123 | 131 | def test_parse_arrays |
|
0 commit comments