Skip to content

Commit

Permalink
Merge pull request #1108 from herwinw/yaml
Browse files Browse the repository at this point in the history
Cleanup of YAML specs
  • Loading branch information
andrykonchin authored Nov 12, 2023
2 parents 745cac7 + 4fdbe00 commit b4e426c
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 27 deletions.
14 changes: 9 additions & 5 deletions library/yaml/dump_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion library/yaml/dump_stream_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions library/yaml/fixtures/common.rb

This file was deleted.

13 changes: 9 additions & 4 deletions library/yaml/load_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion library/yaml/load_stream_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions library/yaml/parse_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions library/yaml/parse_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion library/yaml/shared/each_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
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
Expand Down
10 changes: 6 additions & 4 deletions library/yaml/shared/load.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion library/yaml/to_yaml_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b4e426c

Please sign in to comment.