add info to degrading lambda, tested before and after behaviors
[god.git] / test / helper.rb
blobecf170e765fed62b0a6787c59978da5be423bc2d
1 require File.join(File.dirname(__FILE__), *%w[.. lib god])
3 require 'test/unit'
4 require 'set'
6 include God
8 if RUBY_PLATFORM =~ /linux/i && Process.uid != 0
9   abort <<-EOF
10 *********************************************************************
11 *                                                                   *
12 *    You need to run these tests as root (netlink requires it)      *
13 *                                                                   *
14 *********************************************************************
15 EOF
16 end
18 begin
19   require 'mocha'
20 rescue LoadError
21   unless gems ||= false
22     require 'rubygems'
23     gems = true
24     retry
25   else
26     abort "=> You need the Mocha gem to run these tests."
27   end
28 end
30 module God
31   module Conditions
32     class FakeCondition < Condition
33       def test
34         true
35       end
36     end
37   
38     class FakePollCondition < PollCondition
39       def test
40         true
41       end
42     end
43   
44     class FakeEventCondition < EventCondition
45       def register
46       end
47       def deregister
48       end
49     end
50   end
51   
52   module Behaviors
53     class FakeBehavior < Behavior
54       def before_start
55         'foo'
56       end
57       def after_start
58         'bar'
59       end
60     end
61   end
62   
63   module Contacts
64     class FakeContact < Contact
65     end
66   end
67   
68   class << self
69     alias :at_exit_orig :at_exit
70   end
71   
72   def self.at_exit
73     # disable at_exit
74   end
75   
76   def self.reset
77     self.watches = nil
78     self.groups = nil
79     self.server = nil
80     self.inited = nil
81     self.host = nil
82     self.port = nil
83     self.pid_file_directory = nil
84     self.registry.reset
85   end
86 end
88 def silence_warnings
89   old_verbose, $VERBOSE = $VERBOSE, nil
90   yield
91 ensure
92   $VERBOSE = old_verbose
93 end
95 def no_stdout
96   old_stdout = $stdout.dup
97   $stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
98   yield
99   $stdout.reopen(old_stdout)
102 def no_stderr
103   old_stderr = $stderr.dup
104   $stderr.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
105   yield
106   $stderr.reopen(old_stderr)
109 module Kernel
110   def abort(text)
111     raise SystemExit
112   end
115 module Test::Unit::Assertions
116   def assert_abort
117     assert_raise SystemExit do
118       no_stderr do
119         yield
120       end
121     end
122   end
125 # This allows you to be a good OOP citizen and honor encapsulation, but
126 # still make calls to private methods (for testing) by doing
128 #   obj.bypass.private_thingie(arg1, arg2)
130 # Which is easier on the eye than
132 #   obj.send(:private_thingie, arg1, arg2)
134 class Object
135   class Bypass
136     instance_methods.each do |m|
137       undef_method m unless m =~ /^__/
138     end
140     def initialize(ref)
141       @ref = ref
142     end
143   
144     def method_missing(sym, *args)
145       @ref.__send__(sym, *args)
146     end
147   end
149   def bypass
150     Bypass.new(self)
151   end