seeded_rand_spec.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. require 'spec_helper'
  2. describe 'seeded_rand' do
  3. it { is_expected.not_to eq(nil) }
  4. it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
  5. it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
  6. it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  7. it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  8. it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  9. it { is_expected.to run.with_params('-10', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  10. it { is_expected.to run.with_params('string', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  11. it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  12. it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) }
  13. it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{second argument must be a string}) }
  14. it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be a string}) }
  15. it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be a string}) }
  16. it 'provides a random number strictly less than the given max' do
  17. expect(seeded_rand(3, 'seed')).to satisfy { |n| n.to_i < 3 } # rubocop:disable Lint/AmbiguousBlockAssociation : Cannot parenthesize without break code or violating other Rubocop rules
  18. end
  19. it 'provides a random number greater or equal to zero' do
  20. expect(seeded_rand(3, 'seed')).to satisfy { |n| n.to_i >= 0 } # rubocop:disable Lint/AmbiguousBlockAssociation : Cannot parenthesize without break code or violating other Rubocop rules
  21. end
  22. it "provides the same 'random' value on subsequent calls for the same host" do
  23. expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed'))
  24. end
  25. it 'allows seed to control the random value on a single host' do
  26. first_random = seeded_rand(1000, 'seed1')
  27. second_different_random = seeded_rand(1000, 'seed2')
  28. expect(first_random).not_to eql(second_different_random)
  29. end
  30. it 'does not return different values for different hosts' do
  31. val1 = seeded_rand(1000, 'foo', :host => 'first.host.com')
  32. val2 = seeded_rand(1000, 'foo', :host => 'second.host.com')
  33. expect(val1).to eql(val2)
  34. end
  35. def seeded_rand(max, seed, args = {})
  36. host = args[:host] || '127.0.0.1'
  37. # workaround not being able to use let(:facts) because some tests need
  38. # multiple different hostnames in one context
  39. scope.stubs(:lookupvar).with('::fqdn', {}).returns(host)
  40. scope.function_seeded_rand([max, seed])
  41. end
  42. context 'with UTF8 and double byte characters' do
  43. it { is_expected.to run.with_params(1000, 'ǿňè') }
  44. it { is_expected.to run.with_params(1000, '文字列') }
  45. end
  46. end