From f2b773263054958ca517ba8f74d70797114a5d3d Mon Sep 17 00:00:00 2001 From: Herwin Date: Sat, 30 Nov 2024 16:31:30 +0100 Subject: [PATCH] Add specs for Random::Formatter#alphanumeric --- core/random/alphanumeric_spec.rb | 8 ++++ library/securerandom/alphanumeric_spec.rb | 8 ++++ shared/random/formatter/alphanumeric.rb | 50 +++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 core/random/alphanumeric_spec.rb create mode 100644 library/securerandom/alphanumeric_spec.rb create mode 100644 shared/random/formatter/alphanumeric.rb diff --git a/core/random/alphanumeric_spec.rb b/core/random/alphanumeric_spec.rb new file mode 100644 index 0000000000..326074398c --- /dev/null +++ b/core/random/alphanumeric_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require_relative '../../shared/random/formatter/alphanumeric' + +ruby_version_is "3.1" do + describe "Random#alphanumeric" do + it_behaves_like :random_alphanumeric, :alphanumeric, Random + end +end diff --git a/library/securerandom/alphanumeric_spec.rb b/library/securerandom/alphanumeric_spec.rb new file mode 100644 index 0000000000..ecfac8ea5a --- /dev/null +++ b/library/securerandom/alphanumeric_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require_relative '../../shared/random/formatter/alphanumeric' + +require 'securerandom' + +describe "SecureRandom#alphanumeric" do + it_behaves_like :random_alphanumeric, :alphanumeric, SecureRandom +end diff --git a/shared/random/formatter/alphanumeric.rb b/shared/random/formatter/alphanumeric.rb new file mode 100644 index 0000000000..273056b5c7 --- /dev/null +++ b/shared/random/formatter/alphanumeric.rb @@ -0,0 +1,50 @@ +require_relative '../../../spec_helper' + +describe :random_alphanumeric, shared: true do + ruby_version_is "3.1" do + require 'random/formatter' + end + + it "generates a random alphanumeric string" do + @object.send(@method).should =~ /\A[A-Za-z0-9]+\z/ + end + + it "has a default size of 16 characters" do + @object.send(@method).size.should == 16 + end + + it "accepts a 'size' argument" do + @object.send(@method, 10).size.should == 10 + end + + it "uses the default size if 'nil' is given as size argument" do + @object.send(@method, nil).size.should == 16 + end + + it "raises an ArgumentError if the size is not numeric" do + -> { + @object.send(@method, "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.send(@method, size) + }.should raise_error(ArgumentError) + end + + ruby_version_is "3.3" do + it "accepts a 'chars' argument with the output alphabet" do + @object.send(@method, 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.send(@method, 1, chars: [to_s, to_s]).should == "[mock to_s]" + end + end +end