[ruby] Adds test and code

master
Nemo 2 years ago
parent 8ec8ab839e
commit 89d922062a
  1. 1
      Rakefile
  2. 19
      src/pincode_validator.rb
  3. 3
      src/pincode_validator/version.rb
  4. 6
      tests/validate_spec.rb

@ -1,4 +1,5 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'tests/*_spec.rb'

@ -1,6 +1,23 @@
require "pincode_validator/version"
module PincodeValidator
VERSION = "1.0.3"
FILENAME='regex.txt'
class Error < StandardError; end
# Your code goes here...
def self.root
File.dirname __dir__
end
@@regexes ||= IO.readlines(File.join root, FILENAME).map do |line|
Regexp.new("^#{line.strip[1...-1]}$")
end
def self.valid?(pincode)
@@regexes.each do |r|
return true if r.match? pincode
end
false
end
end

@ -1,3 +0,0 @@
module PincodeValidator
VERSION = "1.0.3"
end

@ -6,4 +6,10 @@ describe PincodeValidator do
expect(described_class::valid? pin).to eq(true)
end
end
it 'should validate incorrect pincodes' do
['999999', '99999', '9999', '999', '99', '9', '111111', '2447131'].each do |pin|
expect(described_class::valid? pin).to eq(false), "#{pin} should be invalid"
end
end
end

Loading…
Cancel
Save