implemented timer
[god.git] / test / test_watch.rb
blob45e256f4622b21e8c7b1dc19df7c9f24303aa31c
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.grace = 5
23     end
24   end
25   
26   # _if methods
27   
28   def test_start_if_should_modify_action_within_scope
29     assert_equal nil, @watch.instance_variable_get(:@action)
30     @watch.start_if do |w|
31       assert_equal :start, @watch.instance_variable_get(:@action)
32     end
33     assert_equal nil, @watch.instance_variable_get(:@action)
34   end
35   
36   def test_restart_if_should_modify_action_within_scope
37     assert_equal nil, @watch.instance_variable_get(:@action)
38     @watch.restart_if do |w|
39       assert_equal :restart, @watch.instance_variable_get(:@action)
40     end
41     assert_equal nil, @watch.instance_variable_get(:@action)
42   end
43   
44   # condition
45   
46   def test_start_condition_should_record_condition_in_correct_list
47     cond = nil
48     @watch.start_if do |w|
49       w.condition(:fake_condition) { |c| cond = c }
50     end
51     assert_equal 1, @watch.conditions[:start].size
52     assert_equal cond, @watch.conditions[:start].first
53   end
54   
55   def test_restart_condition_should_record_condition_in_correct_list
56     cond = nil
57     @watch.restart_if do |w|
58       w.condition(:fake_condition) { |c| cond = c }
59     end
60     assert_equal 1, @watch.conditions[:restart].size
61     assert_equal cond, @watch.conditions[:restart].first
62   end
63   
64   def test_condition_called_from_outside_if_block_should_raise
65     assert_raise AbortCalledError do
66       @watch.condition(:fake_condition) { |c| cond = c }
67     end
68   end
69   
70   def test_condition_should_be_block_optional
71     @watch.start_if do |w|
72       w.condition(:always)
73     end
74     assert_equal 1, @watch.conditions[:start].size
75   end
77   # behavior
78   
79   def test_behavior_should_record_behavior
80     beh = nil
81     @watch.behavior(:fake_behavior) { |b| beh = b }
82     assert_equal 1, @watch.behaviors.size
83     assert_equal beh, @watch.behaviors.first
84   end
85 end