2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "bigdecimal"
|
2017-02-22 08:25:44 -05:00
|
|
|
require "date"
|
|
|
|
require "time"
|
|
|
|
require "pathname"
|
|
|
|
require "uri"
|
2015-07-11 08:48:16 -04:00
|
|
|
|
|
|
|
module JSONTest
|
|
|
|
class Foo
|
|
|
|
def initialize(a, b)
|
|
|
|
@a, @b = a, b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Hashlike
|
|
|
|
def to_hash
|
2016-08-06 13:38:33 -04:00
|
|
|
{ foo: "hello", bar: "world" }
|
2015-07-11 08:48:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Custom
|
|
|
|
def initialize(serialized)
|
|
|
|
@serialized = serialized
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(options = nil)
|
|
|
|
@serialized
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-13 01:09:56 -05:00
|
|
|
MyStruct = Struct.new(:name, :value) do
|
2015-07-11 15:19:39 -04:00
|
|
|
def initialize(*)
|
|
|
|
@unused = "unused instance variable"
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-11 08:48:16 -04:00
|
|
|
module EncodingTestCases
|
|
|
|
TrueTests = [[ true, %(true) ]]
|
|
|
|
FalseTests = [[ false, %(false) ]]
|
|
|
|
NilTests = [[ nil, %(null) ]]
|
|
|
|
NumericTests = [[ 1, %(1) ],
|
|
|
|
[ 2.5, %(2.5) ],
|
2016-10-28 23:05:58 -04:00
|
|
|
[ 0.0 / 0.0, %(null) ],
|
|
|
|
[ 1.0 / 0.0, %(null) ],
|
|
|
|
[ -1.0 / 0.0, %(null) ],
|
|
|
|
[ BigDecimal("0.0") / BigDecimal("0.0"), %(null) ],
|
2016-08-06 12:03:25 -04:00
|
|
|
[ BigDecimal("2.5"), %("#{BigDecimal('2.5')}") ]]
|
2015-07-11 08:48:16 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
StringTests = [[ "this is the <string>", %("this is the \\u003cstring\\u003e")],
|
2015-07-11 08:48:16 -04:00
|
|
|
[ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ],
|
2016-08-06 12:03:25 -04:00
|
|
|
[ "http://test.host/posts/1", %("http://test.host/posts/1")],
|
2015-07-11 08:48:16 -04:00
|
|
|
[ "Control characters: \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\u2028\u2029",
|
|
|
|
%("Control characters: \\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\\u2028\\u2029") ]]
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
ArrayTests = [[ ["a", "b", "c"], %([\"a\",\"b\",\"c\"]) ],
|
|
|
|
[ [1, "a", :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
|
2015-07-11 08:48:16 -04:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
HashTests = [[ { foo: "bar" }, %({\"foo\":\"bar\"}) ],
|
|
|
|
[ { 1 => 1, 2 => "a", 3 => :b, 4 => nil, 5 => false }, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]]
|
2015-07-11 15:19:39 -04:00
|
|
|
|
2015-07-11 08:48:16 -04:00
|
|
|
RangeTests = [[ 1..2, %("1..2")],
|
|
|
|
[ 1...2, %("1...2")],
|
|
|
|
[ 1.5..2.5, %("1.5..2.5")]]
|
|
|
|
|
|
|
|
SymbolTests = [[ :a, %("a") ],
|
|
|
|
[ :this, %("this") ],
|
|
|
|
[ :"a b", %("a b") ]]
|
|
|
|
|
|
|
|
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
|
|
|
|
HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
|
2015-07-11 15:19:39 -04:00
|
|
|
StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
|
|
|
|
[ MyStruct.new(nil, nil), %({\"name\":null,\"value\":null}) ]]
|
2015-07-11 08:48:16 -04:00
|
|
|
CustomTests = [[ Custom.new("custom"), '"custom"' ],
|
2016-08-06 12:03:25 -04:00
|
|
|
[ Custom.new(nil), "null" ],
|
2015-07-11 08:48:16 -04:00
|
|
|
[ Custom.new(:a), '"a"' ],
|
|
|
|
[ Custom.new([ :foo, "bar" ]), '["foo","bar"]' ],
|
2016-08-06 13:44:11 -04:00
|
|
|
[ Custom.new(foo: "hello", bar: "world"), '{"bar":"world","foo":"hello"}' ],
|
2015-07-11 08:48:16 -04:00
|
|
|
[ Custom.new(Hashlike.new), '{"bar":"world","foo":"hello"}' ],
|
|
|
|
[ Custom.new(Custom.new(Custom.new(:a))), '"a"' ]]
|
|
|
|
|
|
|
|
RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']]
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
URITests = [[ URI.parse("http://example.com"), %("http://example.com") ]]
|
Define `URI::Generic#as_json`
When the URI object is converted as JSON,
it is expected that it is a string that means its URI.
Expected:
```
>> URI.parse('http://example.com').as_json
"http://example.com"
```
Actual:
```
>> URI.parse('http://example.com').as_json
{"scheme"=>"http",
"user"=>nil,
"password"=>nil,
"host"=>"example.com",
"port"=>80,
"path"=>"",
"query"=>nil,
"opaque"=>nil,
"fragment"=>nil,
"parser"=>
{"regexp"=>
{"SCHEME"=>"(?-mix:\\A[A-Za-z][A-Za-z0-9+\\-.]*\\z)",
"USERINFO"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=A-Z_a-z~])*\\z)",
"HOST"=>
"(?-mix:\\A(?:(?<IP-literal>\\[(?:(?<IPv6address>(?:\\h{1,4}:){6}(?<ls32>\\h{1,4}:\\h{1,4}|(?<IPv4address>(?<dec-octet>[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]|\\d)\\.\\g<dec-octet>\\.\\g<dec-octet>\\.\\g<dec-octet>))|::(?:\\h{1,4}:){5}\\g<ls32>|\\h{,4}::(?:\\h{1,4}:){4}\\g<ls32>|(?:(?:\\h{1,4}:)?\\h{1,4})?::(?:\\h{1,4}:){3}\\g<ls32>|(?:(?:\\h{1,4}:){,2}\\h{1,4})?::(?:\\h{1,4}:){2}\\g<ls32>|(?:(?:\\h{1,4}:){,3}\\h{1,4})?::\\h{1,4}:\\g<ls32>|(?:(?:\\h{1,4}:){,4}\\h{1,4})?::\\g<ls32>|(?:(?:\\h{1,4}:){,5}\\h{1,4})?::\\h{1,4}|(?:(?:\\h{1,4}:){,6}\\h{1,4})?::)|(?<IPvFuture>v\\h+\\.[!$&-.0-;=A-Z_a-z~]+))\\])|\\g<IPv4address>|(?<reg-name>(?:%\\h\\h|[!$&-.0-9;=A-Z_a-z~])*))\\z)",
"ABS_PATH"=>
"(?-mix:\\A\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*(?:\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*)*\\z)",
"REL_PATH"=>
"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])+(?:\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*)*\\z)",
"QUERY"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~\\/?])*\\z)",
"FRAGMENT"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~\\/?])*\\z)",
"OPAQUE"=>"(?-mix:\\A(?:[^\\/].*)?\\z)",
"PORT"=>
"(?-mix:\\A[\\x09\\x0a\\x0c\\x0d ]*\\d*[\\x09\\x0a\\x0c\\x0d ]*\\z)"}}}
```
2016-06-10 11:57:29 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
PathnameTests = [[ Pathname.new("lib/index.rb"), %("lib/index.rb") ]]
|
2016-06-25 08:50:15 -04:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
DateTests = [[ Date.new(2005, 2, 1), %("2005/02/01") ]]
|
|
|
|
TimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
|
|
|
|
DateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
|
2015-07-11 08:48:16 -04:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
StandardDateTests = [[ Date.new(2005, 2, 1), %("2005-02-01") ]]
|
|
|
|
StandardTimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005-02-01T15:15:10.000Z") ]]
|
|
|
|
StandardDateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005-02-01T15:15:10.000+00:00") ]]
|
2016-08-06 12:03:25 -04:00
|
|
|
StandardStringTests = [[ "this is the <string>", %("this is the <string>")]]
|
2015-07-11 08:48:16 -04:00
|
|
|
end
|
|
|
|
end
|