use 127.0.0.1 for DRb to avoid IPv6 problems
[god.git] / test / helper.rb
blob0b6f7f2dc6194bf04d8697945246581ad53029f9
1 require File.join(File.dirname(__FILE__), *%w[.. lib god])
3 require 'test/unit'
4 require 'set'
6 begin
7   require 'mocha'
8 rescue LoadError
9   unless gems ||= false
10     require 'rubygems'
11     gems = true
12     retry
13   else
14     abort "=> You need the Mocha gem to run these tests."
15   end
16 end
18 include God
20 module God
21   module Conditions
22     class FakeCondition < Condition
23       def test
24         true
25       end
26     end
27   
28     class FakePollCondition < PollCondition
29       def test
30         true
31       end
32     end
33   
34     class FakeEventCondition < EventCondition
35       def test
36         true
37       end
38     end
39   end
40   
41   module Behaviors
42     class FakeBehavior < Behavior
43     end
44   end
45   
46   class << self
47     alias :at_exit_orig :at_exit
48   end
49   
50   def self.at_exit
51     # disable at_exit
52   end
53   
54   def self.reset
55     self.watches = nil
56     self.groups = nil
57     self.server = nil
58     self.inited = nil
59     self.host = nil
60     self.port = nil
61     self.pid_file_directory = nil
62     self.registry.reset
63   end
64 end
66 def silence_warnings
67   old_verbose, $VERBOSE = $VERBOSE, nil
68   yield
69 ensure
70   $VERBOSE = old_verbose
71 end
73 def no_stdout
74   old_stdout = $stdout.dup
75   $stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
76   yield
77   $stdout.reopen(old_stdout)
78 end
80 def no_stderr
81   old_stderr = $stderr.dup
82   $stderr.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
83   yield
84   $stderr.reopen(old_stderr)
85 end
87 module Test::Unit::Assertions
88   def assert_abort
89     assert_raise SystemExit do
90       no_stderr do
91         yield
92       end
93     end
94   end
95 end
97 # This allows you to be a good OOP citizen and honor encapsulation, but
98 # still make calls to private methods (for testing) by doing
100 #   obj.bypass.private_thingie(arg1, arg2)
102 # Which is easier on the eye than
104 #   obj.send(:private_thingie, arg1, arg2)
106 class Object
107   class Bypass
108     instance_methods.each do |m|
109       undef_method m unless m =~ /^__/
110     end
112     def initialize(ref)
113       @ref = ref
114     end
115   
116     def method_missing(sym, *args)
117       @ref.__send__(sym, *args)
118     end
119   end
121   def bypass
122     Bypass.new(self)
123   end