Skip to content

Commit

Permalink
use Array#permutation if it exists
Browse files Browse the repository at this point in the history
otherwise create a Array#permutation method with the permutation gem on
the fly
  • Loading branch information
flori committed Oct 26, 2009
1 parent f599e66 commit 7f5094c
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions tests/test_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
end
require 'stringio'

unless Array.method_defined?(:permutation)
begin
require 'enumerator'
require 'permutation'
class Array
def permutation
Permutation.for(self).to_enum.map { |x| x.project }
end
end
rescue LoadError
warn "Skipping permutation tests."
end
end

class TC_JSON < Test::Unit::TestCase
include JSON

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

begin
require 'permutation'
if Array.method_defined?(:permutation)
def test_parse_more_complex_arrays
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
perms = Permutation.for a
perms.each do |perm|
orig_ary = perm.project
json = pretty_generate(orig_ary)
assert_equal orig_ary, parse(json)
a.permutation.each do |perm|
json = pretty_generate(perm)
assert_equal perm, parse(json)
end
end

def test_parse_complex_objects
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
perms = Permutation.for a
perms.each do |perm|
a.permutation.each do |perm|
s = "a"
orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
json = pretty_generate(orig_obj)
assert_equal orig_obj, parse(json)
end
end
rescue LoadError
warn "Skipping permutation tests."
end

def test_parse_arrays
Expand Down

0 comments on commit 7f5094c

Please sign in to comment.