summaryrefslogtreecommitdiffstats
path: root/legacy/test2.rb
blob: 0a43d58049735cd6728459f93b15949830e75c37 (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
require 'state'
require 'set'
require 'test/unit'

class TC_State < Test::Unit::TestCase
	include UpState

	Tony = State.new_type("tony", [Event.new("tonyup", {:tony => "tony"})], [], [:tony])
	Mike = State.new_type("mike", [], [Dependency.new(Tony)])
	ChainTop = State.new_type("chaintop", [], [Dependency.new(Tony, {:tony => "tony"}, {:tony => :bob}),
											   Dependency.new(Mike)])
	#this state should never appear
	Reversed = State.new_type("reversed", [Dependency.new(Tony)], [Event.new("tonyup", {:tony => "tony"})])

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

	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(
			'TonyState{} (down)'
		)
	end

	def teardown
		State.release(:user)
		State.release(:system)
	end

	def test_reversed
		#this should never ever come up
		Reversed.hold(:user)
		assert_only_states(
			'TonyState{} (down)'
		)
	end

	def test_incomplete_hold
		begin
			Tony.hold(:user)
			assert(false)
		end
		rescue AmbiguousRequest => e
			assert(true)
	end

	def test_bogus_hold
		begin
			Tony.hold(:cookies)
			assert(false)
		end
		rescue TypeError => e
			assert(true)
	end

	def test_bogus_release
		begin
			State.process_event(Event.new("tonyup", {:tony => "tony"}))
			Tony.release(:cookies)
			assert(false)
		end
		rescue TypeError => e
			assert(true)
	end

	def test_chained_dependencies
		State.process_event(Event.new("tonyup", {:tony => "tony"}))
		Mike.hold(:user)
		ChainTop.hold(:system, {:bob => "tony"})
		assert_only_states(
			'TonyState{} (down)',
			'ChaintopState{:bob=>"tony"} (up)',
			'MikeState{} (up)',
			'TonyState{:tony=>"tony"} (up)'
		)
	end

	def test_chained_releases
		test_chained_dependencies
		ChainTop.release(:system)
		Tony.release(:system)
	end
end