diff options
Diffstat (limited to 'spec/ruby/library/yaml')
| -rw-r--r-- | spec/ruby/library/yaml/dump_spec.rb | 64 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/dump_stream_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/fixtures/example_class.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/fixtures/strings.rb | 26 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/fixtures/test_yaml.yml | 2 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/load_file_spec.rb | 18 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/load_spec.rb | 10 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/load_stream_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/parse_file_spec.rb | 10 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/parse_spec.rb | 23 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/shared/each_document.rb | 19 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/shared/load.rb | 142 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/to_yaml_spec.rb | 114 | ||||
| -rw-r--r-- | spec/ruby/library/yaml/unsafe_load_spec.rb | 9 |
14 files changed, 462 insertions, 0 deletions
diff --git a/spec/ruby/library/yaml/dump_spec.rb b/spec/ruby/library/yaml/dump_spec.rb new file mode 100644 index 0000000000..97b665d6a5 --- /dev/null +++ b/spec/ruby/library/yaml/dump_spec.rb @@ -0,0 +1,64 @@ +require_relative '../../spec_helper' + +require 'yaml' + +describe "YAML.dump" do + before :each do + @test_file = tmp("yaml_test_file") + end + + after :each do + 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| + YAML.dump( ['badger', 'elephant', 'tiger'], io ) + end + YAML.load_file(@test_file).should == ['badger', 'elephant', 'tiger'] + end + + it "returns a string containing dumped YAML when no io provided" do + YAML.dump( :locked ).should match_yaml("--- :locked\n") + end + + it "returns the same string that #to_yaml on objects" do + ["a", "b", "c"].to_yaml.should == YAML.dump(["a", "b", "c"]) + end + + it "dumps strings into YAML strings" do + YAML.dump("str").should match_yaml("--- str\n") + end + + it "dumps hashes into YAML key-values" do + YAML.dump({ "a" => "b" }).should match_yaml("--- \na: b\n") + end + + it "dumps Arrays into YAML collection" do + YAML.dump(["a", "b", "c"]).should match_yaml("--- \n- a\n- b\n- c\n") + end + + it "dumps an OpenStruct" do + begin + require "ostruct" + rescue LoadError + skip "OpenStruct is not available" + end + os = OpenStruct.new("age" => 20, "name" => "John") + 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 + file = File.new(__FILE__) + begin + YAML.dump(file).should match_yaml("--- !ruby/object:File {}\n") + ensure + file.close + end + end +end diff --git a/spec/ruby/library/yaml/dump_stream_spec.rb b/spec/ruby/library/yaml/dump_stream_spec.rb new file mode 100644 index 0000000000..f0578fa800 --- /dev/null +++ b/spec/ruby/library/yaml/dump_stream_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' + +require 'yaml' + +describe "YAML.dump_stream" do + it "returns a YAML stream containing the objects passed" do + YAML.dump_stream('foo', 20, [], {}).should match_yaml("--- foo\n--- 20\n--- []\n\n--- {}\n\n") + end +end diff --git a/spec/ruby/library/yaml/fixtures/example_class.rb b/spec/ruby/library/yaml/fixtures/example_class.rb new file mode 100644 index 0000000000..8259870799 --- /dev/null +++ b/spec/ruby/library/yaml/fixtures/example_class.rb @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000000..f478f89823 --- /dev/null +++ b/spec/ruby/library/yaml/fixtures/strings.rb @@ -0,0 +1,26 @@ +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 + + MULTIDOCUMENT = <<~EOY + --- + - Mark McGwire + - Sammy Sosa + - Ken Griffey + + # Team ranking + --- + - Chicago Cubs + - St Louis Cardinals + EOY +end diff --git a/spec/ruby/library/yaml/fixtures/test_yaml.yml b/spec/ruby/library/yaml/fixtures/test_yaml.yml new file mode 100644 index 0000000000..efe3b5cc1a --- /dev/null +++ b/spec/ruby/library/yaml/fixtures/test_yaml.yml @@ -0,0 +1,2 @@ +project: + name: RubySpec diff --git a/spec/ruby/library/yaml/load_file_spec.rb b/spec/ruby/library/yaml/load_file_spec.rb new file mode 100644 index 0000000000..4941d0485b --- /dev/null +++ b/spec/ruby/library/yaml/load_file_spec.rb @@ -0,0 +1,18 @@ +require_relative '../../spec_helper' + +require 'yaml' + +describe "YAML.load_file" do + before :each do + @test_file = tmp("yaml_test_file") + end + + after :each do + 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} + end +end diff --git a/spec/ruby/library/yaml/load_spec.rb b/spec/ruby/library/yaml/load_spec.rb new file mode 100644 index 0000000000..56700a85f9 --- /dev/null +++ b/spec/ruby/library/yaml/load_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' +require_relative 'shared/load' + +describe "YAML.load" do + it_behaves_like :yaml_load_safe, :load + + 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 new file mode 100644 index 0000000000..31bc862f5e --- /dev/null +++ b/spec/ruby/library/yaml/load_stream_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +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/parse_file_spec.rb b/spec/ruby/library/yaml/parse_file_spec.rb new file mode 100644 index 0000000000..a29377f163 --- /dev/null +++ b/spec/ruby/library/yaml/parse_file_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' + +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 new file mode 100644 index 0000000000..832cd99d03 --- /dev/null +++ b/spec/ruby/library/yaml/parse_spec.rb @@ -0,0 +1,23 @@ +require_relative '../../spec_helper' + +require 'yaml' + +describe "YAML.parse with an empty string" do + it "returns false" do + YAML.parse('').should == false + end +end + +describe "YAML.parse" do + before :each do + @string_yaml = "foo".to_yaml + end + + it "returns the value from the object" do + if YAML.to_s == "Psych" + YAML.parse(@string_yaml).to_ruby.should == "foo" + else + YAML.parse(@string_yaml).value.should == "foo" + end + end +end diff --git a/spec/ruby/library/yaml/shared/each_document.rb b/spec/ruby/library/yaml/shared/each_document.rb new file mode 100644 index 0000000000..6f00aee297 --- /dev/null +++ b/spec/ruby/library/yaml/shared/each_document.rb @@ -0,0 +1,19 @@ +describe :yaml_each_document, shared: true do + it "calls the block on each successive document" do + documents = [] + YAML.send(@method, YAMLSpecs::MULTIDOCUMENT) do |doc| + documents << doc + end + documents.should == [["Mark McGwire", "Sammy Sosa", "Ken Griffey"], + ["Chicago Cubs", "St Louis Cardinals"]] + end + + it "works on files" do + 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 + end + 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..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/to_yaml_spec.rb b/spec/ruby/library/yaml/to_yaml_spec.rb new file mode 100644 index 0000000000..328ab25552 --- /dev/null +++ b/spec/ruby/library/yaml/to_yaml_spec.rb @@ -0,0 +1,114 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/example_class' + +require 'yaml' + +describe "Object#to_yaml" do + + it "returns the YAML representation of an Array object" do + %w( 30 ruby maz irb 99 ).to_yaml.gsub("'", '"').should match_yaml("--- \n- \"30\"\n- ruby\n- maz\n- irb\n- \"99\"\n") + end + + it "returns the YAML representation of a Hash object" 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 + 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.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.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.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.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.is_a?(NilClass) + nil_klass.to_yaml.should match_yaml("--- \n") + end + + it "returns the YAML representation of a RegExp object" do + Regexp.new('^a-z+:\\s+\w+').to_yaml.should match_yaml("--- !ruby/regexp /^a-z+:\\s+\\w+/\n") + end + + it "returns the YAML representation of a String object" do + "I love Ruby".to_yaml.should match_yaml("--- I love Ruby\n") + end + + 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 + :symbol.to_yaml.should match_yaml("--- :symbol\n") + end + + it "returns the YAML representation of a Time object" do + Time.utc(2000,"jan",1,20,15,1).to_yaml.sub(/\.0+/, "").should match_yaml("--- 2000-01-01 20:15:01 Z\n") + end + + it "returns the YAML representation of a TrueClass" do + true_klass = true + 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\nbacktrace: \n") + end + + it "returns the YAML representation for Range objects" do + yaml = Range.new(1,3).to_yaml + 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 + nan_value.to_yaml.downcase.should match_yaml("--- .nan\n") + infinity_value.to_yaml.downcase.should match_yaml("--- .inf\n") + (-infinity_value).to_yaml.downcase.should match_yaml("--- -.inf\n") + (0.0).to_yaml.should match_yaml("--- 0.0\n") + end + + it "returns the YAML representation of an array of hashes" do + players = [{"a" => "b"}, {"b" => "c"}] + players.to_yaml.should match_yaml("--- \n- a: b\n- b: c\n") + end +end 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 |
