km-psych 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/README.rdoc +129 -0
  2. data/ext/psych/emitter.c +488 -0
  3. data/ext/psych/emitter.h +8 -0
  4. data/ext/psych/extconf.rb +22 -0
  5. data/ext/psych/parser.c +349 -0
  6. data/ext/psych/parser.h +6 -0
  7. data/ext/psych/psych.c +34 -0
  8. data/ext/psych/psych.h +20 -0
  9. data/ext/psych/to_ruby.c +41 -0
  10. data/ext/psych/to_ruby.h +8 -0
  11. data/ext/psych/yaml_tree.c +24 -0
  12. data/ext/psych/yaml_tree.h +8 -0
  13. data/lib/km-psych.rb +244 -0
  14. data/lib/psych/coder.rb +86 -0
  15. data/lib/psych/core_ext.rb +38 -0
  16. data/lib/psych/deprecated.rb +82 -0
  17. data/lib/psych/handler.rb +221 -0
  18. data/lib/psych/json.rb +6 -0
  19. data/lib/psych/json/stream.rb +32 -0
  20. data/lib/psych/json/tree_builder.rb +32 -0
  21. data/lib/psych/nodes.rb +77 -0
  22. data/lib/psych/nodes/alias.rb +18 -0
  23. data/lib/psych/nodes/document.rb +60 -0
  24. data/lib/psych/nodes/mapping.rb +56 -0
  25. data/lib/psych/nodes/node.rb +42 -0
  26. data/lib/psych/nodes/scalar.rb +67 -0
  27. data/lib/psych/nodes/sequence.rb +81 -0
  28. data/lib/psych/nodes/stream.rb +37 -0
  29. data/lib/psych/omap.rb +4 -0
  30. data/lib/psych/parser.rb +44 -0
  31. data/lib/psych/scalar_scanner.rb +105 -0
  32. data/lib/psych/set.rb +4 -0
  33. data/lib/psych/stream.rb +53 -0
  34. data/lib/psych/tree_builder.rb +94 -0
  35. data/lib/psych/visitors.rb +5 -0
  36. data/lib/psych/visitors/emitter.rb +41 -0
  37. data/lib/psych/visitors/json_tree.rb +14 -0
  38. data/lib/psych/visitors/to_ruby.rb +263 -0
  39. data/lib/psych/visitors/visitor.rb +27 -0
  40. data/lib/psych/visitors/yaml_tree.rb +342 -0
  41. data/test/psych/helper.rb +63 -0
  42. data/test/psych/json/test_stream.rb +75 -0
  43. data/test/psych/test_alias_and_anchor.rb +26 -0
  44. data/test/psych/test_array.rb +19 -0
  45. data/test/psych/test_boolean.rb +36 -0
  46. data/test/psych/test_class.rb +17 -0
  47. data/test/psych/test_coder.rb +169 -0
  48. data/test/psych/test_date_time.rb +17 -0
  49. data/test/psych/test_deprecated.rb +210 -0
  50. data/test/psych/test_document.rb +46 -0
  51. data/test/psych/test_emitter.rb +88 -0
  52. data/test/psych/test_encoding.rb +179 -0
  53. data/test/psych/test_engine_manager.rb +57 -0
  54. data/test/psych/test_exception.rb +39 -0
  55. data/test/psych/test_hash.rb +30 -0
  56. data/test/psych/test_json_tree.rb +43 -0
  57. data/test/psych/test_null.rb +19 -0
  58. data/test/psych/test_object.rb +27 -0
  59. data/test/psych/test_omap.rb +68 -0
  60. data/test/psych/test_parser.rb +216 -0
  61. data/test/psych/test_psych.rb +133 -0
  62. data/test/psych/test_scalar.rb +11 -0
  63. data/test/psych/test_scalar_scanner.rb +70 -0
  64. data/test/psych/test_serialize_subclasses.rb +38 -0
  65. data/test/psych/test_set.rb +49 -0
  66. data/test/psych/test_stream.rb +49 -0
  67. data/test/psych/test_string.rb +49 -0
  68. data/test/psych/test_struct.rb +51 -0
  69. data/test/psych/test_symbol.rb +17 -0
  70. data/test/psych/test_to_yaml_properties.rb +63 -0
  71. data/test/psych/test_tree_builder.rb +79 -0
  72. data/test/psych/test_yaml.rb +1251 -0
  73. data/test/psych/visitors/test_emitter.rb +124 -0
  74. data/test/psych/visitors/test_to_ruby.rb +325 -0
  75. data/test/psych/visitors/test_yaml_tree.rb +149 -0
  76. metadata +187 -0
@@ -0,0 +1,19 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ ###
5
+ # Test null from YAML spec:
6
+ # http://yaml.org/type/null.html
7
+ class TestNull < TestCase
8
+ def test_null_list
9
+ assert_equal [nil] * 5, Psych.load(<<-eoyml)
10
+ ---
11
+ - ~
12
+ - null
13
+ -
14
+ - Null
15
+ - NULL
16
+ eoyml
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class Tagged
5
+ yaml_tag '!foo'
6
+
7
+ attr_accessor :baz
8
+
9
+ def initialize
10
+ @baz = 'bar'
11
+ end
12
+ end
13
+
14
+ class TestObject < TestCase
15
+ def test_dump_with_tag
16
+ tag = Tagged.new
17
+ assert_match('foo', Psych.dump(tag))
18
+ end
19
+
20
+ def test_tag_round_trip
21
+ tag = Tagged.new
22
+ tag2 = Psych.load(Psych.dump(tag))
23
+ assert_equal tag.baz, tag2.baz
24
+ assert_instance_of(Tagged, tag2)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,68 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestOmap < TestCase
5
+ def test_self_referential
6
+ map = Psych::Omap.new
7
+ map['foo'] = 'bar'
8
+ map['self'] = map
9
+ assert_equal(map, Psych.load(Psych.dump(map)))
10
+ end
11
+
12
+ def test_keys
13
+ map = Psych::Omap.new
14
+ map['foo'] = 'bar'
15
+ assert_equal 'bar', map['foo']
16
+ end
17
+
18
+ def test_order
19
+ map = Psych::Omap.new
20
+ map['a'] = 'b'
21
+ map['b'] = 'c'
22
+ assert_equal [%w{a b}, %w{b c}], map.to_a
23
+ end
24
+
25
+ def test_square
26
+ list = [["a", "b"], ["b", "c"]]
27
+ map = Psych::Omap[*list.flatten]
28
+ assert_equal list, map.to_a
29
+ assert_equal 'b', map['a']
30
+ assert_equal 'c', map['b']
31
+ end
32
+
33
+ def test_dump
34
+ map = Psych::Omap['a', 'b', 'c', 'd']
35
+ yaml = Psych.dump(map)
36
+ assert_match('!omap', yaml)
37
+ assert_match('- a: b', yaml)
38
+ assert_match('- c: d', yaml)
39
+ end
40
+
41
+ def test_round_trip
42
+ list = [["a", "b"], ["b", "c"]]
43
+ map = Psych::Omap[*list.flatten]
44
+ assert_cycle(map)
45
+ end
46
+
47
+ def test_load
48
+ list = [["a", "b"], ["c", "d"]]
49
+ map = Psych.load(<<-eoyml)
50
+ --- !omap
51
+ - a: b
52
+ - c: d
53
+ eoyml
54
+ assert_equal list, map.to_a
55
+ end
56
+
57
+ # NOTE: This test will not work with Syck
58
+ def test_load_shorthand
59
+ list = [["a", "b"], ["c", "d"]]
60
+ map = Psych.load(<<-eoyml)
61
+ --- !!omap
62
+ - a: b
63
+ - c: d
64
+ eoyml
65
+ assert_equal list, map.to_a
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,216 @@
1
+ # coding: utf-8
2
+
3
+ require_relative 'helper'
4
+
5
+ module Psych
6
+ class TestParser < TestCase
7
+ class EventCatcher < Handler
8
+ attr_reader :calls
9
+ def initialize
10
+ @calls = []
11
+ end
12
+
13
+ (Handler.instance_methods(true) -
14
+ Object.instance_methods).each do |m|
15
+ class_eval %{
16
+ def #{m} *args
17
+ super
18
+ @calls << [:#{m}, args]
19
+ end
20
+ }
21
+ end
22
+ end
23
+
24
+ def setup
25
+ super
26
+ @parser = Psych::Parser.new EventCatcher.new
27
+ end
28
+
29
+ def test_set_encoding_twice
30
+ @parser.external_encoding = Psych::Parser::UTF16LE
31
+
32
+ e = assert_raises(Psych::Exception) do
33
+ @parser.external_encoding = Psych::Parser::UTF16LE
34
+ end
35
+ assert_equal "don't set the encoding twice!", e.message
36
+ end
37
+
38
+ def test_bom
39
+ tadpole = 'おたまじゃくし'
40
+
41
+ # BOM + text
42
+ yml = "\uFEFF#{tadpole}".encode('UTF-16LE')
43
+ @parser.parse yml
44
+ assert_equal tadpole, @parser.handler.calls[2][1].first
45
+ end
46
+
47
+ def test_external_encoding
48
+ tadpole = 'おたまじゃくし'
49
+
50
+ @parser.external_encoding = Psych::Parser::UTF16LE
51
+ @parser.parse tadpole.encode 'UTF-16LE'
52
+ assert_equal tadpole, @parser.handler.calls[2][1].first
53
+ end
54
+
55
+ def test_bogus_io
56
+ o = Object.new
57
+ def o.read len; self end
58
+
59
+ assert_raises(TypeError) do
60
+ @parser.parse o
61
+ end
62
+ end
63
+
64
+ def test_parse_io
65
+ @parser.parse StringIO.new("--- a")
66
+ assert_called :start_stream
67
+ assert_called :scalar
68
+ assert_called :end_stream
69
+ end
70
+
71
+ def test_syntax_error
72
+ assert_raises(Psych::SyntaxError) do
73
+ @parser.parse("---\n\"foo\"\n\"bar\"\n")
74
+ end
75
+ end
76
+
77
+ def test_mapping_end
78
+ @parser.parse("---\n!!map { key: value }")
79
+ assert_called :end_mapping
80
+ end
81
+
82
+ def test_mapping_tag
83
+ @parser.parse("---\n!!map { key: value }")
84
+ assert_called :start_mapping, ["tag:yaml.org,2002:map", false, Nodes::Mapping::FLOW]
85
+ end
86
+
87
+ def test_mapping_anchor
88
+ @parser.parse("---\n&A { key: value }")
89
+ assert_called :start_mapping, ['A', true, Nodes::Mapping::FLOW]
90
+ end
91
+
92
+ def test_mapping_block
93
+ @parser.parse("---\n key: value")
94
+ assert_called :start_mapping, [true, Nodes::Mapping::BLOCK]
95
+ end
96
+
97
+ def test_mapping_start
98
+ @parser.parse("---\n{ key: value }")
99
+ assert_called :start_mapping
100
+ assert_called :start_mapping, [true, Nodes::Mapping::FLOW]
101
+ end
102
+
103
+ def test_sequence_end
104
+ @parser.parse("---\n&A [1, 2]")
105
+ assert_called :end_sequence
106
+ end
107
+
108
+ def test_sequence_start_anchor
109
+ @parser.parse("---\n&A [1, 2]")
110
+ assert_called :start_sequence, ["A", true, Nodes::Sequence::FLOW]
111
+ end
112
+
113
+ def test_sequence_start_tag
114
+ @parser.parse("---\n!!seq [1, 2]")
115
+ assert_called :start_sequence, ["tag:yaml.org,2002:seq", false, Nodes::Sequence::FLOW]
116
+ end
117
+
118
+ def test_sequence_start_flow
119
+ @parser.parse("---\n[1, 2]")
120
+ assert_called :start_sequence, [true, Nodes::Sequence::FLOW]
121
+ end
122
+
123
+ def test_sequence_start_block
124
+ @parser.parse("---\n - 1\n - 2")
125
+ assert_called :start_sequence, [true, Nodes::Sequence::BLOCK]
126
+ end
127
+
128
+ def test_literal_scalar
129
+ @parser.parse(<<-eoyml)
130
+ %YAML 1.1
131
+ ---
132
+ "literal\n\
133
+ \ttext\n"
134
+ eoyml
135
+ assert_called :scalar, ['literal text ', false, true, Nodes::Scalar::DOUBLE_QUOTED]
136
+ end
137
+
138
+ def test_scalar
139
+ @parser.parse("--- foo\n")
140
+ assert_called :scalar, ['foo', true, false, Nodes::Scalar::PLAIN]
141
+ end
142
+
143
+ def test_scalar_with_tag
144
+ @parser.parse("---\n!!str foo\n")
145
+ assert_called :scalar, ['foo', 'tag:yaml.org,2002:str', false, false, Nodes::Scalar::PLAIN]
146
+ end
147
+
148
+ def test_scalar_with_anchor
149
+ @parser.parse("---\n&A foo\n")
150
+ assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
151
+ end
152
+
153
+ def test_scalar_plain_implicit
154
+ @parser.parse("---\n&A foo\n")
155
+ assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
156
+ end
157
+
158
+ def test_alias
159
+ @parser.parse(<<-eoyml)
160
+ %YAML 1.1
161
+ ---
162
+ !!seq [
163
+ !!str "Without properties",
164
+ &A !!str "Anchored",
165
+ !!str "Tagged",
166
+ *A,
167
+ !!str "",
168
+ ]
169
+ eoyml
170
+ assert_called :alias, ['A']
171
+ end
172
+
173
+ def test_end_stream
174
+ @parser.parse("--- foo\n")
175
+ assert_called :end_stream
176
+ end
177
+
178
+ def test_start_stream
179
+ @parser.parse("--- foo\n")
180
+ assert_called :start_stream
181
+ end
182
+
183
+ def test_end_document_implicit
184
+ @parser.parse("\"foo\"\n")
185
+ assert_called :end_document, [true]
186
+ end
187
+
188
+ def test_end_document_explicit
189
+ @parser.parse("\"foo\"\n...")
190
+ assert_called :end_document, [false]
191
+ end
192
+
193
+ def test_start_document_version
194
+ @parser.parse("%YAML 1.1\n---\n\"foo\"\n")
195
+ assert_called :start_document, [[1,1], [], false]
196
+ end
197
+
198
+ def test_start_document_tag
199
+ @parser.parse("%TAG !yaml! tag:yaml.org,2002\n---\n!yaml!str \"foo\"\n")
200
+ assert_called :start_document, [[], [['!yaml!', 'tag:yaml.org,2002']], false]
201
+ end
202
+
203
+ def assert_called call, with = nil, parser = @parser
204
+ if with
205
+ call = parser.handler.calls.find { |x|
206
+ x.first == call && x.last.compact == with
207
+ }
208
+ assert(call,
209
+ "#{[call,with].inspect} not in #{parser.handler.calls.inspect}"
210
+ )
211
+ else
212
+ assert parser.handler.calls.any? { |x| x.first == call }
213
+ end
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,133 @@
1
+ require_relative 'helper'
2
+
3
+ require 'stringio'
4
+ require 'tempfile'
5
+
6
+ class TestPsych < Psych::TestCase
7
+ def teardown
8
+ Psych.domain_types.clear
9
+ end
10
+
11
+ def test_load_argument_error
12
+ assert_raises(TypeError) do
13
+ Psych.load nil
14
+ end
15
+ end
16
+
17
+ def test_non_existing_class_on_deserialize
18
+ e = assert_raises(ArgumentError) do
19
+ Psych.load("--- !ruby/object:NonExistent\nfoo: 1")
20
+ end
21
+ assert_equal 'undefined class/module NonExistent', e.message
22
+ end
23
+
24
+ def test_dump_stream
25
+ things = [22, "foo \n", {}]
26
+ stream = Psych.dump_stream(*things)
27
+ assert_equal things, Psych.load_stream(stream)
28
+ end
29
+
30
+ def test_dump_file
31
+ hash = {'hello' => 'TGIF!'}
32
+ Tempfile.open('fun.yml') do |io|
33
+ assert_equal io, Psych.dump(hash, io)
34
+ io.rewind
35
+ assert_equal Psych.dump(hash), io.read
36
+ end
37
+ end
38
+
39
+ def test_dump_io
40
+ hash = {'hello' => 'TGIF!'}
41
+ stringio = StringIO.new ''
42
+ assert_equal stringio, Psych.dump(hash, stringio)
43
+ assert_equal Psych.dump(hash), stringio.string
44
+ end
45
+
46
+ def test_simple
47
+ assert_equal 'foo', Psych.load("--- foo\n")
48
+ end
49
+
50
+ def test_libyaml_version
51
+ assert Psych.libyaml_version
52
+ assert_equal Psych.libyaml_version.join('.'), Psych::LIBYAML_VERSION
53
+ end
54
+
55
+ def test_load_documents
56
+ docs = Psych.load_documents("--- foo\n...\n--- bar\n...")
57
+ assert_equal %w{ foo bar }, docs
58
+ end
59
+
60
+ def test_parse_stream
61
+ docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
62
+ assert_equal %w{ foo bar }, docs.children.map { |x| x.transform }
63
+ end
64
+
65
+ def test_add_builtin_type
66
+ got = nil
67
+ Psych.add_builtin_type 'omap', do |type, val|
68
+ got = val
69
+ end
70
+ Psych.load('--- !!omap hello')
71
+ assert_equal 'hello', got
72
+ ensure
73
+ Psych.remove_type 'omap'
74
+ end
75
+
76
+ def test_domain_types
77
+ got = nil
78
+ Psych.add_domain_type 'foo.bar,2002', 'foo' do |type, val|
79
+ got = val
80
+ end
81
+
82
+ Psych.load('--- !foo.bar,2002/foo hello')
83
+ assert_equal 'hello', got
84
+
85
+ Psych.load("--- !foo.bar,2002/foo\n- hello\n- world")
86
+ assert_equal %w{ hello world }, got
87
+
88
+ Psych.load("--- !foo.bar,2002/foo\nhello: world")
89
+ assert_equal({ 'hello' => 'world' }, got)
90
+ end
91
+
92
+ def test_load_file
93
+ t = Tempfile.new(['yikes', 'yml'])
94
+ t.binmode
95
+ t.write('--- hello world')
96
+ t.close
97
+ assert_equal 'hello world', Psych.load_file(t.path)
98
+ t.close(true)
99
+ end
100
+
101
+ def test_parse_file
102
+ t = Tempfile.new(['yikes', 'yml'])
103
+ t.binmode
104
+ t.write('--- hello world')
105
+ t.close
106
+ assert_equal 'hello world', Psych.parse_file(t.path).transform
107
+ t.close(true)
108
+ end
109
+
110
+ def test_degenerate_strings
111
+ assert_equal false, Psych.load(' ')
112
+ assert_equal false, Psych.parse(' ')
113
+ assert_equal false, Psych.load('')
114
+ assert_equal false, Psych.parse('')
115
+ end
116
+
117
+ def test_callbacks
118
+ types = []
119
+ appender = lambda { |*args| types << args }
120
+
121
+ Psych.add_builtin_type('foo', &appender)
122
+ Psych.add_domain_type('example.com,2002', 'foo', &appender)
123
+ Psych.load <<-eoyml
124
+ - !tag:yaml.org,2002:foo bar
125
+ - !tag:example.com,2002:foo bar
126
+ eoyml
127
+
128
+ assert_equal [
129
+ ["tag:yaml.org,2002:foo", "bar"],
130
+ ["tag:example.com,2002:foo", "bar"]
131
+ ], types
132
+ end
133
+ end