test/tests.rb in oj-1.2.9 vs test/tests.rb in oj-1.2.10
- old
+ new
@@ -4,10 +4,12 @@
$: << File.join(File.dirname(__FILE__), "../lib")
$: << File.join(File.dirname(__FILE__), "../ext")
require 'test/unit'
require 'stringio'
+require 'date'
+require 'bigdecimal'
require 'oj'
$ruby = RUBY_DESCRIPTION.split(' ')[0]
$ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
@@ -208,10 +210,11 @@
# Symbol
def test_symbol_strict
begin
json = Oj.dump(:abc, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_symbol_null
@@ -232,10 +235,11 @@
# Time
def test_time_strict
t = Time.local(2012, 1, 5, 23, 58, 7)
begin
json = Oj.dump(t, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_time_null
@@ -252,14 +256,15 @@
t = Time.now()
Oj.default_options = { :mode => :object }
dump_and_load(t, false)
end
-# Class
+ # Class
def test_class_strict
begin
json = Oj.dump(Juice, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_class_null
@@ -273,34 +278,37 @@
def test_class_object
Oj.default_options = { :mode => :object }
dump_and_load(Juice, false)
end
-# Hash
+ # Hash
def test_hash
Oj.default_options = { :mode => :strict }
dump_and_load({}, false)
dump_and_load({ 'true' => true, 'false' => false}, false)
dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
end
def test_non_str_hash_strict
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_null
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :null)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_compat
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
+ assert(false)
rescue Exception => e
assert(e.message.include?('Fixnum'))
assert(true)
end
end
@@ -324,10 +332,11 @@
# Object with to_json()
def test_json_object_strict
obj = Jeez.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_json_object_null
@@ -369,10 +378,11 @@
# Object with to_hash()
def test_to_hash_object_strict
obj = Jazz.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_to_hash_object_null
@@ -404,10 +414,11 @@
# Object with as_json() # contributed by sauliusg
def test_as_json_object_strict
obj = Orange.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
@@ -455,10 +466,11 @@
# Object without to_json() or to_hash()
def test_object_strict
obj = Jam.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_object_null
@@ -494,10 +506,11 @@
# Exception
def test_exception
err = nil
begin
raise StandardError.new('A Message')
+ assert(false)
rescue Exception => e
err = e
end
json = Oj.dump(err, :mode => :object, :indent => 2)
#puts "*** #{json}"
@@ -513,10 +526,11 @@
# Range
def test_range_strict
begin
json = Oj.dump(1..7, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_range_null
@@ -543,9 +557,115 @@
end
dump_and_load(1..7, false)
dump_and_load(1..1, false)
dump_and_load(1...7, false)
end
+ end
+
+ # BigNum
+ def test_bignum_strict
+ json = Oj.dump(7 ** 55, :mode => :strict)
+ assert_equal('30226801971775055948247051683954096612865741943', json)
+ end
+ def test_bignum_null
+ json = Oj.dump(7 ** 55, :mode => :null)
+ assert_equal('30226801971775055948247051683954096612865741943', json)
+ end
+ def test_bignum_compat
+ json = Oj.dump(7 ** 55, :mode => :compat)
+ b = Oj.load(json, :mode => :strict)
+ assert_equal(30226801971775055948247051683954096612865741943, b)
+ end
+ def test_bignum_object
+ dump_and_load(7 ** 55, false)
+ end
+
+ # BigDecimal
+ def test_bigdecimal_strict
+ begin
+ json = Oj.dump(BigDecimal.new('3.14159265358979323846'), :mode => :strict)
+ assert(false)
+ rescue Exception => e
+ assert(true)
+ end
+ end
+ def test_bigdecimal_null
+ json = Oj.dump(BigDecimal.new('3.14159265358979323846'), :mode => :null)
+ assert_equal('null', json)
+ end
+ def test_bigdecimal_compat
+ orig = BigDecimal.new('3.14159265358979323846')
+ json = Oj.dump(orig, :mode => :compat)
+ bg = Oj.load(json, :mode => :compat)
+ assert_equal(orig, bg)
+ end
+ def test_bigdecimal_object
+ dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
+ # Infinity is the same for Float and BigDecimal
+ json = Oj.dump(BigDecimal.new('Infinity'), :mode => :compat)
+ assert_equal('Infinity', json)
+ json = Oj.dump(BigDecimal.new('-Infinity'), :mode => :compat)
+ assert_equal('-Infinity', json)
+ end
+
+ # Date
+ def test_date_strict
+ begin
+ json = Oj.dump(Date.new(2012, 6, 19), :mode => :strict)
+ assert(false)
+ rescue Exception => e
+ assert(true)
+ end
+ end
+ def test_date_null
+ json = Oj.dump(Date.new(2012, 6, 19), :mode => :null)
+ assert_equal('null', json)
+ end
+ def test_date_compat
+ orig = Date.new(2012, 6, 19)
+ json = Oj.dump(orig, :mode => :compat)
+ x = Oj.load(json, :mode => :compat)
+ # Some Rubies implement Date as data and some as a real Object. Either are
+ # okay for the test.
+ if x.is_a?(String)
+ assert_equal(orig.to_s, x)
+ else # better be a Hash
+ assert_equal({"year" => orig.year, "month" => orig.month, "day" => orig.day, "start" => orig.start}, x)
+ end
+ end
+ def test_date_object
+ dump_and_load(Date.new(2012, 6, 19), false)
+ end
+
+ # DateTime
+ def test_datetime_strict
+ begin
+ json = Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :strict)
+ assert(false)
+ rescue Exception => e
+ assert(true)
+ end
+ end
+ def test_datetime_null
+ json = Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :null)
+ assert_equal('null', json)
+ end
+ def test_datetime_compat
+ orig = DateTime.new(2012, 6, 19, 20, 19, 27)
+ json = Oj.dump(orig, :mode => :compat)
+ x = Oj.load(json, :mode => :compat)
+ # Some Rubies implement Date as data and some as a real Object. Either are
+ # okay for the test.
+ if x.is_a?(String)
+ assert_equal(orig.to_s, x)
+ else # better be a Hash
+ assert_equal({ "year" => orig.year, "month" => orig.month, "day" => orig.day,
+ "hour" => orig.hour, "min" => orig.min, "sec" => orig.sec,
+ "offset" => orig.offset, "start" => orig.start}, x)
+ end
+ end
+ def test_datetime_object
+ dump_and_load(DateTime.new(2012, 6, 19), false)
end
# autodefine Oj::Bag
def test_bag
json = %{{