diff options
Diffstat (limited to 'spec/ruby/library/yaml')
-rw-r--r-- | spec/ruby/library/yaml/dump_spec.rb | 14 | ||||
-rw-r--r-- | spec/ruby/library/yaml/dump_stream_spec.rb | 3 | ||||
-rw-r--r-- | spec/ruby/library/yaml/fixtures/common.rb | 4 | ||||
-rw-r--r-- | spec/ruby/library/yaml/fixtures/strings.rb | 56 | ||||
-rw-r--r-- | spec/ruby/library/yaml/load_file_spec.rb | 13 | ||||
-rw-r--r-- | spec/ruby/library/yaml/load_stream_spec.rb | 3 | ||||
-rw-r--r-- | spec/ruby/library/yaml/parse_file_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/yaml/parse_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/library/yaml/shared/each_document.rb | 5 | ||||
-rw-r--r-- | spec/ruby/library/yaml/shared/load.rb | 12 | ||||
-rw-r--r-- | spec/ruby/library/yaml/to_yaml_spec.rb | 32 |
11 files changed, 83 insertions, 74 deletions
diff --git a/spec/ruby/library/yaml/dump_spec.rb b/spec/ruby/library/yaml/dump_spec.rb index 3107a8f51d..ea94b2f856 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 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/fixtures/common.rb b/spec/ruby/library/yaml/fixtures/common.rb deleted file mode 100644 index f7fb4037e7..0000000000 --- a/spec/ruby/library/yaml/fixtures/common.rb +++ /dev/null @@ -1,4 +0,0 @@ -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/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/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_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/parse_file_spec.rb b/spec/ruby/library/yaml/parse_file_spec.rb index 8c59a2d7ef..7bffcdc62f 100644 --- a/spec/ruby/library/yaml/parse_file_spec.rb +++ b/spec/ruby/library/yaml/parse_file_spec.rb @@ -1,8 +1,10 @@ require_relative '../../spec_helper' -require_relative 'fixtures/common' -describe "YAML#parse_file" do +require 'yaml' + +describe "YAML.parse_file" do it "returns a YAML::Syck::Map object after parsing a YAML file" do - YAML.parse_file($test_parse_file).should be_kind_of(Psych::Nodes::Document) + test_parse_file = fixture __FILE__, "test_yaml.yml" + YAML.parse_file(test_parse_file).should be_kind_of(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..37e2b7fa0a 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 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/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 index 185a5a60cd..bd55332fe5 100644 --- a/spec/ruby/library/yaml/shared/load.rb +++ b/spec/ruby/library/yaml/shared/load.rb @@ -1,14 +1,16 @@ -require_relative '../fixtures/common' 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 - File.open($test_file, 'w') do |io| + @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'] + File.open(@test_file) { |yf| YAML.send(@method, yf ) }.should == ['badger', 'elephant', 'tiger'] ensure - rm_r $test_file + rm_r @test_file end it "loads strings" do @@ -104,7 +106,7 @@ describe :yaml_load_unsafe, shared: true do Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ] } - YAML.send(@method, $complex_key_1).should == expected + YAML.send(@method, YAMLSpecs::COMPLEX_KEY_1).should == expected end describe "with iso8601 timestamp" do diff --git a/spec/ruby/library/yaml/to_yaml_spec.rb b/spec/ruby/library/yaml/to_yaml_spec.rb index 03ec4f6916..547009c942 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,13 +13,21 @@ describe "Object#to_yaml" do { "a" => "b"}.to_yaml.should match_yaml("--- \na: b\n") end - it "returns the YAML representation of a Class object" do + 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.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 @@ -58,6 +67,11 @@ describe "Object#to_yaml" do Person.new("Jane", "female").to_yaml.should match_yaml("--- !ruby/struct:Person\nname: Jane\ngender: female\n") 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 @@ -72,16 +86,8 @@ describe "Object#to_yaml" do true_klass.to_yaml.should match_yaml("--- true\n") end - ruby_version_is ""..."2.7" do - it "returns the YAML representation of a Error object" do - StandardError.new("foobar").to_yaml.should match_yaml("--- !ruby/exception:StandardError\nmessage: foobar\n") - end - end - - ruby_version_is "2.7" do - 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 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 |