Debian Codegraph @

test/psych/json/test_stream.rb

raw
1 # frozen_string_literal: true
2 require 'psych/helper'
3
4 module Psych
5 module JSON
6 class TestStream < TestCase
7 def setup
8 @io = StringIO.new
9 @stream = Psych::JSON::Stream.new(@io)
10 @stream.start
11 end
12
13 def test_explicit_documents
14 @io = StringIO.new
15 @stream = Psych::JSON::Stream.new(@io)
16 @stream.start
17
18 @stream.push({ 'foo' => 'bar' })
19
20 assert [email protected]?, 'stream not finished'
21 @stream.finish
22 assert @stream.finished?, 'stream finished'
23
24 assert_match(/^---/, @io.string)
25 assert_match(/\.\.\.$/, @io.string)
26 end
27
28 def test_null
29 @stream.push(nil)
30 assert_match(/^--- null/, @io.string)
31 end
32
33 def test_string
34 @stream.push "foo"
35 assert_match(/(["])foo\1/, @io.string)
36 end
37
38 def test_symbol
39 @stream.push :foo
40 assert_match(/(["])foo\1/, @io.string)
41 end
42
43 def test_int
44 @stream.push 10
45 assert_match(/^--- 10/, @io.string)
46 end
47
48 def test_float
49 @stream.push 1.2
50 assert_match(/^--- 1.2/, @io.string)
51 end
52
53 def test_hash
54 hash = { 'one' => 'two' }
55 @stream.push hash
56
57 json = @io.string
58 assert_match(/}$/, json)
59 assert_match(/^--- \{/, json)
60 assert_match(/["]one['"]/, json)
61 assert_match(/["]two['"]/, json)
62 end
63
64 def test_list_to_json
65 list = %w{ one two }
66 @stream.push list
67
68 json = @io.string
69 assert_match(/\]$/, json)
70 assert_match(/^--- \[/, json)
71 assert_match(/["]one["]/, json)
72 assert_match(/["]two["]/, json)
73 end
74
75 class Foo; end
76
77 def test_json_dump_exclude_tag
78 @stream << Foo.new
79 json = @io.string
80 refute_match('Foo', json)
81 end
82
83 class Bar
84 def encode_with coder
85 coder.represent_seq 'omg', %w{ a b c }
86 end
87 end
88
89 def test_json_list_dump_exclude_tag
90 @stream << Bar.new
91 json = @io.string
92 refute_match('omg', json)
93 end
94
95 def test_time
96 time = Time.utc(2010, 10, 10)
97 @stream.push({'a' => time })
98 json = @io.string
99 assert_match "{\"a\": \"2010-10-10 00:00:00.000000000 Z\"}\n", json
100 end
101
102 def test_datetime
103 time = Time.new(2010, 10, 10).to_datetime
104 @stream.push({'a' => time })
105 json = @io.string
106 assert_match "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", json
107 end
108 end
109 end
110 end