Skip to content

Commit

Permalink
Make Faker::Number return integers and floats instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Jun 3, 2018
1 parent a4ffbd8 commit b4e8d17
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
14 changes: 7 additions & 7 deletions doc/number.md
Expand Up @@ -2,18 +2,18 @@

```ruby
# Required parameter: digits
Faker::Number.number(10) #=> "1968353479"
Faker::Number.number(10) #=> 1968353479

# Required parameter: digits
Faker::Number.leading_zero_number(10) #=> "0669336915"
Faker::Number.leading_zero_number(10) #=> 0669336915

# Required parameter: digits
Faker::Number.decimal_part(2) #=> "0074009009"
Faker::Number.decimal_part(2) #=> 0074009009

# Required parameter: l_digits
Faker::Number.decimal(2) #=> "11.88"
Faker::Number.decimal(2) #=> 11.88

Faker::Number.decimal(2, 3) #=> "18.843"
Faker::Number.decimal(2, 3) #=> 18.843

# Required parameters: mean, standard_deviation
Faker::Number.normal(50, 3.5) #=> 47.14669604069156
Expand All @@ -29,7 +29,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/code.rb
Expand Up @@ -21,7 +21,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
16 changes: 9 additions & 7 deletions lib/faker/number.rb
Expand Up @@ -2,22 +2,24 @@ module Faker
class Number < Base
class << self
def number(digits = 10)
return if digits.zero?

num = ''
if digits > 1
num = non_zero_digit
num = non_zero_digit.to_s
digits -= 1
end
num + leading_zero_number(digits)
(num + leading_zero_number(digits)).to_i
end

def leading_zero_number(digits = 10)
(1..digits).collect { digit }.join
(1..digits).collect { digit.to_s }.join
end

def decimal_part(digits = 10)
num = ''
if digits > 1
num = non_zero_digit
num = non_zero_digit.to_s
digits -= 1
end
leading_zero_number(digits) + num
Expand All @@ -26,15 +28,15 @@ def decimal_part(digits = 10)
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}.#{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
13 changes: 7 additions & 6 deletions lib/faker/omniauth.rb
Expand Up @@ -13,7 +13,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 @@ -62,7 +62,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 @@ -90,7 +91,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 @@ -104,7 +105,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 @@ -177,7 +178,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 @@ -240,7 +241,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
6 changes: 2 additions & 4 deletions test/test_faker_markdown.rb
Expand Up @@ -74,11 +74,9 @@ def test_random

def test_sandwich
test_trigger = @tester.sandwich
test_array = test_trigger.split("\n")

test_array = []
test_trigger.each_line { |substr| test_array << substr }

assert(test_array.length == 3)
assert_equal 3, test_array.length

assert(test_array[0].split(' ').length == 2)
assert(test_array[0].split(' ').first.include?('#'))
Expand Down
26 changes: 13 additions & 13 deletions test/test_faker_number.rb
Expand Up @@ -7,26 +7,26 @@ def setup
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(0) == ''
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}/)
assert @tester.decimal(2).to_s.match(/[0-9]{2}\.[0-9]{2}/)
assert @tester.decimal(4, 5).to_s.match(/[0-9]{4}\.[0-9]{5}/)
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 @@ -104,14 +104,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[1-9]/, @tester.decimal(1, 2).to_s)
end
end
end
Expand Down

0 comments on commit b4e8d17

Please sign in to comment.