From 8b070c38e555df5c98e7d6648672dbe5806c7085 Mon Sep 17 00:00:00 2001 From: Casey Dahlin Date: Fri, 10 Oct 2008 14:27:31 -0400 Subject: Rewrite test.rb in Test::Unit Now its actually a good test suite --- test.rb | 138 ++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 43 deletions(-) (limited to 'test.rb') 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 -- cgit