1
require_relative 'helper'
4
6
class TestParser < TestCase
5
7
class EventCatcher < Handler
9
attr_reader :calls, :marks
11
16
(Handler.instance_methods(true) -
24
@parser = Psych::Parser.new EventCatcher.new
30
@handler = EventCatcher.new
31
@parser = Psych::Parser.new @handler
32
@handler.parser = @parser
36
assert_equal 0, @parser.mark.line
37
@parser.parse "---\n- hello\n- world"
38
line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first))
39
assert_equal [[0, :start_stream],
46
[3, :end_stream]], line_calls
48
assert_equal 3, @parser.mark.line
51
def test_column_numbers
52
assert_equal 0, @parser.mark.column
53
@parser.parse "---\n- hello\n- world"
54
col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first))
55
assert_equal [[0, :start_stream],
62
[0, :end_stream]], col_calls
64
assert_equal 0, @parser.mark.column
67
def test_index_numbers
68
assert_equal 0, @parser.mark.index
69
@parser.parse "---\n- hello\n- world"
70
idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first))
71
assert_equal [[0, :start_stream],
78
[19, :end_stream]], idx_calls
80
assert_equal 19, @parser.mark.index
83
def test_set_encoding_twice
84
@parser.external_encoding = Psych::Parser::UTF16LE
86
e = assert_raises(Psych::Exception) do
87
@parser.external_encoding = Psych::Parser::UTF16LE
89
assert_equal "don't set the encoding twice!", e.message
96
yml = "\uFEFF#{tadpole}".encode('UTF-16LE')
98
assert_equal tadpole, @parser.handler.calls[2][1].first
101
def test_external_encoding
104
@parser.external_encoding = Psych::Parser::UTF16LE
105
@parser.parse tadpole.encode 'UTF-16LE'
106
assert_equal tadpole, @parser.handler.calls[2][1].first
50
def test_exception_line
51
e = assert_raises(Psych::SyntaxError) do
52
@parser.parse(<<-eoyaml)
53
# based on "SGML/XML character entity reference" at http://www.bitjungle.com/isoent/
56
#DOUBLE LOW-9 QUOTATION MARK
61
assert_match 'line 6', e.message
131
def test_syntax_error_twice
132
assert_raises(Psych::SyntaxError) do
133
@parser.parse("---\n\"foo\"\n\"bar\"\n")
136
assert_raises(Psych::SyntaxError) do
137
@parser.parse("---\n\"foo\"\n\"bar\"\n")
141
def test_syntax_error_has_path_for_string
142
e = assert_raises(Psych::SyntaxError) do
143
@parser.parse("---\n\"foo\"\n\"bar\"\n")
145
assert_match '(<unknown>):', e.message
148
def test_syntax_error_has_path_for_io
149
io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
150
def io.path; "hello!"; end
152
e = assert_raises(Psych::SyntaxError) do
155
assert_match "(#{io.path}):", e.message
64
158
def test_mapping_end