[ruby] Adds test and code

This commit is contained in:
Nemo 2020-04-26 08:13:44 +05:30
parent 8ec8ab839e
commit 89d922062a
4 changed files with 25 additions and 4 deletions

View File

@ -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'

View File

@ -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

View File

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

View File

@ -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