km-psych 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,46 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestDocument < TestCase
5
+ def setup
6
+ super
7
+ @stream = Psych.parse_stream(<<-eoyml)
8
+ %YAML 1.1
9
+ %TAG ! tag:tenderlovemaking.com,2009:
10
+ --- !fun
11
+ eoyml
12
+ @doc = @stream.children.first
13
+ end
14
+
15
+ def test_parse_tag
16
+ assert_equal([['!', 'tag:tenderlovemaking.com,2009:']],
17
+ @doc.tag_directives)
18
+ end
19
+
20
+ def test_emit_tag
21
+ assert_match('%TAG ! tag:tenderlovemaking.com,2009:', @stream.to_yaml)
22
+ end
23
+
24
+ def test_emit_multitag
25
+ @doc.tag_directives << ['!!', 'foo.com,2009:']
26
+ yaml = @stream.to_yaml
27
+ assert_match('%TAG ! tag:tenderlovemaking.com,2009:', yaml)
28
+ assert_match('%TAG !! foo.com,2009:', yaml)
29
+ end
30
+
31
+ def test_emit_bad_tag
32
+ assert_raises(RuntimeError) do
33
+ @doc.tag_directives = [['!']]
34
+ @stream.to_yaml
35
+ end
36
+ end
37
+
38
+ def test_parse_version
39
+ assert_equal([1,1], @doc.version)
40
+ end
41
+
42
+ def test_emit_version
43
+ assert_match('%YAML 1.1', @stream.to_yaml)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,88 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require_relative 'helper'
4
+
5
+ module Psych
6
+ class TestEmitter < TestCase
7
+ def setup
8
+ super
9
+ @out = StringIO.new('')
10
+ @emitter = Psych::Emitter.new @out
11
+ end
12
+
13
+ def test_set_canonical
14
+ @emitter.canonical = true
15
+ assert_equal true, @emitter.canonical
16
+
17
+ @emitter.canonical = false
18
+ assert_equal false, @emitter.canonical
19
+ end
20
+
21
+ def test_indentation_set
22
+ assert_equal 2, @emitter.indentation
23
+ @emitter.indentation = 5
24
+ assert_equal 5, @emitter.indentation
25
+ end
26
+
27
+ def test_emit_utf_8
28
+ @emitter.start_stream Psych::Nodes::Stream::UTF8
29
+ @emitter.start_document [], [], false
30
+ @emitter.scalar '日本語', nil, nil, false, true, 1
31
+ @emitter.end_document true
32
+ @emitter.end_stream
33
+ assert_match('日本語', @out.string)
34
+ end
35
+
36
+ def test_start_stream_arg_error
37
+ assert_raises(TypeError) do
38
+ @emitter.start_stream 'asdfasdf'
39
+ end
40
+ end
41
+
42
+ def test_start_doc_arg_error
43
+ @emitter.start_stream Psych::Nodes::Stream::UTF8
44
+
45
+ [
46
+ [nil, [], false],
47
+ [[nil, nil], [], false],
48
+ [[], 'foo', false],
49
+ [[], ['foo'], false],
50
+ [[], [nil,nil], false],
51
+ ].each do |args|
52
+ assert_raises(TypeError) do
53
+ @emitter.start_document(*args)
54
+ end
55
+ end
56
+ end
57
+
58
+ def test_scalar_arg_error
59
+ @emitter.start_stream Psych::Nodes::Stream::UTF8
60
+ @emitter.start_document [], [], false
61
+
62
+ [
63
+ [:foo, nil, nil, false, true, 1],
64
+ ['foo', Object.new, nil, false, true, 1],
65
+ ['foo', nil, Object.new, false, true, 1],
66
+ ['foo', nil, nil, false, true, :foo],
67
+ [nil, nil, nil, false, true, 1],
68
+ ].each do |args|
69
+ assert_raises(TypeError) do
70
+ @emitter.scalar(*args)
71
+ end
72
+ end
73
+ end
74
+
75
+ def test_start_sequence_arg_error
76
+ @emitter.start_stream Psych::Nodes::Stream::UTF8
77
+ @emitter.start_document [], [], false
78
+
79
+ assert_raises(TypeError) do
80
+ @emitter.start_sequence(nil, Object.new, true, 1)
81
+ end
82
+
83
+ assert_raises(TypeError) do
84
+ @emitter.start_sequence(nil, nil, true, :foo)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,179 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require_relative 'helper'
4
+
5
+ module Psych
6
+ class TestEncoding < TestCase
7
+ class EncodingCatcher < Handler
8
+ attr_reader :strings
9
+ def initialize
10
+ @strings = []
11
+ end
12
+
13
+ (Handler.instance_methods(true) -
14
+ Object.instance_methods).each do |m|
15
+ class_eval %{
16
+ def #{m} *args
17
+ @strings += args.flatten.find_all { |a|
18
+ String === a
19
+ }
20
+ end
21
+ }
22
+ end
23
+ end
24
+
25
+ def setup
26
+ super
27
+ @buffer = StringIO.new
28
+ @handler = EncodingCatcher.new
29
+ @parser = Psych::Parser.new @handler
30
+ @utf8 = Encoding.find('UTF-8')
31
+ @emitter = Psych::Emitter.new @buffer
32
+ end
33
+
34
+ def test_emit_alias
35
+ @emitter.start_stream Psych::Parser::UTF8
36
+ @emitter.start_document [], [], true
37
+ e = assert_raises(RuntimeError) do
38
+ @emitter.alias 'ドラえもん'.encode('EUC-JP')
39
+ end
40
+ assert_match(/alias value/, e.message)
41
+ end
42
+
43
+ def test_start_mapping
44
+ foo = 'foo'
45
+ bar = 'バー'
46
+
47
+ @emitter.start_stream Psych::Parser::UTF8
48
+ @emitter.start_document [], [], true
49
+ @emitter.start_mapping(
50
+ foo.encode('Shift_JIS'),
51
+ bar.encode('UTF-16LE'),
52
+ false, Nodes::Sequence::ANY)
53
+ @emitter.end_mapping
54
+ @emitter.end_document false
55
+ @emitter.end_stream
56
+
57
+ @parser.parse @buffer.string
58
+ assert_encodings @utf8, @handler.strings
59
+ assert_equal [foo, bar], @handler.strings
60
+ end
61
+
62
+ def test_start_sequence
63
+ foo = 'foo'
64
+ bar = 'バー'
65
+
66
+ @emitter.start_stream Psych::Parser::UTF8
67
+ @emitter.start_document [], [], true
68
+ @emitter.start_sequence(
69
+ foo.encode('Shift_JIS'),
70
+ bar.encode('UTF-16LE'),
71
+ false, Nodes::Sequence::ANY)
72
+ @emitter.end_sequence
73
+ @emitter.end_document false
74
+ @emitter.end_stream
75
+
76
+ @parser.parse @buffer.string
77
+ assert_encodings @utf8, @handler.strings
78
+ assert_equal [foo, bar], @handler.strings
79
+ end
80
+
81
+ def test_doc_tag_encoding
82
+ key = '鍵'
83
+ @emitter.start_stream Psych::Parser::UTF8
84
+ @emitter.start_document(
85
+ [1, 1],
86
+ [['!'.encode('EUC-JP'), key.encode('EUC-JP')]],
87
+ true
88
+ )
89
+ @emitter.scalar 'foo', nil, nil, true, false, Nodes::Scalar::ANY
90
+ @emitter.end_document false
91
+ @emitter.end_stream
92
+
93
+ @parser.parse @buffer.string
94
+ assert_encodings @utf8, @handler.strings
95
+ assert_equal key, @handler.strings[1]
96
+ end
97
+
98
+ def test_emitter_encoding
99
+ str = "壁に耳あり、障子に目あり"
100
+ thing = Psych.load Psych.dump str.encode('EUC-JP')
101
+ assert_equal str, thing
102
+ end
103
+
104
+ def test_default_internal
105
+ before = Encoding.default_internal
106
+
107
+ Encoding.default_internal = 'EUC-JP'
108
+
109
+ str = "壁に耳あり、障子に目あり"
110
+ yaml = "--- #{str}"
111
+ assert_equal @utf8, str.encoding
112
+
113
+ @parser.parse str
114
+ assert_encodings Encoding.find('EUC-JP'), @handler.strings
115
+ assert_equal str, @handler.strings.first.encode('UTF-8')
116
+ ensure
117
+ Encoding.default_internal = before
118
+ end
119
+
120
+ def test_scalar
121
+ @parser.parse("--- a")
122
+ assert_encodings @utf8, @handler.strings
123
+ end
124
+
125
+ def test_alias
126
+ @parser.parse(<<-eoyml)
127
+ %YAML 1.1
128
+ ---
129
+ !!seq [
130
+ !!str "Without properties",
131
+ &A !!str "Anchored",
132
+ !!str "Tagged",
133
+ *A,
134
+ !!str "",
135
+ ]
136
+ eoyml
137
+ assert_encodings @utf8, @handler.strings
138
+ end
139
+
140
+ def test_list_anchor
141
+ list = %w{ a b }
142
+ list << list
143
+ @parser.parse(Psych.dump(list))
144
+ assert_encodings @utf8, @handler.strings
145
+ end
146
+
147
+ def test_map_anchor
148
+ h = {}
149
+ h['a'] = h
150
+ @parser.parse(Psych.dump(h))
151
+ assert_encodings @utf8, @handler.strings
152
+ end
153
+
154
+ def test_map_tag
155
+ @parser.parse(<<-eoyml)
156
+ %YAML 1.1
157
+ ---
158
+ !!map { a : b }
159
+ eoyml
160
+ assert_encodings @utf8, @handler.strings
161
+ end
162
+
163
+ def test_doc_tag
164
+ @parser.parse(<<-eoyml)
165
+ %YAML 1.1
166
+ %TAG ! tag:tenderlovemaking.com,2009:
167
+ --- !fun
168
+ eoyml
169
+ assert_encodings @utf8, @handler.strings
170
+ end
171
+
172
+ private
173
+ def assert_encodings encoding, strings
174
+ strings.each do |str|
175
+ assert_equal encoding, str.encoding, str
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'helper'
2
+ require 'yaml'
3
+
4
+ module Psych
5
+ class TestEngineManager < TestCase
6
+ def teardown
7
+ YAML::ENGINE.yamler = 'syck'
8
+ end
9
+
10
+ def test_bad_engine
11
+ assert_raises(ArgumentError) do
12
+ YAML::ENGINE.yamler = 'foooo'
13
+ end
14
+ end
15
+
16
+ def test_set_psych
17
+ YAML::ENGINE.yamler = 'psych'
18
+ assert_equal Psych, YAML
19
+ assert_equal 'psych', YAML::ENGINE.yamler
20
+ end
21
+
22
+ def test_set_syck
23
+ YAML::ENGINE.yamler = 'syck'
24
+ assert_equal Syck, YAML
25
+ assert_equal 'syck', YAML::ENGINE.yamler
26
+ end
27
+
28
+ A = Struct.new(:name)
29
+
30
+ def test_dump_types
31
+ YAML::ENGINE.yamler = 'psych'
32
+
33
+ assert_to_yaml ::Object.new
34
+ assert_to_yaml Time.now
35
+ assert_to_yaml Date.today
36
+ assert_to_yaml('a' => 'b')
37
+ assert_to_yaml A.new('foo')
38
+ assert_to_yaml %w{a b}
39
+ assert_to_yaml Exception.new('foo')
40
+ assert_to_yaml "hello!"
41
+ assert_to_yaml :fooo
42
+ assert_to_yaml(1..10)
43
+ assert_to_yaml(/hello!~/)
44
+ assert_to_yaml 1
45
+ assert_to_yaml 1.2
46
+ assert_to_yaml Rational(1, 2)
47
+ assert_to_yaml Complex(1, 2)
48
+ assert_to_yaml true
49
+ assert_to_yaml false
50
+ assert_to_yaml nil
51
+ end
52
+
53
+ def assert_to_yaml obj
54
+ assert obj.to_yaml, "#{obj.class} to_yaml works"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestException < TestCase
5
+ class Wups < Exception
6
+ attr_reader :foo, :bar
7
+ def initialize *args
8
+ super
9
+ @foo = 1
10
+ @bar = 2
11
+ end
12
+ end
13
+
14
+ def setup
15
+ super
16
+ @wups = Wups.new
17
+ end
18
+
19
+ def test_convert
20
+ w = Psych.load(Psych.dump(@wups))
21
+ assert_equal @wups, w
22
+ assert_equal 1, w.foo
23
+ assert_equal 2, w.bar
24
+ end
25
+
26
+ def test_to_yaml_properties
27
+ class << @wups
28
+ def to_yaml_properties
29
+ [:@foo]
30
+ end
31
+ end
32
+
33
+ w = Psych.load(Psych.dump(@wups))
34
+ assert_equal @wups, w
35
+ assert_equal 1, w.foo
36
+ assert_nil w.bar
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestHash < TestCase
5
+ def setup
6
+ super
7
+ @hash = { :a => 'b' }
8
+ end
9
+
10
+ def test_self_referential
11
+ @hash['self'] = @hash
12
+ assert_cycle(@hash)
13
+ end
14
+
15
+ def test_cycles
16
+ assert_cycle(@hash)
17
+ end
18
+
19
+ def test_ref_append
20
+ hash = Psych.load(<<-eoyml)
21
+ ---
22
+ foo: &foo
23
+ hello: world
24
+ bar:
25
+ <<: *foo
26
+ eoyml
27
+ assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestJSONTree < TestCase
5
+ def test_string
6
+ assert_match(/(['"])foo\1/, Psych.to_json("foo"))
7
+ end
8
+
9
+ def test_symbol
10
+ assert_match(/(['"])foo\1/, Psych.to_json(:foo))
11
+ end
12
+
13
+ def test_nil
14
+ assert_match(/^null/, Psych.to_json(nil))
15
+ end
16
+
17
+ def test_int
18
+ assert_match(/^10/, Psych.to_json(10))
19
+ end
20
+
21
+ def test_float
22
+ assert_match(/^1.2/, Psych.to_json(1.2))
23
+ end
24
+
25
+ def test_hash
26
+ hash = { 'one' => 'two' }
27
+ json = Psych.to_json(hash)
28
+ assert_match(/}$/, json)
29
+ assert_match(/^\{/, json)
30
+ assert_match(/['"]one['"]/, json)
31
+ assert_match(/['"]two['"]/, json)
32
+ end
33
+
34
+ def test_list_to_json
35
+ list = %w{ one two }
36
+ json = Psych.to_json(list)
37
+ assert_match(/]$/, json)
38
+ assert_match(/^\[/, json)
39
+ assert_match(/['"]one['"]/, json)
40
+ assert_match(/['"]two['"]/, json)
41
+ end
42
+ end
43
+ end