psych 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +18 -0
- data/.gemtest +0 -0
- data/CHANGELOG.rdoc +3 -0
- data/Manifest.txt +87 -0
- data/README.rdoc +50 -0
- data/Rakefile +66 -0
- data/ext/psych/emitter.c +517 -0
- data/ext/psych/emitter.h +8 -0
- data/ext/psych/extconf.rb +22 -0
- data/ext/psych/parser.c +384 -0
- data/ext/psych/parser.h +6 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +20 -0
- data/ext/psych/to_ruby.c +41 -0
- data/ext/psych/to_ruby.h +8 -0
- data/ext/psych/yaml_tree.c +24 -0
- data/ext/psych/yaml_tree.h +8 -0
- data/lib/psych.rb +263 -0
- data/lib/psych/coder.rb +94 -0
- data/lib/psych/core_ext.rb +39 -0
- data/lib/psych/deprecated.rb +82 -0
- data/lib/psych/handler.rb +221 -0
- data/lib/psych/json.rb +6 -0
- data/lib/psych/json/ruby_events.rb +19 -0
- data/lib/psych/json/stream.rb +15 -0
- data/lib/psych/json/tree_builder.rb +12 -0
- data/lib/psych/json/yaml_events.rb +29 -0
- data/lib/psych/nodes.rb +77 -0
- data/lib/psych/nodes/alias.rb +18 -0
- data/lib/psych/nodes/document.rb +60 -0
- data/lib/psych/nodes/mapping.rb +56 -0
- data/lib/psych/nodes/node.rb +52 -0
- data/lib/psych/nodes/scalar.rb +67 -0
- data/lib/psych/nodes/sequence.rb +81 -0
- data/lib/psych/nodes/stream.rb +37 -0
- data/lib/psych/omap.rb +4 -0
- data/lib/psych/parser.rb +47 -0
- data/lib/psych/scalar_scanner.rb +105 -0
- data/lib/psych/set.rb +4 -0
- data/lib/psych/stream.rb +36 -0
- data/lib/psych/streaming.rb +22 -0
- data/lib/psych/tree_builder.rb +94 -0
- data/lib/psych/visitors.rb +6 -0
- data/lib/psych/visitors/depth_first.rb +26 -0
- data/lib/psych/visitors/emitter.rb +44 -0
- data/lib/psych/visitors/json_tree.rb +21 -0
- data/lib/psych/visitors/to_ruby.rb +267 -0
- data/lib/psych/visitors/visitor.rb +19 -0
- data/lib/psych/visitors/yaml_tree.rb +373 -0
- data/test/psych/helper.rb +63 -0
- data/test/psych/json/test_stream.rb +109 -0
- data/test/psych/nodes/test_enumerable.rb +43 -0
- data/test/psych/test_alias_and_anchor.rb +26 -0
- data/test/psych/test_array.rb +19 -0
- data/test/psych/test_boolean.rb +36 -0
- data/test/psych/test_class.rb +17 -0
- data/test/psych/test_coder.rb +184 -0
- data/test/psych/test_date_time.rb +17 -0
- data/test/psych/test_deprecated.rb +210 -0
- data/test/psych/test_document.rb +46 -0
- data/test/psych/test_emitter.rb +94 -0
- data/test/psych/test_encoding.rb +179 -0
- data/test/psych/test_engine_manager.rb +57 -0
- data/test/psych/test_exception.rb +39 -0
- data/test/psych/test_hash.rb +30 -0
- data/test/psych/test_json_tree.rb +65 -0
- data/test/psych/test_merge_keys.rb +72 -0
- data/test/psych/test_nil.rb +18 -0
- data/test/psych/test_null.rb +19 -0
- data/test/psych/test_object.rb +27 -0
- data/test/psych/test_omap.rb +68 -0
- data/test/psych/test_parser.rb +297 -0
- data/test/psych/test_psych.rb +168 -0
- data/test/psych/test_scalar.rb +11 -0
- data/test/psych/test_scalar_scanner.rb +69 -0
- data/test/psych/test_serialize_subclasses.rb +38 -0
- data/test/psych/test_set.rb +49 -0
- data/test/psych/test_stream.rb +49 -0
- data/test/psych/test_string.rb +49 -0
- data/test/psych/test_struct.rb +51 -0
- data/test/psych/test_symbol.rb +17 -0
- data/test/psych/test_to_yaml_properties.rb +63 -0
- data/test/psych/test_tree_builder.rb +79 -0
- data/test/psych/test_yaml.rb +1256 -0
- data/test/psych/visitors/test_depth_first.rb +49 -0
- data/test/psych/visitors/test_emitter.rb +144 -0
- data/test/psych/visitors/test_to_ruby.rb +325 -0
- data/test/psych/visitors/test_yaml_tree.rb +155 -0
- metadata +232 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'psych/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 'psych/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 'psych/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,65 @@
|
|
1
|
+
require 'psych/helper'
|
2
|
+
|
3
|
+
module Psych
|
4
|
+
class TestJSONTree < TestCase
|
5
|
+
def test_string
|
6
|
+
assert_match(/"foo"/, Psych.to_json("foo"))
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_symbol
|
10
|
+
assert_match(/"foo"/, 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
|
+
class Bar
|
35
|
+
def encode_with coder
|
36
|
+
coder.represent_seq 'omg', %w{ a b c }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_json_list_dump_exclude_tag
|
41
|
+
json = Psych.to_json Bar.new
|
42
|
+
refute_match('omg', json)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_list_to_json
|
46
|
+
list = %w{ one two }
|
47
|
+
json = Psych.to_json(list)
|
48
|
+
assert_match(/]$/, json)
|
49
|
+
assert_match(/^\[/, json)
|
50
|
+
assert_match(/"one"/, json)
|
51
|
+
assert_match(/"two"/, json)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_time
|
55
|
+
time = Time.utc(2010, 10, 10)
|
56
|
+
assert_equal "{\"a\": \"2010-10-10 00:00:00.000000000Z\"}\n",
|
57
|
+
Psych.to_json({'a' => time })
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_datetime
|
61
|
+
time = Time.new(2010, 10, 10).to_datetime
|
62
|
+
assert_equal "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", Psych.to_json({'a' => time })
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'psych/helper'
|
2
|
+
|
3
|
+
module Psych
|
4
|
+
class TestMergeKeys < TestCase
|
5
|
+
# [ruby-core:34679]
|
6
|
+
def test_merge_key
|
7
|
+
yaml = <<-eoyml
|
8
|
+
foo: &foo
|
9
|
+
hello: world
|
10
|
+
bar:
|
11
|
+
<< : *foo
|
12
|
+
baz: boo
|
13
|
+
eoyml
|
14
|
+
|
15
|
+
hash = {
|
16
|
+
"foo" => { "hello" => "world"},
|
17
|
+
"bar" => { "hello" => "world", "baz" => "boo" } }
|
18
|
+
assert_equal hash, Psych.load(yaml)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_multiple_maps
|
22
|
+
yaml = <<-eoyaml
|
23
|
+
---
|
24
|
+
- &CENTER { x: 1, y: 2 }
|
25
|
+
- &LEFT { x: 0, y: 2 }
|
26
|
+
- &BIG { r: 10 }
|
27
|
+
- &SMALL { r: 1 }
|
28
|
+
|
29
|
+
# All the following maps are equal:
|
30
|
+
|
31
|
+
- # Merge multiple maps
|
32
|
+
<< : [ *CENTER, *BIG ]
|
33
|
+
label: center/big
|
34
|
+
eoyaml
|
35
|
+
|
36
|
+
hash = {
|
37
|
+
'x' => 1,
|
38
|
+
'y' => 2,
|
39
|
+
'r' => 10,
|
40
|
+
'label' => 'center/big'
|
41
|
+
}
|
42
|
+
|
43
|
+
assert_equal hash, Psych.load(yaml)[4]
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_override
|
47
|
+
yaml = <<-eoyaml
|
48
|
+
---
|
49
|
+
- &CENTER { x: 1, y: 2 }
|
50
|
+
- &LEFT { x: 0, y: 2 }
|
51
|
+
- &BIG { r: 10 }
|
52
|
+
- &SMALL { r: 1 }
|
53
|
+
|
54
|
+
# All the following maps are equal:
|
55
|
+
|
56
|
+
- # Override
|
57
|
+
<< : [ *BIG, *LEFT, *SMALL ]
|
58
|
+
x: 1
|
59
|
+
label: center/big
|
60
|
+
eoyaml
|
61
|
+
|
62
|
+
hash = {
|
63
|
+
'x' => 1,
|
64
|
+
'y' => 2,
|
65
|
+
'r' => 10,
|
66
|
+
'label' => 'center/big'
|
67
|
+
}
|
68
|
+
|
69
|
+
assert_equal hash, Psych.load(yaml)[4]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'psych/helper'
|
2
|
+
|
3
|
+
module Psych
|
4
|
+
class TestNil < TestCase
|
5
|
+
def test_nil
|
6
|
+
yml = Psych.dump nil
|
7
|
+
assert_equal "--- \n...\n", yml
|
8
|
+
assert_equal nil, Psych.load(yml)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_array_nil
|
12
|
+
yml = Psych.dump [nil]
|
13
|
+
assert_equal "---\n- \n", yml
|
14
|
+
assert_equal [nil], Psych.load(yml)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'psych/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 'psych/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 'psych/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,297 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'psych/helper'
|
4
|
+
|
5
|
+
module Psych
|
6
|
+
class TestParser < TestCase
|
7
|
+
class EventCatcher < Handler
|
8
|
+
attr_accessor :parser
|
9
|
+
attr_reader :calls, :marks
|
10
|
+
def initialize
|
11
|
+
@parser = nil
|
12
|
+
@calls = []
|
13
|
+
@marks = []
|
14
|
+
end
|
15
|
+
|
16
|
+
(Handler.instance_methods(true) -
|
17
|
+
Object.instance_methods).each do |m|
|
18
|
+
class_eval %{
|
19
|
+
def #{m} *args
|
20
|
+
super
|
21
|
+
@marks << @parser.mark if @parser
|
22
|
+
@calls << [:#{m}, args]
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup
|
29
|
+
super
|
30
|
+
@handler = EventCatcher.new
|
31
|
+
@parser = Psych::Parser.new @handler
|
32
|
+
@handler.parser = @parser
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_line_numbers
|
36
|
+
assert_equal 0, @parser.mark.line
|
37
|
+
@parser.parse "---\n- hello\n- world"
|
38
|
+
line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first))
|
39
|
+
assert_equal [[0, :start_stream],
|
40
|
+
[0, :start_document],
|
41
|
+
[1, :start_sequence],
|
42
|
+
[2, :scalar],
|
43
|
+
[3, :scalar],
|
44
|
+
[3, :end_sequence],
|
45
|
+
[3, :end_document],
|
46
|
+
[3, :end_stream]], line_calls
|
47
|
+
|
48
|
+
assert_equal 3, @parser.mark.line
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_column_numbers
|
52
|
+
assert_equal 0, @parser.mark.column
|
53
|
+
@parser.parse "---\n- hello\n- world"
|
54
|
+
col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first))
|
55
|
+
assert_equal [[0, :start_stream],
|
56
|
+
[3, :start_document],
|
57
|
+
[1, :start_sequence],
|
58
|
+
[0, :scalar],
|
59
|
+
[0, :scalar],
|
60
|
+
[0, :end_sequence],
|
61
|
+
[0, :end_document],
|
62
|
+
[0, :end_stream]], col_calls
|
63
|
+
|
64
|
+
assert_equal 0, @parser.mark.column
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_index_numbers
|
68
|
+
assert_equal 0, @parser.mark.index
|
69
|
+
@parser.parse "---\n- hello\n- world"
|
70
|
+
idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first))
|
71
|
+
assert_equal [[0, :start_stream],
|
72
|
+
[3, :start_document],
|
73
|
+
[5, :start_sequence],
|
74
|
+
[12, :scalar],
|
75
|
+
[19, :scalar],
|
76
|
+
[19, :end_sequence],
|
77
|
+
[19, :end_document],
|
78
|
+
[19, :end_stream]], idx_calls
|
79
|
+
|
80
|
+
assert_equal 19, @parser.mark.index
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_set_encoding_twice
|
84
|
+
@parser.external_encoding = Psych::Parser::UTF16LE
|
85
|
+
|
86
|
+
e = assert_raises(Psych::Exception) do
|
87
|
+
@parser.external_encoding = Psych::Parser::UTF16LE
|
88
|
+
end
|
89
|
+
assert_equal "don't set the encoding twice!", e.message
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_bom
|
93
|
+
tadpole = 'おたまじゃくし'
|
94
|
+
|
95
|
+
# BOM + text
|
96
|
+
yml = "\uFEFF#{tadpole}".encode('UTF-16LE')
|
97
|
+
@parser.parse yml
|
98
|
+
assert_equal tadpole, @parser.handler.calls[2][1].first
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_external_encoding
|
102
|
+
tadpole = 'おたまじゃくし'
|
103
|
+
|
104
|
+
@parser.external_encoding = Psych::Parser::UTF16LE
|
105
|
+
@parser.parse tadpole.encode 'UTF-16LE'
|
106
|
+
assert_equal tadpole, @parser.handler.calls[2][1].first
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_bogus_io
|
110
|
+
o = Object.new
|
111
|
+
def o.read len; self end
|
112
|
+
|
113
|
+
assert_raises(TypeError) do
|
114
|
+
@parser.parse o
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_parse_io
|
119
|
+
@parser.parse StringIO.new("--- a")
|
120
|
+
assert_called :start_stream
|
121
|
+
assert_called :scalar
|
122
|
+
assert_called :end_stream
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_syntax_error
|
126
|
+
assert_raises(Psych::SyntaxError) do
|
127
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_syntax_error_twice
|
132
|
+
assert_raises(Psych::SyntaxError) do
|
133
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
134
|
+
end
|
135
|
+
|
136
|
+
assert_raises(Psych::SyntaxError) do
|
137
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_syntax_error_has_path_for_string
|
142
|
+
e = assert_raises(Psych::SyntaxError) do
|
143
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
144
|
+
end
|
145
|
+
assert_match '(<unknown>):', e.message
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_syntax_error_has_path_for_io
|
149
|
+
io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
|
150
|
+
def io.path; "hello!"; end
|
151
|
+
|
152
|
+
e = assert_raises(Psych::SyntaxError) do
|
153
|
+
@parser.parse(io)
|
154
|
+
end
|
155
|
+
assert_match "(#{io.path}):", e.message
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_mapping_end
|
159
|
+
@parser.parse("---\n!!map { key: value }")
|
160
|
+
assert_called :end_mapping
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_mapping_tag
|
164
|
+
@parser.parse("---\n!!map { key: value }")
|
165
|
+
assert_called :start_mapping, ["tag:yaml.org,2002:map", false, Nodes::Mapping::FLOW]
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_mapping_anchor
|
169
|
+
@parser.parse("---\n&A { key: value }")
|
170
|
+
assert_called :start_mapping, ['A', true, Nodes::Mapping::FLOW]
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_mapping_block
|
174
|
+
@parser.parse("---\n key: value")
|
175
|
+
assert_called :start_mapping, [true, Nodes::Mapping::BLOCK]
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_mapping_start
|
179
|
+
@parser.parse("---\n{ key: value }")
|
180
|
+
assert_called :start_mapping
|
181
|
+
assert_called :start_mapping, [true, Nodes::Mapping::FLOW]
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_sequence_end
|
185
|
+
@parser.parse("---\n&A [1, 2]")
|
186
|
+
assert_called :end_sequence
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_sequence_start_anchor
|
190
|
+
@parser.parse("---\n&A [1, 2]")
|
191
|
+
assert_called :start_sequence, ["A", true, Nodes::Sequence::FLOW]
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_sequence_start_tag
|
195
|
+
@parser.parse("---\n!!seq [1, 2]")
|
196
|
+
assert_called :start_sequence, ["tag:yaml.org,2002:seq", false, Nodes::Sequence::FLOW]
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_sequence_start_flow
|
200
|
+
@parser.parse("---\n[1, 2]")
|
201
|
+
assert_called :start_sequence, [true, Nodes::Sequence::FLOW]
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_sequence_start_block
|
205
|
+
@parser.parse("---\n - 1\n - 2")
|
206
|
+
assert_called :start_sequence, [true, Nodes::Sequence::BLOCK]
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_literal_scalar
|
210
|
+
@parser.parse(<<-eoyml)
|
211
|
+
%YAML 1.1
|
212
|
+
---
|
213
|
+
"literal\n\
|
214
|
+
\ttext\n"
|
215
|
+
eoyml
|
216
|
+
assert_called :scalar, ['literal text ', false, true, Nodes::Scalar::DOUBLE_QUOTED]
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_scalar
|
220
|
+
@parser.parse("--- foo\n")
|
221
|
+
assert_called :scalar, ['foo', true, false, Nodes::Scalar::PLAIN]
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_scalar_with_tag
|
225
|
+
@parser.parse("---\n!!str foo\n")
|
226
|
+
assert_called :scalar, ['foo', 'tag:yaml.org,2002:str', false, false, Nodes::Scalar::PLAIN]
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_scalar_with_anchor
|
230
|
+
@parser.parse("---\n&A foo\n")
|
231
|
+
assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_scalar_plain_implicit
|
235
|
+
@parser.parse("---\n&A foo\n")
|
236
|
+
assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_alias
|
240
|
+
@parser.parse(<<-eoyml)
|
241
|
+
%YAML 1.1
|
242
|
+
---
|
243
|
+
!!seq [
|
244
|
+
!!str "Without properties",
|
245
|
+
&A !!str "Anchored",
|
246
|
+
!!str "Tagged",
|
247
|
+
*A,
|
248
|
+
!!str "",
|
249
|
+
]
|
250
|
+
eoyml
|
251
|
+
assert_called :alias, ['A']
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_end_stream
|
255
|
+
@parser.parse("--- foo\n")
|
256
|
+
assert_called :end_stream
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_start_stream
|
260
|
+
@parser.parse("--- foo\n")
|
261
|
+
assert_called :start_stream
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_end_document_implicit
|
265
|
+
@parser.parse("\"foo\"\n")
|
266
|
+
assert_called :end_document, [true]
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_end_document_explicit
|
270
|
+
@parser.parse("\"foo\"\n...")
|
271
|
+
assert_called :end_document, [false]
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_start_document_version
|
275
|
+
@parser.parse("%YAML 1.1\n---\n\"foo\"\n")
|
276
|
+
assert_called :start_document, [[1,1], [], false]
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_start_document_tag
|
280
|
+
@parser.parse("%TAG !yaml! tag:yaml.org,2002\n---\n!yaml!str \"foo\"\n")
|
281
|
+
assert_called :start_document, [[], [['!yaml!', 'tag:yaml.org,2002']], false]
|
282
|
+
end
|
283
|
+
|
284
|
+
def assert_called call, with = nil, parser = @parser
|
285
|
+
if with
|
286
|
+
call = parser.handler.calls.find { |x|
|
287
|
+
x.first == call && x.last.compact == with
|
288
|
+
}
|
289
|
+
assert(call,
|
290
|
+
"#{[call,with].inspect} not in #{parser.handler.calls.inspect}"
|
291
|
+
)
|
292
|
+
else
|
293
|
+
assert parser.handler.calls.any? { |x| x.first == call }
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|