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
|
# frozen_string_literal: true
require 'psych/helper'
module Psych
module Visitors
class TestEmitter < TestCase
def setup
super
@io = StringIO.new
@visitor = Visitors::Emitter.new @io
end
def test_options
io = StringIO.new
visitor = Visitors::Emitter.new io, :indentation => 3
s = Nodes::Stream.new
doc = Nodes::Document.new
mapping = Nodes::Mapping.new
m2 = Nodes::Mapping.new
m2.children << Nodes::Scalar.new('a')
m2.children << Nodes::Scalar.new('b')
mapping.children << Nodes::Scalar.new('key')
mapping.children << m2
doc.children << mapping
s.children << doc
visitor.accept s
assert_match(/^[ ]{3}a/, io.string)
end
def test_stream
s = Nodes::Stream.new
@visitor.accept s
assert_equal '', @io.string
end
def test_document
s = Nodes::Stream.new
doc = Nodes::Document.new [1,1]
scalar = Nodes::Scalar.new 'hello world'
doc.children << scalar
s.children << doc
@visitor.accept s
assert_match(/1.1/, @io.string)
assert_equal @io.string, s.yaml
end
def test_document_implicit_end
s = Nodes::Stream.new
doc = Nodes::Document.new
mapping = Nodes::Mapping.new
mapping.children << Nodes::Scalar.new('key')
mapping.children << Nodes::Scalar.new('value')
doc.children << mapping
s.children << doc
@visitor.accept s
assert_include(@io.string, "key: value")
assert_equal @io.string, s.yaml
assert_not_include(s.yaml, "...")
end
def test_scalar
s = Nodes::Stream.new
doc = Nodes::Document.new
scalar = Nodes::Scalar.new 'hello world'
doc.children << scalar
s.children << doc
@visitor.accept s
assert_include(@io.string, "hello")
assert_equal @io.string, s.yaml
end
def test_scalar_with_tag
s = Nodes::Stream.new
doc = Nodes::Document.new
scalar = Nodes::Scalar.new 'hello world', nil, '!str', false, false, 5
doc.children << scalar
s.children << doc
@visitor.accept s
assert_include(@io.string, "str")
assert_include(@io.string, "hello")
assert_equal @io.string, s.yaml
end
def test_sequence
s = Nodes::Stream.new
doc = Nodes::Document.new
scalar = Nodes::Scalar.new 'hello world'
seq = Nodes::Sequence.new
seq.children << scalar
doc.children << seq
s.children << doc
@visitor.accept s
assert_include(@io.string, "- hello")
assert_equal @io.string, s.yaml
end
def test_mapping
s = Nodes::Stream.new
doc = Nodes::Document.new
mapping = Nodes::Mapping.new
mapping.children << Nodes::Scalar.new('key')
mapping.children << Nodes::Scalar.new('value')
doc.children << mapping
s.children << doc
@visitor.accept s
assert_include(@io.string, "key: value")
assert_equal @io.string, s.yaml
end
def test_alias
s = Nodes::Stream.new
doc = Nodes::Document.new
mapping = Nodes::Mapping.new
mapping.children << Nodes::Scalar.new('key', 'A')
mapping.children << Nodes::Alias.new('A')
doc.children << mapping
s.children << doc
@visitor.accept s
assert_include(@io.string, "&A key: \*A")
assert_equal @io.string, s.yaml
end
end
end
end
|