Skip to content

Commit

Permalink
switch tests for rails vs json
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Aug 17, 2016
1 parent d0f5c2c commit 725dad0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
37 changes: 30 additions & 7 deletions test/isolated/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,48 @@ def test_exception
# dump
def test_dump_string
json = JSON.dump([1, true, nil, @time])
assert_equal(%{[1,true,null,#{@expected_time_string}]}, json)
#assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, json)
if $rails_monkey
assert_equal(%{[1,true,null,#{@expected_time_string}]}, json)
else
assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, json)
end
end

def test_dump_with_options
Oj.default_options= {:indent => 2} # JSON this will not change anything
json = JSON.dump([1, true, nil, @time])
assert_equal(%{[
if $rails_monkey
assert_equal(%{[
1,
true,
null,
#{@expected_time_string}
]
}, json)
else
assert_equal(%{[
1,
true,
null,
{
"json_class":"Time",
"s":1400000000,
"n\":0
}
]
}, json)
end
end

def test_dump_io
s = StringIO.new()
json = JSON.dump([1, true, nil, @time], s)
assert_equal(s, json)
assert_equal(%{[1,true,null,#{@expected_time_string}]}, s.string)
if $rails_monkey
assert_equal(%{[1,true,null,#{@expected_time_string}]}, s.string)
else
assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, s.string)
end
end
# TBD options

Expand All @@ -91,10 +112,12 @@ def test_dump_struct
end
o = s.new(1, 'two', [true, false])
json = JSON.dump(o)
# Rails add the as_json method and changes the behavior.
if o.respond_to?(:as_json)
assert_equal(%|{"a":1,"b":"two","c":[true,false]}|, json)
#assert_equal(%|{"json_class":"Struct::Abc","v":[1,"two",[true,false]]}|, json)
if $rails_monkey
assert_equal(%|{"a":1,"b":"two","c":[true,false]}|, json)
else
assert_equal(%|{"json_class":"Struct::Abc","v":[1,"two",[true,false]]}|, json)
end
else
j = '"' + o.to_s.gsub('"', '\\"') + '"'
assert_equal(j, json)
Expand Down
2 changes: 2 additions & 0 deletions test/isolated/test_mimic_rails_after.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@

require 'isolated/shared'

$rails_monkey = true

class MimicRailsAfter < SharedMimicRailsTest
end # MimicRailsAfter

0 comments on commit 725dad0

Please sign in to comment.