added behavior section to site index
[god.git] / test / test_watch.rb
blob84ac0ed7fdfc3ddd45a7ae105f0a6ed3ba5a260f
1 require 'helper'
3 class TestWatch < Test::Unit::TestCase
4   def setup
5     @watch = Watch.new
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.cwd = '/foo'
20       @watch.start = 'start'
21       @watch.stop = 'stop'
22       @watch.restart = 'restart'
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_condition_should_record_condition_in_correct_list
48     cond = nil
49     @watch.start_if do |w|
50       w.condition(:fake_condition) { |c| cond = c }
51     end
52     assert_equal 1, @watch.conditions[:start].size
53     assert_equal cond, @watch.conditions[:start].first
54   end
55   
56   def test_condition_should_record_condition_in_correct_list
57     cond = nil
58     @watch.restart_if do |w|
59       w.condition(:fake_condition) { |c| cond = c }
60     end
61     assert_equal 1, @watch.conditions[:restart].size
62     assert_equal cond, @watch.conditions[:restart].first
63   end
64   
65   def test_condition_called_from_outside_if_block_should_raise
66     assert_raise ExitCalledError do
67       @watch.condition(:fake_condition) { |c| cond = c }
68     end
69   end
70   
71   def test_condition_should_be_block_optional
72     @watch.start_if do |w|
73       w.condition(:always)
74     end
75     assert_equal 1, @watch.conditions[:start].size
76   end
77 end