summaryrefslogtreecommitdiff
path: root/spec/ruby/library
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2021-05-17 20:17:52 +0200
committerBenoit Daloze <eregontp@gmail.com>2021-05-17 20:17:52 +0200
commit725b6660769c0fb5cb66c725d3271949c68e21d1 (patch)
treeb02cd7a377cffa586f10d83c1b47a4b885199563 /spec/ruby/library
parent2321ade41d9644207efb262137a3748b85b6c635 (diff)
Use a shared spec for YAML.load and YAML.unsafe_load
Diffstat (limited to 'spec/ruby/library')
-rw-r--r--spec/ruby/library/yaml/load_spec.rb135
-rw-r--r--spec/ruby/library/yaml/shared/load.rb136
-rw-r--r--spec/ruby/library/yaml/unsafe_load_spec.rb135
3 files changed, 140 insertions, 266 deletions
diff --git a/spec/ruby/library/yaml/load_spec.rb b/spec/ruby/library/yaml/load_spec.rb
index 525685ffe2..a84b6097c7 100644
--- a/spec/ruby/library/yaml/load_spec.rb
+++ b/spec/ruby/library/yaml/load_spec.rb
@@ -1,139 +1,8 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-require_relative 'fixtures/strings'
+require_relative 'shared/load'
ruby_version_is ""..."3.1" do
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
- -> { 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 "loads a File but raise an error when used as it is uninitialized" do
- loaded = YAML.load("--- !ruby/object:File {}\n")
- -> {
- loaded.read(1)
- }.should raise_error(IOError)
- end
+ it_behaves_like :yaml_load, :load
end
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..5e7b455462
--- /dev/null
+++ b/spec/ruby/library/yaml/shared/load.rb
@@ -0,0 +1,136 @@
+require_relative '../fixtures/common'
+require_relative '../fixtures/strings'
+
+describe :yaml_load, shared: true 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.send(@method, 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.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(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 "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, $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.send(@method, string).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
+ require "ostruct"
+ 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_error(IOError)
+ end
+end
diff --git a/spec/ruby/library/yaml/unsafe_load_spec.rb b/spec/ruby/library/yaml/unsafe_load_spec.rb
index f7901747d7..66eaf859c1 100644
--- a/spec/ruby/library/yaml/unsafe_load_spec.rb
+++ b/spec/ruby/library/yaml/unsafe_load_spec.rb
@@ -1,139 +1,8 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/common'
-require_relative 'fixtures/strings'
+require_relative 'shared/load'
ruby_version_is "3.1" do
describe "YAML.unsafe_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.unsafe_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.unsafe_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.unsafe_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
- -> { YAML.unsafe_load("key1: value\ninvalid_key") }.should raise_error(error)
- end
-
- it "accepts symbols" do
- YAML.unsafe_load( "--- :locked" ).should == :locked
- end
-
- it "accepts numbers" do
- YAML.unsafe_load("47").should == 47
- YAML.unsafe_load("-1").should == -1
- end
-
- it "accepts collections" do
- expected = ["a", "b", "c"]
- YAML.unsafe_load("--- \n- a\n- b\n- c\n").should == expected
- YAML.unsafe_load("--- [a, b, c]").should == expected
- YAML.unsafe_load("[a, b, c]").should == expected
- end
-
- it "parses start markers" do
- YAML.unsafe_load("---\n").should == nil
- YAML.unsafe_load("--- ---\n").should == "---"
- YAML.unsafe_load("--- abc").should == "abc"
- end
-
- it "works with block sequence shortcuts" do
- block_seq = "- - - one\n - two\n - three"
- YAML.unsafe_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.unsafe_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.unsafe_load(string).should == expected
- end
-
- describe "with iso8601 timestamp" do
- it "computes the microseconds" do
- [ [YAML.unsafe_load("2011-03-22t23:32:11.2233+01:00"), 223300],
- [YAML.unsafe_load("2011-03-22t23:32:11.0099+01:00"), 9900],
- [YAML.unsafe_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.unsafe_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.unsafe_load("--- !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.unsafe_load("--- !ruby/object:File {}\n")
- -> {
- loaded.read(1)
- }.should raise_error(IOError)
- end
+ it_behaves_like :yaml_load, :unsafe_load
end
end