From 7f5094cb6f283c3960634e94cfe58f3d6af0af37 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Mon, 26 Oct 2009 20:39:30 +0100 Subject: use Array#permutation if it exists otherwise create a Array#permutation method with the permutation gem on the fly --- tests/test_json.rb | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/tests/test_json.rb b/tests/test_json.rb index 5d71d61..736c57b 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -9,6 +9,20 @@ else require 'json' 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 @@ -94,30 +108,24 @@ class TC_JSON < Test::Unit::TestCase 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 -- cgit v1.2.1