summaryrefslogtreecommitdiff
path: root/jni/ruby/test/psych/test_tree_builder.rb
blob: 7ad3ddd8a0440804a6ce574a8d31d07eacfb9d66 (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
require_relative 'helper'

module Psych
  class TestTreeBuilder < TestCase
    def setup
      super
      @parser = Psych::Parser.new TreeBuilder.new
      @parser.parse(<<-eoyml)
%YAML 1.1
---
- foo
- {
  bar : &A !!str baz,
  boo : *A
}
- *A
      eoyml
      @tree = @parser.handler.root
    end

    def test_stream
      assert_instance_of Nodes::Stream, @tree
    end

    def test_documents
      assert_equal 1, @tree.children.length
      assert_instance_of Nodes::Document, @tree.children.first
      doc = @tree.children.first

      assert_equal [1,1], doc.version
      assert_equal [], doc.tag_directives
      assert_equal false, doc.implicit
    end

    def test_sequence
      doc = @tree.children.first
      assert_equal 1, doc.children.length

      seq = doc.children.first
      assert_instance_of Nodes::Sequence, seq
      assert_nil seq.anchor
      assert_nil seq.tag
      assert_equal true, seq.implicit
      assert_equal Nodes::Sequence::BLOCK, seq.style
    end

    def test_scalar
      doc = @tree.children.first
      seq = doc.children.first

      assert_equal 3, seq.children.length
      scalar = seq.children.first
      assert_instance_of Nodes::Scalar, scalar
      assert_equal 'foo', scalar.value
      assert_nil scalar.anchor
      assert_nil scalar.tag
      assert_equal true, scalar.plain
      assert_equal false, scalar.quoted
      assert_equal Nodes::Scalar::PLAIN, scalar.style
    end

    def test_mapping
      doc = @tree.children.first
      seq = doc.children.first
      map = seq.children[1]

      assert_instance_of Nodes::Mapping, map
    end

    def test_alias
      doc = @tree.children.first
      seq = doc.children.first
      assert_equal 3, seq.children.length
      al  = seq.children[2]
      assert_instance_of Nodes::Alias, al
      assert_equal 'A', al.anchor
    end
  end
end