test/tests.rb in oj-0.8.0 vs test/tests.rb in oj-0.9.0
- old
+ new
@@ -6,11 +6,11 @@
require 'test/unit'
require 'oj'
class Jam
- attr_reader :x, :y
+ attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
@@ -26,24 +26,36 @@
def initialize(x, y)
super
end
def to_json()
- %{{"x":#{@x},"y":#{@y}}}
+ %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
end
-
+
+ def self.json_create(h)
+ self.new(h['x'], h['y'])
+ end
end # Jeez
class Jazz < Jam
def initialize(x, y)
super
end
def to_hash()
- { 'x' => @x, 'y' => @y }
+ { 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
end
+ def self.json_create(h)
+ self.new(h['x'], h['y'])
+ end
end # Jazz
+class Range
+ def to_hash()
+ { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
+ end
+end # Range
+
class Juice < ::Test::Unit::TestCase
def test_get_options
opts = Oj.default_options()
assert_equal(opts, {
@@ -258,13 +270,15 @@
obj = Jeez.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
def test_json_object_compat
+ Oj.default_options = { :mode => :compat }
obj = Jeez.new(true, 58)
- json = Oj.dump(obj, :mode => :compat, :indent => 2)
- assert_equal(%{{"x":true,"y":58}}, json)
+ json = Oj.dump(obj, :indent => 2)
+ assert_equal(%{{"json_class":"Jeez","x":true,"y":58}}, json)
+ dump_and_load(obj, false)
end
def test_json_object_object
obj = Jeez.new(true, 58)
json = Oj.dump(obj, :mode => :object, :indent => 2)
assert_equal(%{{
@@ -291,10 +305,11 @@
end
def test_to_hash_object_compat
obj = Jazz.new(true, 58)
json = Oj.dump(obj, :mode => :compat, :indent => 2)
assert_equal(%{{
+ "json_class":"Jazz",
"x":true,
"y":58}}, json)
end
def test_to_hash_object_object
obj = Jazz.new(true, 58)
@@ -305,11 +320,11 @@
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object)
assert_equal(obj, obj2)
end
-# Object without to_json() or to_hash()
+ # Object without to_json() or to_hash()
def test_object_strict
obj = Jam.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
rescue Exception => e
@@ -337,10 +352,11 @@
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object)
assert_equal(obj, obj2)
end
+ # Exception
def test_exception
err = nil
begin
raise StandardError.new('A Message')
rescue Exception => e
@@ -354,18 +370,103 @@
assert_equal(err.backtrace, e2['~bt'])
e2 = Oj.load(json, :mode => :object)
assert_equal(e, e2);
end
+ # Range
+ def test_range_strict
+ begin
+ json = Oj.dump(1..7, :mode => :strict)
+ rescue Exception => e
+ assert(true)
+ end
+ end
+ def test_range_null
+ json = Oj.dump(1..7, :mode => :null)
+ assert_equal('null', json)
+ end
+ def test_range_compat
+ json = Oj.dump(1..7, :mode => :compat)
+ assert_equal(%{{"begin":1,"end":7,"exclude_end":false}}, json)
+ json = Oj.dump(1...7, :mode => :compat)
+ assert_equal(%{{"begin":1,"end":7,"exclude_end":true}}, json)
+ end
+ def test_range_object
+ Oj.default_options = { :mode => :object }
+ json = Oj.dump(1..7, :mode => :object, :indent => 0)
+ assert_equal(%{{"^u":["Range",1,7,false]}}, json)
+ dump_and_load(1..7, false)
+ dump_and_load(1..1, false)
+ dump_and_load(1...7, false)
+ end
+
+ # autodefine Oj::Bag
def test_bag
json = %{{
"^o":"Jem",
"x":true,
"y":58 }}
obj = Oj.load(json, :mode => :object)
assert_equal('Jem', obj.class.name)
assert_equal(true, obj.x)
assert_equal(58, obj.y)
+ end
+
+ # Circular
+ def test_circular_object
+ obj = Jam.new(nil, 58)
+ obj.x = obj
+ json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
+ assert_equal(%{{
+ "^o":"Jam",
+ "^i":1,
+ "x":"^r1",
+ "y":58}}, json)
+ obj2 = Oj.load(json, :mode => :object, :circular => true)
+ assert_equal(obj2.x.__id__, obj2.__id__)
+ end
+
+ def test_circular_hash
+ h = { 'a' => 7 }
+ h['b'] = h
+ json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
+ assert_equal(%{{
+ "^i":1,
+ "a":7,
+ "b":"^r1"}}, json)
+ h2 = Oj.load(json, :mode => :object, :circular => true)
+ assert_equal(h['b'].__id__, h.__id__)
+ end
+
+ def test_circular_array
+ a = [7]
+ a << a
+ json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true)
+ assert_equal(%{[
+ "^i1",
+ 7,
+ "^r1"]}, json)
+ a2 = Oj.load(json, :mode => :object, :circular => true)
+ assert_equal(a2[1].__id__, a2.__id__)
+ end
+
+ def test_circular
+ h = { 'a' => 7 }
+ obj = Jam.new(h, 58)
+ obj.x['b'] = obj
+ json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
+ assert_equal(%{{
+ "^o":"Jam",
+ "^i":1,
+ "x":{
+ "^i":2,
+ "a":7,
+ "b":"^r1"
+ },
+ "y":58}}, json)
+ obj2 = Oj.load(json, :mode => :object, :circular => true)
+ assert_equal(obj.x.__id__, h.__id__)
+ assert_equal(h['b'].__id__, obj.__id__)
end
def dump_and_load(obj, trace=false)
json = Oj.dump(obj, :indent => 2)
puts json if trace