Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions library/random/formatter/alphanumeric_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../../spec_helper'
require_relative 'shared/interface'

ruby_version_is "3.1" do
require 'random/formatter'
Expand Down Expand Up @@ -58,4 +59,6 @@ def @object.bytes(n)
@object.alphanumeric(1, chars: [to_s, to_s]).should == "[mock to_s]"
end
end

it_behaves_like :random_formatter_interface, :alphanumeric
end
33 changes: 33 additions & 0 deletions library/random/formatter/shared/interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative '../../../../spec_helper'

describe :random_formatter_interface, shared: true do
it "calls the method #bytes for 4 blocks of 4 bytes" do
bytes = mock('bytes')
bytes.extend(Random::Formatter)
bytes.should_receive(:bytes).with(4).exactly(4).and_return("\x00".b * 4)
bytes.send(@method)
end
Copy link
Member

@andrykonchin andrykonchin Dec 13, 2024

Choose a reason for hiding this comment

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

It seems to me like an implementation detail - in what way the #bytes method is called.

Moreover these 4x4 calls approach is specific to the #alphanumeric method e.g. #base64 calls by default #bytes only once:

obj = Object.new
def obj.bytes(n)
   puts "#bytes(#{n})"
   "\x00".b*n
end
obj.singleton_class.include(Random::Formatter)

obj.base64
#bytes(16)
# => "AAAAAAAAAAAAAAAAAAAAAA=="

And #alphanumeric behaviour depends on n:

obj.alphanumeric(8)
#bytes(4)
#bytes(4)
# => "AAAAAAAA"

So I would check only that the interface between the Random::Formatter module and a class that includes it is just a single method #bites, and nothing more is required.


it "has the same output if the random byte streams are the same" do
bytes = Object.new
bytes.extend(Random::Formatter)
def bytes.bytes(n)
"\x00".b * n
end
bytes.send(@method).should == bytes.send(@method)
end

it "has different output if the random byte streams are not the same" do
lhs = Object.new
lhs.extend(Random::Formatter)
def lhs.bytes(n)
"\x00".b * n
end
rhs = Object.new
rhs.extend(Random::Formatter)
def rhs.bytes(n)
"\x01".b * n
end
lhs.send(@method).should != rhs.send(@method)
end
end
Copy link
Member

Choose a reason for hiding this comment

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

subjective: TBH I don't see value in the last two test cases.

I would keep only one test case (that checks that a #bytes method is called and move it to the #alphanumeric specs file (and don't have the shared example at all))

Loading