Skip to content
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

Add specs for Random::Formatter#alphanumeric #1222

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Changes from all commits
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
58 changes: 58 additions & 0 deletions library/random/formatter/alphanumeric_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require_relative '../../../spec_helper'

ruby_version_is "3.1" do
require 'random/formatter'
Copy link
Member

@andrykonchin andrykonchin Dec 7, 2024

Choose a reason for hiding this comment

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

Looks good.

I've considered something like this:

ruby_version_is ""..."3.1" do
  require 'securerandom'
end

ruby_version_is "3.1" do
  require 'random/formatter'
end

describe "Random::Formatter#alphanumeric" do
  # ...
end

Does it make sense?


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
Copy link
Member

Choose a reason for hiding this comment

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

suggestion:

def @object.bytes(n)
  "\x00".b * n
end

Copy link
Member

Choose a reason for hiding this comment

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

Also it seems the #bytes method is a contract/interface between the Random::Formatter module and a class that includes it. So it might make sense to state it explicitly with a dedicated test case.

Copy link
Member

@andrykonchin andrykonchin Dec 7, 2024

Choose a reason for hiding this comment

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

Regarding random generation in these specs. "\x00".b * n seems to simple and is lacking randomness. So specs may not detect an issue with implementation.

With current #bytes method version:

@object.alphanumeric # => "AAAAAAAAAAAAAAAA"
@object.alphanumeric(chars: ['a', 'b']) # => "aaaaaaaaaaaaaaaa"

So I would consider something more random:

def @object.bytes(n)
  (0..255).to_a.take(n).map { |b| b.chr }.join.b
end

or even call Array#shuffle on (0..255).to_a.

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 "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 "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

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
end
end
end
Loading