-
-
Notifications
You must be signed in to change notification settings - Fork 388
Improvements to specs for Random::Formatter #1228
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
Open
herwinw
wants to merge
3
commits into
ruby:master
Choose a base branch
from
herwinw:random_formatter_alphanumeric_improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,64 @@ | ||
require_relative '../../../spec_helper' | ||
require_relative 'shared/interface' | ||
|
||
ruby_version_is "3.1" do | ||
require 'random/formatter' | ||
end | ||
ruby_version_is ""..."3.1" do | ||
require 'securerandom' | ||
end | ||
|
||
describe "Random::Formatter#alphanumeric" do | ||
before :each do | ||
@object = Object.new | ||
@object.extend(Random::Formatter) | ||
@object.define_singleton_method(:bytes) do |n| | ||
"\x00".b * n | ||
end | ||
describe "Random::Formatter#alphanumeric" do | ||
before :each do | ||
@object = Object.new | ||
@object.extend(Random::Formatter) | ||
def @object.bytes(n) | ||
"\x00".b * n | ||
end | ||
end | ||
|
||
it "generates a random alphanumeric string" do | ||
@object.alphanumeric.should =~ /\A[A-Za-z0-9]+\z/ | ||
end | ||
it "generates a random alphanumeric string" do | ||
@object.alphanumeric.should =~ /\A[A-Za-z0-9]+\z/ | ||
end | ||
|
||
it "has a default size of 16 characters" do | ||
@object.alphanumeric.size.should == 16 | ||
end | ||
it "has a default size of 16 characters" do | ||
@object.alphanumeric.size.should == 16 | ||
end | ||
|
||
it "accepts a 'size' argument" do | ||
@object.alphanumeric(10).size.should == 10 | ||
end | ||
it "accepts a 'size' argument" do | ||
@object.alphanumeric(10).size.should == 10 | ||
end | ||
|
||
it "uses the default size if 'nil' is given as size argument" do | ||
@object.alphanumeric(nil).size.should == 16 | ||
end | ||
it "uses the default size if 'nil' is given as size argument" do | ||
@object.alphanumeric(nil).size.should == 16 | ||
end | ||
|
||
it "raises an ArgumentError if the size is not numeric" do | ||
-> { | ||
@object.alphanumeric("10") | ||
}.should raise_error(ArgumentError) | ||
end | ||
it "raises an ArgumentError if the size is not numeric" do | ||
-> { | ||
@object.alphanumeric("10") | ||
}.should raise_error(ArgumentError) | ||
end | ||
|
||
it "does not coerce the size argument with #to_int" do | ||
size = mock("size") | ||
size.should_not_receive(:to_int) | ||
-> { | ||
@object.alphanumeric(size) | ||
}.should raise_error(ArgumentError) | ||
end | ||
|
||
it "does not coerce the size argument with #to_int" do | ||
size = mock("size") | ||
size.should_not_receive(:to_int) | ||
-> { | ||
@object.alphanumeric(size) | ||
}.should raise_error(ArgumentError) | ||
ruby_version_is "3.3" do | ||
it "accepts a 'chars' argument with the output alphabet" do | ||
@object.alphanumeric(chars: ['a', 'b']).should =~ /\A[ab]+\z/ | ||
end | ||
|
||
ruby_version_is "3.3" do | ||
it "accepts a 'chars' argument with the output alphabet" do | ||
@object.alphanumeric(chars: ['a', 'b']).should =~ /\A[ab]+\z/ | ||
end | ||
|
||
it "converts the elements of chars using #to_s" do | ||
to_s = mock("to_s") | ||
to_s.should_receive(:to_s).and_return("[mock to_s]") | ||
# Using 1 value in chars results in an infinite loop | ||
@object.alphanumeric(1, chars: [to_s, to_s]).should == "[mock to_s]" | ||
end | ||
it "converts the elements of chars using #to_s" do | ||
to_s = mock("to_s") | ||
to_s.should_receive(:to_s).and_return("[mock to_s]") | ||
# Using 1 value in chars results in an infinite loop | ||
@object.alphanumeric(1, chars: [to_s, to_s]).should == "[mock to_s]" | ||
end | ||
end | ||
|
||
it_behaves_like :random_formatter_interface, :alphanumeric | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:And
#alphanumeric
behaviour depends onn
: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.