summaryrefslogtreecommitdiffstats
path: root/test.rb
diff options
context:
space:
mode:
authorCasey Dahlin <cdahlin@redhat.com>2008-10-10 14:27:31 -0400
committerCasey Dahlin <cdahlin@redhat.com>2008-10-10 14:27:31 -0400
commit8b070c38e555df5c98e7d6648672dbe5806c7085 (patch)
tree37959a14e092912457f427c3e8701fda5d4ff3f5 /test.rb
parent45698f129c3b222d09e560a243e0d5188bf02243 (diff)
downloadupstate-8b070c38e555df5c98e7d6648672dbe5806c7085.tar.gz
upstate-8b070c38e555df5c98e7d6648672dbe5806c7085.tar.xz
upstate-8b070c38e555df5c98e7d6648672dbe5806c7085.zip
Rewrite test.rb in Test::Unit
Now its actually a good test suite
Diffstat (limited to 'test.rb')
-rw-r--r--test.rb138
1 files changed, 95 insertions, 43 deletions
diff --git a/test.rb b/test.rb
index b43299b..2baa3b0 100644
--- a/test.rb
+++ b/test.rb
@@ -1,53 +1,105 @@
require 'state'
require 'set'
+require 'test/unit'
-include UpState
def put_states
foo = State.send(:class_variable_get, :@@states).map{ |x| x.to_s_color }
puts foo.sort.join(", ")
end
-foo = State.new_type("foo", [Event.new("gimmefoo", {:bob => /^...$/})], [], [:bob])
-bar = State.new_type("bar", [], [Dependency.new(foo)])
-baz = State.new_type("baz", [], [Dependency.new(foo, {:bob => "abc"}, {:bob => :tom}), Dependency.new(bar)])
-bam = State.new_type("bam", [Event::Epsilon], [Dependency.new(baz)])
-bang = State.new_type("bang", [], [Dependency.new(bam)])
-bao = State.new_type("bao", [], [Dependency.new(bam), Dependency.new(bang)])
-
-begin
- put_states
- State.process_event(Event.new("gimmefoo", {:bob => "1234"}))
- put_states
- State.process_event(Event.new("gimmefoo", {:bob => "123"}))
- myfoo = foo.get_all.select{ |x| x.params[:bob] == "123" }[0]
- put_states
- bar.hold(:user)
- put_states
- baz.hold(:user)
- put_states
- bang.hold(:user)
- put_states
- foo.release(:user)
- put_states
- myfoo.drop
- put_states
- State.process_event(Event.new("gimmefoo", {:bob => "123"}))
- put_states
- State.process_event(Event.new("gimmefoo", {:bob => "abc"}))
- myfoo = foo.get_all.select{ |x| x.params[:bob] == "abc" }[0]
- put_states
- bar.hold(:user)
- put_states
- baz.hold(:user)
- put_states
- bang.hold(:user)
- put_states
- foo.release(:user)
- put_states
- myfoo.drop
- put_states
-rescue
- put_states
- raise
+class TC_State < Test::Unit::TestCase
+ include UpState
+
+ Foo = State.new_type("foo", [Event.new("gimmefoo", {:bob => /^...$/})], [], [:bob])
+ Bar = State.new_type("bar", [], [Dependency.new(Foo)])
+ Baz = State.new_type("baz", [], [Dependency.new(Foo, {:bob => "abc"}, {:bob => :tom}), Dependency.new(Bar)])
+ Bam = State.new_type("bam", [Event::Epsilon], [Dependency.new(Baz)])
+ Bang = State.new_type("bang", [], [Dependency.new(Bam)])
+
+ def assert_have_states(*s)
+ assert_equal(s.sort, State.get_all.map{ |x| x.to_s }.sort)
+ end
+
+ def assert_only_states(*s)
+ assert_have_states *s
+ assert_equal(State.get_all.size, s.size)
+ end
+
+ def setup
+ assert_only_states "FooState{} (down)"
+ end
+
+ def teardown
+ State.get_all.clone.each{ |x| x.becomes_defunct }
+ end
+
+ def test_ignored_event
+ State.process_event(Event.new("gimmefoo", {:bob => "1234"}))
+ assert_only_states "FooState{} (down)"
+ end
+
+ def test_responded_event
+ State.process_event(Event.new("gimmefoo", {:bob => "123"}))
+ assert_only_states('FooState{} (down)', 'FooState{:bob=>"123"} (up)', "BarState{} (down)")
+ end
+
+ def test_hold_with_dependent
+ test_responded_event
+ Bar.get_all.first.hold(:user)
+ assert_only_states('FooState{} (down)', 'FooState{:bob=>"123"} (up)', "BarState{} (up)")
+ end
+
+ def test_hol_non_existant_state
+ Bar.hold(:user)
+ assert_only_states "FooState{} (down)"
+ end
+
+ def test_pattern_matched_param_dep
+ State.process_event(Event.new("gimmefoo", {:bob => "abc"}))
+ Bar.hold(:user)
+ assert_only_states(
+ 'BazState{:tom=>"abc"} (down)',
+ 'FooState{} (down)',
+ 'FooState{:bob=>"abc"} (up)',
+ "BarState{} (up)"
+ )
+ end
+
+ def test_duplicitous_state_by_deps
+ test_pattern_matched_param_dep
+ State.process_event(Event.new("gimmefoo", {:bob => "123"}))
+ assert_only_states(
+ 'BazState{:tom=>"abc"} (down)',
+ 'FooState{} (down)',
+ 'FooState{:bob=>"abc"} (up)',
+ 'FooState{:bob=>"123"} (up)',
+ "BarState{} (up)",
+ "BarState{} (down)"
+ )
+ end
+
+ def test_identity_uniqueness
+ test_pattern_matched_param_dep
+ State.process_event(Event.new("gimmefoo", {:bob => "abc"}))
+ assert_only_states(
+ 'BazState{:tom=>"abc"} (down)',
+ 'FooState{} (down)',
+ 'FooState{:bob=>"abc"} (up)',
+ "BarState{} (up)"
+ )
+ end
+
+ def test_epsilon
+ test_pattern_matched_param_dep
+ Baz.hold(:user)
+ assert_only_states(
+ 'BazState{:tom=>"abc"} (up)',
+ 'FooState{} (down)',
+ 'FooState{:bob=>"abc"} (up)',
+ "BarState{} (up)",
+ "BamState{} (up)",
+ "BangState{} (down)"
+ )
+ end
end