spec/timeliness/format_set_spec.rb in timeliness-0.2.0 vs spec/timeliness/format_set_spec.rb in timeliness-0.3.0

- old
+ new

@@ -1,44 +1,18 @@ require 'spec_helper' describe Timeliness::FormatSet do - context ".define_format_method" do - it "should define method which outputs date array with values in correct order" do - define_method_for('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,nil,nil,nil,nil,nil] - end + context "#compile!" do + let(:set) { Timeliness::FormatSet.new(['yyyy-mm-dd', 'dd/mm/yyyy']) } - it "should define method which outputs date array from format with different order" do - define_method_for('dd/mm/yyyy').call('2', '1', '2000').should == [2000,1,2,nil,nil,nil,nil,nil] + it 'should set the regexp for the set' do + set.compile! + set.regexp.should_not be_nil end - - it "should define method which outputs time array" do - define_method_for('hh:nn:ss').call('01', '02', '03').should == [nil,nil,nil,1,2,3,nil,nil] - end - - it "should define method which outputs time array with meridian 'pm' adjusted hour" do - define_method_for('hh:nn:ss ampm').call('01', '02', '03', 'pm').should == [nil,nil,nil,13,2,3,nil,nil] - end - - it "should define method which outputs time array with meridian 'am' unadjusted hour" do - define_method_for('hh:nn:ss ampm').call('01', '02', '03', 'am').should == [nil,nil,nil,1,2,3,nil,nil] - end - - it "should define method which outputs time array with microseconds" do - define_method_for('hh:nn:ss.u').call('01', '02', '03', '99').should == [nil,nil,nil,1,2,3,990000,nil] - end - - it "should define method which outputs datetime array with zone offset" do - define_method_for('yyyy-mm-dd hh:nn:ss.u zo').call('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000] - end - - it "should define method which outputs datetime array with timezone string" do - define_method_for('yyyy-mm-dd hh:nn:ss.u tz').call('2001', '02', '03', '04', '05', '06', '99', 'EST').should == [2001,2,3,4,5,6,990000,'EST'] - end end context "compiled regexp" do - context "for time formats" do format_tests = { 'hh:nn:ss' => {:pass => ['12:12:12', '01:01:01'], :fail => ['1:12:12', '12:1:12', '12:12:1', '12-12-12']}, 'hh-nn-ss' => {:pass => ['12-12-12', '01-01-01'], :fail => ['1-12-12', '12-1-12', '12-12-1', '12:12:12']}, 'h:nn' => {:pass => ['12:12', '1:01'], :fail => ['12:2', '12-12']}, @@ -97,11 +71,24 @@ end end end - def define_method_for(format) - Timeliness::FormatSet.compile([format]).method(:"format_#{format}") + context "#match" do + let(:set) { Timeliness::FormatSet.compile(['yyyy-mm-dd', 'dd/mm/yyyy']) } + + it 'should return array if string matches a format in set' do + set.match('2000-01-02').should be_kind_of(Array) + end + + it 'should return nil if string does not matches a format in set' do + set.match('2nd Feb 2000').should be_nil + end + + it 'should only use specific format string for match if provided' do + set.match('2000-01-02', 'yyyy-mm-dd').should be_kind_of(Array) + set.match('2000-01-02', 'dd/mm/yyyy').should be_nil + end end def compile_regexp(format) Timeliness::FormatSet.compile([format]).regexp end