mirror of
https://github.com/askn/faker.git
synced 2023-08-10 21:13:01 +03:00
regexify test && macro find_fn
This commit is contained in:
parent
36fe59b671
commit
1427480a97
@ -1,7 +1,24 @@
|
||||
require "./spec_helper"
|
||||
|
||||
describe Faker do
|
||||
it "works" do
|
||||
# Faker.numerify("###").match(/\d{3}/) { |md| md.size }.should eq 3
|
||||
it "numerify" do
|
||||
100.times do
|
||||
assert { Faker.numerify("###").match(/[1-9]\d{2}/).should_not eq nil }
|
||||
end
|
||||
end
|
||||
|
||||
it "letterify" do
|
||||
assert { Faker.letterify("???").match(/[A-Z]{3}/).should_not eq nil }
|
||||
end
|
||||
|
||||
it "regexify" do
|
||||
{
|
||||
"uk post code" => /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/,
|
||||
"us phone" => /^(1-?)[2-8][0-1][0-9]-\d{3}-\d{4}$/,
|
||||
}.each do |label, re|
|
||||
10.times do
|
||||
assert { Faker.regexify(re).match(re).should_not eq nil }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
24
src/data.cr
24
src/data.cr
@ -21,18 +21,18 @@ module Faker
|
||||
},
|
||||
|
||||
"credit_card": {
|
||||
"american_express": ["/34##-######-####L/", "/37##-######-####L/"],
|
||||
"dankort": ["/5019-####-####-###L/"],
|
||||
"diners_club": ["/30[0-5]#-######-###L/", "/368#-######-###L/"],
|
||||
"discover": ["/6011-####-####-###L/", "/65##-####-####-###L/", "/64[4-9]#-####-####-###L/", "/6011-62##-####-####-###L/", "/65##-62##-####-####-###L/", "/64[4-9]#-62##-####-####-###L/"],
|
||||
"forbrugsforeningen": ["/6007-22##-####-###L/"],
|
||||
"jcb": ["/3528-####-####-###L/", "/3529-####-####-###L/", "/35[3-8]#-####-####-###L/"],
|
||||
"laser": ["/6304###########L/", "/6706###########L/", "/6771###########L/", "/6709###########L/", "/6304########\#{5,6}L/", "/6706########\#{5,6}L/", "/6771########\#{5,6}L/", "/6709########\#{5,6}L/"],
|
||||
"maestro": ["/50\#{9,16}L/", "/5[6-8]\#{9,16}L/", "/56#\#{9,16}L/"],
|
||||
"mastercard": ["/5[1-5]##-####-####-###L/", "/6771-89##-####-###L/"],
|
||||
"solo": ["/6767-####-####-###L/", "/6767-####-####-####-#L/", "/6767-####-####-####-##L/"],
|
||||
"switch": ["/6759-####-####-###L/", "/6759-####-####-####-#L/", "/6759-####-####-####-##L/"],
|
||||
"visa": ["/4###########L/", "/4###-####-####-###L/"],
|
||||
"american_express": [/34##-######-####L/, /37##-######-####L/],
|
||||
"dankort": [/5019-####-####-###L/],
|
||||
"diners_club": [/30[0-5]#-######-###L/, /368#-######-###L/],
|
||||
"discover": [/6011-####-####-###L/, /65##-####-####-###L/, /64[4-9]#-####-####-###L/, /6011-62##-####-####-###L/, /65##-62##-####-####-###L/, /64[4-9]#-62##-####-####-###L/],
|
||||
"forbrugsforeningen": [/6007-22##-####-###L/],
|
||||
"jcb": [/3528-####-####-###L/, /3529-####-####-###L/, /35[3-8]#-####-####-###L/],
|
||||
"laser": [/6304###########L/, /6706###########L/, /6771###########L/, /6709###########L/, /6304########\#{5,6}L/, /6706########\#{5,6}L/, /6771########\#{5,6}L/, /6709########\#{5,6}L/],
|
||||
"maestro": [/50\#{9,16}L/, /5[6-8]\#{9,16}L/, /56#\#{9,16}L/],
|
||||
"mastercard": [/5[1-5]##-####-####-###L/, /6771-89##-####-###L/],
|
||||
"solo": [/6767-####-####-###L/, /6767-####-####-####-#L/, /6767-####-####-####-##L/],
|
||||
"switch": [/6759-####-####-###L/, /6759-####-####-####-#L/, /6759-####-####-####-##L/],
|
||||
"visa": [/4###########L/, /4###-####-####-###L/],
|
||||
},
|
||||
"company": {
|
||||
"suffix": ["Inc", "and Sons", "LLC", "Group"],
|
||||
|
81
src/faker.cr
81
src/faker.cr
@ -16,16 +16,17 @@ module Faker
|
||||
end
|
||||
|
||||
def self.regexify(re)
|
||||
re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors
|
||||
.gsub(/\{(\d+)\}/, "{\1,\1}").gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1}
|
||||
.gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # [12]{1,2} becomes [12] or [12][12]
|
||||
.gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
|
||||
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
|
||||
.gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample } # (this|that) becomes "this" or "that"
|
||||
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0..range.size).map { |i| range[i] }).join("").split("-").to_a.sample } } # All A-Z inside of [] become C (or X, or whatever)
|
||||
.gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample } # All [ABC] become B (or A or C)
|
||||
.gsub("\d") { |match| (0..9).to_a.sample }
|
||||
.gsub("\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample }
|
||||
re = re.source if re.is_a?(Regex)
|
||||
re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors
|
||||
.gsub(/\{(\d+)\}/) { "{#{$1},#{$1}}" }.gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1}
|
||||
.gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * (Range.new($2.to_i, $3.to_i)).to_a.sample } # [12]{1,2} becomes [12] or [12][12]
|
||||
.gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
|
||||
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
|
||||
.gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample } # (this|that) becomes "this" or "that"
|
||||
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0...range.size).map { |i| range[i].to_s }).join("").split("-").to_a.sample } } # All A-Z inside of [] become C (or X, or whatever)
|
||||
.gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample } # All [ABC] become B (or A or C)
|
||||
.gsub("\\d") { |match| (0..9).to_a.sample }
|
||||
.gsub("\\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample }
|
||||
end
|
||||
|
||||
def self.fetch(data)
|
||||
@ -40,50 +41,30 @@ module Faker
|
||||
|
||||
def self.parse(st)
|
||||
st.gsub(/%\{([^\}]+)\}/) do |str, matches|
|
||||
# find_fn([Name.name, Name.first_name], $1)
|
||||
find_fnx($1)
|
||||
find_fn([
|
||||
Address.building_number,
|
||||
Address.city_prefix,
|
||||
Address.city_suffix,
|
||||
Address.state,
|
||||
Address.street_name,
|
||||
Address.street_suffix,
|
||||
Company.name,
|
||||
Company.suffix,
|
||||
Name.first_name,
|
||||
Name.last_name,
|
||||
Name.name,
|
||||
Name.prefix,
|
||||
Name.suffix,
|
||||
], $1)
|
||||
end
|
||||
end
|
||||
|
||||
# macro find_fn(list, fn)
|
||||
# case {{fn}}
|
||||
# {% for l in list %}
|
||||
# when "{{l}}"
|
||||
# {{l}}
|
||||
# {% end %}
|
||||
# else
|
||||
# "Hoaydaaa"
|
||||
# end
|
||||
# end
|
||||
|
||||
macro find_fnx(fn)
|
||||
macro find_fn(list, fn)
|
||||
case {{fn}}
|
||||
when "Address.building_number"
|
||||
Address.building_number
|
||||
when "Address.city_prefix"
|
||||
Address.city_prefix
|
||||
when "Address.city_suffix"
|
||||
Address.city_suffix
|
||||
when "Address.state"
|
||||
Address.state
|
||||
when "Address.street_name"
|
||||
Address.street_name
|
||||
when "Address.street_suffix"
|
||||
Address.street_suffix
|
||||
when "Company.name"
|
||||
Company.name
|
||||
when "Company.suffix"
|
||||
Company.suffix
|
||||
when "Name.first_name"
|
||||
Name.first_name
|
||||
when "Name.last_name"
|
||||
Name.last_name
|
||||
when "Name.name"
|
||||
Name.name
|
||||
when "Name.prefix"
|
||||
Name.prefix
|
||||
when "Name.suffix"
|
||||
Name.suffix
|
||||
{% for l in list %}
|
||||
when "{{l}}"
|
||||
{{l}}
|
||||
{% end %}
|
||||
else
|
||||
"Hoaydaaa"
|
||||
end
|
||||
|
@ -1,13 +1,9 @@
|
||||
module Faker
|
||||
class Name
|
||||
def self.name
|
||||
Faker.fetch(Data["name"]["name"])
|
||||
end
|
||||
|
||||
{% for data_type in %w(first_name last_name prefix suffix) %}
|
||||
def self.{{data_type.id}}
|
||||
Faker.fetch(Data["name"]["{{data_type.id}}"])
|
||||
end
|
||||
{% for data_type in %w(first_name last_name name prefix suffix) %}
|
||||
def self.{{data_type.id}}
|
||||
Faker.fetch(Data["name"]["{{data_type.id}}"])
|
||||
end
|
||||
{% end %}
|
||||
|
||||
def self.title
|
||||
|
Loading…
Reference in New Issue
Block a user