Created
April 11, 2011 15:07
-
-
Save jancel/913662 to your computer and use it in GitHub Desktop.
an example from my parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Parser do | |
def parser(str = nil) | |
Parser.new(str) | |
end | |
describe "when initialized" do | |
describe "with a valid string" do | |
before(:each) do | |
@string_to_parse = "milk, eggs and bread, #groceries, #after-work @jancel" | |
end | |
it "should not have errors" do | |
parser(@string_to_parse).errors.count.should == 0 | |
end | |
it "should have 3 items" do | |
parser(@string_to_parse).items.count.should == 3 | |
end | |
it "should have 1 ats" do | |
parser(@string_to_parse).ats.count.should == 1 | |
end | |
it "should have 2 tags" do | |
parser(@string_to_parse).tags.count.should == 2 | |
end | |
end | |
describe "including a date where due is" do | |
before(:each) do | |
@string_to_parse = "milk, eggs, and bread, #groceries, due on {date_string}" | |
end | |
it "tuesday" do | |
@string_to_parse.gsub!(/{date_string}/, "tuesday") | |
if (Date.today.wday > 1) | |
parser(@string_to_parse).date.to_s.should eql (Date.today.end_of_week + 2.days).to_s | |
else | |
parser(@string_to_parse).date.to_s.should eql (Date.today.beginning_of_week + 1.days).to_s | |
end | |
end | |
it "wednesday" do | |
@string_to_parse.gsub!(/{date_string}/, "wednesday") | |
if (Date.today.wday > 2) | |
parser(@string_to_parse).date.to_s.should eql (Date.today.end_of_week + 3.days).to_s | |
else | |
parser(@string_to_parse).date.to_s.should eql (Date.today.beginning_of_week + 2.days).to_s | |
end | |
end | |
it "friday" do | |
@string_to_parse.gsub!(/{date_string}/, "friday") | |
if (Date.today.wday > 4) | |
parser(@string_to_parse).date.to_s.should eql (Date.today.end_of_week + 5.days).to_s | |
else | |
parser(@string_to_parse).date.to_s.should eql (Date.today.beginning_of_week + 4.days).to_s | |
end | |
end | |
end | |
describe "including a date with due in" do | |
before(:each) do | |
@string_to_parse = "milk, eggs, and bread, #groceries, due in {date_string}" | |
end | |
it "3 weeks" do | |
@string_to_parse.gsub!(/{date_string}/, "3 weeks") | |
parser(@string_to_parse).date.to_s.should eql 3.weeks.from_now.to_s | |
end | |
it "3 years" do | |
@string_to_parse.gsub!(/{date_string}/, "3 years") | |
parser(@string_to_parse).date.to_s.should eql 3.years.from_now.to_s | |
end | |
it "3 months" do | |
@string_to_parse.gsub!(/{date_string}/, "3 months") | |
parser(@string_to_parse).date.to_s.should eql 3.months.from_now.to_s | |
end | |
end | |
describe "including a date with by" do | |
describe "with a few random strings" do | |
before(:each) do | |
@string_to_parse = "milk, eggs, and bread, #groceries, by {date_string}" | |
end | |
it "should have a date" do | |
@string_to_parse.gsub!(/{date_string}/, "tomorrow") | |
parser(@string_to_parse).date.should == parser(@string_to_parse).date | |
parser(@string_to_parse).items.count.should == 3 | |
end | |
it "should have a date with 'monday'" do | |
@string_to_parse.gsub!(/{date_string}/, "monday") | |
parser(@string_to_parse).date.should == Date.today.end_of_week + 1.day | |
end | |
it "should have a date with 'next monday'" do | |
@string_to_parse.gsub!(/{date_string}/, "next monday") | |
parser(@string_to_parse).date.should == Date.today.end_of_week + 1.day | |
end | |
it "should have a date with '12/25/2010'" do | |
@string_to_parse.gsub!(/{date_string}/, "12/25/2010") # mm/dd/yyyy | |
p = parser(@string_to_parse) | |
p.date.should_not be_nil | |
p.date.should == Date.parse("25/12/2010") | |
end | |
it "should not modify @string_to_params" do | |
@string_to_parse.gsub!(/{date_string}/, "12/25/2010") # mm/dd/yyyy | |
# debugger | |
str = @string_to_parse.clone | |
parser(@string_to_parse).date.should_not be_nil | |
str.should == @string_to_parse | |
end | |
end | |
describe "expected nexts" do | |
before(:each) do | |
@nexts = %w{monday tuesday wednesday thursday friday saturday sunday week month year} | |
@nexts_mixed_case = %w{mOnday TUesday Wednesday Thursday FRIDAY SATURDAY sunDAY} | |
@sentence = "one, two and three by next {next}" | |
end | |
it "should parse each next appropriately" do | |
@nexts.each do |next_one| | |
parser(@sentence.gsub(/{next}/, next_one)).date.should_not be_nil | |
end | |
end | |
it "should ignore case and parse appropriately" do | |
@nexts_mixed_case.each do |next_one| | |
parser(@sentence.gsub(/{next}/, next_one)).date.should_not be_nil | |
end | |
end | |
end | |
end | |
describe "with some random strings that should pass" do | |
it "bills apples bulbs #groceries" do | |
p = "" | |
lambda { | |
p = parser "bills apples bulbs #groceries" | |
}.should_not raise_error | |
p.tags.count.should == 1 | |
p.items.count.should == 1 | |
end | |
it "bills apples bulbs @groceries should have 1 item and 1 at" do | |
p = "" | |
lambda { | |
p = parser "bills apples bulbs @groceries" | |
}.should_not raise_error | |
p.ats.count.should == 1 | |
p.items.count.should == 1 | |
end | |
it "bills apples bulbs, groceries should have two items" do | |
p = "" | |
lambda { | |
p = parser "bills apples bulbs, groceries" | |
}.should_not raise_error | |
p.tags.count.should == 0 | |
p.ats.count.should == 0 | |
p.items.count.should == 2 | |
end | |
it "quick sand with mud pile should have one item" do | |
p = "" | |
lambda { | |
p = parser "quick sand with mud pile" | |
}.should_not raise_error | |
p.tags.count.should == 0 | |
p.ats.count.should == 0 | |
p.items.count.should == 1 | |
end | |
it "one, two and three #tag1 #tag2 should add three items with two tags a piece" do | |
p = parser "one, two and three #tag1 #tag2" | |
p.class.should eql Parser | |
end | |
end | |
describe "with invalid string" do | |
it "should have errors" do | |
lambda { | |
parser().errors.count.should == 1 | |
}.should_not raise_error | |
end | |
end | |
describe "sith singular flag" do | |
it "should strip off trailing spaces" do | |
Parser.new("one, two and three ", true).items[0].length.should == "one, two and three".length | |
end | |
it "should strip off trailing commas" do | |
Parser.new("one, two and three , ", true).items[0].length.should == "one, two and three".length | |
end | |
it "should strip off trailing periods" do | |
Parser.new("one, two and three . ", true).items[0].length.should == "one, two and three".length | |
end | |
it "should only create one item in items array" do | |
Parser.new("one, two and three ", true).items.count.should == 1 | |
end | |
end | |
end | |
describe "#_lists" do | |
before(:each) do | |
@parser = Parser.new "one, two, skip a few #tag1 #tag2 @user1 @user2" | |
end | |
it "should populate tag_list" do | |
@parser.ats_list.should == "user1,user2" | |
end | |
it "should populate ats_list" do | |
@parser.tags_list.should == "tag1,tag2" | |
end | |
end | |
describe "class methods" do | |
describe "#weekday" do | |
it "should reflect that tomorrow is tomorrow" do | |
Parser.weekday(Date.tomorrow.day_of_week).should == Date.tomorrow | |
end | |
end | |
describe "#day_in_past?" do | |
it "should pick next week if day is previous day" do | |
Parser.day_in_past?(Date.yesterday.day_of_week).should == true | |
end | |
it "should pick next week if day is same day" do | |
Parser.day_in_past?(Date.today.day_of_week).should == true | |
end | |
it "should pick this week if day is future day in week" do | |
Parser.day_in_past?(Date.tomorrow.day_of_week).should == false | |
end | |
end | |
describe "#day_of_week" do | |
it "should return a hash of integers equiveelnt to days before ind of ween and days after end of week" do | |
Parser.day_of_week( :monday ).should eql({:past => 6, :future => 1}) | |
Parser.day_of_week( "monday" ).should eql({:past => 6, :future => 1}) | |
Parser.day_of_week( "mOnday" ).should eql({:past => 6, :future => 1}) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment