-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
Showing
456 changed files
with
12,965 additions
and
3,574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: false | ||
require 'test/unit' | ||
require '-test-/integer' | ||
|
||
class TestInteger < Test::Unit::TestCase | ||
FIXNUM_MIN = Integer::FIXNUM_MIN | ||
FIXNUM_MAX = Integer::FIXNUM_MAX | ||
|
||
def test_fixnum_range | ||
assert_bignum(FIXNUM_MIN-1) | ||
assert_fixnum(FIXNUM_MIN) | ||
assert_fixnum(FIXNUM_MAX) | ||
assert_bignum(FIXNUM_MAX+1) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: false | ||
require 'test/unit' | ||
require "-test-/integer" | ||
|
||
class TestIntegerExt < Test::Unit::TestCase | ||
def test_my_integer_to_f | ||
assert_raise(NotImplementedError) do | ||
Bug::Integer::MyInteger.new.to_f | ||
end | ||
|
||
begin | ||
Bug::Integer::MyInteger.class_eval do | ||
def to_f | ||
end | ||
end | ||
|
||
assert_nothing_raised do | ||
Bug::Integer::MyInteger.new.to_f | ||
end | ||
ensure | ||
Bug::Integer::MyInteger.class_eval do | ||
remove_method :to_f | ||
end | ||
end | ||
end | ||
|
||
def test_my_integer_cmp | ||
assert_raise(NotImplementedError) do | ||
Bug::Integer::MyInteger.new <=> 0 | ||
end | ||
|
||
begin | ||
Bug::Integer::MyInteger.class_eval do | ||
def <=>(other) | ||
0 | ||
end | ||
end | ||
|
||
assert_nothing_raised do | ||
Bug::Integer::MyInteger.new <=> 0 | ||
end | ||
ensure | ||
Bug::Integer::MyInteger.class_eval do | ||
remove_method :<=> | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ def foo2(a,b) | |
end | ||
end | ||
|
||
class B<A | ||
class B < A | ||
private :foo1, :foo2 | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
require 'test/unit' | ||
require '-test-/string' | ||
require 'rbconfig/sizeof' | ||
|
||
class Test_StringCapacity < Test::Unit::TestCase | ||
def capa(str) | ||
Bug::String.capacity(str) | ||
end | ||
|
||
def test_capacity_embeded | ||
size = RbConfig::SIZEOF['void*'] * 3 - 1 | ||
assert_equal size, capa('foo') | ||
end | ||
|
||
def test_capacity_shared | ||
assert_equal 0, capa(:abcdefghijklmnopqrstuvwxyz.to_s) | ||
end | ||
|
||
def test_capacity_normal | ||
assert_equal 128, capa('1'*128) | ||
end | ||
|
||
def test_s_new_capacity | ||
assert_equal("", String.new(capacity: 1000)) | ||
assert_equal(String, String.new(capacity: 1000).class) | ||
assert_equal(10000, capa(String.new(capacity: 10000))) | ||
|
||
assert_equal("", String.new(capacity: -1000)) | ||
assert_equal(capa(String.new(capacity: -10000)), capa(String.new(capacity: -1000))) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "-test-/symbol" | ||
|
||
module Test_Symbol | ||
module NonInterned | ||
module_function | ||
|
||
def noninterned_name(prefix = "") | ||
prefix += "_#{Thread.current.object_id.to_s(36).tr('-', '_')}" | ||
begin | ||
name = "#{prefix}_#{rand(0x1000).to_s(16)}_#{Time.now.usec}" | ||
end while Bug::Symbol.find(name) | ||
name | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,74 @@ | ||
# frozen_string_literal: false | ||
require 'runit/testcase' | ||
require 'runit/cui/testrunner' | ||
require 'timerholder' | ||
require 'test/unit' | ||
require 'drb/timeridconv' | ||
|
||
module DRbTests | ||
|
||
class TimerHolderTest < RUNIT::TestCase | ||
def do_test(timeout, keeper_sleep = nil) | ||
holder = TimerHolder.new(timeout) | ||
holder.keeper_sleep = keeper_sleep if keeper_sleep | ||
key = holder.add(self) | ||
sleep(timeout * 0.5) | ||
assert_equal(holder.peek(key), self) | ||
holder.delete(key) | ||
assert(!holder.include?(key)) | ||
key = holder.add(self) | ||
sleep(timeout+0.5) | ||
assert_equal(holder.fetch(key), nil) | ||
key = holder.add(self) | ||
assert_equal(holder.fetch(key), self) | ||
holder.store(key, true) | ||
assert_equal(holder.fetch(key), true) | ||
assert_equal(holder.include?(key), true) | ||
sleep(timeout+0.5) | ||
assert_exception(TimerHolder::InvalidIndexError) do | ||
holder.store(key, 1) | ||
class TimerIdConvTest < Test::Unit::TestCase | ||
def test_usecase_01 | ||
keeping = 0.1 | ||
idconv = DRb::TimerIdConv.new(keeping) | ||
|
||
key = idconv.to_id(self) | ||
assert_equal(key, self.__id__) | ||
sleep(keeping) | ||
assert_equal(idconv.to_id(false), false.__id__) | ||
assert_equal(idconv.to_obj(key), self) | ||
sleep(keeping) | ||
|
||
assert_equal(idconv.to_obj(key), self) | ||
sleep(keeping) | ||
|
||
assert_equal(idconv.to_id(true), true.__id__) | ||
sleep(keeping) | ||
|
||
assert_raise do | ||
assert_equal(idconv.to_obj(key), self) | ||
end | ||
|
||
assert_raise do | ||
assert_equal(idconv.to_obj(false.__id__), false) | ||
end | ||
assert_equal(holder.include?(key), false) | ||
key = holder.add(self) | ||
sleep(timeout * 0.5) | ||
assert(holder.include?(key)) | ||
holder.extend(key, timeout) | ||
sleep(timeout * 0.5) | ||
assert(holder.include?(key)) | ||
sleep(timeout * 0.6) | ||
assert(!holder.include?(key)) | ||
holder.delete(key) | ||
end | ||
|
||
def test_00 | ||
do_test(0.5) | ||
key = idconv.to_id(self) | ||
assert_equal(key, self.__id__) | ||
assert_equal(idconv.to_id(true), true.__id__) | ||
sleep(keeping) | ||
GC.start | ||
sleep(keeping) | ||
GC.start | ||
assert_raise do | ||
assert_equal(idconv.to_obj(key), self) | ||
end | ||
end | ||
|
||
def test_01 | ||
do_test(1, 0.5) | ||
def test_usecase_02 | ||
keeping = 0.1 | ||
idconv = DRb::TimerIdConv.new(keeping) | ||
|
||
key = idconv.to_id(self) | ||
assert_equal(key, self.__id__) | ||
sleep(keeping) | ||
GC.start | ||
sleep(keeping) | ||
GC.start | ||
assert_raise do | ||
assert_equal(idconv.to_obj(key), self) | ||
end | ||
GC.start | ||
|
||
key = idconv.to_id(self) | ||
assert_equal(key, self.__id__) | ||
sleep(keeping) | ||
GC.start | ||
sleep(keeping) | ||
GC.start | ||
assert_raise do | ||
assert_equal(idconv.to_obj(key), self) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
if __FILE__ == $0 | ||
RUNIT::CUI::TestRunner.run(DRbTests::TimerHolderTest.suite) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"A JSON payload should be an object or array, not a string." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
require 'json/add/core' | ||
require 'json/add/complex' | ||
require 'json/add/rational' | ||
require 'json/add/bigdecimal' | ||
require 'json/add/ostruct' | ||
require 'date' | ||
|
||
class JSONAdditionTest < Test::Unit::TestCase | ||
include JSON | ||
|
||
class A | ||
def initialize(a) | ||
@a = a | ||
end | ||
|
||
attr_reader :a | ||
|
||
def ==(other) | ||
a == other.a | ||
end | ||
|
||
def self.json_create(object) | ||
new(*object['args']) | ||
end | ||
|
||
def to_json(*args) | ||
{ | ||
'json_class' => self.class.name, | ||
'args' => [ @a ], | ||
}.to_json(*args) | ||
end | ||
end | ||
|
||
class A2 < A | ||
def to_json(*args) | ||
{ | ||
'json_class' => self.class.name, | ||
'args' => [ @a ], | ||
}.to_json(*args) | ||
end | ||
end | ||
|
||
class B | ||
def self.json_creatable? | ||
false | ||
end | ||
|
||
def to_json(*args) | ||
{ | ||
'json_class' => self.class.name, | ||
}.to_json(*args) | ||
end | ||
end | ||
|
||
class C | ||
def self.json_creatable? | ||
false | ||
end | ||
|
||
def to_json(*args) | ||
{ | ||
'json_class' => 'JSONAdditionTest::Nix', | ||
}.to_json(*args) | ||
end | ||
end | ||
|
||
def test_extended_json | ||
a = A.new(666) | ||
assert A.json_creatable? | ||
json = generate(a) | ||
a_again = parse(json, :create_additions => true) | ||
assert_kind_of a.class, a_again | ||
assert_equal a, a_again | ||
end | ||
|
||
def test_extended_json_default | ||
a = A.new(666) | ||
assert A.json_creatable? | ||
json = generate(a) | ||
a_hash = parse(json) | ||
assert_kind_of Hash, a_hash | ||
end | ||
|
||
def test_extended_json_disabled | ||
a = A.new(666) | ||
assert A.json_creatable? | ||
json = generate(a) | ||
a_again = parse(json, :create_additions => true) | ||
assert_kind_of a.class, a_again | ||
assert_equal a, a_again | ||
a_hash = parse(json, :create_additions => false) | ||
assert_kind_of Hash, a_hash | ||
assert_equal( | ||
{"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k }, | ||
a_hash.sort_by { |k,| k } | ||
) | ||
end | ||
|
||
def test_extended_json_fail1 | ||
b = B.new | ||
assert !B.json_creatable? | ||
json = generate(b) | ||
assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json)) | ||
end | ||
|
||
def test_extended_json_fail2 | ||
c = C.new | ||
assert !C.json_creatable? | ||
json = generate(c) | ||
assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) } | ||
end | ||
|
||
def test_raw_strings | ||
raw = '' | ||
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT) | ||
raw_array = [] | ||
for i in 0..255 | ||
raw << i | ||
raw_array << i | ||
end | ||
json = raw.to_json_raw | ||
json_raw_object = raw.to_json_raw_object | ||
hash = { 'json_class' => 'String', 'raw'=> raw_array } | ||
assert_equal hash, json_raw_object | ||
assert_match(/\A\{.*\}\z/, json) | ||
assert_match(/"json_class":"String"/, json) | ||
assert_match(/"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json) | ||
raw_again = parse(json, :create_additions => true) | ||
assert_equal raw, raw_again | ||
end | ||
|
||
MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar | ||
|
||
def test_core | ||
t = Time.now | ||
assert_equal t, JSON(JSON(t), :create_additions => true) | ||
d = Date.today | ||
assert_equal d, JSON(JSON(d), :create_additions => true) | ||
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161) | ||
assert_equal d, JSON(JSON(d), :create_additions => true) | ||
assert_equal 1..10, JSON(JSON(1..10), :create_additions => true) | ||
assert_equal 1...10, JSON(JSON(1...10), :create_additions => true) | ||
assert_equal "a".."c", JSON(JSON("a".."c"), :create_additions => true) | ||
assert_equal "a"..."c", JSON(JSON("a"..."c"), :create_additions => true) | ||
s = MyJsonStruct.new 4711, 'foot' | ||
assert_equal s, JSON(JSON(s), :create_additions => true) | ||
struct = Struct.new :foo, :bar | ||
s = struct.new 4711, 'foot' | ||
assert_raise(JSONError) { JSON(s) } | ||
begin | ||
raise TypeError, "test me" | ||
rescue TypeError => e | ||
e_json = JSON.generate e | ||
e_again = JSON e_json, :create_additions => true | ||
assert_kind_of TypeError, e_again | ||
assert_equal e.message, e_again.message | ||
assert_equal e.backtrace, e_again.backtrace | ||
end | ||
assert_equal(/foo/, JSON(JSON(/foo/), :create_additions => true)) | ||
assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true)) | ||
end | ||
|
||
def test_utc_datetime | ||
now = Time.now | ||
d = DateTime.parse(now.to_s, :create_additions => true) # usual case | ||
assert_equal d, parse(d.to_json, :create_additions => true) | ||
d = DateTime.parse(now.utc.to_s) # of = 0 | ||
assert_equal d, parse(d.to_json, :create_additions => true) | ||
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24)) | ||
assert_equal d, parse(d.to_json, :create_additions => true) | ||
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24)) | ||
assert_equal d, parse(d.to_json, :create_additions => true) | ||
end | ||
|
||
def test_rational_complex | ||
assert_equal Rational(2, 9), parse(JSON(Rational(2, 9)), :create_additions => true) | ||
assert_equal Complex(2, 9), parse(JSON(Complex(2, 9)), :create_additions => true) | ||
end | ||
|
||
def test_bigdecimal | ||
assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true) | ||
assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true) | ||
end | ||
|
||
def test_ostruct | ||
o = OpenStruct.new | ||
# XXX this won't work; o.foo = { :bar => true } | ||
o.foo = { 'bar' => true } | ||
assert_equal o, parse(JSON(o), :create_additions => true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
require 'stringio' | ||
require 'tempfile' | ||
|
||
class JSONCommonInterfaceTest < Test::Unit::TestCase | ||
include JSON | ||
|
||
def setup | ||
@hash = { | ||
'a' => 2, | ||
'b' => 3.141, | ||
'c' => 'c', | ||
'd' => [ 1, "b", 3.14 ], | ||
'e' => { 'foo' => 'bar' }, | ||
'g' => "\"\0\037", | ||
'h' => 1000.0, | ||
'i' => 0.001 | ||
} | ||
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\ | ||
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' | ||
end | ||
|
||
def test_index | ||
assert_equal @json, JSON[@hash] | ||
assert_equal @hash, JSON[@json] | ||
end | ||
|
||
def test_parser | ||
assert_match /::Parser\z/, JSON.parser.name | ||
end | ||
|
||
def test_generator | ||
assert_match /::Generator\z/, JSON.generator.name | ||
end | ||
|
||
def test_state | ||
assert_match /::Generator::State\z/, JSON.state.name | ||
end | ||
|
||
def test_create_id | ||
assert_equal 'json_class', JSON.create_id | ||
JSON.create_id = 'foo_bar' | ||
assert_equal 'foo_bar', JSON.create_id | ||
ensure | ||
JSON.create_id = 'json_class' | ||
end | ||
|
||
def test_deep_const_get | ||
assert_raise(ArgumentError) { JSON.deep_const_get('Nix::Da') } | ||
assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR') | ||
end | ||
|
||
def test_parse | ||
assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]') | ||
end | ||
|
||
def test_parse_bang | ||
assert_equal [ 1, NaN, 3, ], JSON.parse!('[ 1, NaN, 3 ]') | ||
end | ||
|
||
def test_generate | ||
assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ]) | ||
end | ||
|
||
def test_fast_generate | ||
assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ]) | ||
end | ||
|
||
def test_pretty_generate | ||
assert_equal "[\n 1,\n 2,\n 3\n]", JSON.pretty_generate([ 1, 2, 3 ]) | ||
end | ||
|
||
def test_load | ||
assert_equal @hash, JSON.load(@json) | ||
tempfile = Tempfile.open('@json') | ||
tempfile.write @json | ||
tempfile.rewind | ||
assert_equal @hash, JSON.load(tempfile) | ||
stringio = StringIO.new(@json) | ||
stringio.rewind | ||
assert_equal @hash, JSON.load(stringio) | ||
assert_equal nil, JSON.load(nil) | ||
assert_equal nil, JSON.load('') | ||
ensure | ||
tempfile.close! | ||
end | ||
|
||
def test_load_with_options | ||
json = '{ "foo": NaN }' | ||
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan? | ||
end | ||
|
||
def test_load_null | ||
assert_equal nil, JSON.load(nil, nil, :allow_blank => true) | ||
assert_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) } | ||
assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) } | ||
end | ||
|
||
def test_dump | ||
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' | ||
assert_equal too_deep, dump(eval(too_deep)) | ||
assert_kind_of String, Marshal.dump(eval(too_deep)) | ||
assert_raise(ArgumentError) { dump(eval(too_deep), 100) } | ||
assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) } | ||
assert_equal too_deep, dump(eval(too_deep), 101) | ||
assert_kind_of String, Marshal.dump(eval(too_deep), 101) | ||
output = StringIO.new | ||
dump(eval(too_deep), output) | ||
assert_equal too_deep, output.string | ||
output = StringIO.new | ||
dump(eval(too_deep), output, 101) | ||
assert_equal too_deep, output.string | ||
end | ||
|
||
def test_dump_should_modify_defaults | ||
max_nesting = JSON.dump_default_options[:max_nesting] | ||
dump([], StringIO.new, 10) | ||
assert_equal max_nesting, JSON.dump_default_options[:max_nesting] | ||
end | ||
|
||
def test_JSON | ||
assert_equal @json, JSON(@hash) | ||
assert_equal @hash, JSON(@json) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# encoding: utf-8 | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
|
||
class JSONEncodingTest < Test::Unit::TestCase | ||
include JSON | ||
|
||
def setup | ||
@utf_8 = '"© ≠ €!"' | ||
@ascii_8bit = @utf_8.dup.force_encoding('ascii-8bit') | ||
@parsed = "© ≠ €!" | ||
@generated = '"\u00a9 \u2260 \u20ac!"' | ||
if String.method_defined?(:encode) | ||
@utf_16_data = @parsed.encode('utf-16be', 'utf-8') | ||
@utf_16be = @utf_8.encode('utf-16be', 'utf-8') | ||
@utf_16le = @utf_8.encode('utf-16le', 'utf-8') | ||
@utf_32be = @utf_8.encode('utf-32be', 'utf-8') | ||
@utf_32le = @utf_8.encode('utf-32le', 'utf-8') | ||
else | ||
require 'iconv' | ||
@utf_16_data, = Iconv.iconv('utf-16be', 'utf-8', @parsed) | ||
@utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8) | ||
@utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8) | ||
@utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8) | ||
@utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8) | ||
end | ||
end | ||
|
||
def test_parse | ||
assert_equal @parsed, JSON.parse(@ascii_8bit) | ||
assert_equal @parsed, JSON.parse(@utf_8) | ||
assert_equal @parsed, JSON.parse(@utf_16be) | ||
assert_equal @parsed, JSON.parse(@utf_16le) | ||
assert_equal @parsed, JSON.parse(@utf_32be) | ||
assert_equal @parsed, JSON.parse(@utf_32le) | ||
end | ||
|
||
def test_generate | ||
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true) | ||
assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true) | ||
end | ||
|
||
def test_unicode | ||
assert_equal '""', ''.to_json | ||
assert_equal '"\\b"', "\b".to_json | ||
assert_equal '"\u0001"', 0x1.chr.to_json | ||
assert_equal '"\u001f"', 0x1f.chr.to_json | ||
assert_equal '" "', ' '.to_json | ||
assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json | ||
utf8 = [ "© ≠ €! \01" ] | ||
json = '["© ≠ €! \u0001"]' | ||
assert_equal json, utf8.to_json(:ascii_only => false) | ||
assert_equal utf8, parse(json) | ||
json = '["\u00a9 \u2260 \u20ac! \u0001"]' | ||
assert_equal json, utf8.to_json(:ascii_only => true) | ||
assert_equal utf8, parse(json) | ||
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] | ||
json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]" | ||
assert_equal utf8, parse(json) | ||
assert_equal json, utf8.to_json(:ascii_only => false) | ||
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] | ||
assert_equal utf8, parse(json) | ||
json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]" | ||
assert_equal json, utf8.to_json(:ascii_only => true) | ||
assert_equal utf8, parse(json) | ||
utf8 = ['საქართველო'] | ||
json = '["საქართველო"]' | ||
assert_equal json, utf8.to_json(:ascii_only => false) | ||
json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]" | ||
assert_equal json, utf8.to_json(:ascii_only => true) | ||
assert_equal utf8, parse(json) | ||
assert_equal '["Ã"]', generate(["Ã"], :ascii_only => false) | ||
assert_equal '["\\u00c3"]', generate(["Ã"], :ascii_only => true) | ||
assert_equal ["€"], parse('["\u20ac"]') | ||
utf8 = ["\xf0\xa0\x80\x81"] | ||
json = "[\"\xf0\xa0\x80\x81\"]" | ||
assert_equal json, generate(utf8, :ascii_only => false) | ||
assert_equal utf8, parse(json) | ||
json = '["\ud840\udc01"]' | ||
assert_equal json, generate(utf8, :ascii_only => true) | ||
assert_equal utf8, parse(json) | ||
end | ||
|
||
def test_chars | ||
(0..0x7f).each do |i| | ||
json = '["\u%04x"]' % i | ||
if RUBY_VERSION >= "1.9." | ||
i = i.chr | ||
end | ||
assert_equal i, parse(json).first[0] | ||
if i == ?\b | ||
generated = generate(["" << i]) | ||
assert '["\b"]' == generated || '["\10"]' == generated | ||
elsif [?\n, ?\r, ?\t, ?\f].include?(i) | ||
assert_equal '[' << ('' << i).dump << ']', generate(["" << i]) | ||
elsif i.chr < 0x20.chr | ||
assert_equal json, generate(["" << i]) | ||
end | ||
end | ||
assert_raise(JSON::GeneratorError) do | ||
generate(["\x80"], :ascii_only => true) | ||
end | ||
assert_equal "\302\200", parse('["\u0080"]').first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
|
||
class JSONExtParserTest < Test::Unit::TestCase | ||
if defined?(JSON::Ext::Parser) | ||
def test_allocate | ||
parser = JSON::Ext::Parser.new("{}") | ||
assert_raise(TypeError, '[ruby-core:35079]') do | ||
parser.__send__(:initialize, "{}") | ||
end | ||
parser = JSON::Ext::Parser.allocate | ||
assert_raise(TypeError, '[ruby-core:35079]') { parser.source } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
|
||
class JSONFixturesTest < Test::Unit::TestCase | ||
def setup | ||
fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}.json') | ||
passed, failed = Dir[fixtures].partition { |f| f['pass'] } | ||
@passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort | ||
@failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort | ||
end | ||
|
||
def test_passing | ||
for name, source in @passed | ||
begin | ||
assert JSON.parse(source), | ||
"Did not pass for fixture '#{name}': #{source.inspect}" | ||
rescue => e | ||
warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}" | ||
raise e | ||
end | ||
end | ||
end | ||
|
||
def test_failing | ||
for name, source in @failed | ||
assert_raise(JSON::ParserError, JSON::NestingError, | ||
"Did not fail for fixture '#{name}': #{source.inspect}") do | ||
JSON.parse(source) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,377 @@ | ||
#!/usr/bin/env ruby | ||
# encoding: utf-8 | ||
# frozen_string_literal: false | ||
|
||
require 'test_helper' | ||
|
||
class JSONGeneratorTest < Test::Unit::TestCase | ||
include JSON | ||
|
||
def setup | ||
@hash = { | ||
'a' => 2, | ||
'b' => 3.141, | ||
'c' => 'c', | ||
'd' => [ 1, "b", 3.14 ], | ||
'e' => { 'foo' => 'bar' }, | ||
'g' => "\"\0\037", | ||
'h' => 1000.0, | ||
'i' => 0.001 | ||
} | ||
@json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + | ||
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' | ||
@json3 = <<'EOT'.chomp | ||
{ | ||
"a": 2, | ||
"b": 3.141, | ||
"c": "c", | ||
"d": [ | ||
1, | ||
"b", | ||
3.14 | ||
], | ||
"e": { | ||
"foo": "bar" | ||
}, | ||
"g": "\"\u0000\u001f", | ||
"h": 1000.0, | ||
"i": 0.001 | ||
} | ||
EOT | ||
end | ||
|
||
def test_generate | ||
json = generate(@hash) | ||
assert_equal(parse(@json2), parse(json)) | ||
json = JSON[@hash] | ||
assert_equal(parse(@json2), parse(json)) | ||
parsed_json = parse(json) | ||
assert_equal(@hash, parsed_json) | ||
json = generate({1=>2}) | ||
assert_equal('{"1":2}', json) | ||
parsed_json = parse(json) | ||
assert_equal({"1"=>2}, parsed_json) | ||
assert_equal '666', generate(666) | ||
end | ||
|
||
def test_generate_pretty | ||
json = pretty_generate(@hash) | ||
# hashes aren't (insertion) ordered on every ruby implementation | ||
# assert_equal(@json3, json) | ||
assert_equal(parse(@json3), parse(json)) | ||
parsed_json = parse(json) | ||
assert_equal(@hash, parsed_json) | ||
json = pretty_generate({1=>2}) | ||
assert_equal(<<'EOT'.chomp, json) | ||
{ | ||
"1": 2 | ||
} | ||
EOT | ||
parsed_json = parse(json) | ||
assert_equal({"1"=>2}, parsed_json) | ||
assert_equal '666', pretty_generate(666) | ||
end | ||
|
||
def test_generate_custom | ||
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>") | ||
json = generate({1=>{2=>3,4=>[5,6]}}, state) | ||
assert_equal(<<'EOT'.chomp, json) | ||
{ | ||
<i>"1" : { | ||
<i><i>"2" : 3, | ||
<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>] | ||
<i>} | ||
} | ||
EOT | ||
end | ||
|
||
def test_fast_generate | ||
json = fast_generate(@hash) | ||
assert_equal(parse(@json2), parse(json)) | ||
parsed_json = parse(json) | ||
assert_equal(@hash, parsed_json) | ||
json = fast_generate({1=>2}) | ||
assert_equal('{"1":2}', json) | ||
parsed_json = parse(json) | ||
assert_equal({"1"=>2}, parsed_json) | ||
assert_equal '666', fast_generate(666) | ||
end | ||
|
||
def test_own_state | ||
state = State.new | ||
json = generate(@hash, state) | ||
assert_equal(parse(@json2), parse(json)) | ||
parsed_json = parse(json) | ||
assert_equal(@hash, parsed_json) | ||
json = generate({1=>2}, state) | ||
assert_equal('{"1":2}', json) | ||
parsed_json = parse(json) | ||
assert_equal({"1"=>2}, parsed_json) | ||
assert_equal '666', generate(666, state) | ||
end | ||
|
||
def test_states | ||
json = generate({1=>2}, nil) | ||
assert_equal('{"1":2}', json) | ||
s = JSON.state.new | ||
assert s.check_circular? | ||
assert s[:check_circular?] | ||
h = { 1=>2 } | ||
h[3] = h | ||
assert_raise(JSON::NestingError) { generate(h) } | ||
assert_raise(JSON::NestingError) { generate(h, s) } | ||
s = JSON.state.new | ||
a = [ 1, 2 ] | ||
a << a | ||
assert_raise(JSON::NestingError) { generate(a, s) } | ||
assert s.check_circular? | ||
assert s[:check_circular?] | ||
end | ||
|
||
def test_pretty_state | ||
state = PRETTY_STATE_PROTOTYPE.dup | ||
assert_equal({ | ||
:allow_nan => false, | ||
:array_nl => "\n", | ||
:ascii_only => false, | ||
:buffer_initial_length => 1024, | ||
:depth => 0, | ||
:indent => " ", | ||
:max_nesting => 100, | ||
:object_nl => "\n", | ||
:space => " ", | ||
:space_before => "", | ||
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) | ||
end | ||
|
||
def test_safe_state | ||
state = SAFE_STATE_PROTOTYPE.dup | ||
assert_equal({ | ||
:allow_nan => false, | ||
:array_nl => "", | ||
:ascii_only => false, | ||
:buffer_initial_length => 1024, | ||
:depth => 0, | ||
:indent => "", | ||
:max_nesting => 100, | ||
:object_nl => "", | ||
:space => "", | ||
:space_before => "", | ||
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) | ||
end | ||
|
||
def test_fast_state | ||
state = FAST_STATE_PROTOTYPE.dup | ||
assert_equal({ | ||
:allow_nan => false, | ||
:array_nl => "", | ||
:ascii_only => false, | ||
:buffer_initial_length => 1024, | ||
:depth => 0, | ||
:indent => "", | ||
:max_nesting => 0, | ||
:object_nl => "", | ||
:space => "", | ||
:space_before => "", | ||
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) | ||
end | ||
|
||
def test_allow_nan | ||
assert_raise(GeneratorError) { generate([JSON::NaN]) } | ||
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) | ||
assert_raise(GeneratorError) { fast_generate([JSON::NaN]) } | ||
assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) } | ||
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) | ||
assert_raise(GeneratorError) { generate([JSON::Infinity]) } | ||
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) | ||
assert_raise(GeneratorError) { fast_generate([JSON::Infinity]) } | ||
assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) } | ||
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) | ||
assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) } | ||
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) | ||
assert_raise(GeneratorError) { fast_generate([JSON::MinusInfinity]) } | ||
assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } | ||
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) | ||
end | ||
|
||
def test_depth | ||
ary = []; ary << ary | ||
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth | ||
assert_raise(JSON::NestingError) { generate(ary) } | ||
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth | ||
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth | ||
assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) } | ||
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth | ||
s = JSON.state.new | ||
assert_equal 0, s.depth | ||
assert_raise(JSON::NestingError) { ary.to_json(s) } | ||
assert_equal 100, s.depth | ||
end | ||
|
||
def test_buffer_initial_length | ||
s = JSON.state.new | ||
assert_equal 1024, s.buffer_initial_length | ||
s.buffer_initial_length = 0 | ||
assert_equal 1024, s.buffer_initial_length | ||
s.buffer_initial_length = -1 | ||
assert_equal 1024, s.buffer_initial_length | ||
s.buffer_initial_length = 128 | ||
assert_equal 128, s.buffer_initial_length | ||
end | ||
|
||
def test_gc | ||
if respond_to?(:assert_in_out_err) | ||
assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], []) | ||
bignum_too_long_to_embed_as_string = 1234567890123456789012345 | ||
expect = bignum_too_long_to_embed_as_string.to_s | ||
GC.stress = true | ||
10.times do |i| | ||
tmp = bignum_too_long_to_embed_as_string.to_json | ||
raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect | ||
end | ||
EOS | ||
end | ||
end if GC.respond_to?(:stress=) | ||
|
||
def test_configure_using_configure_and_merge | ||
numbered_state = { | ||
:indent => "1", | ||
:space => '2', | ||
:space_before => '3', | ||
:object_nl => '4', | ||
:array_nl => '5' | ||
} | ||
state1 = JSON.state.new | ||
state1.merge(numbered_state) | ||
assert_equal '1', state1.indent | ||
assert_equal '2', state1.space | ||
assert_equal '3', state1.space_before | ||
assert_equal '4', state1.object_nl | ||
assert_equal '5', state1.array_nl | ||
state2 = JSON.state.new | ||
state2.configure(numbered_state) | ||
assert_equal '1', state2.indent | ||
assert_equal '2', state2.space | ||
assert_equal '3', state2.space_before | ||
assert_equal '4', state2.object_nl | ||
assert_equal '5', state2.array_nl | ||
end | ||
|
||
def test_configure_hash_conversion | ||
state = JSON.state.new | ||
state.configure(:indent => '1') | ||
assert_equal '1', state.indent | ||
state = JSON.state.new | ||
foo = 'foo' | ||
assert_raise(TypeError) do | ||
state.configure(foo) | ||
end | ||
def foo.to_h | ||
{ :indent => '2' } | ||
end | ||
state.configure(foo) | ||
assert_equal '2', state.indent | ||
end | ||
|
||
if defined?(JSON::Ext::Generator) | ||
def test_broken_bignum # [ruby-core:38867] | ||
pid = fork do | ||
x = 1 << 64 | ||
x.class.class_eval do | ||
def to_s | ||
end | ||
end | ||
begin | ||
JSON::Ext::Generator::State.new.generate(x) | ||
exit 1 | ||
rescue TypeError | ||
exit 0 | ||
end | ||
end | ||
_, status = Process.waitpid2(pid) | ||
assert status.success? | ||
rescue NotImplementedError | ||
# forking to avoid modifying core class of a parent process and | ||
# introducing race conditions of tests are run in parallel | ||
end | ||
end | ||
|
||
def test_hash_likeness_set_symbol | ||
state = JSON.state.new | ||
assert_equal nil, state[:foo] | ||
assert_equal nil.class, state[:foo].class | ||
assert_equal nil, state['foo'] | ||
state[:foo] = :bar | ||
assert_equal :bar, state[:foo] | ||
assert_equal :bar, state['foo'] | ||
state_hash = state.to_hash | ||
assert_kind_of Hash, state_hash | ||
assert_equal :bar, state_hash[:foo] | ||
end | ||
|
||
def test_hash_likeness_set_string | ||
state = JSON.state.new | ||
assert_equal nil, state[:foo] | ||
assert_equal nil, state['foo'] | ||
state['foo'] = :bar | ||
assert_equal :bar, state[:foo] | ||
assert_equal :bar, state['foo'] | ||
state_hash = state.to_hash | ||
assert_kind_of Hash, state_hash | ||
assert_equal :bar, state_hash[:foo] | ||
end | ||
|
||
def test_json_generate | ||
assert_raise JSON::GeneratorError do | ||
assert_equal true, generate(["\xea"]) | ||
end | ||
end | ||
|
||
def test_nesting | ||
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' | ||
too_deep_ary = eval too_deep | ||
assert_raise(JSON::NestingError) { generate too_deep_ary } | ||
assert_raise(JSON::NestingError) { generate too_deep_ary, :max_nesting => 100 } | ||
ok = generate too_deep_ary, :max_nesting => 101 | ||
assert_equal too_deep, ok | ||
ok = generate too_deep_ary, :max_nesting => nil | ||
assert_equal too_deep, ok | ||
ok = generate too_deep_ary, :max_nesting => false | ||
assert_equal too_deep, ok | ||
ok = generate too_deep_ary, :max_nesting => 0 | ||
assert_equal too_deep, ok | ||
end | ||
|
||
def test_backslash | ||
data = [ '\\.(?i:gif|jpe?g|png)$' ] | ||
json = '["\\\\.(?i:gif|jpe?g|png)$"]' | ||
assert_equal json, generate(data) | ||
# | ||
data = [ '\\"' ] | ||
json = '["\\\\\""]' | ||
assert_equal json, generate(data) | ||
# | ||
data = [ '/' ] | ||
json = '["/"]' | ||
assert_equal json, generate(data) | ||
# | ||
data = ['"'] | ||
json = '["\""]' | ||
assert_equal json, generate(data) | ||
# | ||
data = ["'"] | ||
json = '["\\\'"]' | ||
assert_equal '["\'"]', generate(data) | ||
end | ||
|
||
def test_string_subclass | ||
s = Class.new(String) do | ||
def to_s; self; end | ||
undef to_json | ||
end | ||
assert_nothing_raised(SystemStackError) do | ||
assert_equal '["foo"]', JSON.generate([s.new('foo')]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
|
||
class JSONGenericObjectTest < Test::Unit::TestCase | ||
include JSON | ||
|
||
def setup | ||
@go = GenericObject[ :a => 1, :b => 2 ] | ||
end | ||
|
||
def test_attributes | ||
assert_equal 1, @go.a | ||
assert_equal 1, @go[:a] | ||
assert_equal 2, @go.b | ||
assert_equal 2, @go[:b] | ||
assert_nil @go.c | ||
assert_nil @go[:c] | ||
end | ||
|
||
def test_generate_json | ||
switch_json_creatable do | ||
assert_equal @go, JSON(JSON(@go), :create_additions => true) | ||
end | ||
end | ||
|
||
def test_parse_json | ||
assert_kind_of Hash, | ||
JSON( | ||
'{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', | ||
:create_additions => true | ||
) | ||
switch_json_creatable do | ||
assert_equal @go, l = | ||
JSON( | ||
'{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', | ||
:create_additions => true | ||
) | ||
assert_equal 1, l.a | ||
assert_equal @go, | ||
l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject) | ||
assert_equal 1, l.a | ||
assert_equal GenericObject[:a => GenericObject[:b => 2]], | ||
l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject) | ||
assert_equal 2, l.a.b | ||
end | ||
end | ||
|
||
def test_from_hash | ||
result = GenericObject.from_hash( | ||
:foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] }) | ||
assert_kind_of GenericObject, result.foo | ||
assert_kind_of GenericObject, result.foo.bar | ||
assert_equal true, result.foo.bar.baz | ||
assert_kind_of GenericObject, result.foo.quux.first | ||
assert_equal true, result.foo.quux.first.foobar | ||
assert_equal true, GenericObject.from_hash(true) | ||
end | ||
|
||
def test_json_generic_object_load | ||
empty = JSON::GenericObject.load(nil) | ||
assert_kind_of JSON::GenericObject, empty | ||
simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}' | ||
simple = JSON::GenericObject.load(simple_json) | ||
assert_kind_of JSON::GenericObject, simple | ||
assert_equal "world", simple.hello | ||
converting = JSON::GenericObject.load('{ "hello": "world" }') | ||
assert_kind_of JSON::GenericObject, converting | ||
assert_equal "world", converting.hello | ||
|
||
json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world']) | ||
assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}') | ||
end | ||
|
||
private | ||
|
||
def switch_json_creatable | ||
JSON::GenericObject.json_creatable = true | ||
yield | ||
ensure | ||
JSON::GenericObject.json_creatable = false | ||
end | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#frozen_string_literal: false | ||
require 'test_helper' | ||
require 'time' | ||
|
||
class JSONStringMatchingTest < Test::Unit::TestCase | ||
include JSON | ||
|
||
class TestTime < ::Time | ||
def self.json_create(string) | ||
Time.parse(string) | ||
end | ||
|
||
def to_json(*) | ||
%{"#{strftime('%FT%T%z')}"} | ||
end | ||
|
||
def ==(other) | ||
to_i == other.to_i | ||
end | ||
end | ||
|
||
def test_match_date | ||
t = TestTime.new | ||
t_json = [ t ].to_json | ||
time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ | ||
assert_equal [ t ], | ||
parse( | ||
t_json, | ||
:create_additions => true, | ||
:match_string => { time_regexp => TestTime } | ||
) | ||
assert_equal [ t.strftime('%FT%T%z') ], | ||
parse( | ||
t_json, | ||
:match_string => { time_regexp => TestTime } | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
case ENV['JSON'] | ||
when 'pure' | ||
$:.unshift 'lib' | ||
require 'json/pure' | ||
when 'ext' | ||
$:.unshift 'ext', 'lib' | ||
require 'json/ext' | ||
else | ||
$:.unshift 'ext', 'lib' | ||
require 'json' | ||
end | ||
|
||
require 'test/unit' | ||
begin | ||
require 'byebug' | ||
rescue LoadError | ||
end | ||
if ENV['START_SIMPLECOV'].to_i == 1 | ||
require 'simplecov' | ||
SimpleCov.start | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'test/unit' | ||
require '-test-/integer.so' | ||
|
||
module Test::Unit::Assertions | ||
def assert_fixnum(v, msg=nil) | ||
assert_instance_of(Integer, v, msg) | ||
assert_predicate(v, :fixnum?, msg) | ||
end | ||
|
||
def assert_bignum(v, msg=nil) | ||
assert_instance_of(Integer, v, msg) | ||
assert_predicate(v, :bignum?, msg) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.