Skip to content

Commit 1cc3894

Browse files
authored
Merge pull request #366 from sorryeh/improve_interface_test_coverage
Added tests for special cases and better overall API test coverage
2 parents e01839a + 517c4e2 commit 1cc3894

File tree

3 files changed

+202
-3
lines changed

3 files changed

+202
-3
lines changed

test/psych/test_psych.rb

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ def test_load_argument_error
6060
end
6161
end
6262

63+
def test_parse
64+
assert_equal %w[a b], Psych.parse("- a\n- b").to_ruby
65+
end
66+
67+
def test_parse_default_fallback
68+
assert_equal false, Psych.parse("")
69+
end
70+
71+
def test_parse_raises_on_bad_input
72+
assert_raises(Psych::SyntaxError) { Psych.parse("--- `") }
73+
end
74+
75+
def test_parse_with_fallback
76+
assert_equal 42, Psych.parse("", fallback: 42)
77+
end
78+
6379
def test_non_existing_class_on_deserialize
6480
e = assert_raises(ArgumentError) do
6581
Psych.load("--- !ruby/object:NonExistent\nfoo: 1")
@@ -103,9 +119,44 @@ def test_load_stream
103119
assert_equal %w{ foo bar }, docs
104120
end
105121

122+
def test_load_stream_default_fallback
123+
assert_equal [], Psych.load_stream("")
124+
end
125+
126+
def test_load_stream_raises_on_bad_input
127+
assert_raises(Psych::SyntaxError) { Psych.load_stream("--- `") }
128+
end
129+
106130
def test_parse_stream
107131
docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
108-
assert_equal %w{ foo bar }, docs.children.map { |x| x.transform }
132+
assert_equal(%w[foo bar], docs.children.map(&:transform))
133+
end
134+
135+
def test_parse_stream_with_block
136+
docs = []
137+
Psych.parse_stream("--- foo\n...\n--- bar\n...") do |node|
138+
docs << node
139+
end
140+
141+
assert_equal %w[foo bar], docs.map(&:to_ruby)
142+
end
143+
144+
def test_parse_stream_default_fallback
145+
docs = Psych.parse_stream("")
146+
assert_equal [], docs.children.map(&:to_ruby)
147+
end
148+
149+
def test_parse_stream_with_block_default_fallback
150+
docs = []
151+
Psych.parse_stream("") do |node|
152+
docs << node
153+
end
154+
155+
assert_equal [], docs.map(&:to_ruby)
156+
end
157+
158+
def test_parse_stream_raises_on_bad_input
159+
assert_raises(Psych::SyntaxError) { Psych.parse_stream("--- `") }
109160
end
110161

111162
def test_add_builtin_type
@@ -135,7 +186,7 @@ def test_domain_types
135186
assert_equal({ 'hello' => 'world' }, got)
136187
end
137188

138-
def test_load_default_return_value
189+
def test_load_default_fallback
139190
assert_equal false, Psych.load("")
140191
end
141192

@@ -169,7 +220,7 @@ def test_load_file
169220
}
170221
end
171222

172-
def test_load_file_default_return_value
223+
def test_load_file_default_fallback
173224
Tempfile.create(['empty', 'yml']) {|t|
174225
assert_equal false, Psych.load_file(t.path)
175226
}
@@ -221,6 +272,12 @@ def test_parse_file
221272
}
222273
end
223274

275+
def test_parse_file_default_fallback
276+
Tempfile.create(['empty', 'yml']) do |t|
277+
assert_equal false, Psych.parse_file(t.path)
278+
end
279+
end
280+
224281
def test_degenerate_strings
225282
assert_equal false, Psych.load(' ')
226283
assert_equal false, Psych.parse(' ')

test/psych/test_safe_load.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ def test_anon_struct
8484
end
8585
end
8686

87+
def test_safe_load_default_fallback
88+
assert_nil Psych.safe_load("")
89+
end
90+
91+
def test_safe_load
92+
assert_equal %w[a b], Psych.safe_load("- a\n- b")
93+
end
94+
95+
def test_safe_load_raises_on_bad_input
96+
assert_raises(Psych::SyntaxError) { Psych.safe_load("--- `") }
97+
end
98+
8799
private
88100

89101
def cycle object, whitelist = []
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'helper'
4+
5+
require 'stringio'
6+
require 'tempfile'
7+
8+
module Psych
9+
class TestYamlSpecialCases < TestCase
10+
def setup
11+
super
12+
end
13+
14+
def test_empty_string
15+
s = ""
16+
assert_equal false, Psych.load(s)
17+
assert_equal [], Psych.load_stream(s)
18+
assert_equal false, Psych.parse(s)
19+
assert_equal [], Psych.parse_stream(s).transform
20+
assert_equal nil, Psych.safe_load(s)
21+
end
22+
23+
def test_false
24+
s = "false"
25+
assert_equal false, Psych.load(s)
26+
assert_equal [false], Psych.load_stream(s)
27+
assert_equal false, Psych.parse(s).transform
28+
assert_equal [false], Psych.parse_stream(s).transform
29+
assert_equal false, Psych.safe_load(s)
30+
end
31+
32+
def test_n
33+
s = "n"
34+
assert_equal "n", Psych.load(s)
35+
assert_equal ["n"], Psych.load_stream(s)
36+
assert_equal "n", Psych.parse(s).transform
37+
assert_equal ["n"], Psych.parse_stream(s).transform
38+
assert_equal "n", Psych.safe_load(s)
39+
end
40+
41+
def test_off
42+
s = "off"
43+
assert_equal false, Psych.load(s)
44+
assert_equal [false], Psych.load_stream(s)
45+
assert_equal false, Psych.parse(s).transform
46+
assert_equal [false], Psych.parse_stream(s).transform
47+
assert_equal false, Psych.safe_load(s)
48+
end
49+
50+
def test_inf
51+
s = "-.inf"
52+
assert_equal -Float::INFINITY, Psych.load(s)
53+
assert_equal [-Float::INFINITY], Psych.load_stream(s)
54+
assert_equal -Float::INFINITY, Psych.parse(s).transform
55+
assert_equal [-Float::INFINITY], Psych.parse_stream(s).transform
56+
assert_equal -Float::INFINITY, Psych.safe_load(s)
57+
end
58+
59+
def test_NaN
60+
s = ".NaN"
61+
assert Float::NAN, Psych.load(s).nan?
62+
assert [Float::NAN], Psych.load_stream(s).first.nan?
63+
assert Psych.parse(s).transform.nan?
64+
assert Psych.parse_stream(s).transform.first.nan?
65+
assert Psych.safe_load(s).nan?
66+
end
67+
68+
def test_0xC
69+
s = "0xC"
70+
assert_equal 12, Psych.load(s)
71+
assert_equal [12], Psych.load_stream(s)
72+
assert_equal 12, Psych.parse(s).transform
73+
assert_equal [12], Psych.parse_stream(s).transform
74+
assert_equal 12, Psych.safe_load(s)
75+
end
76+
77+
def test_arrows
78+
s = "<<"
79+
assert_equal "<<", Psych.load(s)
80+
assert_equal ["<<"], Psych.load_stream(s)
81+
assert_equal "<<", Psych.parse(s).transform
82+
assert_equal ["<<"], Psych.parse_stream(s).transform
83+
assert_equal "<<", Psych.safe_load(s)
84+
end
85+
86+
def test_arrows_hash
87+
s = "<<: {}"
88+
assert_equal({}, Psych.load(s))
89+
assert_equal [{}], Psych.load_stream(s)
90+
assert_equal({}, Psych.parse(s).transform)
91+
assert_equal [{}], Psych.parse_stream(s).transform
92+
assert_equal({}, Psych.safe_load(s))
93+
end
94+
95+
def test_thousand
96+
s = "- 1000\n- +1000\n- 1_000"
97+
assert_equal [1000, 1000, 1000], Psych.load(s)
98+
assert_equal [[1000, 1000, 1000]], Psych.load_stream(s)
99+
assert_equal [1000, 1000, 1000], Psych.parse(s).transform
100+
assert_equal [[1000, 1000, 1000]], Psych.parse_stream(s).transform
101+
assert_equal [1000, 1000, 1000], Psych.safe_load(s)
102+
end
103+
104+
def test_8
105+
s = "[8, 08, 0o10, 010]"
106+
assert_equal [8, "08", "0o10", 8], Psych.load(s)
107+
assert_equal [[8, "08", "0o10", 8]], Psych.load_stream(s)
108+
assert_equal [8, "08", "0o10", 8], Psych.parse(s).transform
109+
assert_equal [[8, "08", "0o10", 8]], Psych.parse_stream(s).transform
110+
assert_equal [8, "08", "0o10", 8], Psych.safe_load(s)
111+
end
112+
113+
def test_null
114+
s = "null"
115+
assert_equal nil, Psych.load(s)
116+
assert_equal [nil], Psych.load_stream(s)
117+
assert_equal nil, Psych.parse(s).transform
118+
assert_equal [nil], Psych.parse_stream(s).transform
119+
assert_equal nil, Psych.safe_load(s)
120+
end
121+
122+
private
123+
124+
def special_case_cycle(object)
125+
%w[load load_stream parse parse_stream safe_load].map do |m|
126+
Psych.public_send(m, object)
127+
end
128+
end
129+
end
130+
end

0 commit comments

Comments
 (0)