|
| 1 | +require_relative '../../../spec_helper' |
| 2 | + |
| 3 | +describe :random_alphanumeric, shared: true do |
| 4 | + ruby_version_is "3.1" do |
| 5 | + require 'random/formatter' |
| 6 | + |
| 7 | + it "generates a random alphanumeric string" do |
| 8 | + @object.send(@method).should =~ /\A[A-Za-z0-9]+\z/ |
| 9 | + end |
| 10 | + |
| 11 | + it "has a default size of 16 characters" do |
| 12 | + @object.send(@method).size.should == 16 |
| 13 | + end |
| 14 | + |
| 15 | + it "accepts a 'size' argument" do |
| 16 | + @object.send(@method, 10).size.should == 10 |
| 17 | + end |
| 18 | + |
| 19 | + it "uses the default size if 'nil' is given as size argument" do |
| 20 | + @object.send(@method, nil).size.should == 16 |
| 21 | + end |
| 22 | + |
| 23 | + it "raises an ArgumentError if the size is not numeric" do |
| 24 | + -> { |
| 25 | + @object.send(@method, "10") |
| 26 | + }.should raise_error(ArgumentError) |
| 27 | + end |
| 28 | + |
| 29 | + it "does not coerce the size argument with #to_int" do |
| 30 | + size = mock("size") |
| 31 | + size.should_not_receive(:to_int) |
| 32 | + -> { |
| 33 | + @object.send(@method, size) |
| 34 | + }.should raise_error(ArgumentError) |
| 35 | + end |
| 36 | + |
| 37 | + ruby_version_is "3.3" do |
| 38 | + it "accepts a 'chars' argument with the output alphabet" do |
| 39 | + @object.send(@method, chars: ['a', 'b']).should =~ /\A[ab]+\z/ |
| 40 | + end |
| 41 | + |
| 42 | + it "converts the elements of chars using #to_s" do |
| 43 | + to_s = mock("to_s") |
| 44 | + to_s.should_receive(:to_s).and_return("[mock to_s]") |
| 45 | + # Using 1 value in chars results in an infinite loop |
| 46 | + @object.send(@method, 1, chars: [to_s, to_s]).should == "[mock to_s]" |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | +end |
0 commit comments