require "spec_helper" describe "Expressive" do before(:each) do @scope = Expressive::TopLevel.new end describe "all_symbols" do it { Expressive.all_symbols.should =~ %w(+ - * / = set sum get put post >= > < <= and or if date lookup round $days_ago $hours_ago $minutes_ago $append $id) } end describe "understands booleans" do it { Expressive.run("true").should eql true } it { Expressive.run("false").should eql false } end describe "understands numbers" do it { Expressive.run("5").should eql 5 } it { Expressive.run("5.5").should eql 5.5 } end describe "understands string literals" do it { Expressive.run('"hello world "').should eql "hello world " } 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 it "understands variables" do @scope["hello"] = "World" Expressive.run("hello", @scope).should eql "World" end describe "under stands arithmetic" do it { Expressive.run("(+ 4 2)").should eql 6.0 } it { Expressive.run("(- 4 2)").should eql 2.0 } it { Expressive.run("(* 4 2)").should eql 8.0 } it { Expressive.run("(/ 4 2)").should eql 2.0 } it { Expressive.run("(sum 1 2 3)").should eql 6.0 } it { Expressive.run("(sum )").should eql 0 } it { Expressive.run("(- (sum 1 5 7) 3)").should eql 10.0} it { Expressive.run("(round 0.12345 2)").should eql 0.12} end describe "understands comparisson" do it { Expressive.run("(= 4 2)").should eql false } it { Expressive.run("(= 4 4)").should eql true } it { Expressive.run("(> 4 2)").should eql true } it { Expressive.run("(> 2 4)").should eql false } it { Expressive.run("(< 4 2)").should eql false } it { Expressive.run("(< 2 4)").should eql true } it { Expressive.run("(>= 4 4)").should eql true } it { Expressive.run("(<= 4 4)").should eql true } end describe "understands compound statements" do it { Expressive.run("(= (+ 4 2) 6)").should eql true } it { Expressive.run("(if (and (< 3 9) (> 2 1)), true, false)").should eql true } it { Expressive.run("(if (and (< 10 9) (> 2 1)), true, false)").should eql false } end describe "understands conditional statements" do it { Expressive.run('(if (> 5 4) "greater" "not so greater")').should eql "greater" } context "it understands the need for commas (if you like that kind of thing" do it { Expressive.run('(if (< 5 4), "greater", "not so greater")').should eql "not so greater" } end context "nil values" do it { Expressive.run('(if (> nil_value 4) "greater" "not so greater")', @scope).should eql "not so greater" } it { Expressive.run('(if (> 4 nil_value) "greater" "not so greater")', @scope).should eql "greater" } it { Expressive.run('(if (> 4 nil_value) "greater" "not so greater")', @scope).should eql "greater" } it { Expressive.run('(if (<= nil_value 4) "greater" "not so greater")', @scope).should eql "greater" } it { Expressive.run('(if (>= 4 nil_value) "greater" "not so greater")', @scope).should eql "greater" } end end describe "understands logical statements" do it { Expressive.run('(and true true)').should eql true } it { Expressive.run('(and true false)').should eql false } it { Expressive.run('(or true true)').should eql true } it { Expressive.run('(or true false)').should eql true } it { Expressive.run('(or false false)').should eql false } context "nil values" do it { Expressive.run('(and true nil_value)', @scope).should eql false } it { Expressive.run('(and nil_value true)', @scope).should eql false } it { Expressive.run('(or nil_value true)', @scope).should eql true } it { Expressive.run('(or nil_value false)', @scope).should eql false } end end describe "understands the modification of scope" do it "using single set commands" do @scope['vsi'] = 'oldvalue' Expressive.run('(set vsi "vsi1234")', @scope).should eql "vsi1234" @scope['vsi'].should eql 'vsi1234' end it "using multiple set commands" do Expressive.run('(and (set vsi "vsi1234") (set vso "vso1234") (set vsbool true))', @scope).should eql true @scope['vsi'].should eql 'vsi1234' @scope['vso'].should eql 'vso1234' @scope['vsbool'].should be_true end end describe "understands date parsing" do it "will return the current date" do now = Date.today.to_time.utc Timecop.freeze(now) do Expressive.run('(date)').should eql now end end it "will return a date parsed from text" do date = Date.new(2012, 11, 22).to_time.utc Expressive.run('(date(22/11/2012))').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) @scope.add_lookup_table("users", "x_current" => @user2) end it "will return a value from a lookup table" do Expressive.run('(lookup users "x_current")', @scope).should eql @user2 Expressive.run('(lookup users "user1")', @scope).should eql @user1 end it "will set multiple values based on results of a lookup" do @owned_by_ext_value = Expressive::ExtendedValue.new(:x_owned_by, @scope) @owned_by_ext_value.setter = Proc.new do |value, scope| scope["owned_by_id"] = value.id scope["owned_by_type"] = value.class.to_s end Expressive.run('(set x_owned_by (lookup users "x_current"))', @scope) @scope["owned_by_id"].should eql @user2.id @scope["owned_by_type"].should eql "RSpec::Mocks::Mock" end end describe "understands adding values to lists" do it "add a single value to a list of values" do Expressive.run('($append participating_teams 1)', @scope) @scope['participating_teams'].should == [1] end 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 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 end describe "understands time ranges" do before do @now = Time.now Time.stub!(:now).and_return(@now) end it { Expressive.run("($days_ago 28)").should eql (@now - 28 * 86400) } it { Expressive.run("($hours_ago 7)").should eql (@now - 7 * 3600) } it { Expressive.run("($minutes_ago 12)").should eql (@now - 12 * 60) } end describe "understands using lookup functions" do it "should perform the lookup function (add account and reverse login)" do @scope.add_lookup_function("account_reverse_login", account: "ea") do |options, login| "#{options[:account]}-#{login.reverse}" end Expressive.run('(lookup account_reverse_login "ijonas")', @scope).should eql "ea-sanoji" end it "should support multiple parameters to a lookup function" do @scope.add_lookup_function("related_count") do |options, related_type, query_specs, last| [related_type, query_specs.length, last] end Expressive.run('(lookup related_count "matters" (1 2 3) 10)', @scope).should eql ['matters', 3, 10] end end describe "understands web-hook statements" do context "put" do it_should_behave_like "a webhook", :put it "should put the cope parameters if given" do @scope['ohai'] = "world" request = stub_request(:put, "www.example.com").with(body: {'ohai' => 'world'}).to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(put "http://www.example.com" ohai)', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end it "should put all parameters if wild card is given" do @scope['ohai'] = "world" @scope['monty'] = "python" request = stub_request(:put, "www.example.com").with(body: {'monty' => 'python', 'ohai' => 'world'}).to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(put "http://www.example.com" "*")', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end end context "get" do it_should_behave_like "a webhook", :get it "should get with parameters if given" do @scope['ohai'] = "world" request = stub_request(:get, "www.example.com/?ohai=world").to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(get "http://www.example.com" ohai)', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end it "should get with all parameters if wild card is given" do @scope['ohai'] = "world" @scope["monty"] = "python" request = stub_request(:get, "www.example.com/?ohai=world&monty=python").to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(get "http://www.example.com" "*")', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end it "should get with headers" do @scope['ohai'] = "world" @scope["monty"] = "python" request = stub_request(:get, "www.example.com/?ohai=world&monty=python") .with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby', "AUTH-TOKEN" => "123456"}) .to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(get "http://www.example.com" "*" headers "AUTH-TOKEN=123456")', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end end context "post" do it_should_behave_like "a webhook", :post it "should post the cope parameters if given" do @scope['ohai'] = "world" request = stub_request(:post, "www.example.com").with(body: {'ohai' => 'world'}).to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(post "http://www.example.com" ohai)', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end it "should get with all parameters if wild card is given" do @scope['ohai'] = "world" @scope["monty"] = "python" request = stub_request(:post, "www.example.com").with(body: {'monty' => 'python', 'ohai' => 'world'}).to_return(body: {'goodbye' => 'srsly'}) Expressive.run('(post "http://www.example.com" "*")', @scope) assert_requested(request) @scope['goodbye'].should eql 'srsly' end end end end