From 6198fc5acbeb15381df75a63f91004c4e6cf3798 Mon Sep 17 00:00:00 2001 From: Herwin Date: Mon, 11 Sep 2023 12:47:26 +0200 Subject: [PATCH] Add tests for to_str conversion in Kernel.(s)printf There is a similar test for IO#printf, but this tests the more generic case. --- core/kernel/printf_spec.rb | 7 +++++++ core/kernel/sprintf_spec.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/kernel/printf_spec.rb b/core/kernel/printf_spec.rb index d8f93ce429..61bf955c25 100644 --- a/core/kernel/printf_spec.rb +++ b/core/kernel/printf_spec.rb @@ -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 diff --git a/core/kernel/sprintf_spec.rb b/core/kernel/sprintf_spec.rb index 7adf71be76..9ef7f86f16 100644 --- a/core/kernel/sprintf_spec.rb +++ b/core/kernel/sprintf_spec.rb @@ -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) @@ -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 @@ -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