summaryrefslogtreecommitdiff
path: root/test/visitors/test_yast_builder.rb
blob: 36feb2197a3a023b0d52dabb6444d964ff7b3f5f (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
require 'minitest/autorun'
require 'psych'

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

        assert_equal 'foo', Psych.load(@v.tree.to_yaml)
        assert_equal 'foo', Psych.load('foo'.to_yaml)
      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 })
      end

      def test_nil
        assert_round_trip nil
      end

      def assert_round_trip obj
        @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