Skip to content

Commit

Permalink
Add timespan validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasuhiro Asaka authored and Yasuhiro Asaka committed Aug 26, 2013
1 parent dcc9fc6 commit c0c879c
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,4 +1,6 @@
*~
*#
.ruby-version
.bundle
ausgabe.csv
mechanize/
95 changes: 71 additions & 24 deletions bin/brand2csv
Expand Up @@ -5,6 +5,7 @@ root = Pathname.new(__FILE__).realpath.parent.parent
$:.unshift root.join('lib') if $0 == __FILE__

require 'optparse'
require "date"
require 'brand2csv'

def help
Expand All @@ -22,32 +23,78 @@ Usage:
EOS
end

parser = OptionParser.new
opts = {}
parser.on('--swiss_only') {|v| opts[:swiss_only] = true }
parser.on_tail('-h', '--help') { puts help; exit }

args = ARGV.dup
begin
parser.parse!(args)
rescue OptionParser::MissingArgument,
OptionParser::InvalidArgument,
OptionParser::InvalidOption
puts help
exit 1
def validates_timespan(arg)
valid = true
timespan = ""
dates = arg.gsub(/[^\d\.-]/, '').split("-")
catch (:error) do
dates.map.with_index do |d, i|
sep = (dates.length > 1 && i != 0) ? "-" : ""
begin
Date.parse(d)
timespan << sep + d
rescue ArgumentError
valid = false
elms = d.split(".")
prms = [elms[2], elms[1], -1].map(&:to_i)
begin
cand = Date.new(*prms).strftime("%d.%m.%Y")
if elms[0] == (elms - cand.to_s.split(".")).first
timespan << sep + cand.to_s
else
raise
end
rescue ArgumentError
timespan = "" # unknown
throw :error
end
end
end
end
message = nil
unless valid
if timespan.empty?
message = "Timespan is invalid"
else
message = "Did you mean #{timespan} ?"
end
end
[valid, message]
end

if $0 == __FILE__
parser = OptionParser.new
opts = {}
parser.on('--swiss_only') {|v| opts[:swiss_only] = true }
parser.on_tail('-h', '--help') { puts help; exit }

unless args.size >= 1
puts help
exit 1
end
args = ARGV.dup
begin
parser.parse!(args)
rescue OptionParser::MissingArgument,
OptionParser::InvalidArgument,
OptionParser::InvalidOption
puts help
exit 1
end

begin
Brand2csv::run(args[0], args[1], opts[:swiss_only])
rescue Interrupt
puts "Unterbrochen. Breche mit Fehler ab"
exit 1
end
unless args.size >= 1
puts help
exit 1
end

valid,message = validates_timespan(args[0])
unless valid
puts message
exit 1
end

puts "#{__FILE__} completed successfully" if $VERBOSE
begin
Brand2csv::run(args[0], args[1], opts[:swiss_only])
rescue Interrupt
puts "Unterbrochen. Breche mit Fehler ab"
exit 1
end

puts "#{__FILE__} completed successfully" if $VERBOSE
end
64 changes: 64 additions & 0 deletions spec/brand2csv_spec.rb
@@ -0,0 +1,64 @@
# encoding : utf-8

require "spec_helper"
load File.expand_path("../../bin/brand2csv", __FILE__)

describe Brand2csv do
describe "#validates_timespan" do
subject { validates_timespan(arg) }

context "when timespan is a single date" do
context "when invalid last day of month in a date is given" do
let(:arg) { "29.02.2010" }
it { should eql [false, "Did you mean 28.02.2010 ?"] }
end

context "when invalid month in a date is given" do
let(:arg) { "28.13.2010" }
it { should eql [false, "Timespan is invalid"] }
end

context "when a valid date is given" do
let(:arg) { "31.12.2010" }
it { should eql [true, nil] }
end
end

context "when timespan is term" do
context "when invalid last day of month in start date is given" do
let(:arg) { "29.02.2010-31.03.2010" }
it { should eql [false, "Did you mean 28.02.2010-31.03.2010 ?"] }
end

context "when invalid last day of month in end date is given" do
let(:arg) { "28.04.2010-99.03.2010" }
it { should eql [false, "Did you mean 28.04.2010-31.03.2010 ?"] }
end

context "when invalid last day of month in both dates is given" do
let(:arg) { "32.01.2010-99.05.2010" }
it { should eql [false, "Did you mean 31.01.2010-31.05.2010 ?"] }
end

context "when invalid month in start date is given" do
let(:arg) { "31.99.2010-31.05.2010" }
it { should eql [false, "Timespan is invalid"] }
end

context "when invalid month in end date is given" do
let(:arg) { "31.01.2010-31.20.2010" }
it { should eql [false, "Timespan is invalid"] }
end

context "when invalid month in both dates is given" do
let(:arg) { "31.30.2010-31.20.2010" }
it { should eql [false, "Timespan is invalid"] }
end

context "when valid term is given" do
let(:arg) { "28.02.2010-31.05.2010" }
it { should eql [true, nil] }
end
end
end
end
25 changes: 25 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,25 @@
# encoding: utf-8
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift File.dirname(__FILE__)

require 'bundler/setup'
Bundler.require

require 'rspec'
require 'brand2csv'

Dir[File.join(File.dirname(__FILE__), "spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.filter_run_excluding :slow
#config.exclusion_filter = {:slow => true}

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
end

0 comments on commit c0c879c

Please sign in to comment.