Skip to content

Commit 9d36be6

Browse files
committed
Rework the tests to check actual values
Rather that testing that when called twice the function return the same or different values, test the actual returned value so that we are sure the behavior does not change unexpectedly.
1 parent a241039 commit 9d36be6

File tree

1 file changed

+5
-39
lines changed

1 file changed

+5
-39
lines changed

spec/functions/seeded_rand_spec.rb

+5-39
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,10 @@
1717
it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be a string}) }
1818
it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be a string}) }
1919

20-
it 'provides a random number strictly less than the given max' do
21-
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
22-
end
23-
24-
it 'provides a random number greater or equal to zero' do
25-
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
26-
end
27-
28-
it "provides the same 'random' value on subsequent calls for the same host" do
29-
expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed'))
30-
end
31-
32-
it 'allows seed to control the random value on a single host' do
33-
first_random = seeded_rand(1000, 'seed1')
34-
second_different_random = seeded_rand(1000, 'seed2')
35-
36-
expect(first_random).not_to eql(second_different_random)
37-
end
38-
39-
it 'does not return different values for different hosts' do
40-
val1 = seeded_rand(1000, 'foo', host: 'first.host.com')
41-
val2 = seeded_rand(1000, 'foo', host: 'second.host.com')
42-
43-
expect(val1).to eql(val2)
44-
end
45-
46-
def seeded_rand(max, seed, args = {})
47-
host = args[:host] || '127.0.0.1'
48-
49-
# workaround not being able to use let(:facts) because some tests need
50-
# multiple different hostnames in one context
51-
allow(scope).to receive(:lookupvar).with('::fqdn', {}).and_return(host)
52-
53-
scope.function_seeded_rand([max, seed])
54-
end
55-
56-
context 'with UTF8 and double byte characters' do
57-
it { is_expected.to run.with_params(1000, 'ǿňè') }
58-
it { is_expected.to run.with_params(1000, '文字列') }
20+
context "produce predictible and reproducible results" do
21+
it { is_expected.to run.with_params(20, 'foo').and_return(1) }
22+
it { is_expected.to run.with_params(100, 'bar').and_return(35) }
23+
it { is_expected.to run.with_params(1000, 'ǿňè').and_return(247) }
24+
it { is_expected.to run.with_params(1000, '文字列').and_return(67) }
5925
end
6026
end

0 commit comments

Comments
 (0)