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
|
# frozen_string_literal: false
require_relative 'helper'
module Psych
class TestDeprecated < TestCase
def teardown
$VERBOSE = @orig_verbose
Psych.domain_types.clear
end
class QuickEmitter; end
def setup
@orig_verbose, $VERBOSE = $VERBOSE, false
end
class QuickEmitterEncodeWith
attr_reader :name
attr_reader :value
def initialize
@name = 'hello!!'
@value = 'Friday!'
end
def encode_with coder
coder.map do |map|
map.add 'name', @name
map.add 'value', nil
end
end
def to_yaml opts = {}
raise
end
end
###
# An object that defines both to_yaml and encode_with should only call
# encode_with.
def test_recursive_quick_emit_encode_with
qeew = QuickEmitterEncodeWith.new
hash = { :qe => qeew }
hash2 = Psych.load Psych.dump hash
qe = hash2[:qe]
assert_equal qeew.name, qe.name
assert_instance_of QuickEmitterEncodeWith, qe
assert_nil qe.value
end
class YamlInit
attr_reader :name
attr_reader :value
def initialize
@name = 'hello!!'
@value = 'Friday!'
end
def yaml_initialize tag, vals
vals.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
end
end
def test_yaml_initialize
hash = { :yi => YamlInit.new }
hash2 = Psych.load Psych.dump hash
yi = hash2[:yi]
assert_equal 'TGIF!', yi.name
assert_equal 'TGIF!', yi.value
assert_instance_of YamlInit, yi
end
class YamlInitAndInitWith
attr_reader :name
attr_reader :value
def initialize
@name = 'shaners'
@value = 'Friday!'
end
def init_with coder
coder.map.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
end
def yaml_initialize tag, vals
raise
end
end
###
# An object that implements both yaml_initialize and init_with should not
# receive the yaml_initialize call.
def test_yaml_initialize_and_init_with
hash = { :yi => YamlInitAndInitWith.new }
hash2 = Psych.load Psych.dump hash
yi = hash2[:yi]
assert_equal 'TGIF!', yi.name
assert_equal 'TGIF!', yi.value
assert_instance_of YamlInitAndInitWith, yi
end
def test_coder_scalar
coder = Psych::Coder.new 'foo'
coder.scalar('tag', 'some string', :plain)
assert_equal 'tag', coder.tag
assert_equal 'some string', coder.scalar
assert_equal :scalar, coder.type
end
class YamlAs
TestCase.suppress_warning do
yaml_as 'helloworld'
end
end
def test_yaml_as
assert_match(/helloworld/, Psych.dump(YamlAs.new))
end
end
end
|