-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test generator for Luhn, with example solution
- Loading branch information
Showing
5 changed files
with
124 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,30 @@ | ||
class Luhn | ||
def self.create(number) | ||
test_number = number * 10 | ||
luhn = Luhn.new(test_number) | ||
return test_number if luhn.valid? | ||
test_number + 10 - (luhn.checksum % 10) | ||
end | ||
DOUBLE = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9] | ||
DOUBLE.freeze | ||
|
||
attr_reader :number | ||
def initialize(number) | ||
@number = number | ||
def self.valid?(string) | ||
Luhn.new(string).valid? | ||
end | ||
|
||
def addends | ||
numbers = [] | ||
number.to_s.reverse.split('').map(&:to_i).each_with_index do |n, i| | ||
if i % 2 == 0 | ||
numbers << n | ||
else | ||
value = n * 2 | ||
value -= 9 if value > 9 | ||
numbers << value | ||
end | ||
end | ||
numbers.reverse | ||
def initialize(string) | ||
@string = string.tr(' ', '') | ||
end | ||
|
||
def checksum | ||
addends.inject(0, :+) | ||
@string. | ||
reverse.each_char.with_index. | ||
reduce(0) {|sum, (c, i)| sum + (i.odd? ? DOUBLE[c.to_i] : c.to_i) } | ||
end | ||
|
||
def valid? | ||
checksum % 10 == 0 | ||
clean? && (checksum % 10).zero? | ||
end | ||
|
||
def clean? | ||
@string.match(/^\d{2,}$/) | ||
end | ||
end | ||
|
||
module BookKeeping | ||
VERSION = 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env ruby | ||
gem 'minitest', '>= 5.0.0' | ||
require 'minitest/autorun' | ||
require_relative 'luhn' | ||
|
||
# Common test data version: <%= abbreviated_commit_hash %> | ||
class LuhnTest < Minitest::Test<% test_cases.each do |test_case| %> | ||
def <%= test_case.name %> | ||
<%= test_case.skipped %> | ||
<%= test_case.workload %> | ||
end | ||
<% end %> | ||
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> | ||
def test_bookkeeping | ||
skip | ||
assert_equal <%= version %>, BookKeeping::VERSION | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'exercise_cases' | ||
|
||
class LuhnCase < OpenStruct | ||
def name | ||
'test_%s' % description.tr('- ', '__') | ||
end | ||
|
||
def workload | ||
%Q(#{assertion} Luhn.valid?(#{input.inspect})) | ||
end | ||
|
||
def skipped | ||
index.zero? ? '# skip' : 'skip' | ||
end | ||
|
||
private | ||
|
||
def assertion | ||
expected ? 'assert' : 'refute' | ||
end | ||
end | ||
|
||
LuhnCases = proc do |data| | ||
JSON.parse(data)['cases'].map.with_index do |row, i| | ||
LuhnCase.new(row.merge('index' => i)) | ||
end | ||
end |