summaryrefslogtreecommitdiff
path: root/spec/ruby/library/yaml
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/yaml')
-rw-r--r--spec/ruby/library/yaml/add_builtin_type_spec.rb2
-rw-r--r--spec/ruby/library/yaml/add_domain_type_spec.rb2
-rw-r--r--spec/ruby/library/yaml/add_private_type_spec.rb2
-rw-r--r--spec/ruby/library/yaml/add_ruby_type_spec.rb2
-rw-r--r--spec/ruby/library/yaml/detect_implicit_spec.rb2
-rw-r--r--spec/ruby/library/yaml/dump_spec.rb27
-rw-r--r--spec/ruby/library/yaml/dump_stream_spec.rb3
-rw-r--r--spec/ruby/library/yaml/each_node_spec.rb2
-rw-r--r--spec/ruby/library/yaml/emitter_spec.rb2
-rw-r--r--spec/ruby/library/yaml/fixtures/common.rb10
-rw-r--r--spec/ruby/library/yaml/fixtures/example_class.rb8
-rw-r--r--spec/ruby/library/yaml/fixtures/strings.rb56
-rw-r--r--spec/ruby/library/yaml/generic_parser_spec.rb2
-rw-r--r--spec/ruby/library/yaml/load_documents_spec.rb10
-rw-r--r--spec/ruby/library/yaml/load_file_spec.rb13
-rw-r--r--spec/ruby/library/yaml/load_spec.rb135
-rw-r--r--spec/ruby/library/yaml/load_stream_spec.rb3
-rw-r--r--spec/ruby/library/yaml/object_maker_spec.rb2
-rw-r--r--spec/ruby/library/yaml/parse_documents_spec.rb2
-rw-r--r--spec/ruby/library/yaml/parse_file_spec.rb12
-rw-r--r--spec/ruby/library/yaml/parse_spec.rb9
-rw-r--r--spec/ruby/library/yaml/parser_spec.rb2
-rw-r--r--spec/ruby/library/yaml/quick_emit_spec.rb2
-rw-r--r--spec/ruby/library/yaml/read_type_class_spec.rb2
-rw-r--r--spec/ruby/library/yaml/shared/each_document.rb5
-rw-r--r--spec/ruby/library/yaml/shared/load.rb142
-rw-r--r--spec/ruby/library/yaml/tagurize_spec.rb11
-rw-r--r--spec/ruby/library/yaml/to_yaml_spec.rb41
-rw-r--r--spec/ruby/library/yaml/transfer_spec.rb2
-rw-r--r--spec/ruby/library/yaml/try_implicit_spec.rb2
-rw-r--r--spec/ruby/library/yaml/unsafe_load_spec.rb9
31 files changed, 258 insertions, 266 deletions
diff --git a/spec/ruby/library/yaml/add_builtin_type_spec.rb b/spec/ruby/library/yaml/add_builtin_type_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/add_builtin_type_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/add_domain_type_spec.rb b/spec/ruby/library/yaml/add_domain_type_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/add_domain_type_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/add_private_type_spec.rb b/spec/ruby/library/yaml/add_private_type_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/add_private_type_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/add_ruby_type_spec.rb b/spec/ruby/library/yaml/add_ruby_type_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/add_ruby_type_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/detect_implicit_spec.rb b/spec/ruby/library/yaml/detect_implicit_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/detect_implicit_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/dump_spec.rb b/spec/ruby/library/yaml/dump_spec.rb
index 5af794b7f8..97b665d6a5 100644
--- a/spec/ruby/library/yaml/dump_spec.rb
+++ b/spec/ruby/library/yaml/dump_spec.rb
@@ -1,17 +1,21 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-# TODO: WTF is this using a global?
+require 'yaml'
+
describe "YAML.dump" do
+ before :each do
+ @test_file = tmp("yaml_test_file")
+ end
+
after :each do
- rm_r $test_file
+ rm_r @test_file
end
it "converts an object to YAML and write result to io when io provided" do
- File.open($test_file, 'w' ) do |io|
+ File.open(@test_file, 'w' ) do |io|
YAML.dump( ['badger', 'elephant', 'tiger'], io )
end
- YAML.load_file($test_file).should == ['badger', 'elephant', 'tiger']
+ YAML.load_file(@test_file).should == ['badger', 'elephant', 'tiger']
end
it "returns a string containing dumped YAML when no io provided" do
@@ -35,9 +39,18 @@ describe "YAML.dump" do
end
it "dumps an OpenStruct" do
- require "ostruct"
+ begin
+ require "ostruct"
+ rescue LoadError
+ skip "OpenStruct is not available"
+ end
os = OpenStruct.new("age" => 20, "name" => "John")
- YAML.dump(os).should match_yaml("--- !ruby/object:OpenStruct\ntable:\n :age: 20\n :name: John\n")
+ yaml_dump = YAML.dump(os)
+
+ [
+ "--- !ruby/object:OpenStruct\nage: 20\nname: John\n",
+ "--- !ruby/object:OpenStruct\ntable:\n :age: 20\n :name: John\n",
+ ].should.include?(yaml_dump)
end
it "dumps a File without any state" do
diff --git a/spec/ruby/library/yaml/dump_stream_spec.rb b/spec/ruby/library/yaml/dump_stream_spec.rb
index 9d30fef819..f0578fa800 100644
--- a/spec/ruby/library/yaml/dump_stream_spec.rb
+++ b/spec/ruby/library/yaml/dump_stream_spec.rb
@@ -1,5 +1,6 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
+
+require 'yaml'
describe "YAML.dump_stream" do
it "returns a YAML stream containing the objects passed" do
diff --git a/spec/ruby/library/yaml/each_node_spec.rb b/spec/ruby/library/yaml/each_node_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/each_node_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/emitter_spec.rb b/spec/ruby/library/yaml/emitter_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/emitter_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/fixtures/common.rb b/spec/ruby/library/yaml/fixtures/common.rb
deleted file mode 100644
index 1d868806f1..0000000000
--- a/spec/ruby/library/yaml/fixtures/common.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-begin
- require 'syck'
-rescue LoadError
- # do nothing
-end
-
-require 'yaml'
-
-$test_file = tmp("yaml_test_file")
-$test_parse_file = File.dirname(__FILE__) + "/test_yaml.yml"
diff --git a/spec/ruby/library/yaml/fixtures/example_class.rb b/spec/ruby/library/yaml/fixtures/example_class.rb
index 751435a305..8259870799 100644
--- a/spec/ruby/library/yaml/fixtures/example_class.rb
+++ b/spec/ruby/library/yaml/fixtures/example_class.rb
@@ -1,5 +1,7 @@
-class FooBar
- def initialize(name)
- @name = name
+module YAMLSpecs
+ class Example
+ def initialize(name)
+ @name = name
+ end
end
end
diff --git a/spec/ruby/library/yaml/fixtures/strings.rb b/spec/ruby/library/yaml/fixtures/strings.rb
index 6f66dc3659..f478f89823 100644
--- a/spec/ruby/library/yaml/fixtures/strings.rb
+++ b/spec/ruby/library/yaml/fixtures/strings.rb
@@ -1,36 +1,26 @@
-$complex_key_1 = <<EOY
- ? # PLAY SCHEDULE
- - Detroit Tigers
- - Chicago Cubs
- :
- - 2001-07-23
+module YAMLSpecs
+ COMPLEX_KEY_1 = <<~EOY
+ ? # PLAY SCHEDULE
+ - Detroit Tigers
+ - Chicago Cubs
+ :
+ - 2001-07-23
- ? [ New York Yankees,
- Atlanta Braves ]
- : [ 2001-07-02, 2001-08-12,
- 2001-08-14 ]
-EOY
+ ? [ New York Yankees,
+ Atlanta Braves ]
+ : [ 2001-07-02, 2001-08-12,
+ 2001-08-14 ]
+ EOY
-$to_yaml_hash =
-<<EOY
--
- avg: 0.278
- hr: 65
- name: Mark McGwire
--
- avg: 0.288
- hr: 63
- name: Sammy Sosa
-EOY
+ MULTIDOCUMENT = <<~EOY
+ ---
+ - Mark McGwire
+ - Sammy Sosa
+ - Ken Griffey
-$multidocument = <<EOY
----
-- Mark McGwire
-- Sammy Sosa
-- Ken Griffey
-
-# Team ranking
----
-- Chicago Cubs
-- St Louis Cardinals
-EOY
+ # Team ranking
+ ---
+ - Chicago Cubs
+ - St Louis Cardinals
+ EOY
+end
diff --git a/spec/ruby/library/yaml/generic_parser_spec.rb b/spec/ruby/library/yaml/generic_parser_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/generic_parser_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/load_documents_spec.rb b/spec/ruby/library/yaml/load_documents_spec.rb
deleted file mode 100644
index 27edbcaa86..0000000000
--- a/spec/ruby/library/yaml/load_documents_spec.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-require_relative 'fixtures/strings'
-require_relative 'shared/each_document'
-
-ruby_version_is ''...'2.5' do
- describe "YAML.load_documents" do
- it_behaves_like :yaml_each_document, :load_documents
- end
-end
diff --git a/spec/ruby/library/yaml/load_file_spec.rb b/spec/ruby/library/yaml/load_file_spec.rb
index 2363c08120..4941d0485b 100644
--- a/spec/ruby/library/yaml/load_file_spec.rb
+++ b/spec/ruby/library/yaml/load_file_spec.rb
@@ -1,13 +1,18 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
+
+require 'yaml'
describe "YAML.load_file" do
+ before :each do
+ @test_file = tmp("yaml_test_file")
+ end
+
after :each do
- rm_r $test_file
+ rm_r @test_file
end
it "returns a hash" do
- File.open($test_file,'w' ){|io| YAML.dump( {"bar"=>2, "car"=>1}, io ) }
- YAML.load_file($test_file).should == {"bar"=>2, "car"=>1}
+ File.open(@test_file,'w' ){|io| YAML.dump( {"bar"=>2, "car"=>1}, io ) }
+ YAML.load_file(@test_file).should == {"bar"=>2, "car"=>1}
end
end
diff --git a/spec/ruby/library/yaml/load_spec.rb b/spec/ruby/library/yaml/load_spec.rb
index 1e16bed4e6..56700a85f9 100644
--- a/spec/ruby/library/yaml/load_spec.rb
+++ b/spec/ruby/library/yaml/load_spec.rb
@@ -1,137 +1,10 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-require_relative 'fixtures/strings'
+require_relative 'shared/load'
describe "YAML.load" do
- after :each do
- rm_r $test_file
- end
-
- it "returns a document from current io stream when io provided" do
- File.open($test_file, 'w') do |io|
- YAML.dump( ['badger', 'elephant', 'tiger'], io )
- end
- File.open($test_file) { |yf| YAML.load( yf ) }.should == ['badger', 'elephant', 'tiger']
- end
-
- it "loads strings" do
- strings = ["str",
- " str",
- "'str'",
- "str",
- " str",
- "'str'",
- "\"str\"",
- "\n str",
- "--- str",
- "---\nstr",
- "--- \nstr",
- "--- \n str",
- "--- 'str'"
- ]
- strings.each do |str|
- YAML.load(str).should == "str"
- end
- end
-
- it "loads strings with chars from non-base Unicode plane" do
- # We add these strings as bytes and force the encoding for safety
- # as bugs in parsing unicode characters can obscure bugs in this
- # area.
-
- yaml_and_strings = {
- # "--- 🌵" => "🌵"
- [45, 45, 45, 32, 240, 159, 140, 181] =>
- [240, 159, 140, 181],
- # "--- 🌵 and some text" => "🌵 and some text"
- [45, 45, 45, 32, 240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116] =>
- [240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116],
- # "--- Some text 🌵 and some text" => "Some text 🌵 and some text"
- [45, 45, 45, 32, 83, 111, 109, 101, 32, 116, 101, 120, 116, 32, 240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116] =>
- [83, 111, 109, 101, 32, 116, 101, 120, 116, 32, 240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116]
- }
- yaml_and_strings.each do |yaml, str|
- YAML.load(yaml.pack("C*").force_encoding("UTF-8")).should == str.pack("C*").force_encoding("UTF-8")
- end
- end
-
- it "fails on invalid keys" do
- if YAML.to_s == "Psych"
- error = Psych::SyntaxError
- else
- error = ArgumentError
- end
- lambda { YAML.load("key1: value\ninvalid_key") }.should raise_error(error)
- end
-
- it "accepts symbols" do
- YAML.load( "--- :locked" ).should == :locked
- end
-
- it "accepts numbers" do
- YAML.load("47").should == 47
- YAML.load("-1").should == -1
- end
-
- it "accepts collections" do
- expected = ["a", "b", "c"]
- YAML.load("--- \n- a\n- b\n- c\n").should == expected
- YAML.load("--- [a, b, c]").should == expected
- YAML.load("[a, b, c]").should == expected
- end
-
- it "parses start markers" do
- YAML.load("---\n").should == nil
- YAML.load("--- ---\n").should == "---"
- YAML.load("--- abc").should == "abc"
- end
-
- it "works with block sequence shortcuts" do
- block_seq = "- - - one\n - two\n - three"
- YAML.load(block_seq).should == [[["one", "two", "three"]]]
- end
-
- it "works on complex keys" do
- require 'date'
- expected = {
- [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ],
- [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ),
- Date.new( 2001, 8, 12 ),
- Date.new( 2001, 8, 14 ) ]
- }
- YAML.load($complex_key_1).should == expected
- end
-
- it "loads a symbol key that contains spaces" do
- string = ":user name: This is the user name."
- expected = { :"user name" => "This is the user name."}
- YAML.load(string).should == expected
- end
-
- describe "with iso8601 timestamp" do
- it "computes the microseconds" do
- [ [YAML.load("2011-03-22t23:32:11.2233+01:00"), 223300],
- [YAML.load("2011-03-22t23:32:11.0099+01:00"), 9900],
- [YAML.load("2011-03-22t23:32:11.000076+01:00"), 76]
- ].should be_computed_by(:usec)
- end
-
- it "rounds values smaller than 1 usec to 0 " do
- YAML.load("2011-03-22t23:32:11.000000342222+01:00").usec.should == 0
- end
- end
-
- it "loads an OpenStruct" do
- require "ostruct"
- os = OpenStruct.new("age" => 20, "name" => "John")
- loaded = YAML.load("--- !ruby/object:OpenStruct\ntable:\n :age: 20\n :name: John\n")
- loaded.should == os
- end
+ it_behaves_like :yaml_load_safe, :load
- it "loads a File but raise an error when used as it is uninitialized" do
- loaded = YAML.load("--- !ruby/object:File {}\n")
- lambda {
- loaded.read(1)
- }.should raise_error(IOError)
+ guard -> { Psych::VERSION < "4.0.0" } do
+ it_behaves_like :yaml_load_unsafe, :load
end
end
diff --git a/spec/ruby/library/yaml/load_stream_spec.rb b/spec/ruby/library/yaml/load_stream_spec.rb
index 689653c8cd..31bc862f5e 100644
--- a/spec/ruby/library/yaml/load_stream_spec.rb
+++ b/spec/ruby/library/yaml/load_stream_spec.rb
@@ -1,8 +1,9 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
require_relative 'fixtures/strings'
require_relative 'shared/each_document'
+require 'yaml'
+
describe "YAML.load_stream" do
it_behaves_like :yaml_each_document, :load_stream
end
diff --git a/spec/ruby/library/yaml/object_maker_spec.rb b/spec/ruby/library/yaml/object_maker_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/object_maker_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/parse_documents_spec.rb b/spec/ruby/library/yaml/parse_documents_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/parse_documents_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/parse_file_spec.rb b/spec/ruby/library/yaml/parse_file_spec.rb
index 8d307c5daf..a29377f163 100644
--- a/spec/ruby/library/yaml/parse_file_spec.rb
+++ b/spec/ruby/library/yaml/parse_file_spec.rb
@@ -1,10 +1,10 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-describe "YAML#parse_file" do
- quarantine! do
- it "returns a YAML::Syck::Map object after parsing a YAML file" do
- YAML.parse_file($test_parse_file).should be_kind_of(YAML::Syck::Map)
- end
+require 'yaml'
+
+describe "YAML.parse_file" do
+ it "returns a YAML::Syck::Map object after parsing a YAML file" do
+ test_parse_file = fixture __FILE__, "test_yaml.yml"
+ YAML.parse_file(test_parse_file).should.is_a?(Psych::Nodes::Document)
end
end
diff --git a/spec/ruby/library/yaml/parse_spec.rb b/spec/ruby/library/yaml/parse_spec.rb
index d5dbfdcee2..832cd99d03 100644
--- a/spec/ruby/library/yaml/parse_spec.rb
+++ b/spec/ruby/library/yaml/parse_spec.rb
@@ -1,13 +1,14 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-describe "YAML#parse with an empty string" do
+require 'yaml'
+
+describe "YAML.parse with an empty string" do
it "returns false" do
- YAML.parse('').should be_false
+ YAML.parse('').should == false
end
end
-describe "YAML#parse" do
+describe "YAML.parse" do
before :each do
@string_yaml = "foo".to_yaml
end
diff --git a/spec/ruby/library/yaml/parser_spec.rb b/spec/ruby/library/yaml/parser_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/parser_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/quick_emit_spec.rb b/spec/ruby/library/yaml/quick_emit_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/quick_emit_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/read_type_class_spec.rb b/spec/ruby/library/yaml/read_type_class_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/read_type_class_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/shared/each_document.rb b/spec/ruby/library/yaml/shared/each_document.rb
index 999123dc2a..6f00aee297 100644
--- a/spec/ruby/library/yaml/shared/each_document.rb
+++ b/spec/ruby/library/yaml/shared/each_document.rb
@@ -1,7 +1,7 @@
describe :yaml_each_document, shared: true do
it "calls the block on each successive document" do
documents = []
- YAML.send(@method, $multidocument) do |doc|
+ YAML.send(@method, YAMLSpecs::MULTIDOCUMENT) do |doc|
documents << doc
end
documents.should == [["Mark McGwire", "Sammy Sosa", "Ken Griffey"],
@@ -9,7 +9,8 @@ describe :yaml_each_document, shared: true do
end
it "works on files" do
- File.open($test_parse_file, "r") do |file|
+ test_parse_file = fixture __FILE__, "test_yaml.yml"
+ File.open(test_parse_file, "r") do |file|
YAML.send(@method, file) do |doc|
doc.should == {"project"=>{"name"=>"RubySpec"}}
end
diff --git a/spec/ruby/library/yaml/shared/load.rb b/spec/ruby/library/yaml/shared/load.rb
new file mode 100644
index 0000000000..7e5669f2d0
--- /dev/null
+++ b/spec/ruby/library/yaml/shared/load.rb
@@ -0,0 +1,142 @@
+require_relative '../fixtures/strings'
+
+require 'yaml'
+
+describe :yaml_load_safe, shared: true do
+ it "returns a document from current io stream when io provided" do
+ @test_file = tmp("yaml_test_file")
+ File.open(@test_file, 'w') do |io|
+ YAML.dump( ['badger', 'elephant', 'tiger'], io )
+ end
+ File.open(@test_file) { |yf| YAML.send(@method, yf ) }.should == ['badger', 'elephant', 'tiger']
+ ensure
+ rm_r @test_file
+ end
+
+ it "loads strings" do
+ strings = ["str",
+ " str",
+ "'str'",
+ "str",
+ " str",
+ "'str'",
+ "\"str\"",
+ "\n str",
+ "--- str",
+ "---\nstr",
+ "--- \nstr",
+ "--- \n str",
+ "--- 'str'"
+ ]
+ strings.each do |str|
+ YAML.send(@method, str).should == "str"
+ end
+ end
+
+ it "loads strings with chars from non-base Unicode plane" do
+ # We add these strings as bytes and force the encoding for safety
+ # as bugs in parsing unicode characters can obscure bugs in this
+ # area.
+
+ yaml_and_strings = {
+ # "--- 🌵" => "🌵"
+ [45, 45, 45, 32, 240, 159, 140, 181] =>
+ [240, 159, 140, 181],
+ # "--- 🌵 and some text" => "🌵 and some text"
+ [45, 45, 45, 32, 240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116] =>
+ [240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116],
+ # "--- Some text 🌵 and some text" => "Some text 🌵 and some text"
+ [45, 45, 45, 32, 83, 111, 109, 101, 32, 116, 101, 120, 116, 32, 240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116] =>
+ [83, 111, 109, 101, 32, 116, 101, 120, 116, 32, 240, 159, 140, 181, 32, 97, 110, 100, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116]
+ }
+ yaml_and_strings.each do |yaml, str|
+ YAML.send(@method, yaml.pack("C*").force_encoding("UTF-8")).should == str.pack("C*").force_encoding("UTF-8")
+ end
+ end
+
+ it "fails on invalid keys" do
+ if YAML.to_s == "Psych"
+ error = Psych::SyntaxError
+ else
+ error = ArgumentError
+ end
+ -> { YAML.send(@method, "key1: value\ninvalid_key") }.should.raise(error)
+ end
+
+ it "accepts symbols" do
+ YAML.send(@method, "--- :locked" ).should == :locked
+ end
+
+ it "accepts numbers" do
+ YAML.send(@method, "47").should == 47
+ YAML.send(@method, "-1").should == -1
+ end
+
+ it "accepts collections" do
+ expected = ["a", "b", "c"]
+ YAML.send(@method, "--- \n- a\n- b\n- c\n").should == expected
+ YAML.send(@method, "--- [a, b, c]").should == expected
+ YAML.send(@method, "[a, b, c]").should == expected
+ end
+
+ it "parses start markers" do
+ YAML.send(@method, "---\n").should == nil
+ YAML.send(@method, "--- ---\n").should == "---"
+ YAML.send(@method, "--- abc").should == "abc"
+ end
+
+ it "works with block sequence shortcuts" do
+ block_seq = "- - - one\n - two\n - three"
+ YAML.send(@method, block_seq).should == [[["one", "two", "three"]]]
+ end
+
+ it "loads a symbol key that contains spaces" do
+ string = ":user name: This is the user name."
+ expected = { :"user name" => "This is the user name."}
+ YAML.send(@method, string).should == expected
+ end
+end
+
+describe :yaml_load_unsafe, shared: true do
+ it "works on complex keys" do
+ require 'date'
+ expected = {
+ [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ],
+ [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ),
+ Date.new( 2001, 8, 12 ),
+ Date.new( 2001, 8, 14 ) ]
+ }
+ YAML.send(@method, YAMLSpecs::COMPLEX_KEY_1).should == expected
+ end
+
+ describe "with iso8601 timestamp" do
+ it "computes the microseconds" do
+ [ [YAML.send(@method, "2011-03-22t23:32:11.2233+01:00"), 223300],
+ [YAML.send(@method, "2011-03-22t23:32:11.0099+01:00"), 9900],
+ [YAML.send(@method, "2011-03-22t23:32:11.000076+01:00"), 76]
+ ].should be_computed_by(:usec)
+ end
+
+ it "rounds values smaller than 1 usec to 0" do
+ YAML.send(@method, "2011-03-22t23:32:11.000000342222+01:00").usec.should == 0
+ end
+ end
+
+ it "loads an OpenStruct" do
+ begin
+ require "ostruct"
+ rescue LoadError
+ skip "OpenStruct is not available"
+ end
+ os = OpenStruct.new("age" => 20, "name" => "John")
+ loaded = YAML.send(@method, "--- !ruby/object:OpenStruct\ntable:\n :age: 20\n :name: John\n")
+ loaded.should == os
+ end
+
+ it "loads a File but raise an error when used as it is uninitialized" do
+ loaded = YAML.send(@method, "--- !ruby/object:File {}\n")
+ -> {
+ loaded.read(1)
+ }.should.raise(IOError)
+ end
+end
diff --git a/spec/ruby/library/yaml/tagurize_spec.rb b/spec/ruby/library/yaml/tagurize_spec.rb
deleted file mode 100644
index cc1b757ce9..0000000000
--- a/spec/ruby/library/yaml/tagurize_spec.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-
-ruby_version_is ''...'2.5' do
- describe "YAML.tagurize" do
- it "converts a type_id to a taguri" do
- YAML.tagurize('wtf').should == "tag:yaml.org,2002:wtf"
- YAML.tagurize(1).should == 1
- end
- end
-end
diff --git a/spec/ruby/library/yaml/to_yaml_spec.rb b/spec/ruby/library/yaml/to_yaml_spec.rb
index 9713657a26..328ab25552 100644
--- a/spec/ruby/library/yaml/to_yaml_spec.rb
+++ b/spec/ruby/library/yaml/to_yaml_spec.rb
@@ -1,7 +1,8 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
require_relative 'fixtures/example_class'
+require 'yaml'
+
describe "Object#to_yaml" do
it "returns the YAML representation of an Array object" do
@@ -12,37 +13,44 @@ describe "Object#to_yaml" do
{ "a" => "b"}.to_yaml.should match_yaml("--- \na: b\n")
end
+ it "returns the YAML representation of an object" do
+ YAMLSpecs::Example.new("baz").to_yaml.should match_yaml("--- !ruby/object:YAMLSpecs::Example\nname: baz\n")
+ end
+
it "returns the YAML representation of a Class object" do
- FooBar.new("baz").to_yaml.should match_yaml("--- !ruby/object:FooBar\nname: baz\n")
+ YAMLSpecs::Example.to_yaml.should match_yaml("--- !ruby/class 'YAMLSpecs::Example'\n")
+ end
+ it "returns the YAML representation of a Module object" do
+ Enumerable.to_yaml.should match_yaml("--- !ruby/module 'Enumerable'\n")
end
it "returns the YAML representation of a Date object" do
require 'date'
- Date.parse('1997/12/30').to_yaml.should match_yaml("--- 1997-12-30\n")
+ Date.new(1997, 12, 30).to_yaml.should match_yaml("--- 1997-12-30\n")
end
it "returns the YAML representation of a FalseClass" do
false_klass = false
- false_klass.should be_kind_of(FalseClass)
+ false_klass.should.is_a?(FalseClass)
false_klass.to_yaml.should match_yaml("--- false\n")
end
it "returns the YAML representation of a Float object" do
float = 1.2
- float.should be_kind_of(Float)
+ float.should.is_a?(Float)
float.to_yaml.should match_yaml("--- 1.2\n")
end
it "returns the YAML representation of an Integer object" do
int = 20
- int.should be_kind_of(Integer)
+ int.should.is_a?(Integer)
int.to_yaml.should match_yaml("--- 20\n")
end
it "returns the YAML representation of a NilClass object" do
nil_klass = nil
- nil_klass.should be_kind_of(NilClass)
+ nil_klass.should.is_a?(NilClass)
nil_klass.to_yaml.should match_yaml("--- \n")
end
@@ -57,6 +65,13 @@ describe "Object#to_yaml" do
it "returns the YAML representation of a Struct object" do
Person = Struct.new(:name, :gender)
Person.new("Jane", "female").to_yaml.should match_yaml("--- !ruby/struct:Person\nname: Jane\ngender: female\n")
+ ensure
+ Object.send(:remove_const, :Person)
+ end
+
+ it "returns the YAML representation of an unnamed Struct object" do
+ person = Struct.new(:name, :gender)
+ person.new("Jane", "female").to_yaml.should match_yaml("--- !ruby/struct\nname: Jane\ngender: female\n")
end
it "returns the YAML representation of a Symbol object" do
@@ -69,20 +84,20 @@ describe "Object#to_yaml" do
it "returns the YAML representation of a TrueClass" do
true_klass = true
- true_klass.should be_kind_of(TrueClass)
+ true_klass.should.is_a?(TrueClass)
true_klass.to_yaml.should match_yaml("--- true\n")
end
it "returns the YAML representation of a Error object" do
- StandardError.new("foobar").to_yaml.should match_yaml("--- !ruby/exception:StandardError\nmessage: foobar\n")
+ StandardError.new("foobar").to_yaml.should match_yaml("--- !ruby/exception:StandardError\nmessage: foobar\nbacktrace: \n")
end
it "returns the YAML representation for Range objects" do
yaml = Range.new(1,3).to_yaml
- yaml.include?("!ruby/range").should be_true
- yaml.include?("begin: 1").should be_true
- yaml.include?("end: 3").should be_true
- yaml.include?("excl: false").should be_true
+ yaml.include?("!ruby/range").should == true
+ yaml.include?("begin: 1").should == true
+ yaml.include?("end: 3").should == true
+ yaml.include?("excl: false").should == true
end
it "returns the YAML representation of numeric constants" do
diff --git a/spec/ruby/library/yaml/transfer_spec.rb b/spec/ruby/library/yaml/transfer_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/transfer_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/try_implicit_spec.rb b/spec/ruby/library/yaml/try_implicit_spec.rb
deleted file mode 100644
index 44c820940f..0000000000
--- a/spec/ruby/library/yaml/try_implicit_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'fixtures/common'
diff --git a/spec/ruby/library/yaml/unsafe_load_spec.rb b/spec/ruby/library/yaml/unsafe_load_spec.rb
new file mode 100644
index 0000000000..385cd2a6e2
--- /dev/null
+++ b/spec/ruby/library/yaml/unsafe_load_spec.rb
@@ -0,0 +1,9 @@
+require_relative '../../spec_helper'
+require_relative 'shared/load'
+
+guard -> { Psych::VERSION >= "4.0.0" } do
+ describe "YAML.unsafe_load" do
+ it_behaves_like :yaml_load_safe, :unsafe_load
+ it_behaves_like :yaml_load_unsafe, :unsafe_load
+ end
+end