Move rb_intern calls to extension init
[god.git] / test / test_watch.rb
blob49ef1f616a4a6c1231cdece1ceee82ca270cdd2c
1 require File.dirname(__FILE__) + '/helper'
3 class TestWatch < Test::Unit::TestCase
4   def setup
5     @watch = Watch.new(nil)
6   end
7   
8   def test_should_have_empty_start_conditions
9     assert_equal [], @watch.conditions[:start]
10   end
11   
12   def test_should_have_empty_restart_conditions
13     assert_equal [], @watch.conditions[:restart]
14   end
15   
16   def test_should_have_standard_attributes
17     assert_nothing_raised do
18       @watch.name = 'foo'
19       @watch.start = 'start'
20       @watch.stop = 'stop'
21       @watch.restart = 'restart'
22       @watch.interval = 30
23       @watch.grace = 5
24     end
25   end
26   
27   # _if methods
28   
29   def test_start_if_should_modify_action_within_scope
30     assert_equal nil, @watch.instance_variable_get(:@action)
31     @watch.start_if do |w|
32       assert_equal :start, @watch.instance_variable_get(:@action)
33     end
34     assert_equal nil, @watch.instance_variable_get(:@action)
35   end
36   
37   def test_restart_if_should_modify_action_within_scope
38     assert_equal nil, @watch.instance_variable_get(:@action)
39     @watch.restart_if do |w|
40       assert_equal :restart, @watch.instance_variable_get(:@action)
41     end
42     assert_equal nil, @watch.instance_variable_get(:@action)
43   end
44   
45   # condition
46   
47   def test_start_condition_should_record_condition_in_correct_list
48     cond = nil
49     @watch.interval = 0
50     @watch.start_if do |w|
51       w.condition(:fake_poll_condition) { |c| cond = c }
52     end
53     assert_equal 1, @watch.conditions[:start].size
54     assert_equal cond, @watch.conditions[:start].first
55   end
56   
57   def test_restart_condition_should_record_condition_in_correct_list
58     cond = nil
59     @watch.interval = 0
60     @watch.restart_if do |w|
61       w.condition(:fake_poll_condition) { |c| cond = c }
62     end
63     assert_equal 1, @watch.conditions[:restart].size
64     assert_equal cond, @watch.conditions[:restart].first
65   end
66   
67   def test_condition_called_from_outside_if_block_should_raise
68     assert_raise AbortCalledError do
69       @watch.condition(:fake_poll_condition) { |c| cond = c }
70     end
71   end
72   
73   def test_condition_should_be_block_optional
74     @watch.interval = 0
75     @watch.start_if do |w|
76       w.condition(:always)
77     end
78     assert_equal 1, @watch.conditions[:start].size
79   end
80   
81   def test_poll_condition_should_inherit_interval_from_watch_if_not_specified
82     @watch.interval = 27
83     @watch.start_if do |w|
84       w.condition(:fake_poll_condition)
85     end
86     assert_equal 27, @watch.conditions[:start].first.interval
87   end
88   
89   def test_poll_condition_should_abort_if_no_interval_and_no_watch_interval
90     assert_raise AbortCalledError do
91       @watch.start_if do |w|
92         w.condition(:fake_poll_condition)
93       end
94     end
95   end
96   
97   def test_condition_should_allow_generation_of_subclasses_of_poll_or_event
98     @watch.interval = 27
99     assert_nothing_raised do
100       @watch.start_if do |w|
101         w.condition(:fake_poll_condition)
102         w.condition(:fake_event_condition)
103       end
104     end
105   end
106   
107   def test_condition_should_abort_if_not_subclass_of_poll_or_event
108     assert_raise AbortCalledError do
109       @watch.start_if do |w|
110         w.condition(:fake_condition) { |c| }
111       end
112     end
113   end
115   # behavior
116   
117   def test_behavior_should_record_behavior
118     beh = nil
119     @watch.behavior(:fake_behavior) { |b| beh = b }
120     assert_equal 1, @watch.behaviors.size
121     assert_equal beh, @watch.behaviors.first
122   end