From 67e6511a15faddacfe3b2a210c112f1f1664f42b Mon Sep 17 00:00:00 2001 From: Erich Soares Machado Date: Fri, 6 Oct 2023 20:34:31 -0400 Subject: [PATCH] Improve `#deterministically_verify` helper (#2828) * 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 --- test/test_helper.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 872de509b3..fab1465b52 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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