Sha256: 57a9054664773f9c0d9361755b7ac8db2c6d7fa977fc44a17caec558d28eed9e

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

require 'test/unit'
require 'ffaker'

# Helpers for checking if a method is deterministic -- e.g., that the Random
# results are repeatable given the same random seed.
module DeterministicHelper
  def self.included(base)
    base.extend ClassMethods
  end

  # Accepts a block. Executes the block multiple times after resetting
  # the internal Random Number Generator state and compared the results of
  # each execution to make sure they are the same.
  def assert_deterministic(options = {})
    raise ArgumentError, 'Must pass a block' unless block_given?

    options = { message: 'Results are not repeatable' }.merge(options)

    returns = Array.new(2) do
      FFaker::Random.reset!
      Array.new(5) do
        yield
      end
    end

    assert(returns.uniq.length == 1, options[:message])
  end

  # Methods to be called outside of individual examples.
  module ClassMethods
    # Shorthand method to quickly test the determinability of multiple methods.
    #
    #  assert_methods_are_deterministic(
    #    <faker module under test>,
    #    <method names to test, as array of symbols>
    #  )
    #
    #  assert_methods_are_deterministic(
    #    FFaker::SomeModule,
    #    :some_method, :some_other_method
    #  }
    def assert_methods_are_deterministic(klass, *methods)
      Array(methods).each do |meth|
        define_method "test_#{meth}_is_deterministic" do
          assert_deterministic(message: "Results from `#{klass}.#{meth}` are not repeatable") do
            klass.send(meth)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ffaker-2.11.0 test/helper.rb
ffaker-2.10.0 test/helper.rb
ffaker-2.9.0 test/helper.rb
ffaker-2.8.1 test/helper.rb
ffaker-2.8.0 test/helper.rb
ffaker-2.7.0 test/helper.rb
ffaker-2.6.0 test/helper.rb
ffaker-2.5.0 test/helper.rb
ffaker-2.4.0 test/helper.rb
ffaker-2.3.0 test/helper.rb