Module: ResolverSpecs

Defined in:
lib/puppet/vendor/safe_yaml/spec/resolver_specs.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/puppet/vendor/safe_yaml/spec/resolver_specs.rb', line 2

def self.included(base)
  base.module_eval do
    let(:resolver) { nil }
    let(:result) { @result }

    def parse(yaml)
      tree = YAML.parse(yaml.unindent)
      @result = resolver.resolve_node(tree)
    end

    # Isn't this how I should've been doing it all along?
    def parse_and_test(yaml)
      parse(yaml)
      @result.should == YAML.unsafe_load(yaml)
    end

    context "by default" do
      it "translates maps to hashes" do
        parse "          potayto: potahto\n          tomayto: tomahto\n        YAML\n\n        result.should == {\n          \"potayto\" => \"potahto\",\n          \"tomayto\" => \"tomahto\"\n        }\n      end\n\n      it \"translates sequences to arrays\" do\n        parse <<-YAML\n          - foo\n          - bar\n          - baz\n        YAML\n\n        result.should == [\"foo\", \"bar\", \"baz\"]\n      end\n\n      it \"translates most values to strings\" do\n        parse \"string: value\"\n        result.should == { \"string\" => \"value\" }\n      end\n\n      it \"does not deserialize symbols\" do\n        parse \":symbol: value\"\n        result.should == { \":symbol\" => \"value\" }\n      end\n\n      it \"translates valid integral numbers to integers\" do\n        parse \"integer: 1\"\n        result.should == { \"integer\" => 1 }\n      end\n\n      it \"translates valid decimal numbers to floats\" do\n        parse \"float: 3.14\"\n        result.should == { \"float\" => 3.14 }\n      end\n\n      it \"translates valid dates\" do\n        parse \"date: 2013-01-24\"\n        result.should == { \"date\" => Date.parse(\"2013-01-24\") }\n      end\n\n      it \"translates valid true/false values to booleans\" do\n        parse <<-YAML\n          - yes\n          - true\n          - no\n          - false\n        YAML\n\n        result.should == [true, true, false, false]\n      end\n\n      it \"translates valid nulls to nil\" do\n        parse <<-YAML\n          - \n          - ~\n          - null\n        YAML\n\n        result.should == [nil] * 3\n      end\n\n      it \"matches the behavior of the underlying YAML engine w/ respect to capitalization of boolean values\" do\n        parse_and_test <<-YAML\n          - true\n          - True\n          - TRUE\n          - tRue\n          - TRue\n          - False\n          - FALSE\n          - fAlse\n          - FALse\n        YAML\n\n        # using Syck: [true, true, true, \"tRue\", \"TRue\", false, false, \"fAlse\", \"FALse\"]\n        # using Psych: all booleans\n      end\n\n      it \"matches the behavior of the underlying YAML engine w/ respect to capitalization of nil values\" do\n        parse_and_test <<-YAML\n          - Null\n          - NULL\n          - nUll\n          - NUll\n        YAML\n\n        # using Syck: [nil, nil, \"nUll\", \"NUll\"]\n        # using Psych: all nils\n      end\n\n      it \"translates quoted empty strings to strings (not nil)\" do\n        parse \"foo: ''\"\n        result.should == { \"foo\" => \"\" }\n      end\n\n      it \"correctly reverse-translates strings encoded via #to_yaml\" do\n        parse \"5.10\".to_yaml\n        result.should == \"5.10\"\n      end\n\n      it \"does not specially parse any double-quoted strings\" do\n        parse <<-YAML\n          - \"1\"\n          - \"3.14\"\n          - \"true\"\n          - \"false\"\n          - \"2013-02-03\"\n          - \"2013-02-03 16:27:00 -0600\"\n        YAML\n\n        result.should == [\"1\", \"3.14\", \"true\", \"false\", \"2013-02-03\", \"2013-02-03 16:27:00 -0600\"]\n      end\n\n      it \"does not specially parse any single-quoted strings\" do\n        parse <<-YAML\n          - '1'\n          - '3.14'\n          - 'true'\n          - 'false'\n          - '2013-02-03'\n          - '2013-02-03 16:27:00 -0600'\n        YAML\n\n        result.should == [\"1\", \"3.14\", \"true\", \"false\", \"2013-02-03\", \"2013-02-03 16:27:00 -0600\"]\n      end\n\n      it \"deals just fine with nested maps\" do\n        parse <<-YAML\n          foo:\n            bar:\n              marco: polo\n        YAML\n\n        result.should == { \"foo\" => { \"bar\" => { \"marco\" => \"polo\" } } }\n      end\n\n      it \"deals just fine with nested sequences\" do\n        parse <<-YAML\n          - foo\n          -\n            - bar1\n            - bar2\n            -\n              - baz1\n              - baz2\n        YAML\n\n        result.should == [\"foo\", [\"bar1\", \"bar2\", [\"baz1\", \"baz2\"]]]\n      end\n\n      it \"applies the same transformations to keys as to values\" do\n        parse <<-YAML\n          foo: string\n          :bar: symbol\n          1: integer\n          3.14: float\n          2013-01-24: date\n        YAML\n\n        result.should == {\n          \"foo\"  => \"string\",\n          \":bar\" => \"symbol\",\n          1      => \"integer\",\n          3.14   => \"float\",\n          Date.parse(\"2013-01-24\") => \"date\",\n        }\n      end\n\n      it \"applies the same transformations to elements in sequences as to all values\" do\n        parse <<-YAML\n          - foo\n          - :bar\n          - 1\n          - 3.14\n          - 2013-01-24\n        YAML\n\n        result.should == [\"foo\", \":bar\", 1, 3.14, Date.parse(\"2013-01-24\")]\n      end\n    end\n\n    context \"for Ruby version \#{RUBY_VERSION}\" do\n      it \"translates valid time values\" do\n        parse \"time: 2013-01-29 05:58:00 -0800\"\n        result.should == { \"time\" => Time.utc(2013, 1, 29, 13, 58, 0) }\n      end\n\n      it \"applies the same transformation to elements in sequences\" do\n        parse \"- 2013-01-29 05:58:00 -0800\"\n        result.should == [Time.utc(2013, 1, 29, 13, 58, 0)]\n      end\n\n      it \"applies the same transformation to keys\" do\n        parse \"2013-01-29 05:58:00 -0800: time\"\n        result.should == { Time.utc(2013, 1, 29, 13, 58, 0) => \"time\" }\n      end\n    end\n\n    context \"with symbol deserialization enabled\" do\n      before :each do\n        SafeYAML::OPTIONS[:deserialize_symbols] = true\n      end\n\n      after :each do\n        SafeYAML.restore_defaults!\n      end\n\n      it \"translates values starting with ':' to symbols\" do\n        parse \"symbol: :value\"\n        result.should == { \"symbol\" => :value }\n      end\n\n      it \"applies the same transformation to keys\" do\n        parse \":bar: symbol\"\n        result.should == { :bar  => \"symbol\" }\n      end\n\n      it \"applies the same transformation to elements in sequences\" do\n        parse \"- :bar\"\n        result.should == [:bar]\n      end\n    end\n  end\nend\n"