greenbar 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +47 -0
- data/README +33 -0
- data/doc/examples/ClassMethodSetup_define_class_method.rb +32 -0
- data/doc/examples/ClassMethodSetup_replace_class_method.rb +25 -0
- data/doc/examples/ClassMethodSetup_replace_new.rb +30 -0
- data/doc/examples/DateSetup.rb +32 -0
- data/doc/examples/EnvSetup.rb +28 -0
- data/doc/examples/RandSetup.rb +24 -0
- data/doc/examples/TestSetup.rb +33 -0
- data/doc/examples/TimeSetup.rb +27 -0
- data/lib/greenbar.rb +17 -0
- data/lib/greenbar/ClassMethodSetup.rb +133 -0
- data/lib/greenbar/DateSetup.rb +62 -0
- data/lib/greenbar/EnvSetup.rb +51 -0
- data/lib/greenbar/RailsSetup.rb +57 -0
- data/lib/greenbar/RandSetup.rb +82 -0
- data/lib/greenbar/TestSetup.rb +105 -0
- data/lib/greenbar/TimeSetup.rb +66 -0
- data/rakefile.rb +102 -0
- data/test/ClassMethodSetupTest.rb +113 -0
- data/test/DateSetupTest.rb +39 -0
- data/test/EnvSetupTest.rb +29 -0
- data/test/RailsSetupTest.rb +35 -0
- data/test/RandSetupTest.rb +109 -0
- data/test/TestSetupTest.rb +122 -0
- data/test/TimeSetupTest.rb +45 -0
- data/test/allTests.rb +17 -0
- metadata +77 -0
data/LICENSE
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
NOTE: this is essentially the same license as Ruby has, without paragraphs
|
2
|
+
that do not apply.
|
3
|
+
|
4
|
+
|
5
|
+
Greenbar is copyrighted free software.
|
6
|
+
You can redistribute it and/or modify it under the conditions below:
|
7
|
+
|
8
|
+
1. You may make and give away verbatim copies of the source form of the
|
9
|
+
software without restriction, provided that you duplicate all of the
|
10
|
+
original copyright notices and associated disclaimers.
|
11
|
+
|
12
|
+
2. You may modify your copy of the software in any way, provided that
|
13
|
+
you do at least ONE of the following:
|
14
|
+
|
15
|
+
a) place your modifications in the Public Domain or otherwise
|
16
|
+
make them Freely Available, such as by posting said
|
17
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
18
|
+
the author to include your modifications in the software.
|
19
|
+
|
20
|
+
b) use the modified software only within your corporation or
|
21
|
+
organization.
|
22
|
+
|
23
|
+
c) rename any non-standard executables so the names do not conflict
|
24
|
+
with standard executables, which must also be provided.
|
25
|
+
|
26
|
+
d) make other distribution arrangements with the author.
|
27
|
+
|
28
|
+
3. You may distribute the software in object code or executable
|
29
|
+
form, provided that you do at least ONE of the following:
|
30
|
+
|
31
|
+
a) distribute the executables and library files of the software,
|
32
|
+
together with instructions (in the manual page or equivalent)
|
33
|
+
on where to get the original distribution.
|
34
|
+
|
35
|
+
b) accompany the distribution with the machine-readable source of
|
36
|
+
the software.
|
37
|
+
|
38
|
+
c) give non-standard executables non-standard names, with
|
39
|
+
instructions on where to get the original software distribution.
|
40
|
+
|
41
|
+
d) make other distribution arrangements with the author.
|
42
|
+
|
43
|
+
4. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
44
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
45
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
46
|
+
PURPOSE.
|
47
|
+
|
data/README
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= Greenbar -- Test::Unit Tools
|
2
|
+
|
3
|
+
Greenbar provides aids for testing with Test::Unit.
|
4
|
+
|
5
|
+
== Download
|
6
|
+
|
7
|
+
Get the latest version of Greenbar at
|
8
|
+
|
9
|
+
* http://greenbar.rubyforge.org/downloads.html
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
=== Normal Installation
|
14
|
+
|
15
|
+
"Standard" installation using ruby install.rb is not yet supported.
|
16
|
+
|
17
|
+
=== GEM Installation
|
18
|
+
|
19
|
+
Download and install Greenbar with the following.
|
20
|
+
|
21
|
+
gem install --remote greenbar
|
22
|
+
|
23
|
+
== Getting Started
|
24
|
+
Greenbar provides a framework for re-using setup and teardown code across multiple Test::Unit::TestCases. The key
|
25
|
+
to this is the TestSetup module. Greenbar also provides several generally useful TestSetup mix-ins.
|
26
|
+
|
27
|
+
== Online Resources
|
28
|
+
|
29
|
+
|
30
|
+
== License
|
31
|
+
|
32
|
+
:include: LICENSE
|
33
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require_gem 'greenbar'
|
4
|
+
|
5
|
+
|
6
|
+
class UserTest < Test::Unit::TestCase
|
7
|
+
include Greenbar::ClassMethodSetup
|
8
|
+
|
9
|
+
def test_signup
|
10
|
+
confirmation_args=
|
11
|
+
define_class_method(Mailer, :deliver_confirmation) do |*args|
|
12
|
+
confirmation_args = args
|
13
|
+
end
|
14
|
+
user = User.new('user@test.domain')
|
15
|
+
user.signup()
|
16
|
+
assert_equal ['user@test.domain'], confirmation_args
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class User
|
21
|
+
def initialize(email)
|
22
|
+
@email = email
|
23
|
+
end
|
24
|
+
|
25
|
+
def signup
|
26
|
+
Mailer.deliver_confirmation(@email)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Mailer
|
31
|
+
# assume that 'deliver_confirmation' is handled by a method_missing method
|
32
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require_gem 'greenbar'
|
4
|
+
|
5
|
+
class FooTest < Test::Unit::TestCase
|
6
|
+
include Greenbar::ClassMethodSetup
|
7
|
+
|
8
|
+
def test_dot_files
|
9
|
+
files= [ '.rails', '.ruby', 'timesheet']
|
10
|
+
dirs = [ 'a', 'b', '.', '..', '.bash.d']
|
11
|
+
replace_class_method(Dir, :entries) do
|
12
|
+
dirs + files
|
13
|
+
end
|
14
|
+
replace_class_method(File, :directory?) do |file_name|
|
15
|
+
dirs.include? file_name
|
16
|
+
end
|
17
|
+
assert_equal ['.rails', '.ruby'], ConfigFinder.dotFiles('/root')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class ConfigFinder
|
22
|
+
def self.dotFiles homeDir
|
23
|
+
Dir.entries(homeDir).select {|file_name| !File.directory?(file_name) && file_name =~ /^\./}
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require_gem 'greenbar'
|
4
|
+
|
5
|
+
class DataCounterTest < Test::Unit::TestCase
|
6
|
+
include Greenbar::ClassMethodSetup
|
7
|
+
|
8
|
+
def test_DataCounter
|
9
|
+
replace_new(DataService, { ['testhost'] => MockDataService.new })
|
10
|
+
assert_equal 5, DataCounter.new.count('testhost')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class DataCounter
|
15
|
+
def count host
|
16
|
+
DataService.new(host).fetch.length
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# assume fetch accesses a remote system, normally
|
21
|
+
class DataService
|
22
|
+
def fetch
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MockDataService
|
27
|
+
def fetch
|
28
|
+
"x,y,z"
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require_gem 'greenbar'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class DateDueCalculatorTest < Test::Unit::TestCase
|
7
|
+
include Greenbar::DateSetup
|
8
|
+
|
9
|
+
def test_calculate_date_due
|
10
|
+
Date.today = Date.civil(2006, 4, 15)
|
11
|
+
calculator = DateDueCalculator.new
|
12
|
+
assert_equal Date.civil(2006, 4, 22), calculator.calculate_due_date(Movie.new(:standard))
|
13
|
+
assert_equal Date.civil(2006, 4, 19), calculator.calculate_due_date(Movie.new(:hotflick))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Movie
|
18
|
+
def initialize group
|
19
|
+
@group = group
|
20
|
+
end
|
21
|
+
|
22
|
+
def checkout_duration
|
23
|
+
return 4 if @group == :hotflick
|
24
|
+
return 7
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class DateDueCalculator
|
29
|
+
def calculate_due_date movie
|
30
|
+
Date.today + movie.checkout_duration
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require_gem 'greenbar'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class HomeFinderTest < Test::Unit::TestCase
|
7
|
+
include Greenbar::EnvSetup
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@finder = HomeFinder.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_find_home
|
14
|
+
ENV['APP_HOME'] = '/usr/lib/app'
|
15
|
+
|
16
|
+
assert_equal '/usr/lib/app', @finder.find_home
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_find_home_default
|
20
|
+
assert_equal '/usr/local/lib/app', @finder.find_home
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class HomeFinder
|
25
|
+
def find_home
|
26
|
+
ENV['APP_HOME'] || '/usr/local/lib/app'
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require_gem 'greenbar'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class DieTest < Test::Unit::TestCase
|
7
|
+
include Greenbar::RandSetup
|
8
|
+
|
9
|
+
def test_roll_one_die
|
10
|
+
seed_rand_sequence 6, [5, 1, 3, 2]
|
11
|
+
die = Die.new
|
12
|
+
assert_equal 5, die.roll(6)
|
13
|
+
assert_equal 1, die.roll(6)
|
14
|
+
assert_equal 3, die.roll(6)
|
15
|
+
assert_equal 2, die.roll(6)
|
16
|
+
assert_equal 5, die.roll(6)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Die
|
21
|
+
def roll size
|
22
|
+
rand(size)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require_gem 'greenbar'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
module BarSetup
|
7
|
+
include Greenbar::TestSetup
|
8
|
+
def setup_mixin
|
9
|
+
@original_bar = $bar
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown_mixin
|
13
|
+
$bar = @original_bar
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Foo
|
18
|
+
def bar
|
19
|
+
$bar
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class FooTest < Test::Unit::TestCase
|
24
|
+
include BarSetup
|
25
|
+
|
26
|
+
def setup
|
27
|
+
$bar = 'xyzzy'
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_foo
|
31
|
+
assert_equal 'xyzzy', Foo.new.bar
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require_gem 'greenbar'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class WebServiceTest < Test::Unit::TestCase
|
7
|
+
include Greenbar::TimeSetup
|
8
|
+
|
9
|
+
def test_expires
|
10
|
+
Time.now = Time.local(2006, 4, 16, 15, 23)
|
11
|
+
response = WebService.new.handle_request
|
12
|
+
assert_equal Time.local(2006, 4, 16, 16, 23), response.expires_at
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Response
|
17
|
+
def initialize expires_at
|
18
|
+
@expires_at = expires_at
|
19
|
+
end
|
20
|
+
attr_reader :expires_at
|
21
|
+
end
|
22
|
+
|
23
|
+
class WebService
|
24
|
+
def handle_request
|
25
|
+
Response.new Time.now + 60 * 60
|
26
|
+
end
|
27
|
+
end
|
data/lib/greenbar.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Greenbar
|
2
|
+
private
|
3
|
+
def self.lazyLoad symbol
|
4
|
+
autoload symbol, 'greenbar/' + symbol.to_s
|
5
|
+
end
|
6
|
+
lazyLoad :TestSetup
|
7
|
+
lazyLoad :EnvSetup
|
8
|
+
lazyLoad :RandSetup
|
9
|
+
lazyLoad :TimeSetup
|
10
|
+
lazyLoad :ClassMethodSetup
|
11
|
+
lazyLoad :DateSetup
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
# :stopdoc:
|
16
|
+
# this rails-related hack doesn't fit normal usage
|
17
|
+
require 'greenbar/RailsSetup'
|
@@ -0,0 +1,133 @@
|
|
1
|
+
#:stopdoc:
|
2
|
+
# Copyright 2005, 2006 Enttek, Inc, All Rights Reserved
|
3
|
+
# Copyright 2005, 2006 Kiel Hodges, All Rights Reserved
|
4
|
+
# Copyright 2005, 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
|
5
|
+
#
|
6
|
+
# This software may be copied and used only in accordance with the
|
7
|
+
# terms provided in the include LICENSE file. If no such file is included
|
8
|
+
# then please contact the authors of the Greenbar project at
|
9
|
+
# http://rubyforge.org/projects/greenbar/
|
10
|
+
#
|
11
|
+
require 'greenbar/TestSetup'
|
12
|
+
|
13
|
+
class Module
|
14
|
+
public :define_method
|
15
|
+
|
16
|
+
def metaclass
|
17
|
+
class << self
|
18
|
+
return self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def alias_class_method new_name, old_name
|
23
|
+
metaclass.instance_eval {
|
24
|
+
alias_method new_name, old_name
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove_class_method method_name
|
29
|
+
metaclass.instance_eval {
|
30
|
+
remove_method method_name
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
#:startdoc:
|
35
|
+
#
|
36
|
+
# ===Purpose
|
37
|
+
#
|
38
|
+
# Sometimes it is very convenient to replace (or even provide)
|
39
|
+
# an alternative implementation of a method in class A to aid
|
40
|
+
# in testing class B. ClassMethodSetup allows you to do this
|
41
|
+
# easily, and restores the original method implementation on
|
42
|
+
# completion.
|
43
|
+
#
|
44
|
+
# ===Usage
|
45
|
+
#
|
46
|
+
# 1. Include Greenbar::ClassMethodSetup in your test case.
|
47
|
+
# 2. Use replace_class_method, define_class_method, or replace_new
|
48
|
+
# as needed.
|
49
|
+
#
|
50
|
+
# You may change 'today' as often as you want.
|
51
|
+
#
|
52
|
+
# ===Examples
|
53
|
+
#
|
54
|
+
# ====for define_class_method
|
55
|
+
#
|
56
|
+
# :include: doc/examples/ClassMethodSetup_define_class_method.rb
|
57
|
+
#
|
58
|
+
# ====for replace_class_method
|
59
|
+
#
|
60
|
+
# :include: doc/examples/ClassMethodSetup_replace_class_method.rb
|
61
|
+
#
|
62
|
+
# ====for replace_new
|
63
|
+
#
|
64
|
+
# :include: doc/examples/ClassMethodSetup_replace_new.rb
|
65
|
+
#
|
66
|
+
#
|
67
|
+
module Greenbar::ClassMethodSetup
|
68
|
+
include Greenbar::TestSetup
|
69
|
+
|
70
|
+
def setup_mixin #:nodoc:
|
71
|
+
@class_methods_to_restore = []
|
72
|
+
@class_methods_to_remove = []
|
73
|
+
end
|
74
|
+
|
75
|
+
def teardown_mixin #:nodoc:
|
76
|
+
@class_methods_to_restore.each {|target_class, method_name|
|
77
|
+
target_class.alias_class_method method_name, name_of_copied_method(method_name)
|
78
|
+
}
|
79
|
+
|
80
|
+
@class_methods_to_remove.each {|target_class, method_name|
|
81
|
+
target_class.remove_class_method method_name
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
# Replaces a class method _method_name_ on class _target_class_. The implementation
|
86
|
+
# will be the _block_, and may take any number of arguments. The original method
|
87
|
+
# will be restored automatically. The primary purpose for this method is when you
|
88
|
+
# need to provide a mock implementation of some class_method.
|
89
|
+
#
|
90
|
+
def replace_class_method target_class, method_name, &block
|
91
|
+
@class_methods_to_restore << [target_class, method_name]
|
92
|
+
target_class.alias_class_method name_of_copied_method(method_name), method_name
|
93
|
+
target_class.metaclass.define_method method_name, &block
|
94
|
+
end
|
95
|
+
|
96
|
+
# Defines a new class method _method_name_ on class _target_class_. The implementation
|
97
|
+
# will be the _block_, and may take any number of arguments. The method
|
98
|
+
# will be destroyed automatically at the end of the test.
|
99
|
+
# The primary purpose for this method is when you
|
100
|
+
# need to provide a mock implementation of a method that is usually handled by
|
101
|
+
# method_missing implementation
|
102
|
+
#
|
103
|
+
def define_class_method target_class, method_name, &block
|
104
|
+
@class_methods_to_remove << [target_class, method_name]
|
105
|
+
target_class.metaclass.define_method method_name, &block
|
106
|
+
end
|
107
|
+
|
108
|
+
# Replaces the new method on class _target_class_.
|
109
|
+
#
|
110
|
+
# In the canonical form, _results_ should will respond to to_hash.
|
111
|
+
# The hash is keyed by arrays that match the expected arguments to the new method.
|
112
|
+
# the hash value is the object to return for that set of arguments.
|
113
|
+
#
|
114
|
+
# If _results_ does not respond to to_hash, is is assumed to be the desired
|
115
|
+
# return value for a no-argument call to new.
|
116
|
+
#
|
117
|
+
def replace_new target_class, results
|
118
|
+
argument_instance_map = results.respond_to?(:to_hash) ? results.to_hash : { [] => results }
|
119
|
+
|
120
|
+
replace_class_method(target_class, :new) {|*args|
|
121
|
+
return argument_instance_map[args] if argument_instance_map.has_key? args
|
122
|
+
raise "No instance has been set to return from #{target_class}.new for #{args.inspect}."
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def name_of_copied_method(method_name)
|
130
|
+
'original_' + method_name.to_s
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|