2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2011-03-29 23:38:53 -04:00
|
|
|
require 'psych/helper'
|
2010-05-22 18:12:39 -04:00
|
|
|
|
|
|
|
module Psych
|
|
|
|
module JSON
|
|
|
|
class TestStream < TestCase
|
|
|
|
def setup
|
|
|
|
@io = StringIO.new
|
|
|
|
@stream = Psych::JSON::Stream.new(@io)
|
|
|
|
@stream.start
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_documents
|
|
|
|
@io = StringIO.new
|
|
|
|
@stream = Psych::JSON::Stream.new(@io)
|
|
|
|
@stream.start
|
|
|
|
|
|
|
|
@stream.push({ 'foo' => 'bar' })
|
|
|
|
|
|
|
|
assert !@stream.finished?, 'stream not finished'
|
|
|
|
@stream.finish
|
|
|
|
assert @stream.finished?, 'stream finished'
|
|
|
|
|
|
|
|
assert_match(/^---/, @io.string)
|
|
|
|
assert_match(/\.\.\.$/, @io.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_null
|
|
|
|
@stream.push(nil)
|
|
|
|
assert_match(/^--- null/, @io.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_string
|
|
|
|
@stream.push "foo"
|
2011-02-20 20:02:41 -05:00
|
|
|
assert_match(/(["])foo\1/, @io.string)
|
2010-05-22 18:12:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_symbol
|
|
|
|
@stream.push :foo
|
2011-02-20 20:02:41 -05:00
|
|
|
assert_match(/(["])foo\1/, @io.string)
|
2010-05-22 18:12:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_int
|
|
|
|
@stream.push 10
|
|
|
|
assert_match(/^--- 10/, @io.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_float
|
|
|
|
@stream.push 1.2
|
|
|
|
assert_match(/^--- 1.2/, @io.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
hash = { 'one' => 'two' }
|
|
|
|
@stream.push hash
|
|
|
|
|
|
|
|
json = @io.string
|
|
|
|
assert_match(/}$/, json)
|
|
|
|
assert_match(/^--- \{/, json)
|
2011-02-20 20:02:41 -05:00
|
|
|
assert_match(/["]one['"]/, json)
|
|
|
|
assert_match(/["]two['"]/, json)
|
2010-05-22 18:12:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_list_to_json
|
|
|
|
list = %w{ one two }
|
|
|
|
@stream.push list
|
|
|
|
|
|
|
|
json = @io.string
|
2014-11-20 03:04:29 -05:00
|
|
|
assert_match(/\]$/, json)
|
2010-05-22 18:12:39 -04:00
|
|
|
assert_match(/^--- \[/, json)
|
2011-02-20 20:02:41 -05:00
|
|
|
assert_match(/["]one["]/, json)
|
|
|
|
assert_match(/["]two["]/, json)
|
|
|
|
end
|
|
|
|
|
2011-02-20 20:56:10 -05:00
|
|
|
class Foo; end
|
|
|
|
|
|
|
|
def test_json_dump_exclude_tag
|
|
|
|
@stream << Foo.new
|
|
|
|
json = @io.string
|
|
|
|
refute_match('Foo', json)
|
|
|
|
end
|
|
|
|
|
|
|
|
class Bar
|
|
|
|
def encode_with coder
|
|
|
|
coder.represent_seq 'omg', %w{ a b c }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_json_list_dump_exclude_tag
|
|
|
|
@stream << Bar.new
|
|
|
|
json = @io.string
|
|
|
|
refute_match('omg', json)
|
|
|
|
end
|
|
|
|
|
2011-02-20 20:02:41 -05:00
|
|
|
def test_time
|
|
|
|
time = Time.utc(2010, 10, 10)
|
|
|
|
@stream.push({'a' => time })
|
|
|
|
json = @io.string
|
2011-05-03 19:06:19 -04:00
|
|
|
assert_match "{\"a\": \"2010-10-10 00:00:00.000000000 Z\"}\n", json
|
2011-02-20 20:02:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_datetime
|
|
|
|
time = Time.new(2010, 10, 10).to_datetime
|
|
|
|
@stream.push({'a' => time })
|
|
|
|
json = @io.string
|
|
|
|
assert_match "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", json
|
2010-05-22 18:12:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|