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