Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs for IO#autoclose? and IO#autoclose= #1077

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions core/io/autoclose_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "IO#autoclose" do
before :each do
@io = IOSpecs.io_fixture "lines.txt"
end

after :each do
@io.autoclose = true unless @io.closed?
@io.close unless @io.closed?
end

it "is set to true by default" do
@io.should.autoclose?
end

it "can be set to true" do
@io.autoclose = false
@io.autoclose = true
@io.should.autoclose?
end

it "can be set to false" do
@io.autoclose = true
@io.autoclose = false
@io.should_not.autoclose?
end

it "can be set to any truthy value" do
@io.autoclose = false
@io.autoclose = 42
@io.should.autoclose?
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

@io.autoclose = false
@io.autoclose = Object.new
@io.should.autoclose?
end

it "can be set to any falsy value" do
@io.autoclose = true
@io.autoclose = nil
@io.should_not.autoclose?
end

it "can be set multple times" do
@io.autoclose = true
@io.should.autoclose?

@io.autoclose = false
@io.should_not.autoclose?

@io.autoclose = true
@io.should.autoclose?
end

it "cannot be queried on a closed IO object" do
@io.close
-> { @io.autoclose? }.should raise_error(IOError, /closed stream/)
end

it "cannot be set on a closed IO object" do
@io.close
-> { @io.autoclose = false }.should raise_error(IOError, /closed stream/)
end
end
Copy link
Member

@andrykonchin andrykonchin Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but it makes sense to have separate test cases for the methods #autoclose= and #autoclose?.

There could also be added the following checks:

  • nil value is treated as false in #autoclose=
  • by default #autoclose? returns true (it's actually describing behaviour of a constructor method)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both additional checks have been added