require 'spec_helper' require_relative '../../lib/sredder/arg_parser' require_relative '../../lib/sredder/version' require_relative '../../lib/sredder/util' describe Sredder::ArgParser do let(:parser) { Sredder::ArgParser.new } describe 'parse' do it 'parses the message' do result = parser.parse(['-m', 'some message']) result[:message].should == 'some message' result = parser.parse(['--message', 'some message']) result[:message].should == 'some message' end it 'parses hours as a double' do result = parser.parse(['-h', '3.5']) result[:hours].should == 3.5 result = parser.parse(['--hours', '3.5']) result[:hours].should == 3.5 end it 'parses the date as a time' do time = Time.new(2012, 12, 19) time_str = time.strftime('%y-%m-%d') result = parser.parse(['-d', time_str]) result[:date].should == Sredder::Util.time_stamp(time) result = parser.parse(['--date', time_str]) result[:date].should == Sredder::Util.time_stamp(time) end it 'it can print the help message' do lambda { parser.parse(['--help']) }.should raise_error SystemExit end it 'it can print the version number' do lambda { parser.parse(['--version']) }.should raise_error SystemExit end it 'sets defaults' do result = parser.parse([]) result[:date].should_not be_nil result[:folder].should == 'Programming' end end describe '#valid?' do it 'returns false by default' do parser.parse([]) parser.valid?.should be_false end it 'returns true if all the options are set' do parser.parse([ '-t', 'a title', '-m', 'a message', '-h', '3.5' ]) parser.valid?.should be_true end end describe '#validate!' do it 'system exits if the options are not valid' do parser.stub(:valid? => false) lambda { parser.validate! }.should raise_error SystemExit end it 'is a no-op if the options are valid' do parser.stub(:valid? => true) lambda { parser.validate! }.should_not raise_error end end end