spec/expressive_spec.rb in expressive-0.0.25 vs spec/expressive_spec.rb in expressive-0.0.26

- old
+ new

@@ -4,11 +4,11 @@ before(:each) do @scope = Expressive::TopLevel.new end describe "all_symbols" do - it { Expressive.all_symbols.should =~ %w(+ - * / = set sum $sub get put post >= > < <= and or if date lookup round $round $days_ago $hours_ago $minutes_ago $append $id $head $lookup $reverse $tail) } + it { Expressive.all_symbols.should =~ %w(+ - * / = set sum $sub get put post >= > < <= and or if date datetime lookup round $round $days_ago $hours_ago $minutes_ago $append $id $head $lookup $reverse $tail $hash) } end describe "understands booleans" do it { Expressive.run("true").should eql true } it { Expressive.run("false").should eql false } @@ -24,10 +24,19 @@ it { Expressive.run('"hello world 2012"').should eql "hello world 2012" } it { Expressive.run('"hello world 2.5"').should eql "hello world 2.5" } it { Expressive.run('"hello world false"').should eql "hello world false" } end + describe "understands hash values" do + it { Expressive.run('($hash (test "value"))').should eql ({"test" => "value"})} + it { Expressive.run('($hash (test 5))').should eql ({"test" => 5})} + it { Expressive.run('($hash (test 5.3))').should eql ({"test" => 5.3})} + it { Expressive.run('($hash (test "value") (test2 64))').should eql ({"test" => "value", "test2" => 64})} + it { Expressive.run('($hash (test "value") (test2 (date)))').should eql ({"test" => "value", "test2" => Time.parse(Date.today.to_s).utc})} + it { Expressive.run('($hash (test "value") (test2 (date)) (key ($hash (within "a_hash"))))').should eql ({"test" => "value", "test2" => Time.parse(Date.today.to_s).utc, "key" => {"within" => "a_hash"}})} + end + it "understands variables" do @scope["hello"] = "World" Expressive.run("hello", @scope).should eql "World" end @@ -177,10 +186,24 @@ date = Date.new(2012, 11, 22).to_time.utc Expressive.run('(date(22/11/2012))').should eql date end end + describe "understands datetime parsing" do + it "will return the current date and time" do + Timecop.freeze(DateTime.now) do + now = DateTime.now + Expressive.run('(datetime)').should eql now + end + end + + it "will return a date and time parsed from text" do + date = DateTime.parse("22nd November 2012 13:04") + Expressive.run('(datetime(22/11/2012 13:04:00))').should eql date + end + end + describe "understands using lookup tables" do before(:each) do @user1 = mock(:user, id:1, login: "user1", display_name: "User 1") @user2 = mock(:user, id:2, login: "user2", display_name: "User 2") @scope.add_lookup_table("users", @user1.login => @user1, @user2.login => @user2) @@ -213,11 +236,16 @@ it "append to a list values list of values" do @scope['participating_teams'] = [0] Expressive.run('($append participating_teams (1 2 3))', @scope) @scope['participating_teams'].should == [0, 1, 2, 3] end + it "add a hash to a list of values" do + @scope['documents'] = [] + Expressive.run('($append documents ($hash (test "value") (another_test 53.2) (and_a_date (date))))', @scope) + @scope['documents'].should == [{"test" => "value", "another_test" => 53.2, "and_a_date" => Time.parse(Date.today.to_s).utc}] + end end - + describe "understands retrieving the id of an object" do it do @scope["an_object"] = mock(:an_object, id: 5) Expressive.run('($id an_object)', @scope).should == 5 end