faker 2.20.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +346 -7
- data/History.md +1 -1
- data/README.md +193 -107
- data/lib/faker/blockchain/aeternity.rb +1 -1
- data/lib/faker/blockchain/ethereum.rb +1 -1
- data/lib/faker/blockchain/tezos.rb +1 -1
- data/lib/faker/books/dune.rb +2 -10
- data/lib/faker/books/lovecraft.rb +7 -38
- data/lib/faker/default/address.rb +5 -25
- data/lib/faker/default/alphanumeric.rb +2 -8
- data/lib/faker/default/app.rb +1 -10
- data/lib/faker/default/avatar.rb +1 -12
- data/lib/faker/default/bank.rb +9 -17
- data/lib/faker/default/boolean.rb +1 -4
- data/lib/faker/default/chile_rut.rb +25 -25
- data/lib/faker/default/code.rb +68 -39
- data/lib/faker/default/color.rb +77 -6
- data/lib/faker/default/commerce.rb +3 -17
- data/lib/faker/default/company.rb +50 -19
- data/lib/faker/default/crypto_coin.rb +3 -15
- data/lib/faker/default/date.rb +65 -42
- data/lib/faker/default/demographic.rb +1 -5
- data/lib/faker/default/driving_licence.rb +20 -19
- data/lib/faker/default/drone.rb +1 -1
- data/lib/faker/default/file.rb +5 -24
- data/lib/faker/default/finance.rb +17 -7
- data/lib/faker/default/food.rb +14 -1
- data/lib/faker/default/hipster.rb +6 -45
- data/lib/faker/default/html.rb +230 -0
- data/lib/faker/default/id_number.rb +125 -14
- data/lib/faker/default/internet.rb +116 -111
- data/lib/faker/default/invoice.rb +4 -17
- data/lib/faker/default/json.rb +2 -20
- data/lib/faker/default/lorem.rb +25 -74
- data/lib/faker/default/lorem_flickr.rb +4 -32
- data/lib/faker/default/markdown.rb +6 -11
- data/lib/faker/default/marketing.rb +1 -1
- data/lib/faker/default/measurement.rb +16 -48
- data/lib/faker/default/name.rb +1 -5
- data/lib/faker/default/nhs.rb +2 -6
- data/lib/faker/default/number.rb +10 -55
- data/lib/faker/default/omniauth.rb +6 -40
- data/lib/faker/default/phone_number.rb +2 -6
- data/lib/faker/default/placeholdit.rb +1 -12
- data/lib/faker/default/relationship.rb +1 -5
- data/lib/faker/default/release notes.md +59 -0
- data/lib/faker/default/source.rb +3 -16
- data/lib/faker/default/string.rb +1 -5
- data/lib/faker/default/stripe.rb +4 -20
- data/lib/faker/default/time.rb +4 -32
- data/lib/faker/default/twitter.rb +7 -23
- data/lib/faker/default/types.rb +7 -28
- data/lib/faker/default/vehicle.rb +40 -51
- data/lib/faker/default/vulnerability_identifier.rb +23 -0
- data/lib/faker/default/world_cup.rb +2 -11
- data/lib/faker/games/clash_of_clans.rb +1 -1
- data/lib/faker/games/dnd.rb +49 -7
- data/lib/faker/games/dota.rb +1 -5
- data/lib/faker/games/final_fantasy_xiv.rb +73 -0
- data/lib/faker/games/minecraft.rb +1 -1
- data/lib/faker/games/myst.rb +1 -1
- data/lib/faker/games/tarkov.rb +205 -0
- data/lib/faker/japanese_media/cowboy_bebop.rb +61 -0
- data/lib/faker/japanese_media/kamen_rider.rb +2 -2
- data/lib/faker/japanese_media/one_piece.rb +1 -1
- data/lib/faker/movies/avatar.rb +49 -0
- data/lib/faker/movies/hackers.rb +48 -0
- data/lib/faker/movies/star_wars.rb +1 -5
- data/lib/faker/movies/tron.rb +161 -0
- data/lib/faker/quotes/quote.rb +13 -0
- data/lib/faker/sports/chess.rb +90 -0
- data/lib/faker/sports/mountaineering.rb +22 -0
- data/lib/faker/sports/sport.rb +116 -0
- data/lib/faker/travel/airport.rb +43 -0
- data/lib/faker/travel/train_station.rb +54 -0
- data/lib/faker/tv_shows/archer.rb +51 -0
- data/lib/faker/tv_shows/south_park.rb +15 -0
- data/lib/faker/tv_shows/spongebob.rb +50 -0
- data/lib/faker/version.rb +1 -1
- data/lib/faker.rb +26 -67
- data/lib/helpers/base58.rb +1 -1
- data/lib/helpers/positional_generator.rb +480 -0
- data/lib/helpers/unique_generator.rb +13 -11
- data/lib/locales/README.md +18 -2
- data/lib/locales/ar.yml +1 -0
- data/lib/locales/bg.yml +1 -1
- data/lib/locales/da-DK.yml +1 -1
- data/lib/locales/de-AT.yml +1 -2
- data/lib/locales/de-CH.yml +4336 -12
- data/lib/locales/de.yml +1 -1
- data/lib/locales/en/airport.yml +381 -0
- data/lib/locales/en/archer.yml +75 -0
- data/lib/locales/en/australia.yml +3 -4
- data/lib/locales/en/avatar.yml +31 -0
- data/lib/locales/en/chess.yml +103 -0
- data/lib/locales/en/company.yml +1 -0
- data/lib/locales/en/computer.yml +23 -4
- data/lib/locales/en/cowboy_bebop.yml +163 -0
- data/lib/locales/en/dnd.yml +186 -1
- data/lib/locales/en/dota.yml +113 -0
- data/lib/locales/en/file.yml +9 -1
- data/lib/locales/en/final_fantasy_xiv.yml +754 -0
- data/lib/locales/en/finance.yml +3 -1
- data/lib/locales/en/food.yml +12 -0
- data/lib/locales/en/game.yml +0 -1
- data/lib/locales/en/hackers.yml +53 -0
- data/lib/locales/en/heroes.yml +2 -2
- data/lib/locales/en/internet.yml +32 -0
- data/lib/locales/en/minecraft.yml +4 -4
- data/lib/locales/en/mitch_hedberg.yml +46 -0
- data/lib/locales/en/mountaineering.yml +14 -0
- data/lib/locales/en/movie.yml +2 -1
- data/lib/locales/en/naruto.yml +2 -3
- data/lib/locales/en/one_piece.yml +1 -1
- data/lib/locales/en/opera.yml +2 -2
- data/lib/locales/en/overwatch.yml +5 -7
- data/lib/locales/en/source.yml +5 -0
- data/lib/locales/en/south_park.yml +360 -2
- data/lib/locales/en/spongebob.yml +489 -0
- data/lib/locales/en/sport.yml +130 -0
- data/lib/locales/en/star_wars.yml +1 -1
- data/lib/locales/en/tarkov.yml +593 -0
- data/lib/locales/en/train_station.yml +280 -0
- data/lib/locales/en/tron.yml +227 -0
- data/lib/locales/en/vehicle.yml +2809 -75
- data/lib/locales/en-AU.yml +2 -2
- data/lib/locales/en-CA.yml +2 -1
- data/lib/locales/en-GB.yml +2 -1
- data/lib/locales/en-MS.yml +1 -0
- data/lib/locales/en-NG.yml +1 -0
- data/lib/locales/en-NZ.yml +1 -0
- data/lib/locales/en-PAK.yml +1 -0
- data/lib/locales/en-SG.yml +1 -0
- data/lib/locales/en-UG.yml +1 -0
- data/lib/locales/en-US.yml +1 -1
- data/lib/locales/en-ZA.yml +1 -1
- data/lib/locales/en-au-ocker.yml +2 -0
- data/lib/locales/es-AR.yml +3 -2
- data/lib/locales/es-MX.yml +1 -1
- data/lib/locales/es.yml +2 -2
- data/lib/locales/fi-FI.yml +3 -1
- data/lib/locales/fr/address.yml +0 -1
- data/lib/locales/fr/adjective.yml +266 -0
- data/lib/locales/fr/ancient.yml +141 -0
- data/lib/locales/fr/name.yml +2 -1
- data/lib/locales/fr-CA.yml +1 -1
- data/lib/locales/fr-CH.yml +1 -1
- data/lib/locales/hy.yml +2 -1
- data/lib/locales/it.yml +1 -0
- data/lib/locales/ja/adjective.yml +148 -0
- data/lib/locales/ja/dog.yml +1 -0
- data/lib/locales/ja/emotion.yml +51 -0
- data/lib/locales/ja/naruto.yml +230 -0
- data/lib/locales/ja/relationship.yml +10 -0
- data/lib/locales/ja/sport.yml +130 -0
- data/lib/locales/ja/super_mario.yml +1 -1
- data/lib/locales/ko.yml +1 -0
- data/lib/locales/lt.yml +0 -1
- data/lib/locales/mi-NZ.yml +2 -0
- data/lib/locales/nb-NO.yml +1 -0
- data/lib/locales/nl.yml +1 -0
- data/lib/locales/pl.yml +2 -2
- data/lib/locales/pt-BR.yml +1 -0
- data/lib/locales/pt.yml +1 -1
- data/lib/locales/ru.yml +1 -0
- data/lib/locales/sk.yml +1 -0
- data/lib/locales/sv.yml +1 -0
- data/lib/locales/th.yml +76 -76
- data/lib/locales/tr.yml +1 -0
- data/lib/locales/uk.yml +3 -0
- data/lib/locales/vi.yml +1 -0
- data/lib/locales/zh-CN.yml +1 -0
- data/lib/locales/zh-TW.yml +1 -0
- metadata +46 -127
- data/lib/faker/default/fillmurray.rb +0 -45
- data/lib/faker/default/lorem_pixel.rb +0 -70
- /data/lib/faker/default/{faker_adjective.rb → adjective.rb} +0 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Faker
|
|
4
|
+
class TvShows
|
|
5
|
+
class Spongebob < Base
|
|
6
|
+
flexible :spongebob
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
##
|
|
10
|
+
# Produces a character from Spongebob.
|
|
11
|
+
#
|
|
12
|
+
# @return [String]
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# Faker::TvShows::Spongebob.character #=> "Patrick"
|
|
16
|
+
#
|
|
17
|
+
# @faker.version next
|
|
18
|
+
def character
|
|
19
|
+
fetch('spongebob.characters')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
##
|
|
23
|
+
# Produces a quote from Spongebob.
|
|
24
|
+
#
|
|
25
|
+
# @return [String]
|
|
26
|
+
#
|
|
27
|
+
# @example
|
|
28
|
+
# Faker::TvShows::Spongebob.quote #=> "I'm ready! I'm ready!"
|
|
29
|
+
#
|
|
30
|
+
# @faker.version next
|
|
31
|
+
def quote
|
|
32
|
+
fetch('spongebob.quotes')
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
##
|
|
36
|
+
# Produces an episode from Spongebob.
|
|
37
|
+
#
|
|
38
|
+
# @return [String]
|
|
39
|
+
#
|
|
40
|
+
# @example
|
|
41
|
+
# Faker::TvShows::Spongebob.episode #=> "Reef Blower"
|
|
42
|
+
#
|
|
43
|
+
# @faker.version next
|
|
44
|
+
def episode
|
|
45
|
+
fetch('spongebob.episodes')
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/lib/faker/version.rb
CHANGED
data/lib/faker.rb
CHANGED
|
@@ -4,32 +4,37 @@ mydir = __dir__
|
|
|
4
4
|
|
|
5
5
|
require 'psych'
|
|
6
6
|
require 'i18n'
|
|
7
|
-
require 'set' # Fixes a bug in i18n 0.6.11
|
|
8
7
|
|
|
9
8
|
Dir.glob(File.join(mydir, 'helpers', '*.rb')).sort.each { |file| require file }
|
|
10
9
|
|
|
11
10
|
I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')]
|
|
12
|
-
I18n.reload! if I18n.backend.initialized?
|
|
13
11
|
|
|
14
12
|
module Faker
|
|
15
13
|
module Config
|
|
16
|
-
@
|
|
17
|
-
@random = nil
|
|
14
|
+
@default_locale = nil
|
|
18
15
|
|
|
19
16
|
class << self
|
|
20
|
-
attr_writer :
|
|
17
|
+
attr_writer :default_locale
|
|
18
|
+
|
|
19
|
+
def locale=(new_locale)
|
|
20
|
+
Thread.current[:faker_config_locale] = new_locale
|
|
21
|
+
end
|
|
21
22
|
|
|
22
23
|
def locale
|
|
23
24
|
# Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen
|
|
24
|
-
@
|
|
25
|
+
Thread.current[:faker_config_locale] || @default_locale || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
28
|
def own_locale
|
|
28
|
-
|
|
29
|
+
Thread.current[:faker_config_locale]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def random=(new_random)
|
|
33
|
+
Thread.current[:faker_config_random] = new_random
|
|
29
34
|
end
|
|
30
35
|
|
|
31
36
|
def random
|
|
32
|
-
|
|
37
|
+
Thread.current[:faker_config_random] || Random
|
|
33
38
|
end
|
|
34
39
|
end
|
|
35
40
|
end
|
|
@@ -41,23 +46,29 @@ module Faker
|
|
|
41
46
|
Letters = ULetters + LLetters
|
|
42
47
|
|
|
43
48
|
class << self
|
|
49
|
+
attr_reader :flexible_key
|
|
50
|
+
|
|
44
51
|
NOT_GIVEN = Object.new
|
|
45
52
|
|
|
46
53
|
## by default numerify results do not start with a zero
|
|
47
54
|
def numerify(number_string, leading_zero: false)
|
|
48
|
-
return number_string.gsub(
|
|
55
|
+
return number_string.gsub('#') { rand(10).to_s } if leading_zero
|
|
49
56
|
|
|
50
|
-
number_string.sub(
|
|
57
|
+
number_string.sub('#') { rand(1..9).to_s }.gsub('#') { rand(10).to_s }
|
|
51
58
|
end
|
|
52
59
|
|
|
53
60
|
def letterify(letter_string)
|
|
54
|
-
letter_string.gsub(
|
|
61
|
+
letter_string.gsub('?') { sample(ULetters) }
|
|
55
62
|
end
|
|
56
63
|
|
|
57
64
|
def bothify(string)
|
|
58
65
|
letterify(numerify(string))
|
|
59
66
|
end
|
|
60
67
|
|
|
68
|
+
def generate(as_type, &block)
|
|
69
|
+
PositionalGenerator.new(as_type, &block).generate
|
|
70
|
+
end
|
|
71
|
+
|
|
61
72
|
# Given a regular expression, attempt to generate a string
|
|
62
73
|
# that would match it. This is a rather simple implementation,
|
|
63
74
|
# so don't be shocked if it blows up on you in a spectacular fashion.
|
|
@@ -81,13 +92,13 @@ module Faker
|
|
|
81
92
|
reg = reg.source if reg.respond_to?(:source) # Handle either a Regexp or a String that looks like a Regexp
|
|
82
93
|
reg
|
|
83
94
|
.gsub(%r{^/?\^?}, '').gsub(%r{\$?/?$}, '') # Ditch the anchors
|
|
84
|
-
.gsub(/\{(\d+)\}/, '{\1,\1}').gsub(
|
|
95
|
+
.gsub(/\{(\d+)\}/, '{\1,\1}').gsub('?', '{0,1}') # All {2} become {2,2} and ? become {0,1}
|
|
85
96
|
.gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # [12]{1,2} becomes [12] or [12][12]
|
|
86
97
|
.gsub(/(\([^)]+\))\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
|
|
87
98
|
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
|
|
88
99
|
.gsub(/\((.*?)\)/) { |match| sample(match.gsub(/[()]/, '').split('|')) } # (this|that) becomes 'this' or 'that'
|
|
89
100
|
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w-\w)/) { |range| sample(Array(Range.new(*range.split('-')))) } } # All A-Z inside of [] become C (or X, or whatever)
|
|
90
|
-
.gsub(/\[([^\]]+)\]/) { |_match| sample(Regexp.last_match(1).
|
|
101
|
+
.gsub(/\[([^\]]+)\]/) { |_match| sample(Regexp.last_match(1).chars) } # All [ABC] become B (or A or C)
|
|
91
102
|
.gsub('\d') { |_match| sample(Numbers) }
|
|
92
103
|
.gsub('\w') { |_match| sample(Letters) }
|
|
93
104
|
end
|
|
@@ -185,9 +196,9 @@ module Faker
|
|
|
185
196
|
# girls_name: ["Alice", "Cheryl", "Tatiana"]
|
|
186
197
|
# Then you can call Faker::Name.girls_name and it will act like #first_name
|
|
187
198
|
def method_missing(mth, *args, &block)
|
|
188
|
-
super unless
|
|
199
|
+
super unless flexible_key
|
|
189
200
|
|
|
190
|
-
if (translation = translate("faker.#{
|
|
201
|
+
if (translation = translate("faker.#{flexible_key}.#{mth}"))
|
|
191
202
|
sample(translation)
|
|
192
203
|
else
|
|
193
204
|
super
|
|
@@ -255,58 +266,6 @@ module Faker
|
|
|
255
266
|
ensure
|
|
256
267
|
I18n.enforce_available_locales = old_enforce_available_locales
|
|
257
268
|
end
|
|
258
|
-
|
|
259
|
-
private
|
|
260
|
-
|
|
261
|
-
def warn_for_deprecated_arguments
|
|
262
|
-
keywords = []
|
|
263
|
-
yield(keywords)
|
|
264
|
-
|
|
265
|
-
return if keywords.empty?
|
|
266
|
-
|
|
267
|
-
method_name = caller.first.match(/`(?<method_name>.*)'/)[:method_name]
|
|
268
|
-
|
|
269
|
-
keywords.each.with_index(1) do |keyword, index|
|
|
270
|
-
i = case index
|
|
271
|
-
when 1 then '1st'
|
|
272
|
-
when 2 then '2nd'
|
|
273
|
-
when 3 then '3rd'
|
|
274
|
-
else "#{index}th"
|
|
275
|
-
end
|
|
276
|
-
|
|
277
|
-
warn_with_uplevel(<<~MSG, uplevel: 5)
|
|
278
|
-
Passing `#{keyword}` with the #{i} argument of `#{method_name}` is deprecated. Use keyword argument like `#{method_name}(#{keyword}: ...)` instead.
|
|
279
|
-
MSG
|
|
280
|
-
end
|
|
281
|
-
|
|
282
|
-
warn(<<~MSG)
|
|
283
|
-
|
|
284
|
-
To automatically update from positional arguments to keyword arguments,
|
|
285
|
-
install rubocop-faker and run:
|
|
286
|
-
|
|
287
|
-
rubocop \\
|
|
288
|
-
--require rubocop-faker \\
|
|
289
|
-
--only Faker/DeprecatedArguments \\
|
|
290
|
-
--auto-correct
|
|
291
|
-
|
|
292
|
-
MSG
|
|
293
|
-
end
|
|
294
|
-
|
|
295
|
-
# Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower.
|
|
296
|
-
def warn_with_uplevel(message, uplevel: 1)
|
|
297
|
-
at = parse_caller(caller[uplevel]).join(':')
|
|
298
|
-
warn "#{at}: #{message}"
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
def parse_caller(at)
|
|
302
|
-
# rubocop:disable Style/GuardClause
|
|
303
|
-
if /^(.+?):(\d+)(?::in `.*')?/ =~ at
|
|
304
|
-
file = Regexp.last_match(1)
|
|
305
|
-
line = Regexp.last_match(2).to_i
|
|
306
|
-
[file, line]
|
|
307
|
-
end
|
|
308
|
-
# rubocop:enable Style/GuardClause
|
|
309
|
-
end
|
|
310
269
|
end
|
|
311
270
|
end
|
|
312
271
|
end
|
data/lib/helpers/base58.rb
CHANGED
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
##
|
|
4
|
+
# A high level way to generate a list of generated values that fit a specific
|
|
5
|
+
# format, such as an ID, postal code, or phone number.
|
|
6
|
+
#
|
|
7
|
+
# It provides generators for random digits and letters, hardcoded literal
|
|
8
|
+
# strings, computed values based on previously-generated values, union (one-of)
|
|
9
|
+
# selectors, and grouped generators.
|
|
10
|
+
#
|
|
11
|
+
# The generation allows for dependencies on previously generated values -- most
|
|
12
|
+
# useful for computations -- and this object knows how to build that dependency
|
|
13
|
+
# graph.
|
|
14
|
+
#
|
|
15
|
+
# See {PositionalGenerator::Builder} for more.
|
|
16
|
+
class PositionalGenerator
|
|
17
|
+
##
|
|
18
|
+
# @param as_type [Symbol] +:string+ to generate a String
|
|
19
|
+
# @param block [Method] a function that interacts with the {Builder}
|
|
20
|
+
def initialize(as_type, &block)
|
|
21
|
+
@block = block
|
|
22
|
+
@generator_builder = Builder.new(as_type)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# @return [String] if +as_type+ is +:string+
|
|
27
|
+
def generate
|
|
28
|
+
@block.call(@generator_builder)
|
|
29
|
+
@generator_builder.build
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
Component = Struct.new(:position, :name, :deps, :generator)
|
|
33
|
+
|
|
34
|
+
class Builder
|
|
35
|
+
attr_reader :as_type
|
|
36
|
+
|
|
37
|
+
def initialize(as_type)
|
|
38
|
+
@components = []
|
|
39
|
+
@as_type = as_type
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
##
|
|
43
|
+
# Generate a value in the range of 0..9.
|
|
44
|
+
#
|
|
45
|
+
# @param name [Symbol] the name for this node in the group
|
|
46
|
+
# @param length [Integer] how many digits to generate
|
|
47
|
+
# @param ranges [Array<Range, Array, Set>] an array of limitations on the
|
|
48
|
+
# generation. Elements can be a Range to select from within that range,
|
|
49
|
+
# or an Array or Set to select an element from within the list.
|
|
50
|
+
# @return [void]
|
|
51
|
+
#
|
|
52
|
+
# @example a digit
|
|
53
|
+
# int
|
|
54
|
+
#
|
|
55
|
+
# @example five digits named :a
|
|
56
|
+
# int(name: :a, length: 5)
|
|
57
|
+
#
|
|
58
|
+
# @example digits of any length between 4 to 10
|
|
59
|
+
# int(ranges: [1_000 .. 9_999_999_999)
|
|
60
|
+
def int(name: nil, length: 1, ranges: nil)
|
|
61
|
+
@components << Component.new(@components.count, name, [], Int.new(length, ranges))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
##
|
|
65
|
+
# Generate a value in the range of 'a'..'Z'.
|
|
66
|
+
#
|
|
67
|
+
# @param name [Symbol] the name for this node in the group
|
|
68
|
+
# @param length [Integer, Range] how many letters to generate
|
|
69
|
+
# @param ranges [Array<Range, Array, Set>] an array of limitations on the
|
|
70
|
+
# generation. Elements can be a Range to select from within that range,
|
|
71
|
+
# or an Array or Set to select an element from within the list.
|
|
72
|
+
# @return [void]
|
|
73
|
+
#
|
|
74
|
+
# @example Generate a letter
|
|
75
|
+
# letter
|
|
76
|
+
#
|
|
77
|
+
# @example Generate five uppercase letters named :b
|
|
78
|
+
# letter(name: :b, length: 5, ranges: ['A'..'Z'])
|
|
79
|
+
#
|
|
80
|
+
# @example Generate three-letter strings from within specific values
|
|
81
|
+
# letter(ranges: ['700'..'799', '7A0'..'7F9'])
|
|
82
|
+
def letter(name: nil, length: 1, ranges: ['a'..'z', 'A'..'Z'])
|
|
83
|
+
@components << Component.new(@components.count, name, [], Letter.new(length, ranges))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
##
|
|
87
|
+
# Generate a literal String
|
|
88
|
+
#
|
|
89
|
+
# @param value [String]
|
|
90
|
+
# @param name [Symbol] the name for this node in the group
|
|
91
|
+
# @return [void]
|
|
92
|
+
# @example
|
|
93
|
+
# lit("-")
|
|
94
|
+
def lit(value, name: nil)
|
|
95
|
+
@components << Component.new(@components.count, name, [], Literal.new(value))
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
##
|
|
99
|
+
# Fill the position with an arbitrary value.
|
|
100
|
+
#
|
|
101
|
+
# @param name [Symbol] the name for this node in the group
|
|
102
|
+
# @param deps [Array<Symbol>] the name of other fields that this one depends on
|
|
103
|
+
# @param block [Method] the block that yields the arbitrary value. Its
|
|
104
|
+
# arguments are the deps.
|
|
105
|
+
# @return [void]
|
|
106
|
+
#
|
|
107
|
+
# @example Today's date
|
|
108
|
+
# computed do
|
|
109
|
+
# Date.today
|
|
110
|
+
# end
|
|
111
|
+
#
|
|
112
|
+
# @example A check digit
|
|
113
|
+
# int(name: :a, length: 5)
|
|
114
|
+
# computed(deps: [:a]) do |a|
|
|
115
|
+
# a.to_s.bytes.sum % 10
|
|
116
|
+
# end
|
|
117
|
+
def computed(name: nil, deps: [], &block)
|
|
118
|
+
@components << Component.new(@components.count, name, deps, Computed.new(block))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
##
|
|
122
|
+
# Fill the position with one of the results from the given generators.
|
|
123
|
+
#
|
|
124
|
+
# @param name [Symbol] the name for this node in the group
|
|
125
|
+
# @param block [Method] subgenerator block
|
|
126
|
+
# @return [void]
|
|
127
|
+
#
|
|
128
|
+
# @example Either five digits, or two letters
|
|
129
|
+
# oneof do |or_else|
|
|
130
|
+
# or_else.int(length: 5)
|
|
131
|
+
# or_else.letter(length: 2)
|
|
132
|
+
# end
|
|
133
|
+
#
|
|
134
|
+
# @example Either one letter; or a slash, five digits, then a slash.
|
|
135
|
+
# oneof do |or_else|
|
|
136
|
+
# or_else.letter
|
|
137
|
+
# or_else.group do |g_|
|
|
138
|
+
# g_.lit("/")
|
|
139
|
+
# g_.digit(length: 5)
|
|
140
|
+
# g_.lit("/")
|
|
141
|
+
# end
|
|
142
|
+
# end
|
|
143
|
+
def oneof(name: nil, &block)
|
|
144
|
+
@components << Component.new(@components.count, name, [], Oneof.new(self, block))
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
##
|
|
148
|
+
# A group of generators. Useful for {#oneof}.
|
|
149
|
+
#
|
|
150
|
+
# @param name [Symbol] the name for this node in the group
|
|
151
|
+
# @param block [Method] a subgenerator block
|
|
152
|
+
# @return [void]
|
|
153
|
+
def group(name: nil, &block)
|
|
154
|
+
@components << Component.new(@components.count, name, [], Group.new(@as_type, block))
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
##
|
|
158
|
+
# Generate the value.
|
|
159
|
+
#
|
|
160
|
+
# @return [String] if +as_type+ is +:string+
|
|
161
|
+
def build
|
|
162
|
+
graph = build_graph
|
|
163
|
+
stack = build_stack(graph)
|
|
164
|
+
values = generate_values(stack)
|
|
165
|
+
convert(values)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private
|
|
169
|
+
|
|
170
|
+
##
|
|
171
|
+
# Turn the components into a graph following dependencies.
|
|
172
|
+
#
|
|
173
|
+
# @return [Array<(Integer, Integer)>]
|
|
174
|
+
#
|
|
175
|
+
# Components can have dependencies. Here's one where a computation (b)
|
|
176
|
+
# depends on a value generated after it (c):
|
|
177
|
+
#
|
|
178
|
+
# @components = [
|
|
179
|
+
# Int.new(0, :a, 1, nil),
|
|
180
|
+
# Computed.new(1, :b, [:c]) { |c| c + 1 },
|
|
181
|
+
# Int.new(2, :c, 1, nil),
|
|
182
|
+
# ]
|
|
183
|
+
#
|
|
184
|
+
# We can think of a graph like so:
|
|
185
|
+
#
|
|
186
|
+
# (a) (c)
|
|
187
|
+
# | |
|
|
188
|
+
# | (b)
|
|
189
|
+
# \ /
|
|
190
|
+
# end
|
|
191
|
+
#
|
|
192
|
+
# Or in Mermaid:
|
|
193
|
+
#
|
|
194
|
+
# ```mermaid
|
|
195
|
+
# stateDiagram-v2
|
|
196
|
+
# a --> [*]
|
|
197
|
+
# c --> b
|
|
198
|
+
# b --> [*]
|
|
199
|
+
# ```
|
|
200
|
+
#
|
|
201
|
+
# This method builds that graph, using their positional locations as the
|
|
202
|
+
# ID. The end state is represented as +nil+. So continuing the example
|
|
203
|
+
# above, it will give this output:
|
|
204
|
+
#
|
|
205
|
+
# [
|
|
206
|
+
# [0, nil],
|
|
207
|
+
# [2, 1],
|
|
208
|
+
# [1, nil],
|
|
209
|
+
# ]
|
|
210
|
+
#
|
|
211
|
+
# Later we can look up the appropriate component by indexing into the
|
|
212
|
+
# +@components+ array.
|
|
213
|
+
def build_graph
|
|
214
|
+
graph = []
|
|
215
|
+
|
|
216
|
+
# rubocop:disable Style/CombinableLoops
|
|
217
|
+
@components.each do |component|
|
|
218
|
+
component.deps.each do |dep|
|
|
219
|
+
dep_component = @components.detect { |c| c.name == dep }
|
|
220
|
+
raise if dep_component.nil?
|
|
221
|
+
|
|
222
|
+
graph.push([dep_component.position, component.position])
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
@components.each do |component|
|
|
227
|
+
graph.push([component.position, nil]) if graph.none? { |(from, _to)| from == component.position }
|
|
228
|
+
end
|
|
229
|
+
# rubocop:enable Style/CombinableLoops
|
|
230
|
+
|
|
231
|
+
graph
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
##
|
|
235
|
+
# Produce a stack of components to evaluate in sequence.
|
|
236
|
+
#
|
|
237
|
+
# @param graph [Array<(Integer, Integer)>]
|
|
238
|
+
# @return [Array<Array<Int>>]
|
|
239
|
+
#
|
|
240
|
+
# Now that we have a graph, we know enough to determine how to traverse the
|
|
241
|
+
# generators such that all dependencies are met.
|
|
242
|
+
#
|
|
243
|
+
# The initial stack is an array of all the free traversals to the goal
|
|
244
|
+
# (where the +to+ is +nil+).
|
|
245
|
+
#
|
|
246
|
+
# Loop over the top of the stack:
|
|
247
|
+
# - The next array is all the nodes that lead into the nodes atop the
|
|
248
|
+
# stack.
|
|
249
|
+
# - If the next array has values, push that onto the top of the stack.
|
|
250
|
+
# - If the next array is empty, we are done.
|
|
251
|
+
#
|
|
252
|
+
# For example, given the graph:
|
|
253
|
+
#
|
|
254
|
+
# [
|
|
255
|
+
# [0, nil],
|
|
256
|
+
# [2, 1],
|
|
257
|
+
# [1, nil],
|
|
258
|
+
# ]
|
|
259
|
+
#
|
|
260
|
+
# The initial stack is:
|
|
261
|
+
#
|
|
262
|
+
# [
|
|
263
|
+
# [0, 1]
|
|
264
|
+
# ]
|
|
265
|
+
#
|
|
266
|
+
# We loop over the top of the stack, +[0, 1]+, and find all the nodes of
|
|
267
|
+
# the graph that lead there. Nothing leads to +0+, and +2+ leads to +1+.
|
|
268
|
+
#
|
|
269
|
+
# Therefore, push +[2]+ to the top of the stack.
|
|
270
|
+
#
|
|
271
|
+
# Repeat for +[2]+. Nothing leads to +2+, so our new goal is +[]+. This is
|
|
272
|
+
# empty, so don't push it onto the stack. We are done.
|
|
273
|
+
#
|
|
274
|
+
# The final stack is:
|
|
275
|
+
#
|
|
276
|
+
# [
|
|
277
|
+
# [0, 1],
|
|
278
|
+
# [2]
|
|
279
|
+
# ]
|
|
280
|
+
def build_stack(graph)
|
|
281
|
+
require 'set'
|
|
282
|
+
|
|
283
|
+
terminals = graph.filter_map { |(from, to)| to.nil? && from }
|
|
284
|
+
stack = [terminals]
|
|
285
|
+
seen = Set.new(terminals)
|
|
286
|
+
deps = []
|
|
287
|
+
|
|
288
|
+
loop do
|
|
289
|
+
stack[-1].each do |e|
|
|
290
|
+
deps = graph.select { |(from, to)| to == e && !seen.include?(from) }.map do |from, _to|
|
|
291
|
+
seen << from
|
|
292
|
+
from
|
|
293
|
+
end
|
|
294
|
+
stack << deps if deps.any?
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
break if deps.empty?
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
stack
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
##
|
|
304
|
+
# Turn a stack into a list of generated values.
|
|
305
|
+
#
|
|
306
|
+
# @param stack [Array<Array<Int>>]
|
|
307
|
+
# @return [Array<Object>] values sorted by desired order
|
|
308
|
+
#
|
|
309
|
+
# We start with a stack of components we need evaluated. We have been
|
|
310
|
+
# tracking these components by position, so first we need to look up the
|
|
311
|
+
# component in our list.
|
|
312
|
+
#
|
|
313
|
+
# From there we can get a list of all the dependencies for the component.
|
|
314
|
+
# These have already been evaluated, since +stack+ is sorted, so we fetch
|
|
315
|
+
# them.
|
|
316
|
+
#
|
|
317
|
+
# Since the stack was sorted by computation order, we must re-sort them
|
|
318
|
+
# into positional order at the end.
|
|
319
|
+
def generate_values(stack)
|
|
320
|
+
result = []
|
|
321
|
+
|
|
322
|
+
while (top = stack.pop)
|
|
323
|
+
top.each do |component_id|
|
|
324
|
+
component = @components[component_id]
|
|
325
|
+
raise if component.nil?
|
|
326
|
+
|
|
327
|
+
values = result.filter_map do |(_id, name, value)|
|
|
328
|
+
value if component.deps.include?(name)
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
result << [component.position, component.name, component.generator.generate(values)]
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
result.sort_by do |component_position, _, _|
|
|
336
|
+
component_position
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
##
|
|
341
|
+
# @param values [Array<Object>]
|
|
342
|
+
# @return [String] if +@as_type+ is +:string+
|
|
343
|
+
# @raise [ArgumentError] if +@as_type+ is unsupported
|
|
344
|
+
def convert(values)
|
|
345
|
+
case @as_type
|
|
346
|
+
when :string
|
|
347
|
+
values.inject('') do |acc, (_, _, v)|
|
|
348
|
+
"#{acc}#{v}"
|
|
349
|
+
end
|
|
350
|
+
else
|
|
351
|
+
raise ArgumentError, "unknown return type: #{@as_type}"
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
class Group
|
|
356
|
+
def initialize(as_type, block)
|
|
357
|
+
@as_type = as_type
|
|
358
|
+
@block = block
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def generate(_)
|
|
362
|
+
builder = Builder.new(@as_type)
|
|
363
|
+
@block.call(builder)
|
|
364
|
+
builder.build
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
class Oneof
|
|
369
|
+
def initialize(builder, block)
|
|
370
|
+
@block = block
|
|
371
|
+
@builder = builder
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def generate(...)
|
|
375
|
+
subgens = OneofSelector.new(@builder)
|
|
376
|
+
@block.call(subgens)
|
|
377
|
+
subgens.sample
|
|
378
|
+
subgens.generate(...)
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
class OneofSelector
|
|
382
|
+
def initialize(builder)
|
|
383
|
+
@subgens = []
|
|
384
|
+
@builder = Builder.new(builder.as_type)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def method_missing(meth, *args, **kwargs, &block)
|
|
388
|
+
@subgens << [meth, args, kwargs, block]
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
392
|
+
@builder.respond_to?(method_name, include_private)
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
def sample
|
|
396
|
+
(meth, args, kwargs, block) = Faker::Base.sample(@subgens)
|
|
397
|
+
@builder.send(meth, *args, **kwargs, &block)
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
def generate(...)
|
|
401
|
+
@builder.build
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
class Int
|
|
407
|
+
def initialize(length, ranges)
|
|
408
|
+
# Internally we store only an Enumerable of Range values. So if we are
|
|
409
|
+
# not given any Ranges but are given a length, we need to convert the
|
|
410
|
+
# length to a Range.
|
|
411
|
+
#
|
|
412
|
+
# If the length is `5`, that means we should compute the Range `10000..99999`.
|
|
413
|
+
# We can compute the lower end with a simple exponent: 10^4 = 10000.
|
|
414
|
+
# The upper end is one less than an exponent: 10^5 - 1 = 99999.
|
|
415
|
+
if ranges.nil?
|
|
416
|
+
lower = 10**(length - 1)
|
|
417
|
+
upper = (10**length) - 1
|
|
418
|
+
ranges = [lower..upper]
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
@ranges = ranges
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
def generate(_)
|
|
425
|
+
Faker::Base.rand(@ranges.sample(random: Faker::Config.random))
|
|
426
|
+
end
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
class Letter
|
|
430
|
+
def initialize(length, ranges)
|
|
431
|
+
@length = length
|
|
432
|
+
@ranges = ranges
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def generate(_)
|
|
436
|
+
@length.times.inject('') do |acc, _index|
|
|
437
|
+
generated_character = char
|
|
438
|
+
"#{acc}#{generated_character}"
|
|
439
|
+
end
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
private
|
|
443
|
+
|
|
444
|
+
def char
|
|
445
|
+
if @ranges
|
|
446
|
+
case s = @ranges.sample(random: Faker::Config.random)
|
|
447
|
+
when Range
|
|
448
|
+
s.to_a.sample(random: Faker::Config.random)
|
|
449
|
+
when Array, Set
|
|
450
|
+
s.sample(random: Faker::Config.random)
|
|
451
|
+
else
|
|
452
|
+
raise ArgumentError, "unsupported range type: #{s.inspect}"
|
|
453
|
+
end
|
|
454
|
+
else
|
|
455
|
+
Faker::Base.sample(Faker::Base::Letters)
|
|
456
|
+
end
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
class Literal
|
|
461
|
+
def initialize(value)
|
|
462
|
+
@value = value
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
def generate(_)
|
|
466
|
+
@value
|
|
467
|
+
end
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
class Computed
|
|
471
|
+
def initialize(block)
|
|
472
|
+
@block = block
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
def generate(args)
|
|
476
|
+
@block.call(*args)
|
|
477
|
+
end
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
end
|