psych-shopifork 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +15 -0
  2. data/.autotest +18 -0
  3. data/.gemtest +0 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.rdoc +414 -0
  6. data/Manifest.txt +113 -0
  7. data/README.rdoc +71 -0
  8. data/Rakefile +72 -0
  9. data/ext/psych/depend +3 -0
  10. data/ext/psych/extconf.rb +36 -0
  11. data/ext/psych/psych.c +34 -0
  12. data/ext/psych/psych.h +20 -0
  13. data/ext/psych/psych_emitter.c +538 -0
  14. data/ext/psych/psych_emitter.h +8 -0
  15. data/ext/psych/psych_parser.c +579 -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 +1392 -0
  23. data/ext/psych/yaml/config.h +11 -0
  24. data/ext/psych/yaml/dumper.c +394 -0
  25. data/ext/psych/yaml/emitter.c +2335 -0
  26. data/ext/psych/yaml/loader.c +432 -0
  27. data/ext/psych/yaml/parser.c +1374 -0
  28. data/ext/psych/yaml/reader.c +465 -0
  29. data/ext/psych/yaml/scanner.c +3570 -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 +643 -0
  33. data/lib/psych.rb +497 -0
  34. data/lib/psych/class_loader.rb +101 -0
  35. data/lib/psych/coder.rb +94 -0
  36. data/lib/psych/core_ext.rb +35 -0
  37. data/lib/psych/deprecated.rb +85 -0
  38. data/lib/psych/exception.rb +13 -0
  39. data/lib/psych/handler.rb +249 -0
  40. data/lib/psych/handlers/document_stream.rb +22 -0
  41. data/lib/psych/handlers/recorder.rb +39 -0
  42. data/lib/psych/json/ruby_events.rb +19 -0
  43. data/lib/psych/json/stream.rb +16 -0
  44. data/lib/psych/json/tree_builder.rb +12 -0
  45. data/lib/psych/json/yaml_events.rb +29 -0
  46. data/lib/psych/nodes.rb +77 -0
  47. data/lib/psych/nodes/alias.rb +18 -0
  48. data/lib/psych/nodes/document.rb +60 -0
  49. data/lib/psych/nodes/mapping.rb +56 -0
  50. data/lib/psych/nodes/node.rb +55 -0
  51. data/lib/psych/nodes/scalar.rb +67 -0
  52. data/lib/psych/nodes/sequence.rb +81 -0
  53. data/lib/psych/nodes/stream.rb +37 -0
  54. data/lib/psych/omap.rb +4 -0
  55. data/lib/psych/parser.rb +51 -0
  56. data/lib/psych/scalar_scanner.rb +149 -0
  57. data/lib/psych/set.rb +4 -0
  58. data/lib/psych/stream.rb +37 -0
  59. data/lib/psych/streaming.rb +27 -0
  60. data/lib/psych/syntax_error.rb +21 -0
  61. data/lib/psych/tree_builder.rb +96 -0
  62. data/lib/psych/visitors.rb +6 -0
  63. data/lib/psych/visitors/depth_first.rb +26 -0
  64. data/lib/psych/visitors/emitter.rb +51 -0
  65. data/lib/psych/visitors/json_tree.rb +24 -0
  66. data/lib/psych/visitors/to_ruby.rb +372 -0
  67. data/lib/psych/visitors/visitor.rb +19 -0
  68. data/lib/psych/visitors/yaml_tree.rb +507 -0
  69. data/lib/psych/y.rb +9 -0
  70. data/test/psych/handlers/test_recorder.rb +25 -0
  71. data/test/psych/helper.rb +114 -0
  72. data/test/psych/json/test_stream.rb +109 -0
  73. data/test/psych/nodes/test_enumerable.rb +43 -0
  74. data/test/psych/test_alias_and_anchor.rb +96 -0
  75. data/test/psych/test_array.rb +57 -0
  76. data/test/psych/test_boolean.rb +36 -0
  77. data/test/psych/test_class.rb +36 -0
  78. data/test/psych/test_coder.rb +184 -0
  79. data/test/psych/test_date_time.rb +25 -0
  80. data/test/psych/test_deprecated.rb +214 -0
  81. data/test/psych/test_document.rb +46 -0
  82. data/test/psych/test_emitter.rb +94 -0
  83. data/test/psych/test_encoding.rb +254 -0
  84. data/test/psych/test_engine_manager.rb +47 -0
  85. data/test/psych/test_exception.rb +151 -0
  86. data/test/psych/test_hash.rb +44 -0
  87. data/test/psych/test_json_tree.rb +65 -0
  88. data/test/psych/test_merge_keys.rb +132 -0
  89. data/test/psych/test_nil.rb +18 -0
  90. data/test/psych/test_null.rb +19 -0
  91. data/test/psych/test_numeric.rb +45 -0
  92. data/test/psych/test_object.rb +44 -0
  93. data/test/psych/test_object_references.rb +67 -0
  94. data/test/psych/test_omap.rb +75 -0
  95. data/test/psych/test_parser.rb +339 -0
  96. data/test/psych/test_psych.rb +168 -0
  97. data/test/psych/test_safe_load.rb +97 -0
  98. data/test/psych/test_scalar.rb +11 -0
  99. data/test/psych/test_scalar_scanner.rb +106 -0
  100. data/test/psych/test_serialize_subclasses.rb +38 -0
  101. data/test/psych/test_set.rb +49 -0
  102. data/test/psych/test_stream.rb +93 -0
  103. data/test/psych/test_string.rb +153 -0
  104. data/test/psych/test_struct.rb +49 -0
  105. data/test/psych/test_symbol.rb +17 -0
  106. data/test/psych/test_tainted.rb +130 -0
  107. data/test/psych/test_to_yaml_properties.rb +63 -0
  108. data/test/psych/test_tree_builder.rb +79 -0
  109. data/test/psych/test_yaml.rb +1289 -0
  110. data/test/psych/test_yamldbm.rb +197 -0
  111. data/test/psych/test_yamlstore.rb +87 -0
  112. data/test/psych/visitors/test_depth_first.rb +49 -0
  113. data/test/psych/visitors/test_emitter.rb +144 -0
  114. data/test/psych/visitors/test_to_ruby.rb +326 -0
  115. data/test/psych/visitors/test_yaml_tree.rb +173 -0
  116. metadata +257 -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
+
@@ -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,114 @@
1
+ require 'minitest/autorun'
2
+ require 'stringio'
3
+ require 'tempfile'
4
+ require 'date'
5
+ require 'psych'
6
+
7
+ module Psych
8
+ class TestCase < MiniTest::Unit::TestCase
9
+ def self.suppress_warning
10
+ verbose, $VERBOSE = $VERBOSE, nil
11
+ yield
12
+ ensure
13
+ $VERBOSE = verbose
14
+ end
15
+
16
+ def with_default_external(enc)
17
+ verbose, $VERBOSE = $VERBOSE, nil
18
+ origenc, Encoding.default_external = Encoding.default_external, enc
19
+ $VERBOSE = verbose
20
+ yield
21
+ ensure
22
+ verbose, $VERBOSE = $VERBOSE, nil
23
+ Encoding.default_external = origenc
24
+ $VERBOSE = verbose
25
+ end
26
+
27
+ def with_default_internal(enc)
28
+ verbose, $VERBOSE = $VERBOSE, nil
29
+ origenc, Encoding.default_internal = Encoding.default_internal, enc
30
+ $VERBOSE = verbose
31
+ yield
32
+ ensure
33
+ verbose, $VERBOSE = $VERBOSE, nil
34
+ Encoding.default_internal = origenc
35
+ $VERBOSE = verbose
36
+ end
37
+
38
+ #
39
+ # Convert between Psych and the object to verify correct parsing and
40
+ # emitting
41
+ #
42
+ def assert_to_yaml( obj, yaml )
43
+ assert_equal( obj, Psych::load( yaml ) )
44
+ assert_equal( obj, Psych::parse( yaml ).transform )
45
+ assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
46
+ assert_equal( obj, Psych::parse( obj.psych_to_yaml ).transform )
47
+ assert_equal( obj, Psych::load(
48
+ obj.psych_to_yaml(
49
+ :UseVersion => true, :UseHeader => true, :SortKeys => true
50
+ )
51
+ ))
52
+ end
53
+
54
+ #
55
+ # Test parser only
56
+ #
57
+ def assert_parse_only( obj, yaml )
58
+ assert_equal( obj, Psych::load( yaml ) )
59
+ assert_equal( obj, Psych::parse( yaml ).transform )
60
+ end
61
+
62
+ def assert_cycle( obj )
63
+ v = Visitors::YAMLTree.create
64
+ v << obj
65
+ assert_equal(obj, Psych.load(v.tree.yaml))
66
+ assert_equal( obj, Psych::load(Psych.dump(obj)))
67
+ assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
68
+ end
69
+
70
+ #
71
+ # Make a time with the time zone
72
+ #
73
+ def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
74
+ usec = Rational(usec.to_s) * 1000000
75
+ val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
76
+ if zone != "Z"
77
+ hour = zone[0,3].to_i * 3600
78
+ min = zone[3,2].to_i * 60
79
+ ofs = (hour + min)
80
+ val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
81
+ end
82
+ return val
83
+ end
84
+ end
85
+ end
86
+
87
+ # backport so that tests will run on 1.9 and 2.0.0
88
+ unless Tempfile.respond_to? :create
89
+ def Tempfile.create(basename, *rest)
90
+ tmpfile = nil
91
+ Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
92
+ mode = File::RDWR|File::CREAT|File::EXCL
93
+ perm = 0600
94
+ if opts
95
+ mode |= opts.delete(:mode) || 0
96
+ opts[:perm] = perm
97
+ perm = nil
98
+ else
99
+ opts = perm
100
+ end
101
+ tmpfile = File.open(tmpname, mode, opts)
102
+ end
103
+ if block_given?
104
+ begin
105
+ yield tmpfile
106
+ ensure
107
+ tmpfile.close if !tmpfile.closed?
108
+ File.unlink tmpfile
109
+ end
110
+ else
111
+ tmpfile
112
+ end
113
+ end
114
+ 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