-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specs for Random::Formatter#alphanumeric
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
require_relative '../../spec_helper' | ||
require 'random/formatter' | ||
|
||
describe "Random.alphanumeric" do | ||
it "generates a random alphanumeric string" do | ||
Random.alphanumeric.should =~ /\A[A-Za-z0-9]+\z/ | ||
end | ||
|
||
it "has a default size of 16 characters" do | ||
Random.alphanumeric.size.should == 16 | ||
end | ||
|
||
it "accepts a 'size' argument" do | ||
Random.alphanumeric(10).size.should == 10 | ||
end | ||
|
||
it "uses the default size if 'nil' is given as size argument" do | ||
Random.alphanumeric(nil).size.should == 16 | ||
end | ||
|
||
it "raises an ArgumentError if the size is not numeric" do | ||
-> { | ||
Random.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) | ||
-> { | ||
Random.alphanumeric(size) | ||
}.should raise_error(ArgumentError) | ||
end | ||
|
||
it "can be used by any object that responds to #random_number" do | ||
random = mock("random") | ||
random.should_receive(:random_number).and_return(42) | ||
random.extend(Random::Formatter) | ||
random.alphanumeric(2).should == "qA" | ||
end | ||
|
||
ruby_version_is "3.3" do | ||
it "accepts a 'chars' argument with the output alphabet" do | ||
Random.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 | ||
Random.alphanumeric(1, chars: [to_s, to_s]).should == "[mock to_s]" | ||
end | ||
end | ||
end |