1require 'psych/helper' 2 3module Psych 4 module Visitors 5 class TestEmitter < TestCase 6 def setup 7 super 8 @io = StringIO.new 9 @visitor = Visitors::Emitter.new @io 10 end 11 12 def test_options 13 io = StringIO.new 14 visitor = Visitors::Emitter.new io, :indentation => 3 15 16 s = Nodes::Stream.new 17 doc = Nodes::Document.new 18 mapping = Nodes::Mapping.new 19 m2 = Nodes::Mapping.new 20 m2.children << Nodes::Scalar.new('a') 21 m2.children << Nodes::Scalar.new('b') 22 23 mapping.children << Nodes::Scalar.new('key') 24 mapping.children << m2 25 doc.children << mapping 26 s.children << doc 27 28 visitor.accept s 29 assert_match(/^[ ]{3}a/, io.string) 30 end 31 32 def test_stream 33 s = Nodes::Stream.new 34 @visitor.accept s 35 assert_equal '', @io.string 36 end 37 38 def test_document 39 s = Nodes::Stream.new 40 doc = Nodes::Document.new [1,1] 41 scalar = Nodes::Scalar.new 'hello world' 42 43 doc.children << scalar 44 s.children << doc 45 46 @visitor.accept s 47 48 assert_match(/1.1/, @io.string) 49 assert_equal @io.string, s.yaml 50 end 51 52 def test_document_implicit_end 53 s = Nodes::Stream.new 54 doc = Nodes::Document.new 55 mapping = Nodes::Mapping.new 56 mapping.children << Nodes::Scalar.new('key') 57 mapping.children << Nodes::Scalar.new('value') 58 doc.children << mapping 59 s.children << doc 60 61 @visitor.accept s 62 63 assert_match(/key: value/, @io.string) 64 assert_equal @io.string, s.yaml 65 assert(/\.\.\./ !~ s.yaml) 66 end 67 68 def test_scalar 69 s = Nodes::Stream.new 70 doc = Nodes::Document.new 71 scalar = Nodes::Scalar.new 'hello world' 72 73 doc.children << scalar 74 s.children << doc 75 76 @visitor.accept s 77 78 assert_match(/hello/, @io.string) 79 assert_equal @io.string, s.yaml 80 end 81 82 def test_scalar_with_tag 83 s = Nodes::Stream.new 84 doc = Nodes::Document.new 85 scalar = Nodes::Scalar.new 'hello world', nil, '!str', false, false, 5 86 87 doc.children << scalar 88 s.children << doc 89 90 @visitor.accept s 91 92 assert_match(/str/, @io.string) 93 assert_match(/hello/, @io.string) 94 assert_equal @io.string, s.yaml 95 end 96 97 def test_sequence 98 s = Nodes::Stream.new 99 doc = Nodes::Document.new 100 scalar = Nodes::Scalar.new 'hello world' 101 seq = Nodes::Sequence.new 102 103 seq.children << scalar 104 doc.children << seq 105 s.children << doc 106 107 @visitor.accept s 108 109 assert_match(/- hello/, @io.string) 110 assert_equal @io.string, s.yaml 111 end 112 113 def test_mapping 114 s = Nodes::Stream.new 115 doc = Nodes::Document.new 116 mapping = Nodes::Mapping.new 117 mapping.children << Nodes::Scalar.new('key') 118 mapping.children << Nodes::Scalar.new('value') 119 doc.children << mapping 120 s.children << doc 121 122 @visitor.accept s 123 124 assert_match(/key: value/, @io.string) 125 assert_equal @io.string, s.yaml 126 end 127 128 def test_alias 129 s = Nodes::Stream.new 130 doc = Nodes::Document.new 131 mapping = Nodes::Mapping.new 132 mapping.children << Nodes::Scalar.new('key', 'A') 133 mapping.children << Nodes::Alias.new('A') 134 doc.children << mapping 135 s.children << doc 136 137 @visitor.accept s 138 139 assert_match(/&A key: \*A/, @io.string) 140 assert_equal @io.string, s.yaml 141 end 142 end 143 end 144end 145