Skip to content

Commit

Permalink
Test the Vehicle VIN validator with known good and bad values, fixes … (
Browse files Browse the repository at this point in the history
#2640)

* Test the Vehicle VIN validator with known good and bad values, fixes #2639

* Fix rubocop issues

* Deterministically verify vehicle vins, move VIN_REGEX to test file, refactor deterministic tests

* Remove duplicate vin regex test

Co-authored-by: Vitor Oliveira <vbrazo@gmail.com>
  • Loading branch information
alextaujenis and vbrazo committed Jan 24, 2023
1 parent 9246681 commit 2f63c85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/faker/default/vehicle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Vehicle < Base
VIN_KEYSPACE = %w[A B C D E F G H J K L M N P R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9].freeze
VIN_TRANSLITERATION = { A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9, S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9 }.freeze
VIN_WEIGHT = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2].freeze
VIN_REGEX = /\A[A-HJ-NPR-Z0-9]{17}\z/.freeze
SG_CHECKSUM_WEIGHTS = [3, 14, 2, 12, 2, 11, 1].freeze
SG_CHECKSUM_CHARS = 'AYUSPLJGDBZXTRMKHEC'

Expand Down
8 changes: 4 additions & 4 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def test_username_with_string_arg
end

def test_username_with_string_arg_determinism
deterministically_verify -> { @tester.username(specifier: 'bo peep') }, depth: 4 do |subject|
assert_match(/(bo(_|\.)peep|peep(_|\.)bo)/, subject)
deterministically_verify -> { @tester.username(specifier: 'bo peep') }, depth: 4 do |username|
assert_match(/(bo(_|\.)peep|peep(_|\.)bo)/, username)
end
end

Expand Down Expand Up @@ -236,8 +236,8 @@ def test_deterministic_password_with_compatible_min_length_and_requirements
[false, true].each do |value|
min_length = value ? 2 : 1

deterministically_verify -> { @tester.password(min_length: min_length, mix_case: value, special_characters: !value) }, depth: 4 do |subject|
assert_nothing_raised { subject }
deterministically_verify -> { @tester.password(min_length: min_length, mix_case: value, special_characters: !value) }, depth: 4 do |password|
assert_nothing_raised { password }
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions test/faker/default/test_faker_vehicle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

class TestFakerVehicle < Test::Unit::TestCase
WORD_MATCH = /\w+\.?/.freeze
VIN_REGEX = /\A[A-HJ-NPR-Z0-9]{17}\z/.freeze

def setup
@tester = Faker::Vehicle
end

def test_vin
assert_match Faker::Vehicle::VIN_REGEX, @tester.vin
end
assert valid_vin('11111111111111111') # known valid test string
assert valid_vin('FAKERGEM5FAKERGEM') # valid checksum
refute valid_vin('ABCDEFGH123456789') # invalid checksum

def test_vin_validity
100.times do
assert valid_vin(@tester.vin)
deterministically_verify -> { @tester.vin }, depth: 4 do |vin|
assert valid_vin(vin)
end
end

Expand Down Expand Up @@ -117,7 +118,7 @@ def doors_condition(doors)
end

def valid_vin(vin)
if vin && vin =~ Faker::Vehicle::VIN_REGEX
if vin && vin =~ VIN_REGEX
total = 0
vin.chars.each_with_index do |char, index|
value = (char =~ /\A\d\z/ ? char.to_i : Faker::Vehicle::VIN_TRANSLITERATION[char.to_sym])
Expand Down

0 comments on commit 2f63c85

Please sign in to comment.