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

Add max_rut option to Faker::ChileRut.rut #2778

Merged
merged 3 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions lib/faker/default/chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ class << self
##
# Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits).
#
# @param min_rut [Integer] Specifies the minimum value of the rut.
# @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
# @param min_rut [Integer] Specifies the minimum value of the RUT.
# @param max_rut [Integer] Specifies the maximum value of the RUT.
# @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
# @return [Number]
#
# @example
# Faker::ChileRut.rut #=> 11235813
# Faker::ChileRut.rut(min_rut: 20890156) #=> 31853211
# Faker::ChileRut.rut(min_rut: 20890156, fixed: true) #=> 20890156
# Faker::ChileRut.rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> 21853211
# Faker::ChileRut.rut(min_rut: 20_890_156, fixed: true) #=> 20890156
#
# @faker.version 1.9.2
def rut(min_rut: 1, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
# @faker.version next
def rut(min_rut: 1, max_rut: 99_999_999, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, max_rut)
end

##
Expand Down Expand Up @@ -68,22 +69,21 @@ def check_digit
##
# Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits) with a dv (digito verificador, check-digit).
#
# @param min_rut [Integer] Specifies the minimum value of the rut.
# @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
# @param min_rut [Integer] Specifies the minimum value of the RUT.
# @param max_rut [Integer] Specifies the maximum value of the RUT.
# @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
# @return [String]
#
# @example
# Faker::ChileRut.full_rut #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 20890156) #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> "20686957-4"
3ynm marked this conversation as resolved.
Show resolved Hide resolved
# Faker::ChileRut.full_rut(min_rut: 30_686_957, fixed: true) #=> "30686957-4"
#
# @faker.version next
def full_rut(min_rut: 0, fixed: false, formatted: false)
if formatted
"#{rut(min_rut: min_rut, fixed: fixed).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse}-#{dv}"
else
"#{rut(min_rut: min_rut, fixed: fixed)}-#{dv}"
end
def full_rut(min_rut: 1, max_rut: 99_999_999, fixed: false, formatted: false)
this_rut = rut(min_rut: min_rut, max_rut: max_rut, fixed: fixed)
this_rut = this_rut.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse if formatted
3ynm marked this conversation as resolved.
Show resolved Hide resolved
"#{this_rut}-#{dv}"
end

attr_reader :last_rut
Expand Down
5 changes: 5 additions & 0 deletions test/faker/default/test_faker_chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup

def test_full_rut
assert_equal('6-k', @tester.full_rut(min_rut: 6, fixed: true))
assert_equal('8', @tester.full_rut(min_rut: 8, max_rut: 8)[0])
3ynm marked this conversation as resolved.
Show resolved Hide resolved
assert_equal('30686957-4', @tester.full_rut(min_rut: 30_686_957, fixed: true))
end

Expand All @@ -17,6 +18,10 @@ def test_rut_length
assert @tester.rut.to_s.length <= 8
end

def test_rut_min_max
assert_equal(7, @tester.rut(min_rut: 7, max_rut: 7))
end

# We need to set specific rut before testing the check digit
# since the whole idea of the method revolves around calculating
# the check digit for that specific rut.
Expand Down
Loading