summaryrefslogtreecommitdiff
path: root/test/visitors/test_yast_builder.rb
blob: 532ab09bf9e058e9849d012198e5db5e73d60cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'minitest/autorun'
require 'psych'

module Psych
  module Visitors
    class TestYASTBuilder < MiniTest::Unit::TestCase
      def setup
        @v = Visitors::YASTBuilder.new
      end

      def test_struct_const
        foo = Struct.new("Foo", :bar)
        assert_round_trip foo.new('bar')
      end

      A = Struct.new(:foo)

      def test_struct
        assert_round_trip A.new('bar')
      end

      def test_struct_anon
        s = Struct.new(:foo).new('bar')
        obj =  Psych.load(Psych.dump(s))
        assert_equal s.foo, obj.foo
      end

      def test_exception
        ex = Exception.new 'foo'
        loaded = Psych.load(Psych.dump(ex))

        assert_equal ex.message, loaded.message
        assert_equal ex.class, loaded.class
      end

      def test_regexp
        assert_round_trip(/foo/)
        assert_round_trip(/foo/i)
        assert_round_trip(/foo/mx)
      end

      def test_time
        assert_round_trip Time.now
      end

      def test_date
        date = Date.strptime('2002-12-14', '%Y-%m-%d')
        assert_round_trip date
      end

      def test_rational
        assert_round_trip Rational(1,2)
      end

      def test_complex
        assert_round_trip Complex(1,2)
      end

      def test_circular_list
        a = []
        2.times { a << a }
        assert_equal a.inspect, Psych.load(a.to_yaml).inspect
      end

      def test_circular_map
        a = {}
        a[a] = a
        assert_equal a.inspect, Psych.load(a.to_yaml).inspect
      end

      def test_scalar
        assert_round_trip 'foo'
        assert_round_trip ':foo'
        assert_round_trip ''
        assert_round_trip ':'
      end

      def test_boolean
        assert_round_trip true
        assert_round_trip 'true'
        assert_round_trip false
        assert_round_trip 'false'
      end

      def test_range_inclusive
        assert_round_trip 1..2
      end

      def test_range_exclusive
        assert_round_trip 1...2
      end

      def test_binary
        string = [0, 123,22, 44, 9, 32, 34, 39].pack('C*')
        assert_equal string, Psych.load(string.to_yaml)
      end

      def test_anon_class
        assert_raises(TypeError) do
          @v.accept Class.new
        end

        assert_raises(TypeError) do
          Class.new.to_yaml
        end
      end

      def test_hash
        assert_round_trip('a' => 'b')
      end

      def test_list
        assert_round_trip(%w{ a b })
        assert_round_trip([1, 2.2])
      end

      def test_symbol
        assert_round_trip :foo
      end

      def test_int
        assert_round_trip 1
        assert_round_trip(-1)
        assert_round_trip '1'
        assert_round_trip '-1'
      end

      def test_float
        assert_round_trip 1.2
        assert_round_trip '1.2'

        assert Psych.load(Psych.dump(0.0 / 0.0)).nan?
        assert_equal 1, Psych.load(Psych.dump(1 / 0.0)).infinite?
        assert_equal(-1, Psych.load(Psych.dump(-1 / 0.0)).infinite?)
      end

      # http://yaml.org/type/null.html
      def test_nil
        assert_round_trip nil
        assert_equal nil, Psych.load('null')
        assert_equal nil, Psych.load('Null')
        assert_equal nil, Psych.load('NULL')
        assert_equal nil, Psych.load('~')
        assert_equal({'foo' => nil}, Psych.load('foo: '))

        assert_round_trip 'null'
        assert_round_trip 'nUll'
        assert_round_trip '~'
      end

      def assert_round_trip obj
        v = Visitors::YASTBuilder.new
        v.accept(obj)
        assert_equal(obj, Psych.load(v.tree.to_yaml))
        assert_equal(obj, Psych.load(obj.to_yaml))
        assert_equal(obj, Psych.load(Psych.dump(obj)))
      end
    end
  end
end