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,26 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestAliasAndAnchor < TestCase
5
+ def test_mri_compatibility
6
+ yaml = <<EOYAML
7
+ ---
8
+ - &id001 !ruby/object {}
9
+
10
+ - *id001
11
+ - *id001
12
+ EOYAML
13
+ result = Psych.load yaml
14
+ result.each {|el| assert_same(result[0], el) }
15
+ end
16
+
17
+ def test_anchor_alias_round_trip
18
+ o = Object.new
19
+ original = [o,o,o]
20
+
21
+ yaml = Psych.dump original
22
+ result = Psych.load yaml
23
+ result.each {|el| assert_same(result[0], el) }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestArray < TestCase
5
+ def setup
6
+ super
7
+ @list = [{ :a => 'b' }, 'foo']
8
+ end
9
+
10
+ def test_self_referential
11
+ @list << @list
12
+ assert_cycle(@list)
13
+ end
14
+
15
+ def test_cycle
16
+ assert_cycle(@list)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ ###
5
+ # Test booleans from YAML spec:
6
+ # http://yaml.org/type/bool.html
7
+ class TestBoolean < TestCase
8
+ %w{ yes Yes YES true True TRUE on On ON }.each do |truth|
9
+ define_method(:"test_#{truth}") do
10
+ assert_equal true, Psych.load("--- #{truth}")
11
+ end
12
+ end
13
+
14
+ %w{ no No NO false False FALSE off Off OFF }.each do |truth|
15
+ define_method(:"test_#{truth}") do
16
+ assert_equal false, Psych.load("--- #{truth}")
17
+ end
18
+ end
19
+
20
+ ###
21
+ # YAML spec says "y" and "Y" may be used as true, but Syck treats them
22
+ # as literal strings
23
+ def test_y
24
+ assert_equal "y", Psych.load("--- y")
25
+ assert_equal "Y", Psych.load("--- Y")
26
+ end
27
+
28
+ ###
29
+ # YAML spec says "n" and "N" may be used as false, but Syck treats them
30
+ # as literal strings
31
+ def test_n
32
+ assert_equal "n", Psych.load("--- n")
33
+ assert_equal "N", Psych.load("--- N")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestClass < TestCase
5
+ def test_cycle
6
+ assert_raises(::TypeError) do
7
+ assert_cycle(TestClass)
8
+ end
9
+ end
10
+
11
+ def test_dump
12
+ assert_raises(::TypeError) do
13
+ Psych.dump TestClass
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,169 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestCoder < TestCase
5
+ class InitApi
6
+ attr_accessor :implicit
7
+ attr_accessor :style
8
+ attr_accessor :tag
9
+ attr_accessor :a, :b, :c
10
+
11
+ def initialize
12
+ @a = 1
13
+ @b = 2
14
+ @c = 3
15
+ end
16
+
17
+ def init_with coder
18
+ @a = coder['aa']
19
+ @b = coder['bb']
20
+ @implicit = coder.implicit
21
+ @tag = coder.tag
22
+ @style = coder.style
23
+ end
24
+
25
+ def encode_with coder
26
+ coder['aa'] = @a
27
+ coder['bb'] = @b
28
+ end
29
+ end
30
+
31
+ class TaggingCoder < InitApi
32
+ def encode_with coder
33
+ super
34
+ coder.tag = coder.tag.sub(/!/, '!hello')
35
+ coder.implicit = false
36
+ coder.style = Psych::Nodes::Mapping::FLOW
37
+ end
38
+ end
39
+
40
+ class ScalarCoder
41
+ def encode_with coder
42
+ coder.scalar = "foo"
43
+ end
44
+ end
45
+
46
+ class Represent
47
+ yaml_tag 'foo'
48
+ def encode_with coder
49
+ coder.represent_scalar 'foo', 'bar'
50
+ end
51
+ end
52
+
53
+ class RepresentWithInit
54
+ yaml_tag name
55
+ attr_accessor :str
56
+
57
+ def init_with coder
58
+ @str = coder.scalar
59
+ end
60
+
61
+ def encode_with coder
62
+ coder.represent_scalar self.class.name, 'bar'
63
+ end
64
+ end
65
+
66
+ class RepresentWithSeq
67
+ yaml_tag name
68
+ attr_accessor :seq
69
+
70
+ def init_with coder
71
+ @seq = coder.seq
72
+ end
73
+
74
+ def encode_with coder
75
+ coder.represent_seq self.class.name, %w{ foo bar }
76
+ end
77
+ end
78
+
79
+ class RepresentWithMap
80
+ yaml_tag name
81
+ attr_accessor :map
82
+
83
+ def init_with coder
84
+ @map = coder.map
85
+ end
86
+
87
+ def encode_with coder
88
+ coder.represent_map self.class.name, { 'a' => 'b' }
89
+ end
90
+ end
91
+
92
+ def test_map_takes_block
93
+ coder = Psych::Coder.new 'foo'
94
+ tag = coder.tag
95
+ style = coder.style
96
+ coder.map { |map| map.add 'foo', 'bar' }
97
+ assert_equal 'bar', coder['foo']
98
+ assert_equal tag, coder.tag
99
+ assert_equal style, coder.style
100
+ end
101
+
102
+ def test_map_with_tag
103
+ coder = Psych::Coder.new 'foo'
104
+ coder.map('hello') { |map| map.add 'foo', 'bar' }
105
+ assert_equal 'bar', coder['foo']
106
+ assert_equal 'hello', coder.tag
107
+ end
108
+
109
+ def test_map_with_tag_and_style
110
+ coder = Psych::Coder.new 'foo'
111
+ coder.map('hello', 'world') { |map| map.add 'foo', 'bar' }
112
+ assert_equal 'bar', coder['foo']
113
+ assert_equal 'hello', coder.tag
114
+ assert_equal 'world', coder.style
115
+ end
116
+
117
+ def test_represent_map
118
+ thing = Psych.load(Psych.dump(RepresentWithMap.new))
119
+ assert_equal({ 'a' => 'b' }, thing.map)
120
+ end
121
+
122
+ def test_represent_sequence
123
+ thing = Psych.load(Psych.dump(RepresentWithSeq.new))
124
+ assert_equal %w{ foo bar }, thing.seq
125
+ end
126
+
127
+ def test_represent_with_init
128
+ thing = Psych.load(Psych.dump(RepresentWithInit.new))
129
+ assert_equal 'bar', thing.str
130
+ end
131
+
132
+ def test_represent!
133
+ assert_match(/foo/, Psych.dump(Represent.new))
134
+ assert_instance_of(Represent, Psych.load(Psych.dump(Represent.new)))
135
+ end
136
+
137
+ def test_scalar_coder
138
+ foo = Psych.load(Psych.dump(ScalarCoder.new))
139
+ assert_equal 'foo', foo
140
+ end
141
+
142
+ def test_load_dumped_tagging
143
+ foo = InitApi.new
144
+ bar = Psych.load(Psych.dump(foo))
145
+ assert_equal false, bar.implicit
146
+ assert_equal "!ruby/object:Psych::TestCoder::InitApi", bar.tag
147
+ assert_equal Psych::Nodes::Mapping::BLOCK, bar.style
148
+ end
149
+
150
+ def test_dump_with_tag
151
+ foo = TaggingCoder.new
152
+ assert_match(/hello/, Psych.dump(foo))
153
+ assert_match(/\{aa/, Psych.dump(foo))
154
+ end
155
+
156
+ def test_dump_encode_with
157
+ foo = InitApi.new
158
+ assert_match(/aa/, Psych.dump(foo))
159
+ end
160
+
161
+ def test_dump_init_with
162
+ foo = InitApi.new
163
+ bar = Psych.load(Psych.dump(foo))
164
+ assert_equal foo.a, bar.a
165
+ assert_equal foo.b, bar.b
166
+ assert_nil bar.c
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'helper'
2
+ require 'date'
3
+
4
+ module Psych
5
+ class TestDateTime < TestCase
6
+ def test_string_tag
7
+ dt = DateTime.now
8
+ yaml = Psych.dump dt
9
+ assert_match(/DateTime/, yaml)
10
+ end
11
+
12
+ def test_round_trip
13
+ dt = DateTime.now
14
+ assert_cycle dt
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,210 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestDeprecated < TestCase
5
+ def teardown
6
+ Psych.domain_types.clear
7
+ end
8
+
9
+ class QuickEmitter
10
+ attr_reader :name
11
+ attr_reader :value
12
+
13
+ def initialize
14
+ @name = 'hello!!'
15
+ @value = 'Friday!'
16
+ end
17
+
18
+ def to_yaml opts = {}
19
+ Psych.quick_emit object_id, opts do |out|
20
+ out.map taguri, to_yaml_style do |map|
21
+ map.add 'name', @name
22
+ map.add 'value', nil
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def setup
29
+ @qe = QuickEmitter.new
30
+ end
31
+
32
+ def test_quick_emit
33
+ qe2 = Psych.load @qe.to_yaml
34
+ assert_equal @qe.name, qe2.name
35
+ assert_instance_of QuickEmitter, qe2
36
+ assert_nil qe2.value
37
+ end
38
+
39
+ def test_recursive_quick_emit
40
+ hash = { :qe => @qe }
41
+ hash2 = Psych.load Psych.dump hash
42
+ qe = hash2[:qe]
43
+
44
+ assert_equal @qe.name, qe.name
45
+ assert_instance_of QuickEmitter, qe
46
+ assert_nil qe.value
47
+ end
48
+
49
+ class QuickEmitterEncodeWith
50
+ attr_reader :name
51
+ attr_reader :value
52
+
53
+ def initialize
54
+ @name = 'hello!!'
55
+ @value = 'Friday!'
56
+ end
57
+
58
+ def encode_with coder
59
+ coder.map do |map|
60
+ map.add 'name', @name
61
+ map.add 'value', nil
62
+ end
63
+ end
64
+
65
+ def to_yaml opts = {}
66
+ raise
67
+ end
68
+ end
69
+
70
+ ###
71
+ # An object that defines both to_yaml and encode_with should only call
72
+ # encode_with.
73
+ def test_recursive_quick_emit_encode_with
74
+ qeew = QuickEmitterEncodeWith.new
75
+ hash = { :qe => qeew }
76
+ hash2 = Psych.load Psych.dump hash
77
+ qe = hash2[:qe]
78
+
79
+ assert_equal qeew.name, qe.name
80
+ assert_instance_of QuickEmitterEncodeWith, qe
81
+ assert_nil qe.value
82
+ end
83
+
84
+ class YamlInit
85
+ attr_reader :name
86
+ attr_reader :value
87
+
88
+ def initialize
89
+ @name = 'hello!!'
90
+ @value = 'Friday!'
91
+ end
92
+
93
+ def yaml_initialize tag, vals
94
+ vals.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
95
+ end
96
+ end
97
+
98
+ def test_yaml_initialize
99
+ hash = { :yi => YamlInit.new }
100
+ hash2 = Psych.load Psych.dump hash
101
+ yi = hash2[:yi]
102
+
103
+ assert_equal 'TGIF!', yi.name
104
+ assert_equal 'TGIF!', yi.value
105
+ assert_instance_of YamlInit, yi
106
+ end
107
+
108
+ class YamlInitAndInitWith
109
+ attr_reader :name
110
+ attr_reader :value
111
+
112
+ def initialize
113
+ @name = 'shaners'
114
+ @value = 'Friday!'
115
+ end
116
+
117
+ def init_with coder
118
+ coder.map.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
119
+ end
120
+
121
+ def yaml_initialize tag, vals
122
+ raise
123
+ end
124
+ end
125
+
126
+ ###
127
+ # An object that implements both yaml_initialize and init_with should not
128
+ # receive the yaml_initialize call.
129
+ def test_yaml_initialize_and_init_with
130
+ hash = { :yi => YamlInitAndInitWith.new }
131
+ hash2 = Psych.load Psych.dump hash
132
+ yi = hash2[:yi]
133
+
134
+ assert_equal 'TGIF!', yi.name
135
+ assert_equal 'TGIF!', yi.value
136
+ assert_instance_of YamlInitAndInitWith, yi
137
+ end
138
+
139
+ def test_coder_scalar
140
+ coder = Psych::Coder.new 'foo'
141
+ coder.scalar('tag', 'some string', :plain)
142
+ assert_equal 'tag', coder.tag
143
+ assert_equal 'some string', coder.scalar
144
+ assert_equal :scalar, coder.type
145
+ end
146
+
147
+ class YamlAs
148
+ yaml_as 'helloworld'
149
+ end
150
+
151
+ def test_yaml_as
152
+ assert_match(/helloworld/, Psych.dump(YamlAs.new))
153
+ end
154
+
155
+ def test_ruby_type
156
+ types = []
157
+ appender = lambda { |*args| types << args }
158
+
159
+ Psych.add_ruby_type('foo', &appender)
160
+ Psych.load <<-eoyml
161
+ - !ruby.yaml.org,2002/foo bar
162
+ eoyml
163
+
164
+ assert_equal [["tag:ruby.yaml.org,2002:foo", "bar"]], types
165
+ end
166
+
167
+ def test_detect_implicit
168
+ assert_equal '', Psych.detect_implicit(nil)
169
+ assert_equal '', Psych.detect_implicit(Object.new)
170
+ assert_equal '', Psych.detect_implicit(1.2)
171
+ assert_equal 'null', Psych.detect_implicit('')
172
+ assert_equal 'string', Psych.detect_implicit('foo')
173
+ end
174
+
175
+ def test_private_type
176
+ types = []
177
+ Psych.add_private_type('foo') { |*args| types << args }
178
+ Psych.load <<-eoyml
179
+ - !x-private:foo bar
180
+ eoyml
181
+
182
+ assert_equal [["x-private:foo", "bar"]], types
183
+ end
184
+
185
+ def test_tagurize
186
+ assert_nil Psych.tagurize nil
187
+ assert_equal Psych, Psych.tagurize(Psych)
188
+ assert_equal 'tag:yaml.org,2002:foo', Psych.tagurize('foo')
189
+ end
190
+
191
+ def test_read_type_class
192
+ things = Psych.read_type_class 'tag:yaml.org,2002:int:Psych::TestDeprecated::QuickEmitter', Object
193
+ assert_equal 'int', things.first
194
+ assert_equal Psych::TestDeprecated::QuickEmitter, things.last
195
+ end
196
+
197
+ def test_read_type_class_no_class
198
+ things = Psych.read_type_class 'tag:yaml.org,2002:int', Object
199
+ assert_equal 'int', things.first
200
+ assert_equal Object, things.last
201
+ end
202
+
203
+ def test_object_maker
204
+ thing = Psych.object_maker(Object, { 'a' => 'b', 'c' => 'd' })
205
+ assert_instance_of(Object, thing)
206
+ assert_equal 'b', thing.instance_variable_get(:@a)
207
+ assert_equal 'd', thing.instance_variable_get(:@c)
208
+ end
209
+ end
210
+ end