~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/psych/test_parser.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require_relative 'helper'
 
1
# coding: utf-8
 
2
 
 
3
require 'psych/helper'
2
4
 
3
5
module Psych
4
6
  class TestParser < TestCase
5
7
    class EventCatcher < Handler
6
 
      attr_reader :calls
 
8
      attr_accessor :parser
 
9
      attr_reader :calls, :marks
7
10
      def initialize
8
 
        @calls = []
 
11
        @parser = nil
 
12
        @calls  = []
 
13
        @marks  = []
9
14
      end
10
15
 
11
16
      (Handler.instance_methods(true) -
13
18
        class_eval %{
14
19
          def #{m} *args
15
20
            super
 
21
            @marks << @parser.mark if @parser
16
22
            @calls << [:#{m}, args]
17
23
          end
18
24
        }
21
27
 
22
28
    def setup
23
29
      super
24
 
      @parser = Psych::Parser.new EventCatcher.new
 
30
      @handler        = EventCatcher.new
 
31
      @parser         = Psych::Parser.new @handler
 
32
      @handler.parser = @parser
 
33
    end
 
34
 
 
35
    def test_line_numbers
 
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],
 
40
                    [0, :start_document],
 
41
                    [1, :start_sequence],
 
42
                    [2, :scalar],
 
43
                    [3, :scalar],
 
44
                    [3, :end_sequence],
 
45
                    [3, :end_document],
 
46
                    [3, :end_stream]], line_calls
 
47
 
 
48
      assert_equal 3, @parser.mark.line
 
49
    end
 
50
 
 
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],
 
56
                    [3, :start_document],
 
57
                    [1, :start_sequence],
 
58
                    [0, :scalar],
 
59
                    [0, :scalar],
 
60
                    [0, :end_sequence],
 
61
                    [0, :end_document],
 
62
                    [0, :end_stream]], col_calls
 
63
 
 
64
      assert_equal 0, @parser.mark.column
 
65
    end
 
66
 
 
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],
 
72
                    [3, :start_document],
 
73
                    [5, :start_sequence],
 
74
                    [12, :scalar],
 
75
                    [19, :scalar],
 
76
                    [19, :end_sequence],
 
77
                    [19, :end_document],
 
78
                    [19, :end_stream]], idx_calls
 
79
 
 
80
      assert_equal 19, @parser.mark.index
 
81
    end
 
82
 
 
83
    def test_set_encoding_twice
 
84
      @parser.external_encoding = Psych::Parser::UTF16LE
 
85
 
 
86
      e = assert_raises(Psych::Exception) do
 
87
        @parser.external_encoding = Psych::Parser::UTF16LE
 
88
      end
 
89
      assert_equal "don't set the encoding twice!", e.message
 
90
    end
 
91
 
 
92
    def test_bom
 
93
      tadpole = 'おたまじゃくし'
 
94
 
 
95
      # BOM + text
 
96
      yml = "\uFEFF#{tadpole}".encode('UTF-16LE')
 
97
      @parser.parse yml
 
98
      assert_equal tadpole, @parser.handler.calls[2][1].first
 
99
    end
 
100
 
 
101
    def test_external_encoding
 
102
      tadpole = 'おたまじゃくし'
 
103
 
 
104
      @parser.external_encoding = Psych::Parser::UTF16LE
 
105
      @parser.parse tadpole.encode 'UTF-16LE'
 
106
      assert_equal tadpole, @parser.handler.calls[2][1].first
25
107
    end
26
108
 
27
109
    def test_bogus_io
46
128
      end
47
129
    end
48
130
 
49
 
    # ruby-core:34690
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/
54
 
#
55
 
---
56
 
#DOUBLE LOW-9 QUOTATION MARK
57
 
#requires fontenc:T1
58
 
ldquor: ,,
59
 
        eoyaml
60
 
      end
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")
 
134
      end
 
135
 
 
136
      assert_raises(Psych::SyntaxError) do
 
137
        @parser.parse("---\n\"foo\"\n\"bar\"\n")
 
138
      end
 
139
    end
 
140
 
 
141
    def test_syntax_error_has_path_for_string
 
142
      e = assert_raises(Psych::SyntaxError) do
 
143
        @parser.parse("---\n\"foo\"\n\"bar\"\n")
 
144
      end
 
145
      assert_match '(<unknown>):', e.message
 
146
    end
 
147
 
 
148
    def test_syntax_error_has_path_for_io
 
149
      io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
 
150
      def io.path; "hello!"; end
 
151
 
 
152
      e = assert_raises(Psych::SyntaxError) do
 
153
        @parser.parse(io)
 
154
      end
 
155
      assert_match "(#{io.path}):", e.message
62
156
    end
63
157
 
64
158
    def test_mapping_end