Skip to content

Commit

Permalink
Improve #deterministically_verify helper (faker-ruby#2828)
Browse files Browse the repository at this point in the history
* Ensure that the #deterministically_verify helper works as expected when :random is provided

* Prevent the #deterministically_verify helper from mutating the Faker::Config.random state on each :depth iteration

* Prevent the #deterministically_verify helper from performing assertions on a generated value against itself

* Remove the multi-line block chain from the #deterministically_verify helper implementation

* Replace #inject with #map on the #deterministically_verify helper implementation

* Use yield instead of &block on the #deterministically_verify helper

* Move the default value of :random to the #deterministically_verify method signature

* Provide :seed instead of :random instance to the #deterministically_verify helper method

* Update #deterministically_verify helper method documentation
  • Loading branch information
erichmachado authored Oct 7, 2023
1 parent ea21d56 commit 67e6511
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@
# times with the same deterministic_random seed.
# @param subject_proc [Proc] a proc object that returns the subject under test
# when called.
# @param depth [Integer] the depth of deterministic comparisons to run.
# @param random [Integer] A random number seed; Used to override the default.
# @param depth [Integer] the depth of deterministic comparisons to run; the default value is 2.
# @param seed [Integer] A random number seed; Used to override the default value which is 42.
#
# @example
# deterministically_verify ->{ @tester.username('bo peep') } do |subject|
# assert subject.match(/(bo(_|\.)peep|peep(_|\.)bo)/)
# end
#
def deterministically_verify(subject_proc, depth: 2, random: nil, &block)
raise 'need block' unless block_given?
def deterministically_verify(subject_proc, depth: 2, seed: 42)
results = depth.times.map do
Faker::Config.stub :random, Random.new(seed) do
yield subject_proc.call.freeze
end
end

# rubocop:disable Style/MultilineBlockChain
depth.times.inject([]) do |results, _index|
Faker::Config.random = random || Random.new(42)
results << subject_proc.call.freeze.tap(&block)
end.repeated_combination(2) { |(first, second)| assert_equal first, second }
# rubocop:enable Style/MultilineBlockChain
results.combination(2) { |(first, second)| assert_equal first, second }
end

0 comments on commit 67e6511

Please sign in to comment.