Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Categorise tests by function.

  • Loading branch information...
commit eb2a7d40c379c0079d86c4e9a01f1ff933e89d7a 1 parent d80d1d2
@threedaymonk threedaymonk authored
Showing with 154 additions and 143 deletions.
  1. +151 −0 test/step_test.rb
  2. +3 −143 test/transform_test.rb
View
151 test/step_test.rb
@@ -0,0 +1,151 @@
+require "common"
+require "sibyl/parser"
+require "sibyl/ruby_transform"
+require "sibyl/errors"
+require "ostruct"
+
+describe "Step" do
+ def ruby(source)
+ parser = Sibyl::Parser.new
+ transform = Sibyl::RubyTransform.new
+ transform.apply(parser.parse(source)).first
+ end
+
+ it "should set a variable" do
+ step = ruby(%{
+ step number a
+ set foo_bar { 1 + 2 }
+ go -> b
+ })
+
+ context = OpenStruct.new
+ step.compute(0, context)
+ assert_equal 3, context.foo_bar
+ end
+
+ it "should list valid inputs for a multiple choice step" do
+ step = ruby(%{
+ step multiple a
+ option yes -> b
+ option no -> c
+ })
+
+ assert_equal ["yes", "no"], step.inputs
+ assert_equal "a", step.name
+ end
+
+ it "should list exits for a multiple choice step" do
+ step = ruby(%{
+ step multiple a
+ option yes -> b
+ option no -> c
+ })
+
+ assert_equal ["b", "c"], step.exits
+ end
+
+ it "should list exits for a multiple choice step with logic" do
+ step = ruby(%{
+ step multiple a
+ option yes ->
+ if { logic } -> b
+ otherwise -> c
+ option no -> d
+ })
+
+ assert_equal ["b", "c", "d"], step.exits
+ end
+
+ it "should list exits for a direct transition step" do
+ step = ruby(%{
+ step salary a
+ go -> b
+ })
+
+ assert_equal ["b"], step.exits
+ end
+
+ it "should list exits for a direct transition step with logic" do
+ step = ruby(%{
+ step salary a
+ go ->
+ if { logic } -> b
+ otherwise -> c
+ })
+
+ assert_equal ["b", "c"], step.exits
+ end
+
+ it "should compute exit for a direct transition step" do
+ step = ruby(%{
+ step number a
+ go -> b
+ })
+
+ assert_equal "b", step.compute(2, OpenStruct.new)
+ end
+
+ it "should compute exit for a direct transition step with logic" do
+ step = ruby(%{
+ step number a
+ go ->
+ if { input > 1 } -> b
+ otherwise -> c
+ })
+
+ assert_equal "b", step.compute(2, OpenStruct.new)
+ assert_equal "c", step.compute(1, OpenStruct.new)
+ end
+
+ it "should compute exit for a multiple choice step" do
+ step = ruby(%{
+ step number a
+ option foo -> b
+ option bar -> c
+ })
+
+ assert_equal "b", step.compute("foo", OpenStruct.new)
+ assert_equal "c", step.compute("bar", OpenStruct.new)
+ end
+
+ it "should compute exit for a multiple choice step with logic" do
+ step = ruby(%{
+ step number a
+ option foo ->
+ if { 2 > 1 } -> b
+ otherwise -> c
+ option bar ->
+ if { 2 < 1 } -> d
+ otherwise -> e
+ })
+
+ assert_equal "b", step.compute("foo", OpenStruct.new)
+ assert_equal "e", step.compute("bar", OpenStruct.new)
+ end
+
+ it "should raise an exception if a reject block fails" do
+ step = ruby(%{
+ step number a
+ reject { input > 1 }
+ go -> b
+ })
+
+ assert_raises Sibyl::ValidationError do
+ step.compute(2, OpenStruct.new)
+ end
+ assert_equal "b", step.compute(1, OpenStruct.new)
+ end
+
+ it "should raise an exception if an invalid option is supplied" do
+ step = ruby(%{
+ step number a
+ option foo -> b
+ option bar -> c
+ })
+
+ assert_raises Sibyl::ValidationError do
+ step.compute "baz", OpenStruct.new
+ end
+ end
+end
+
View
146 test/transform_test.rb
@@ -1,8 +1,6 @@
require "common"
require "sibyl/parser"
require "sibyl/ruby_transform"
-require "sibyl/errors"
-require "ostruct"
describe "Ruby Transform" do
def ruby(source)
@@ -12,149 +10,11 @@ def ruby(source)
end
it "should translate a metadata line" do
- actual = ruby(%{
+ md = ruby(%{
metadata need 1660
})
- assert_equal "need", actual.key
- assert_equal 1660, actual.value
+ assert_equal "need", md.key
+ assert_equal 1660, md.value
end
-
- it "should translate a set block" do
- step = ruby(%{
- step number a
- set foo_bar { 1 + 2 }
- go -> b
- })
-
- context = OpenStruct.new
- step.compute(0, context)
- assert_equal 3, context.foo_bar
- end
-
- it "should list valid inputs for a multiple choice step" do
- step = ruby(%{
- step multiple a
- option yes -> b
- option no -> c
- })
-
- assert_equal ["yes", "no"], step.inputs
- assert_equal "a", step.name
- end
-
- it "should list exits for a multiple choice step" do
- step = ruby(%{
- step multiple a
- option yes -> b
- option no -> c
- })
-
- assert_equal ["b", "c"], step.exits
- end
-
- it "should list exits for a multiple choice step with logic" do
- step = ruby(%{
- step multiple a
- option yes ->
- if { logic } -> b
- otherwise -> c
- option no -> d
- })
-
- assert_equal ["b", "c", "d"], step.exits
- end
-
- it "should list exits for a direct transition step" do
- step = ruby(%{
- step salary a
- go -> b
- })
-
- assert_equal ["b"], step.exits
- end
-
- it "should list exits for a direct transition step with logic" do
- step = ruby(%{
- step salary a
- go ->
- if { logic } -> b
- otherwise -> c
- })
-
- assert_equal ["b", "c"], step.exits
- end
-
- it "should compute exit for a direct transition step" do
- step = ruby(%{
- step number a
- go -> b
- })
-
- assert_equal "b", step.compute(2, OpenStruct.new)
- end
-
- it "should compute exit for a direct transition step with logic" do
- step = ruby(%{
- step number a
- go ->
- if { input > 1 } -> b
- otherwise -> c
- })
-
- assert_equal "b", step.compute(2, OpenStruct.new)
- assert_equal "c", step.compute(1, OpenStruct.new)
- end
-
- it "should compute exit for a multiple choice step" do
- step = ruby(%{
- step number a
- option foo -> b
- option bar -> c
- })
-
- assert_equal "b", step.compute("foo", OpenStruct.new)
- assert_equal "c", step.compute("bar", OpenStruct.new)
- end
-
- it "should compute exit for a multiple choice step with logic" do
- step = ruby(%{
- step number a
- option foo ->
- if { 2 > 1 } -> b
- otherwise -> c
- option bar ->
- if { 2 < 1 } -> d
- otherwise -> e
- })
-
- assert_equal "b", step.compute("foo", OpenStruct.new)
- assert_equal "e", step.compute("bar", OpenStruct.new)
- end
-
- it "should raise an exception if a reject block fails" do
- step = ruby(%{
- step number a
- reject { input > 1 }
- go -> b
- })
-
- assert_raises Sibyl::ValidationError do
- step.compute(2, OpenStruct.new)
- end
- assert_equal "b", step.compute(1, OpenStruct.new)
- end
-
- it "should raise an exception if an invalid option is supplied" do
- step = ruby(%{
- step number a
- option foo -> b
- option bar -> c
- })
-
- assert_raises Sibyl::ValidationError do
- step.compute "baz", OpenStruct.new
- end
- end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.