spec/expressive_spec.rb in expressive-0.0.26 vs spec/expressive_spec.rb in expressive-0.0.27
- 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 datetime lookup round $round $days_ago $hours_ago $minutes_ago $append $id $head $lookup $reverse $tail $hash) }
+ 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 $concat $split $sms) }
end
describe "understands booleans" do
it { Expressive.run("true").should eql true }
it { Expressive.run("false").should eql false }
@@ -22,19 +22,35 @@
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" }
+
+ context "string manipulation" do
+ it {Expressive.run('($concat "hello" " world")').should eql "hello world" }
+ it {
+ @scope["scope_value"] = "world"
+ Expressive.run('($concat "hello " scope_value)', @scope).should eql "hello world"
+ }
+ it {Expressive.run('($split "a,b,c,d,e" ",")').should eql ['a', 'b', 'c', 'd', 'e'] }
+ end
+
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"}})}
+ describe "hash manipulation" do
+ context "hash definition" 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
+
+ context "hash value" do
+ it { Expressive.run('($hval "test" ($hash (test "value")))').should eql "value"}
+ end
end
it "understands variables" do
@scope["hello"] = "World"
Expressive.run("hello", @scope).should eql "World"
@@ -140,12 +156,89 @@
it do
@scope["depth"] = "1.6mm"
Expressive.run(@statement, @scope).should eql 0
end
end
- end
+ context "multiple if statements with set" do
+ before do
+ @scope["letter_template"] = ""
+ @statement = <<-EOH
+ (set letter_001_sent (if (= letter_template "001-OFFCHA-GENERAL") "Y", letter_001_sent))
+ (set letter_002_sent (if (= letter_template "002-OFFCHA-NO8PCTINT") "Y", letter_002_sent))
+ (set letter_003_sent (if (= letter_template "003-OFFCHA-DEBTPURCH") "Y", letter_003_sent))
+ (set letter_004_sent (if (= letter_template "004-SIGNEDACC-CLIENT") "Y", letter_004_sent))
+ (set letter_005_sent (if (= letter_template "005-SIGNEDACC-LENDER") "Y", letter_005_sent))
+EOH
+ end
+ it "does nothing when letter_template is nil" do
+ @scope["letter_template"] = be_nil
+ Expressive.run(@statement, @scope)
+
+ @scope["letter_001_sent"].should be_nil
+ @scope["letter_002_sent"].should be_nil
+ @scope["letter_003_sent"].should be_nil
+ @scope["letter_004_sent"].should be_nil
+ @scope["letter_005_sent"].should be_nil
+ end
+ it "does not affect existing values" do
+ @scope["letter_001_sent"] = "Y"
+ @scope["letter_template"] = "002-OFFCHA-NO8PCTINT"
+ Expressive.run(@statement, @scope)
+ @scope["letter_002_sent"].should == "Y"
+ @scope["letter_001_sent"].should == "Y"
+ @scope["letter_003_sent"].should be_nil
+ @scope["letter_004_sent"].should be_nil
+ @scope["letter_005_sent"].should be_nil
+ end
+ it "sets letter_001_sent to Y" do
+ @scope["letter_template"] = "001-OFFCHA-GENERAL"
+ Expressive.run(@statement, @scope)
+ @scope["letter_001_sent"].should == "Y"
+ @scope["letter_002_sent"].should be_nil
+ @scope["letter_003_sent"].should be_nil
+ @scope["letter_004_sent"].should be_nil
+ @scope["letter_005_sent"].should be_nil
+ end
+ it "sets letter_002_sent to Y" do
+ @scope["letter_template"] = "002-OFFCHA-NO8PCTINT"
+ Expressive.run(@statement, @scope)
+ @scope["letter_002_sent"].should == "Y"
+ @scope["letter_001_sent"].should be_nil
+ @scope["letter_003_sent"].should be_nil
+ @scope["letter_004_sent"].should be_nil
+ @scope["letter_005_sent"].should be_nil
+ end
+ it "sets letter_003_sent to Y" do
+ @scope["letter_template"] = "003-OFFCHA-DEBTPURCH"
+ Expressive.run(@statement, @scope)
+ @scope["letter_003_sent"].should == "Y"
+ @scope["letter_001_sent"].should be_nil
+ @scope["letter_002_sent"].should be_nil
+ @scope["letter_004_sent"].should be_nil
+ @scope["letter_005_sent"].should be_nil
+ end
+ it "sets letter_004_sent to Y" do
+ @scope["letter_template"] = "004-SIGNEDACC-CLIENT"
+ Expressive.run(@statement, @scope)
+ @scope["letter_004_sent"].should == "Y"
+ @scope["letter_001_sent"].should be_nil
+ @scope["letter_002_sent"].should be_nil
+ @scope["letter_003_sent"].should be_nil
+ @scope["letter_005_sent"].should be_nil
+ end
+ it "sets letter_005_sent to Y" do
+ @scope["letter_template"] = "005-SIGNEDACC-LENDER"
+ Expressive.run(@statement, @scope)
+ @scope["letter_005_sent"].should == "Y"
+ @scope["letter_001_sent"].should be_nil
+ @scope["letter_002_sent"].should be_nil
+ @scope["letter_003_sent"].should be_nil
+ @scope["letter_004_sent"].should be_nil
+ end
+ 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 }
@@ -164,10 +257,18 @@
@scope['vsi'] = 'oldvalue'
Expressive.run('(set vsi "vsi1234")', @scope).should eql "vsi1234"
@scope['vsi'].should eql 'vsi1234'
end
+ it "using single set commands with if" do
+ @scope['vsi'] = 'oldvalue'
+ Expressive.run('(set vsi (if (= 1 2) "vsi1234", vsi))', @scope)
+ @scope['vsi'].should eql 'oldvalue'
+ Expressive.run('(set vsi (if (= 1 1) "vsi1234", vsi))', @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
@@ -279,10 +380,51 @@
Expressive.run('(lookup related_count "matters" (1 2 3) 10)', @scope).should eql ['matters', 3, 10]
end
end
+ describe "understands sms statements" do
+ before(:each) do
+ ENV["clickatell_username"] = "user"
+ ENV["clickatell_password"] = "pass"
+ ENV["clickatell_api_id"] = "apiid"
+ @base_url = "http://api.clickatell.com/http/sendmsg?user=user&password=pass&api_id=apiid"
+ end
+ it "should call http with hardcoded text" do
+
+ end
+ it "should send sms based off values in case" do
+ @scope['mobile_phone_no'] = "447968115250"
+ request = stub_request(:get, @base_url + "&to=447968115250&text=this%20is%20a%20test").to_return(body: {'goodbye' => 'srsly'})
+ Expressive.run('($sms "447968115250" "this is a test")', @scope)
+ assert_requested(request)
+ @scope['goodbye'].should eql 'srsly'
+ end
+ it "should normalise regular phone numbers" do
+ @scope['mobile_phone_no'] = "07968115250"
+ request = stub_request(:get, @base_url + "&to=447968115250&text=this%20is%20a%20test").to_return(body: {'goodbye' => 'srsly'})
+ Expressive.run('($sms "447968115250" "this is a test")', @scope)
+ assert_requested(request)
+ @scope['goodbye'].should eql 'srsly'
+ end
+ it "should normalise international phone numbers" do
+ @scope['mobile_phone_no'] = "+447968115250"
+ request = stub_request(:get, @base_url + "&to=447968115250&text=this%20is%20a%20test").to_return(body: {'goodbye' => 'srsly'})
+ Expressive.run('($sms "447968115250" "this is a test")', @scope)
+ assert_requested(request)
+ @scope['goodbye'].should eql 'srsly'
+ end
+ it "should encode data" do
+ @scope['mobile_phone_no'] = "+447968115250"
+ request = stub_request(:get, @base_url + "&to=447968115250&text=This%20is%20you%27re%20text%20-%20it%20%26%20the%20message%20should%20be%20encoded").to_return(body: {'goodbye' => 'srsly'})
+ Expressive.run('($sms "447968115250" "This is you\'re text - it & the message should be encoded")', @scope)
+ assert_requested(request)
+ @scope['goodbye'].should eql 'srsly'
+ 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"
@@ -310,10 +452,18 @@
Expressive.run('(get "http://www.example.com" ohai)', @scope)
assert_requested(request)
@scope['goodbye'].should eql 'srsly'
end
+ it "should get with generated url" do
+ @scope['ohai'] = "world"
+ request = stub_request(:get, "www.example.com/?ohai=world").to_return(body: {'goodbye' => 'srsly'})
+ Expressive.run('(get ($concat "http://www.example.com/?ohai=" 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)
@@ -349,9 +499,59 @@
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
+
+ it "should take a hash of values to post" do
+ @scope['ohai'] = "world"
+ @scope["monty"] = "python"
+ request = stub_request(:post, "www.example.com").with(body: {'param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3'}).to_return(body: {'param4' => 'value4'})
+ Expressive.run('(post "http://www.example.com" ($hash (param1 "value1") (param2 "value2") (param3 "value3")))', @scope)
+ assert_requested(request)
+ @scope['param4'].should eql 'value4'
+ end
+
+ it "should be able to create a new hash of values to post" do
+ @scope['ohai'] = "world"
+ @scope["monty"] = "python"
+ request = stub_request(:post, "www.example.com").with(body: {'param1' => 'world', 'param2' => 'value2', 'param3' => 'python'}).to_return(body: {'param4' => 'value4'})
+ Expressive.run('(post "http://www.example.com" ($hash (param1 ohai) (param2 "value2") (param3 monty)))', @scope)
+ assert_requested(request)
+ @scope['param4'].should eql 'value4'
+ end
+
+ it "should be able to create a nested hash of values to post" do
+ @scope['ohai'] = "world"
+ @scope["monty"] = "python"
+ @scope["nested"] = "conference"
+ request = stub_request(:post, "www.example.com").with(body: {'param1' => 'world', 'param2' => {'level1' => {'level2' => 'conference'}}, 'param3' => 'python'}).to_return(body: {'param4' => 'value4'})
+ Expressive.run('(post "http://www.example.com" ($hash (param1 ohai) (param2 ($hash (level1 ($hash (level2 nested))))) (param3 monty)))', @scope)
+ assert_requested(request)
+ @scope['param4'].should eql 'value4'
+ end
+
+ it "should extract data from response of post" do
+ @scope['ohai'] = "world"
+ @scope["monty"] = "python"
+ request = stub_request(:post, "www.example.com").with(body: {'param1' => 'world', 'param2' => 'value2', 'param3' => 'python'}).to_return(body: {'param4' => 'value4'})
+ Expressive.run('(post "http://www.example.com" ($hash (param1 ohai) (param2 "value2") (param3 monty)))', @scope)
+ assert_requested(request)
+ @scope['param4'].should eql 'value4'
+ end
+
+ it "should update scope with data extracted from response and no other" do
+ @scope['ohai'] = "world"
+ @scope["monty"] = "python"
+
+ request = stub_request(:post, "www.example.com").with(body: {'param1' => 'world', 'param2' => 'value2', 'param3' => 'python'}).to_return(body: {'case_id' => '452'})
+ Expressive.run('(set related_id ($hval "case_id" (post "http://www.example.com" ($hash (param1 ohai) (param2 "value2") (param3 monty)) false)))', @scope)
+ assert_requested(request)
+ @scope.to_hash.keys.should_not include 'case_id'
+ puts @scope.to_hash
+ @scope['related_id'].should eql '452'
+ end
+
end
context "conditional post" do
it "should post to correct url" do
@scope['ohai'] = "world"