Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Faker::Number return integers and floats instead of strings #510

Merged
merged 1 commit into from Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 5 additions & 13 deletions doc/unreleased/default/number.md
Expand Up @@ -3,22 +3,14 @@
```ruby
# Required parameter: digits
# Produces a number of the specified digits where the leading digit is never 0
Faker::Number.number(10) #=> "1968353479"

# Required parameter: digits
# Produces a number of the specified digits with a leading zero
Faker::Number.leading_zero_number(10) #=> "0669336915"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed leading_zero_number because it has no meaning now that we are returning integers.


# Required parameter: digits
# Produces a 2-digit number, preserves leading 0's
Faker::Number.decimal_part(2) #=> "09"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed decimal_part because it can now be replaced by number/number.to_s as required.

Faker::Number.number(10) #=> 1968353479

# Required parameter: l_digits
# Produces a number with 2 digits leading and trailing the decimal
Faker::Number.decimal(2) #=> "11.88"
Faker::Number.decimal(2) #=> 11.88

# Specify different values for leading and trailing digits
Faker::Number.decimal(2, 3) #=> "18.843"
Faker::Number.decimal(3, 3) #=> 181.843

# Required parameters: mean, standard_deviation
Faker::Number.normal(50, 3.5) #=> 47.14669604069156
Expand All @@ -38,7 +30,7 @@ Faker::Number.positive #=> 235.59238499107653

Faker::Number.negative #=> -4480.042585669558

Faker::Number.non_zero_digit #=> "8"
Faker::Number.non_zero_digit #=> 8

Faker::Number.digit #=> "1"
Faker::Number.digit #=> 1
```
2 changes: 1 addition & 1 deletion lib/faker/default/code.rb
Expand Up @@ -23,7 +23,7 @@ def ean(base = 13)
end

def rut
value = Number.number(8)
value = Number.number(8).to_s
vd = rut_verificator_digit(value)
value << "-#{vd}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/company.rb
Expand Up @@ -194,7 +194,7 @@ def mod11(number)
def luhn_algorithm(number)
multiplications = []

number.reverse.split(//).each_with_index do |digit, i|
number.to_s.reverse.split(//).each_with_index do |digit, i|
multiplications << if i.even?
digit.to_i * 2
else
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/driving_licence.rb
Expand Up @@ -19,7 +19,7 @@ def british_driving_licence(last_name: Faker::Name.last_name,
end

def northern_irish_driving_licence
Faker::Number.number(8)
Faker::Number.number(8).to_s
end

def uk_driving_licence(*args)
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/id_number.rb
Expand Up @@ -76,7 +76,7 @@ def invalid_south_african_id_number
end

def brazilian_citizen_number(formatted: false)
digits = Faker::Number.leading_zero_number(9) until digits&.match(/(\d)((?!\1)\d)+/)
digits = Faker::Number.number(9).to_s until digits&.match(/(\d)((?!\1)\d)+/)
first_digit = brazilian_citizen_number_checksum_digit(digits)
second_digit = brazilian_citizen_number_checksum_digit(digits + first_digit)
number = [digits, first_digit, second_digit].join
Expand Down
47 changes: 21 additions & 26 deletions lib/faker/default/number.rb
Expand Up @@ -4,42 +4,31 @@ module Faker
class Number < Base
class << self
def number(digits = 10)
num = ''
return if digits < 1
return 0 if digits == 1

if digits > 1
num = non_zero_digit
digits -= 1
end

num + (1..digits).collect { digit }.join
end

def leading_zero_number(digits = 10)
'0' + (2..digits).collect { digit }.join
end

def decimal_part(digits = 10)
num = ''
if digits > 1
num = non_zero_digit
digits -= 1
end
leading_zero_number(digits) + num
# Ensure the first digit is not zero
([non_zero_digit] + generate(digits - 1)).join.to_i
end

def decimal(l_digits = 5, r_digits = 2)
l_d = number(l_digits)
r_d = decimal_part(r_digits)

"#{l_d}.#{r_d}"
l_d = number(l_digits).to_s
r_d = if r_digits == 1
0
else
# Ensure the last digit is not zero
# so it does not get truncated on converting to float
generate(r_digits - 1).join + non_zero_digit.to_s
end
"#{l_d}.#{r_d}".to_f
end

def non_zero_digit
rand(1..9).to_s
rand(1..9)
end

def digit
rand(10).to_s
rand(10)
end

def hexadecimal(digits = 6)
Expand Down Expand Up @@ -77,6 +66,12 @@ def negative(from = -5000.00, to = -1.00)

private

def generate(count)
return [] if count.zero?

Array.new(count) { digit }
end

def greater_than_zero(number)
should_be(number, :>)
end
Expand Down
13 changes: 7 additions & 6 deletions lib/faker/default/omniauth.rb
Expand Up @@ -15,7 +15,7 @@ def initialize(name: nil, email: nil)
end

class << self
def google(name: nil, email: nil, uid: Number.number(9))
def google(name: nil, email: nil, uid: Number.number(9).to_s)
auth = Omniauth.new(name: name, email: email)
{
provider: 'google_oauth2',
Expand Down Expand Up @@ -64,7 +64,8 @@ def google(name: nil, email: nil, uid: Number.number(9))
}
end

def facebook(name: nil, email: nil, username: nil, uid: Number.number(7))
def facebook(name: nil, email: nil, username: nil,
uid: Number.number(7).to_s)
auth = Omniauth.new(name: name, email: email)
username ||= "#{auth.first_name.downcase[0]}#{auth.last_name.downcase}"
{
Expand Down Expand Up @@ -92,7 +93,7 @@ def facebook(name: nil, email: nil, username: nil, uid: Number.number(7))
link: "http://www.facebook.com/#{username}",
username: username,
location: {
id: Number.number(9),
id: Number.number(9).to_s,
name: city_state
},
gender: gender,
Expand All @@ -106,7 +107,7 @@ def facebook(name: nil, email: nil, username: nil, uid: Number.number(7))
}
end

def twitter(name: nil, nickname: nil, uid: Number.number(6))
def twitter(name: nil, nickname: nil, uid: Number.number(6).to_s)
auth = Omniauth.new(name: name)
nickname ||= auth.name.downcase.delete(' ')
location = city_state
Expand Down Expand Up @@ -179,7 +180,7 @@ def twitter(name: nil, nickname: nil, uid: Number.number(6))
}
end

def linkedin(name: nil, email: nil, uid: Number.number(6))
def linkedin(name: nil, email: nil, uid: Number.number(6).to_s)
auth = Omniauth.new(name: name, email: email)
first_name = auth.first_name.downcase
last_name = auth.last_name.downcase
Expand Down Expand Up @@ -242,7 +243,7 @@ def linkedin(name: nil, email: nil, uid: Number.number(6))
}
end

def github(name: nil, email: nil, uid: Number.number(8))
def github(name: nil, email: nil, uid: Number.number(8).to_s)
auth = Omniauth.new(name: name, email: email)
login = auth.name.downcase.tr(' ', '-')
html_url = "https://github.com/#{login}"
Expand Down
4 changes: 2 additions & 2 deletions test/faker/default/test_faker_company.rb
Expand Up @@ -147,12 +147,12 @@ def test_south_african_trust_registration_number
def test_luhn_algorithm
# Odd length base for luhn algorithm
odd_base = Faker::Number.number([5, 7, 9, 11, 13].sample)
odd_luhn_complement = @tester.send(:luhn_algorithm, odd_base)
odd_luhn_complement = @tester.send(:luhn_algorithm, odd_base).to_s
odd_control = "#{odd_base}#{odd_luhn_complement}"

# Even length base for luhn algorithm
even_base = Faker::Number.number([4, 6, 8, 10, 12].sample)
even_luhn_complement = @tester.send(:luhn_algorithm, even_base)
even_luhn_complement = @tester.send(:luhn_algorithm, even_base).to_s
even_control = "#{even_base}#{even_luhn_complement}"

assert((luhn_checksum(odd_control) % 10).zero?)
Expand Down
32 changes: 15 additions & 17 deletions test/faker/default/test_faker_number.rb
Expand Up @@ -8,31 +8,29 @@ def setup
@tester = Faker::Number
end

def test_leading_zero_number
assert_match(/^0[0-9]{9}/, @tester.leading_zero_number)
assert_match(/^0[0-9]{8}/, @tester.leading_zero_number(9))
end

def test_number
assert @tester.number(10).match(/[0-9]{10}/)
assert @tester.number(10).to_s.match(/[0-9]{10}/)

10.times do |digits|
digits += 1
assert @tester.number(digits).match(/^[0-9]{#{digits}}$/)
assert @tester.number(digits).to_s.match(/^[0-9]{#{digits}}$/)
end

assert @tester.number(10).length == 10
assert @tester.number(1).length == 1
assert @tester.number(10).to_s.length == 10
assert @tester.number(1).to_s.length == 1
assert @tester.number(0).nil?
end

def test_decimal
assert @tester.decimal(2).match(/[0-9]{2}\.[0-9]{2}/)
assert @tester.decimal(4, 5).match(/[0-9]{4}\.[0-9]{5}/)
100.times do
assert @tester.decimal(2).to_s.match(/[1-9][0-9]\.[0-9][1-9]/)
assert @tester.decimal(4, 5).to_s.match(/[1-9][0-9]{3}\.[0-9]{4}[1-9]/)
end
end

def test_digit
assert @tester.digit.match(/[0-9]{1}/)
assert((1..1000).collect { |_i| @tester.digit == '9' }.include?(true))
assert @tester.digit.to_s.match(/[0-9]{1}/)
assert((1..1000).collect { |_i| @tester.digit == 9 }.include?(true))
end

def test_even_distribution
Expand Down Expand Up @@ -118,14 +116,14 @@ def test_hexadecimal

def test_insignificant_zero
@tester.stub :digit, 0 do
assert_equal '0', @tester.number(1)
assert_equal 0, @tester.number(1)
100.times do
assert_match(/^[1-9]0/, @tester.number(2))
assert_match(/^[1-9]0/, @tester.number(2).to_s)
end

assert_equal '0.0', @tester.decimal(1, 1)
assert_equal 0.0, @tester.decimal(1, 1)
100.times do
assert_match(/^0\.0[1-9]/, @tester.decimal(1, 2))
assert_match(/^0\.[0-9]/, @tester.decimal(1, 2).to_s)
end
end
end
Expand Down