psych 2.0.14-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +18 -0
  3. data/.gemtest +0 -0
  4. data/.travis.yml +16 -0
  5. data/CHANGELOG.rdoc +576 -0
  6. data/Manifest.txt +114 -0
  7. data/README.rdoc +71 -0
  8. data/Rakefile +123 -0
  9. data/ext/psych/depend +3 -0
  10. data/ext/psych/extconf.rb +38 -0
  11. data/ext/psych/psych.c +34 -0
  12. data/ext/psych/psych.h +20 -0
  13. data/ext/psych/psych_emitter.c +555 -0
  14. data/ext/psych/psych_emitter.h +8 -0
  15. data/ext/psych/psych_parser.c +597 -0
  16. data/ext/psych/psych_parser.h +6 -0
  17. data/ext/psych/psych_to_ruby.c +43 -0
  18. data/ext/psych/psych_to_ruby.h +8 -0
  19. data/ext/psych/psych_yaml_tree.c +24 -0
  20. data/ext/psych/psych_yaml_tree.h +8 -0
  21. data/ext/psych/yaml/LICENSE +19 -0
  22. data/ext/psych/yaml/api.c +1415 -0
  23. data/ext/psych/yaml/config.h +10 -0
  24. data/ext/psych/yaml/dumper.c +394 -0
  25. data/ext/psych/yaml/emitter.c +2329 -0
  26. data/ext/psych/yaml/loader.c +459 -0
  27. data/ext/psych/yaml/parser.c +1370 -0
  28. data/ext/psych/yaml/reader.c +469 -0
  29. data/ext/psych/yaml/scanner.c +3576 -0
  30. data/ext/psych/yaml/writer.c +141 -0
  31. data/ext/psych/yaml/yaml.h +1971 -0
  32. data/ext/psych/yaml/yaml_private.h +664 -0
  33. data/lib/psych.jar +0 -0
  34. data/lib/psych.rb +504 -0
  35. data/lib/psych/class_loader.rb +101 -0
  36. data/lib/psych/coder.rb +94 -0
  37. data/lib/psych/core_ext.rb +35 -0
  38. data/lib/psych/deprecated.rb +85 -0
  39. data/lib/psych/exception.rb +13 -0
  40. data/lib/psych/handler.rb +249 -0
  41. data/lib/psych/handlers/document_stream.rb +22 -0
  42. data/lib/psych/handlers/recorder.rb +39 -0
  43. data/lib/psych/json/ruby_events.rb +19 -0
  44. data/lib/psych/json/stream.rb +16 -0
  45. data/lib/psych/json/tree_builder.rb +12 -0
  46. data/lib/psych/json/yaml_events.rb +29 -0
  47. data/lib/psych/nodes.rb +77 -0
  48. data/lib/psych/nodes/alias.rb +18 -0
  49. data/lib/psych/nodes/document.rb +60 -0
  50. data/lib/psych/nodes/mapping.rb +56 -0
  51. data/lib/psych/nodes/node.rb +55 -0
  52. data/lib/psych/nodes/scalar.rb +67 -0
  53. data/lib/psych/nodes/sequence.rb +81 -0
  54. data/lib/psych/nodes/stream.rb +37 -0
  55. data/lib/psych/omap.rb +4 -0
  56. data/lib/psych/parser.rb +51 -0
  57. data/lib/psych/scalar_scanner.rb +149 -0
  58. data/lib/psych/set.rb +4 -0
  59. data/lib/psych/stream.rb +37 -0
  60. data/lib/psych/streaming.rb +27 -0
  61. data/lib/psych/syntax_error.rb +21 -0
  62. data/lib/psych/tree_builder.rb +96 -0
  63. data/lib/psych/versions.rb +3 -0
  64. data/lib/psych/visitors.rb +6 -0
  65. data/lib/psych/visitors/depth_first.rb +26 -0
  66. data/lib/psych/visitors/emitter.rb +51 -0
  67. data/lib/psych/visitors/json_tree.rb +24 -0
  68. data/lib/psych/visitors/to_ruby.rb +404 -0
  69. data/lib/psych/visitors/visitor.rb +19 -0
  70. data/lib/psych/visitors/yaml_tree.rb +605 -0
  71. data/lib/psych/y.rb +9 -0
  72. data/lib/psych_jars.rb +5 -0
  73. data/test/psych/handlers/test_recorder.rb +25 -0
  74. data/test/psych/helper.rb +121 -0
  75. data/test/psych/json/test_stream.rb +109 -0
  76. data/test/psych/nodes/test_enumerable.rb +43 -0
  77. data/test/psych/test_alias_and_anchor.rb +96 -0
  78. data/test/psych/test_array.rb +57 -0
  79. data/test/psych/test_boolean.rb +36 -0
  80. data/test/psych/test_class.rb +36 -0
  81. data/test/psych/test_coder.rb +206 -0
  82. data/test/psych/test_date_time.rb +38 -0
  83. data/test/psych/test_deprecated.rb +214 -0
  84. data/test/psych/test_document.rb +46 -0
  85. data/test/psych/test_emitter.rb +93 -0
  86. data/test/psych/test_encoding.rb +259 -0
  87. data/test/psych/test_exception.rb +157 -0
  88. data/test/psych/test_hash.rb +94 -0
  89. data/test/psych/test_json_tree.rb +65 -0
  90. data/test/psych/test_merge_keys.rb +180 -0
  91. data/test/psych/test_nil.rb +18 -0
  92. data/test/psych/test_null.rb +19 -0
  93. data/test/psych/test_numeric.rb +45 -0
  94. data/test/psych/test_object.rb +44 -0
  95. data/test/psych/test_object_references.rb +71 -0
  96. data/test/psych/test_omap.rb +75 -0
  97. data/test/psych/test_parser.rb +339 -0
  98. data/test/psych/test_psych.rb +168 -0
  99. data/test/psych/test_safe_load.rb +97 -0
  100. data/test/psych/test_scalar.rb +11 -0
  101. data/test/psych/test_scalar_scanner.rb +106 -0
  102. data/test/psych/test_serialize_subclasses.rb +38 -0
  103. data/test/psych/test_set.rb +49 -0
  104. data/test/psych/test_stream.rb +93 -0
  105. data/test/psych/test_string.rb +226 -0
  106. data/test/psych/test_struct.rb +49 -0
  107. data/test/psych/test_symbol.rb +25 -0
  108. data/test/psych/test_tainted.rb +130 -0
  109. data/test/psych/test_to_yaml_properties.rb +63 -0
  110. data/test/psych/test_tree_builder.rb +79 -0
  111. data/test/psych/test_yaml.rb +1292 -0
  112. data/test/psych/test_yamldbm.rb +193 -0
  113. data/test/psych/test_yamlstore.rb +85 -0
  114. data/test/psych/visitors/test_depth_first.rb +49 -0
  115. data/test/psych/visitors/test_emitter.rb +144 -0
  116. data/test/psych/visitors/test_to_ruby.rb +333 -0
  117. data/test/psych/visitors/test_yaml_tree.rb +173 -0
  118. metadata +240 -0
data/lib/psych/y.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Kernel
2
+ ###
3
+ # An alias for Psych.dump_stream meant to be used with IRB.
4
+ def y *objects
5
+ puts Psych.dump_stream(*objects)
6
+ end
7
+ private :y
8
+ end
9
+
data/lib/psych_jars.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'psych/versions'
2
+ require 'psych.jar'
3
+
4
+ require 'jar-dependencies'
5
+ require_jar('org.yaml', 'snakeyaml', Psych::DEFAULT_SNAKEYAML_VERSION)
@@ -0,0 +1,25 @@
1
+ require 'psych/helper'
2
+ require 'psych/handlers/recorder'
3
+
4
+ module Psych
5
+ module Handlers
6
+ class TestRecorder < TestCase
7
+ def test_replay
8
+ yaml = "--- foo\n...\n"
9
+ output = StringIO.new
10
+
11
+ recorder = Psych::Handlers::Recorder.new
12
+ parser = Psych::Parser.new recorder
13
+ parser.parse yaml
14
+
15
+ assert_equal 5, recorder.events.length
16
+
17
+ emitter = Psych::Emitter.new output
18
+ recorder.events.each do |m, args|
19
+ emitter.send m, *args
20
+ end
21
+ assert_equal yaml, output.string
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,121 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+ require 'tempfile'
4
+ require 'date'
5
+
6
+ require 'psych'
7
+
8
+ module Psych
9
+ superclass = if defined?(Minitest::Test)
10
+ Minitest::Test
11
+ else
12
+ MiniTest::Unit::TestCase
13
+ end
14
+
15
+ class TestCase < superclass
16
+ def self.suppress_warning
17
+ verbose, $VERBOSE = $VERBOSE, nil
18
+ yield
19
+ ensure
20
+ $VERBOSE = verbose
21
+ end
22
+
23
+ def with_default_external(enc)
24
+ verbose, $VERBOSE = $VERBOSE, nil
25
+ origenc, Encoding.default_external = Encoding.default_external, enc
26
+ $VERBOSE = verbose
27
+ yield
28
+ ensure
29
+ verbose, $VERBOSE = $VERBOSE, nil
30
+ Encoding.default_external = origenc
31
+ $VERBOSE = verbose
32
+ end
33
+
34
+ def with_default_internal(enc)
35
+ verbose, $VERBOSE = $VERBOSE, nil
36
+ origenc, Encoding.default_internal = Encoding.default_internal, enc
37
+ $VERBOSE = verbose
38
+ yield
39
+ ensure
40
+ verbose, $VERBOSE = $VERBOSE, nil
41
+ Encoding.default_internal = origenc
42
+ $VERBOSE = verbose
43
+ end
44
+
45
+ #
46
+ # Convert between Psych and the object to verify correct parsing and
47
+ # emitting
48
+ #
49
+ def assert_to_yaml( obj, yaml )
50
+ assert_equal( obj, Psych::load( yaml ) )
51
+ assert_equal( obj, Psych::parse( yaml ).transform )
52
+ assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
53
+ assert_equal( obj, Psych::parse( obj.psych_to_yaml ).transform )
54
+ assert_equal( obj, Psych::load(
55
+ obj.psych_to_yaml(
56
+ :UseVersion => true, :UseHeader => true, :SortKeys => true
57
+ )
58
+ ))
59
+ end
60
+
61
+ #
62
+ # Test parser only
63
+ #
64
+ def assert_parse_only( obj, yaml )
65
+ assert_equal( obj, Psych::load( yaml ) )
66
+ assert_equal( obj, Psych::parse( yaml ).transform )
67
+ end
68
+
69
+ def assert_cycle( obj )
70
+ v = Visitors::YAMLTree.create
71
+ v << obj
72
+ assert_equal(obj, Psych.load(v.tree.yaml))
73
+ assert_equal( obj, Psych::load(Psych.dump(obj)))
74
+ assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
75
+ end
76
+
77
+ #
78
+ # Make a time with the time zone
79
+ #
80
+ def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
81
+ usec = Rational(usec.to_s) * 1000000
82
+ val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
83
+ if zone != "Z"
84
+ hour = zone[0,3].to_i * 3600
85
+ min = zone[3,2].to_i * 60
86
+ ofs = (hour + min)
87
+ val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
88
+ end
89
+ return val
90
+ end
91
+ end
92
+ end
93
+
94
+ # backport so that tests will run on 1.9 and 2.0.0
95
+ unless Tempfile.respond_to? :create
96
+ def Tempfile.create(basename, *rest)
97
+ tmpfile = nil
98
+ Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
99
+ mode = File::RDWR|File::CREAT|File::EXCL
100
+ perm = 0600
101
+ if opts
102
+ mode |= opts.delete(:mode) || 0
103
+ opts[:perm] = perm
104
+ perm = nil
105
+ else
106
+ opts = perm
107
+ end
108
+ tmpfile = File.open(tmpname, mode, opts)
109
+ end
110
+ if block_given?
111
+ begin
112
+ yield tmpfile
113
+ ensure
114
+ tmpfile.close if !tmpfile.closed?
115
+ File.unlink tmpfile
116
+ end
117
+ else
118
+ tmpfile
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,109 @@
1
+ require 'psych/helper'
2
+
3
+ module Psych
4
+ module JSON
5
+ class TestStream < TestCase
6
+ def setup
7
+ @io = StringIO.new
8
+ @stream = Psych::JSON::Stream.new(@io)
9
+ @stream.start
10
+ end
11
+
12
+ def test_explicit_documents
13
+ @io = StringIO.new
14
+ @stream = Psych::JSON::Stream.new(@io)
15
+ @stream.start
16
+
17
+ @stream.push({ 'foo' => 'bar' })
18
+
19
+ assert !@stream.finished?, 'stream not finished'
20
+ @stream.finish
21
+ assert @stream.finished?, 'stream finished'
22
+
23
+ assert_match(/^---/, @io.string)
24
+ assert_match(/\.\.\.$/, @io.string)
25
+ end
26
+
27
+ def test_null
28
+ @stream.push(nil)
29
+ assert_match(/^--- null/, @io.string)
30
+ end
31
+
32
+ def test_string
33
+ @stream.push "foo"
34
+ assert_match(/(["])foo\1/, @io.string)
35
+ end
36
+
37
+ def test_symbol
38
+ @stream.push :foo
39
+ assert_match(/(["])foo\1/, @io.string)
40
+ end
41
+
42
+ def test_int
43
+ @stream.push 10
44
+ assert_match(/^--- 10/, @io.string)
45
+ end
46
+
47
+ def test_float
48
+ @stream.push 1.2
49
+ assert_match(/^--- 1.2/, @io.string)
50
+ end
51
+
52
+ def test_hash
53
+ hash = { 'one' => 'two' }
54
+ @stream.push hash
55
+
56
+ json = @io.string
57
+ assert_match(/}$/, json)
58
+ assert_match(/^--- \{/, json)
59
+ assert_match(/["]one['"]/, json)
60
+ assert_match(/["]two['"]/, json)
61
+ end
62
+
63
+ def test_list_to_json
64
+ list = %w{ one two }
65
+ @stream.push list
66
+
67
+ json = @io.string
68
+ assert_match(/\]$/, json)
69
+ assert_match(/^--- \[/, json)
70
+ assert_match(/["]one["]/, json)
71
+ assert_match(/["]two["]/, json)
72
+ end
73
+
74
+ class Foo; end
75
+
76
+ def test_json_dump_exclude_tag
77
+ @stream << Foo.new
78
+ json = @io.string
79
+ refute_match('Foo', json)
80
+ end
81
+
82
+ class Bar
83
+ def encode_with coder
84
+ coder.represent_seq 'omg', %w{ a b c }
85
+ end
86
+ end
87
+
88
+ def test_json_list_dump_exclude_tag
89
+ @stream << Bar.new
90
+ json = @io.string
91
+ refute_match('omg', json)
92
+ end
93
+
94
+ def test_time
95
+ time = Time.utc(2010, 10, 10)
96
+ @stream.push({'a' => time })
97
+ json = @io.string
98
+ assert_match "{\"a\": \"2010-10-10 00:00:00.000000000 Z\"}\n", json
99
+ end
100
+
101
+ def test_datetime
102
+ time = Time.new(2010, 10, 10).to_datetime
103
+ @stream.push({'a' => time })
104
+ json = @io.string
105
+ assert_match "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", json
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,43 @@
1
+ require 'psych/helper'
2
+
3
+ module Psych
4
+ module Nodes
5
+ class TestEnumerable < TestCase
6
+ def test_includes_enumerable
7
+ yaml = '--- hello'
8
+ assert_equal 3, Psych.parse_stream(yaml).to_a.length
9
+ end
10
+
11
+ def test_returns_enumerator
12
+ yaml = '--- hello'
13
+ assert_equal 3, Psych.parse_stream(yaml).each.map { |x| x }.length
14
+ end
15
+
16
+ def test_scalar
17
+ assert_equal 3, calls('--- hello').length
18
+ end
19
+
20
+ def test_sequence
21
+ assert_equal 4, calls("---\n- hello").length
22
+ end
23
+
24
+ def test_mapping
25
+ assert_equal 5, calls("---\nhello: world").length
26
+ end
27
+
28
+ def test_alias
29
+ assert_equal 5, calls("--- &yay\n- foo\n- *yay\n").length
30
+ end
31
+
32
+ private
33
+
34
+ def calls yaml
35
+ calls = []
36
+ Psych.parse_stream(yaml).each do |node|
37
+ calls << node
38
+ end
39
+ calls
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,96 @@
1
+ require_relative 'helper'
2
+
3
+ class ObjectWithInstanceVariables
4
+ attr_accessor :var1, :var2
5
+ end
6
+
7
+ class SubStringWithInstanceVariables < String
8
+ attr_accessor :var1
9
+ end
10
+
11
+ module Psych
12
+ class TestAliasAndAnchor < TestCase
13
+ def test_mri_compatibility
14
+ yaml = <<EOYAML
15
+ ---
16
+ - &id001 !ruby/object {}
17
+
18
+ - *id001
19
+ - *id001
20
+ EOYAML
21
+ result = Psych.load yaml
22
+ result.each {|el| assert_same(result[0], el) }
23
+ end
24
+
25
+ def test_mri_compatibility_object_with_ivars
26
+ yaml = <<EOYAML
27
+ ---
28
+ - &id001 !ruby/object:ObjectWithInstanceVariables
29
+ var1: test1
30
+ var2: test2
31
+ - *id001
32
+ - *id001
33
+ EOYAML
34
+
35
+ result = Psych.load yaml
36
+ result.each do |el|
37
+ assert_same(result[0], el)
38
+ assert_equal('test1', el.var1)
39
+ assert_equal('test2', el.var2)
40
+ end
41
+ end
42
+
43
+ def test_mri_compatibility_substring_with_ivars
44
+ yaml = <<EOYAML
45
+ ---
46
+ - &id001 !str:SubStringWithInstanceVariables
47
+ str: test
48
+ "@var1": test
49
+ - *id001
50
+ - *id001
51
+ EOYAML
52
+ result = Psych.load yaml
53
+ result.each do |el|
54
+ assert_same(result[0], el)
55
+ assert_equal('test', el.var1)
56
+ end
57
+ end
58
+
59
+ def test_anchor_alias_round_trip
60
+ o = Object.new
61
+ original = [o,o,o]
62
+
63
+ yaml = Psych.dump original
64
+ result = Psych.load yaml
65
+ result.each {|el| assert_same(result[0], el) }
66
+ end
67
+
68
+ def test_anchor_alias_round_trip_object_with_ivars
69
+ o = ObjectWithInstanceVariables.new
70
+ o.var1 = 'test1'
71
+ o.var2 = 'test2'
72
+ original = [o,o,o]
73
+
74
+ yaml = Psych.dump original
75
+ result = Psych.load yaml
76
+ result.each do |el|
77
+ assert_same(result[0], el)
78
+ assert_equal('test1', el.var1)
79
+ assert_equal('test2', el.var2)
80
+ end
81
+ end
82
+
83
+ def test_anchor_alias_round_trip_substring_with_ivars
84
+ o = SubStringWithInstanceVariables.new
85
+ o.var1 = 'test'
86
+ original = [o,o,o]
87
+
88
+ yaml = Psych.dump original
89
+ result = Psych.load yaml
90
+ result.each do |el|
91
+ assert_same(result[0], el)
92
+ assert_equal('test', el.var1)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'helper'
2
+
3
+ module Psych
4
+ class TestArray < TestCase
5
+ class X < Array
6
+ end
7
+
8
+ class Y < Array
9
+ attr_accessor :val
10
+ end
11
+
12
+ def setup
13
+ super
14
+ @list = [{ :a => 'b' }, 'foo']
15
+ end
16
+
17
+ def test_another_subclass_with_attributes
18
+ y = Y.new.tap {|y| y.val = 1}
19
+ y << "foo" << "bar"
20
+ y = Psych.load Psych.dump y
21
+
22
+ assert_equal %w{foo bar}, y
23
+ assert_equal Y, y.class
24
+ assert_equal 1, y.val
25
+ end
26
+
27
+ def test_subclass
28
+ yaml = Psych.dump X.new
29
+ assert_match X.name, yaml
30
+
31
+ list = X.new
32
+ list << 1
33
+ assert_equal X, list.class
34
+ assert_equal 1, list.first
35
+ end
36
+
37
+ def test_subclass_with_attributes
38
+ y = Psych.load Psych.dump Y.new.tap {|y| y.val = 1}
39
+ assert_equal Y, y.class
40
+ assert_equal 1, y.val
41
+ end
42
+
43
+ def test_backwards_with_syck
44
+ x = Psych.load "--- !seq:#{X.name} []\n\n"
45
+ assert_equal X, x.class
46
+ end
47
+
48
+ def test_self_referential
49
+ @list << @list
50
+ assert_cycle(@list)
51
+ end
52
+
53
+ def test_cycle
54
+ assert_cycle(@list)
55
+ end
56
+ end
57
+ end