summaryrefslogtreecommitdiffstats
path: root/test.rb
blob: 87cb5bfaf4964ca28300b081fae16672d40cc1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'state'
require 'set'
require 'test/unit'


def put_states
	foo = State.send(:class_variable_get, :@@states).map{ |x| x.to_s_color }
	puts foo.sort.join(", ")
end

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.release(:user)
		State.release(:system)
	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_hold_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

	def test_release_noop
		test_epsilon
		Bar.release(:user)
		assert_only_states(
			'BazState{:tom=>"abc"} (up)',
			'FooState{} (down)',
			'FooState{:bob=>"abc"} (up)',
			"BarState{} (up)",
			"BamState{} (up)",
			"BangState{} (down)"
		)
	end

	def test_drop_cleanup
		test_release_noop
		Bar.get_all.each{ |x| x.drop }
		assert_only_states(
			'FooState{} (down)',
			'FooState{:bob=>"abc"} (up)',
			"BarState{} (down)"
		)
	end
end