Skip to content

Commit

Permalink
Merge pull request #1078 from herwinw/kernel_sprintf_to_str
Browse files Browse the repository at this point in the history
Add tests for to_str conversion in Kernel.(s)printf
  • Loading branch information
andrykonchin authored Sep 28, 2023
2 parents ef91b0e + 6198fc5 commit 8b0b471
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/kernel/printf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
object.should_receive(:write).with("string")
Kernel.printf(object, "%s", "string")
end

it "calls #to_str to convert the format object to a String" do
object = mock('format string')
object.should_receive(:to_str).and_return("to_str: %i")
$stdout.should_receive(:write).with("to_str: 42")
Kernel.printf($stdout, object, 42)
end
end

describe "Kernel.printf" do
Expand Down
16 changes: 16 additions & 0 deletions core/kernel/sprintf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
require_relative 'shared/sprintf'
require_relative 'shared/sprintf_encoding'

describe :kernel_sprintf_to_str, shared: true do
it "calls #to_str to convert the format object to a String" do
obj = mock('format string')
obj.should_receive(:to_str).and_return("to_str: %i")
@method.call(obj, 42).should == "to_str: 42"
end
end

describe "Kernel#sprintf" do
it_behaves_like :kernel_sprintf, -> format, *args {
sprintf(format, *args)
Expand All @@ -11,6 +19,10 @@
it_behaves_like :kernel_sprintf_encoding, -> format, *args {
sprintf(format, *args)
}

it_behaves_like :kernel_sprintf_to_str, -> format, *args {
sprintf(format, *args)
}
end

describe "Kernel.sprintf" do
Expand All @@ -21,4 +33,8 @@
it_behaves_like :kernel_sprintf_encoding, -> format, *args {
Kernel.sprintf(format, *args)
}

it_behaves_like :kernel_sprintf_to_str, -> format, *args {
Kernel.sprintf(format, *args)
}
end

0 comments on commit 8b0b471

Please sign in to comment.