spec/expressive_spec.rb in expressive-0.0.30 vs spec/expressive_spec.rb in expressive-0.0.31
- 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 $concat $split $sms $hval $index) }
+ 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 $hval $index $random $join) }
end
describe "understands booleans" do
it { Expressive.run("true").should eql true }
it { Expressive.run("false").should eql false }
@@ -25,10 +25,11 @@
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 {Expressive.run('($join "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'] }
@@ -249,19 +250,60 @@
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 }
+ it { Expressive.run('(or false (or false false))').should eql false }
+ it { Expressive.run('(or false (or false true))').should eql true }
+ it { Expressive.run('(or false (or true false))').should eql true }
+ it { Expressive.run('(or true (or false false))').should eql true }
+ it "handles compound ors" do
+ expressive = "(if (or (= answer_2 \"Unemployed\") (or (= answer_2 \"Self-employed\") (or (= answer_2 \"Retired\") (= answer_2 \"Student\")))) 2 \"\")"
+
+ @scope["answer_2"] = "asdf"
+ result = Expressive.run(expressive, @scope)
+ result.should == ""
+
+ @scope["answer_2"] = "Unemployed"
+ result = Expressive.run(expressive, @scope)
+ result.should == 2
+
+ @scope["answer_2"] = "Self-employed"
+ result = Expressive.run(expressive, @scope)
+ result.should == 2
+
+ @scope["answer_2"] = "Retired"
+ result = Expressive.run(expressive, @scope)
+ result.should == 2
+
+ @scope["answer_2"] = "Student"
+ result = Expressive.run(expressive, @scope)
+ result.should == 2
+ end
+
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 "generates random values" do
+ it "generates random literal values" do
+ [1, 2, 3, 4].include?(Expressive.run('($random 1 2 3 4)', @scope)).should be_true
+ end
+ it "generates random values from scope" do
+ @scope["field1"] = "value1"
+ @scope["field2"] = "value2"
+ @scope["field3"] = "value3"
+ @scope["field4"] = "value4"
+ %W(value1 value2 value3 value4).include?(Expressive.run('($random field1 field2 field3 field4)', @scope)).should be_true
+ end
+ end
+
describe "understands the modification of scope" do
it "sets scope based off rounded equality calculation" do
@scope['total_payment_amount'] = 2.32
@scope['refund_amount'] = 2.32
@@ -277,11 +319,46 @@
@scope['payment_reconciled'] = "asdf"
Expressive.run('(if (= ($round total_payment_amount 2) ($round refund_amount 2)) true false)', @scope).should eql false
Expressive.run('(if (= ($round total_payment_amount 2) ($round refund_amount 2)) true, false)', @scope).should eql false
Expressive.run('(= (- ($round total_payment_amount 2) ($round refund_amount 2)) 0)', @scope).should eql false
Expressive.run('(set payment_reconciled (if (= ($round total_payment_amount 2) ($round refund_amount 2)) true false))', @scope)
- puts @scope['payment_reconciled']
@scope['payment_reconciled'].should eql false
+
+ end
+
+ describe "set field only if value of other fields equate" do
+ before(:each) do
+ @scope.add_lookup_function("state_by_name") do |options, name, additional_query_specs = []|
+ if name == "Reconciled"
+ 2
+ else
+ 3
+ end
+ end
+
+ @scope["number_of_related_matters"] = "asdf"
+ @scope["x_current_state"] = 3
+ end
+
+ it "when value matches" do
+ @scope.add_lookup_function("related_matters_count") do |options, additional_query_specs = []|
+ 1
+ end
+ result = Expressive.run("(set x_current_state (if (= (lookup related_matters_count) 1) (lookup state_by_name 'Reconciled') x_current_state)) (lookup related_matters_count)", @scope)
+
+ result.should eql 1
+ @scope["x_current_state"].should eql 2
+ end
+
+ it "when value does not match" do
+ @scope.add_lookup_function("related_matters_count") do |options, additional_query_specs = []|
+ 0
+ end
+ result = Expressive.run("(set x_current_state (if (= (lookup related_matters_count) 1) (lookup state_by_name 'Reconciled') x_current_state)) (lookup related_matters_count)", @scope)
+
+ result.should eql 0
+ @scope["x_current_state"].should eql 3
+ end
end
it "using single set commands" do
@scope['vsi'] = 'oldvalue'