spec/expressive_spec.rb in expressive-0.0.3 vs spec/expressive_spec.rb in expressive-0.0.5

- old
+ new

@@ -1,10 +1,11 @@ + require "spec_helper" describe "Expressive" do before(:each) do - @scope = TopLevel.new + @scope = Expressive::TopLevel.new end describe "all_symbols" do it { Expressive.all_symbols.should =~ %w(+ - * / = set sum post >= > < <= and or if) } end @@ -86,107 +87,66 @@ it { Expressive.run('(or nil_value false)', @scope).should eql false } end end describe "understands web-hook statements" do - context "with a valid response" do - it "should post to the given uri" do - request = stub_request(:post, "www.example.com") - Expressive.run('(post "http://www.example.com")') + 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 post the cope parameters if given" do + it "should put all parameters if wild card is given" do @scope['ohai'] = "world" - request = stub_request(:post, "www.example.com").with(body: {'ohai' => 'world'}) - Expressive.run('(post "http://www.example.com" ohai)', @scope) + @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 - it "should update the scope based on the response" do + context "get" do + it_should_behave_like "a webhook", :get + it "should get with 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) + 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 - end - context "when the response is invalid" do - context "sets error custom message" do - it "should add an entry in the errors element of the scope" do - message = 'Something went Pete Tong' - exception = RestClient::Exception.new - exception.message = message - stub_request(:post, 'www.example.com').to_raise(exception) - Expressive.run('(post "http://www.example.com")', @scope) - @scope['_errors'].should eql message - 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 + end - context "bad uri" do - it "should add an entry in the errors element of the scope" do - stub_request(:post, "badexample.com").to_raise(RestClient::ResourceNotFound.new) - Expressive.run('(post "badexample.com")', @scope) - @scope['_errors'].should eql 'Resource Not Found' - 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 - context "timeout" do - it "should add an entry in the errors element of the scope" do - stub_request(:post, 'www.example.com').to_timeout - Expressive.run('(post "http://www.example.com")', @scope) - @scope['_errors'].should eql 'Request Timeout' - 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 - - context "looking up an id based on a value" do - it "should use the lookup method, find an id based on value" do - #Expressive.run("lookup_case_state 'not_started'", @scope) - #@payload[:_].should eql 1 - end - end - - context "when setting simple properties" do - it "should set order_no to 1234" do - Expressive.run("(set order_no 1234)", @scope).should eql 1234 - @scope["order_no"].should eql 1234 - end - it "should reuse set properties" do - Expressive.run("(set order_no 1234)(+ order_no 11)", @scope).should eql 1245.0 - end - it "should sum scoped arrays" do - @scope["sub_totals"] = [100, 200, 300] - Expressive.run("(sum sub_totals)", @scope).should eql 600.0 - end - end - - context "when setting complex properties" do - context "e.g. current_state" do - before(:each) { Expressive.run("(set current_state (lookup_case_state, 'not started'))", @scope) } - it "should set current_state to 'started'" - it "should set current_state_id to 2" - end - context "e.g. owned_by" do - it "should set owned_by_id to 1" - it "should set owned_by_type to 'CaseBlocks::User'" - end - end - - context "when setting multiple properties" do - # it { - - # Expressive.apply_to @scope do |scope| - # apply "set order_no 1234" - # apply "set current_state (lookup case_state 'not started')" - # apply "set owned_by (lookup_user_by_email 'ijonas@ijonas.com')" - # end - # @scope["order_no"].should eql "1234" - # } - end - - - context "register external lookup functions" do - end end