forked from faker-ruby/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_determinism.rb
More file actions
58 lines (48 loc) · 1.79 KB
/
Copy pathtest_determinism.rb
File metadata and controls
58 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true
require_relative 'test_helper'
# rubocop:disable Security/Eval,Style/EvalWithLocation
class TestDeterminism < Test::Unit::TestCase
def setup
@all_methods = all_methods.freeze
@first_run = []
end
def test_determinism
Faker::Config.random = Random.new(42)
@all_methods.each_index do |index|
store_result @all_methods[index]
end
@first_run.freeze
Faker::Config.random = Random.new(42)
@all_methods.each_index do |index|
assert deterministic_random? @first_run[index], @all_methods[index]
end
end
private
def deterministic_random?(first, method_name)
second = eval(method_name)
(first == second) || raise(
"#{method_name} has an entropy leak; use \"Faker::Config.random.rand\" or \"Array#sample(random: Faker::Config.random)\". Method to lookup for: sample, shuffle, rand"
)
end
def store_result(method_name)
@first_run << eval(method_name)
rescue StandardError => e
raise %(#{method_name} raised "#{e}")
end
def all_methods
subclasses.map do |subclass|
subclass_methods(subclass).flatten
end.flatten.sort
end
def subclasses
Faker.constants.delete_if do |subclass|
%i[Base Bank Books Cat Char Base58 ChileRut CLI Config Creature Date Dog DragonBall Dota ElderScrolls Fallout Games GamesHalfLife HeroesOfTheStorm Internet JapaneseMedia LeagueOfLegends Movies Myst Overwatch OnePiece Pokemon Religion Sports SwordArtOnline TvShows Time VERSION Witcher WorldOfWarcraft Zelda].include?(subclass)
end.sort
end
def subclass_methods(subclass)
eval("Faker::#{subclass}.public_methods(false) - Faker::Base.public_methods(false)").sort.map do |method|
"Faker::#{subclass}.#{method}"
end.sort
end
end
# rubocop:enable Security/Eval,Style/EvalWithLocation