Skip to content

Commit 7f5094c

Browse files
committed
use Array#permutation if it exists
otherwise create a Array#permutation method with the permutation gem on the fly
1 parent f599e66 commit 7f5094c

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

tests/test_json.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
end
1010
require 'stringio'
1111

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+
1226
class TC_JSON < Test::Unit::TestCase
1327
include JSON
1428

@@ -94,30 +108,24 @@ def test_parse_simple_objects
94108
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
95109
end
96110

97-
begin
98-
require 'permutation'
111+
if Array.method_defined?(:permutation)
99112
def test_parse_more_complex_arrays
100113
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)
106117
end
107118
end
108119

109120
def test_parse_complex_objects
110121
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|
113123
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 }
115125
json = pretty_generate(orig_obj)
116126
assert_equal orig_obj, parse(json)
117127
end
118128
end
119-
rescue LoadError
120-
warn "Skipping permutation tests."
121129
end
122130

123131
def test_parse_arrays

0 commit comments

Comments
 (0)