greenbar 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -0,0 +1,113 @@
|
|
1
|
+
# Copyright 2005, 2006 Enttek, Inc, All Rights Reserved
|
2
|
+
# Copyright 2005, 2006 Kiel Hodges, All Rights Reserved
|
3
|
+
# Copyright 2005, 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
|
4
|
+
#
|
5
|
+
# This software may be copied and used only in accordance with the
|
6
|
+
# terms provided in the include LICENSE file. If no such file is included
|
7
|
+
# then please contact the authors of the Greenbar project at
|
8
|
+
# http://rubyforge.org/projects/greenbar/
|
9
|
+
#
|
10
|
+
require 'test/unit'
|
11
|
+
require 'greenbar'
|
12
|
+
|
13
|
+
class ClassMethodSetupTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
class Alpha
|
16
|
+
def self.foo x, y
|
17
|
+
"foo for #{x}, #{y}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize x, y
|
21
|
+
@x = x
|
22
|
+
@y = y
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :x, :y
|
26
|
+
|
27
|
+
def == other
|
28
|
+
self.class == other.class &&
|
29
|
+
self.x == other.x &&
|
30
|
+
self.y == other.y
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class Beta
|
37
|
+
def initialize
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Test
|
42
|
+
include Greenbar::ClassMethodSetup
|
43
|
+
end
|
44
|
+
|
45
|
+
def setup
|
46
|
+
@test = Test.new
|
47
|
+
@test.setup
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_define_class_method
|
51
|
+
assert_raise(NoMethodError) {
|
52
|
+
Alpha.undefined('a', 7)
|
53
|
+
}
|
54
|
+
|
55
|
+
value = 'hey!'
|
56
|
+
@test.define_class_method(Alpha, :undefined) {|x, y|
|
57
|
+
"#{value} new undefined for #{x} and #{y}"
|
58
|
+
}
|
59
|
+
assert_equal 'hey! new undefined for a and 7', Alpha.undefined('a', 7)
|
60
|
+
|
61
|
+
@test.teardown
|
62
|
+
assert_raise(NoMethodError) {
|
63
|
+
Alpha.undefined('a', 7)
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def test_replace_class_method
|
69
|
+
assert_equal 'foo for a, 7', Alpha.foo('a', 7)
|
70
|
+
|
71
|
+
value = 'hey!'
|
72
|
+
@test.replace_class_method(Alpha, :foo) {|x, y|
|
73
|
+
"#{value} new foo for #{x} and #{y}"
|
74
|
+
}
|
75
|
+
assert_equal 'hey! new foo for a and 7', Alpha.foo('a', 7)
|
76
|
+
|
77
|
+
@test.teardown
|
78
|
+
assert_equal 'foo for a, 7', Alpha.foo('a', 7)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_replace_new
|
82
|
+
true_alpha = Alpha.new 'a', 7
|
83
|
+
alpha1 = 'alpha1'
|
84
|
+
alpha2 = 'alpha2'
|
85
|
+
beta1 = 'beta1'
|
86
|
+
|
87
|
+
@test.replace_new Alpha, ['a', 7] => alpha1,
|
88
|
+
['b', 8] => alpha2
|
89
|
+
@test.replace_new Beta, [] => beta1
|
90
|
+
|
91
|
+
assert_equal alpha1, Alpha.new('a', 7)
|
92
|
+
assert_equal alpha2, Alpha.new('b', 8)
|
93
|
+
assert_equal beta1, Beta.new
|
94
|
+
exception = assert_raise(RuntimeError) {Alpha.new 'b', 5}
|
95
|
+
assert_equal "No instance has been set to return from #{Alpha}.new for #{['b', 5].inspect}.",
|
96
|
+
exception.message
|
97
|
+
assert_raise(RuntimeError) {Beta.new 'extraneous argument'}
|
98
|
+
|
99
|
+
@test.teardown
|
100
|
+
assert_equal true_alpha, Alpha.new('a', 7)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_replace_new_for_no_args
|
104
|
+
beta1 = 'beta1'
|
105
|
+
|
106
|
+
@test.replace_new Beta, beta1
|
107
|
+
|
108
|
+
assert_equal beta1, Beta.new
|
109
|
+
assert_raise(RuntimeError) {Beta.new 'extraneous argument'}
|
110
|
+
|
111
|
+
@test.teardown
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Copyright 2005, 2006 Enttek, Inc, All Rights Reserved
|
2
|
+
# Copyright 2005, 2006 Kiel Hodges, All Rights Reserved
|
3
|
+
# Copyright 2005, 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
|
4
|
+
#
|
5
|
+
# This software may be copied and used only in accordance with the
|
6
|
+
# terms provided in the include LICENSE file. If no such file is included
|
7
|
+
# then please contact the authors of the Greenbar project at
|
8
|
+
# http://rubyforge.org/projects/greenbar/
|
9
|
+
#
|
10
|
+
require 'test/unit'
|
11
|
+
require 'greenbar/DateSetup'
|
12
|
+
|
13
|
+
class DateSetupTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
class Test
|
16
|
+
include Greenbar::DateSetup
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_date_mixin
|
20
|
+
july4th2004 = Date.civil 2004, 7, 4
|
21
|
+
|
22
|
+
test = Test.new
|
23
|
+
test.setup
|
24
|
+
|
25
|
+
assert_raise(RuntimeError, 'Expected a RuntimeError if Date.now= not called before Date.today') {
|
26
|
+
Date.today
|
27
|
+
}
|
28
|
+
|
29
|
+
Date.today = july4th2004
|
30
|
+
assert_equal july4th2004, Date.today
|
31
|
+
|
32
|
+
test.teardown
|
33
|
+
|
34
|
+
assert Date.today > july4th2004
|
35
|
+
assert_raise(NoMethodError) {
|
36
|
+
Date.today = july4th2004
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright 2005, 2006 Enttek, Inc, All Rights Reserved
|
2
|
+
# Copyright 2005, 2006 Kiel Hodges, All Rights Reserved
|
3
|
+
# Copyright 2005, 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
|
4
|
+
#
|
5
|
+
# This software may be copied and used only in accordance with the
|
6
|
+
# terms provided in the include LICENSE file. If no such file is included
|
7
|
+
# then please contact the authors of the Greenbar project at
|
8
|
+
# http://rubyforge.org/projects/greenbar/
|
9
|
+
#
|
10
|
+
require 'test/unit'
|
11
|
+
require 'greenbar'
|
12
|
+
|
13
|
+
class EnvSetupTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
class Test
|
16
|
+
include Greenbar::EnvSetup
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_env_setup
|
20
|
+
test = Test.new
|
21
|
+
test.setup
|
22
|
+
ENV['EnvSetupTest'] = 'test value'
|
23
|
+
assert_equal 'test value', ENV['EnvSetupTest']
|
24
|
+
|
25
|
+
test.teardown
|
26
|
+
assert_nil ENV['EnvSetupTest']
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
module Test::Unit
|
3
|
+
def TestCase.fixtures *args
|
4
|
+
@@rails_mixin_args = args
|
5
|
+
end
|
6
|
+
|
7
|
+
def TestCase.fixture_args
|
8
|
+
@@rails_mixin_args
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
#TODO teardown not really tested
|
13
|
+
#TODO find a way to do this where we can undo what's been done in teardown
|
14
|
+
RAILS_ROOT=true
|
15
|
+
load 'greenbar/RailsSetup.rb'
|
16
|
+
|
17
|
+
class RailsSetupWithRailsTest < Test::Unit::TestCase
|
18
|
+
fixtures :foo
|
19
|
+
|
20
|
+
def setup_with_fixtures
|
21
|
+
@setup_with_mixins_called = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def teardown_with_fixtures
|
25
|
+
@teardown_with_mixins_called = true
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def test_mixin_works
|
30
|
+
assert_equal [:foo], Test::Unit::TestCase.fixture_args
|
31
|
+
|
32
|
+
assert_kind_of Greenbar::RailsSetup, self
|
33
|
+
assert @setup_with_mixins_called
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# Copyright 2005, 2006 Enttek, Inc, All Rights Reserved
|
2
|
+
# Copyright 2005, 2006 Kiel Hodges, All Rights Reserved
|
3
|
+
# Copyright 2005, 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
|
4
|
+
#
|
5
|
+
# This software may be copied and used only in accordance with the
|
6
|
+
# terms provided in the include LICENSE file. If no such file is included
|
7
|
+
# then please contact the authors of the Greenbar project at
|
8
|
+
# http://rubyforge.org/projects/greenbar/
|
9
|
+
#
|
10
|
+
require 'test/unit'
|
11
|
+
require 'greenbar'
|
12
|
+
|
13
|
+
class RandSetupTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
class Test
|
16
|
+
include Greenbar::RandSetup
|
17
|
+
|
18
|
+
def random_sequence(length, upTo)
|
19
|
+
(1..length).collect {rand(upTo)}
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_rand_setup
|
25
|
+
test = Test.new
|
26
|
+
sequence_for_8 = [3, 4, 5, 1, 7, 1, 0, 3, 2]
|
27
|
+
sequence_for_10 = [3, 8, 5, 9, 7, 9, 3, 0, 3, 7]
|
28
|
+
newSequence = [5, 7, 4, 2, 6]
|
29
|
+
|
30
|
+
test.setup
|
31
|
+
seed_rand_sequence 8, sequence_for_8
|
32
|
+
seed_rand_sequence 10, sequence_for_10
|
33
|
+
assert_equal sequence_for_8 * 3, test.random_sequence(sequence_for_8.length * 3, 8)
|
34
|
+
assert_equal sequence_for_10 * 3, test.random_sequence(sequence_for_10.length * 3, 10)
|
35
|
+
|
36
|
+
seed_rand_sequence 10, newSequence
|
37
|
+
assert_equal newSequence * 3, test.random_sequence(newSequence.length * 3, 10)
|
38
|
+
|
39
|
+
test.teardown
|
40
|
+
10.times {
|
41
|
+
test.setup
|
42
|
+
test.teardown
|
43
|
+
return if sequence_for_8 != test.random_sequence(sequence_for_8.length, 8)
|
44
|
+
}
|
45
|
+
flunk 'Should have restored the non-predictable sequence'
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_seeding_with_array_duck
|
50
|
+
test = Test.new
|
51
|
+
test.setup
|
52
|
+
|
53
|
+
duck = Object.new
|
54
|
+
class << duck
|
55
|
+
def to_a
|
56
|
+
[0, 1, 2, 3, 4]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
seed_rand_sequence 5, duck
|
61
|
+
assert_equal [0, 1, 2, 3, 4], test.random_sequence(5, 5)
|
62
|
+
|
63
|
+
test.teardown
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
class ProblemRandSetupTest < Test::Unit::TestCase
|
69
|
+
|
70
|
+
class Test
|
71
|
+
include Greenbar::RandSetup
|
72
|
+
|
73
|
+
def prepare max, sequence
|
74
|
+
seed_rand_sequence max, sequence
|
75
|
+
end
|
76
|
+
|
77
|
+
def random max
|
78
|
+
rand max
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def setup
|
83
|
+
@test = Test.new
|
84
|
+
@test.setup
|
85
|
+
end
|
86
|
+
|
87
|
+
def teardown
|
88
|
+
@test.teardown
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def test_uninitialized_sequence
|
93
|
+
|
94
|
+
e = assert_raise(RuntimeError) {
|
95
|
+
@test.random 10
|
96
|
+
}
|
97
|
+
assert_equal "Must use seed_rand_sequence(#{10}) before using rand(10).", e.message
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_empty_sequence
|
101
|
+
|
102
|
+
e = assert_raise(RuntimeError) {
|
103
|
+
@test.prepare 10,[]
|
104
|
+
}
|
105
|
+
assert_equal "Must provide a non-empty sequence.", e.message
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# Copyright 2005, 2006 Enttek, Inc, All Rights Reserved
|
2
|
+
# Copyright 2005, 2006 Kiel Hodges, All Rights Reserved
|
3
|
+
# Copyright 2005, 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
|
4
|
+
#
|
5
|
+
# This software may be copied and used only in accordance with the
|
6
|
+
# terms provided in the include LICENSE file. If no such file is included
|
7
|
+
# then please contact the authors of the Greenbar project at
|
8
|
+
# http://rubyforge.org/projects/greenbar/
|
9
|
+
#
|
10
|
+
require 'test/unit'
|
11
|
+
require 'greenbar'
|
12
|
+
|
13
|
+
module AlphaSetup
|
14
|
+
include Greenbar::TestSetup
|
15
|
+
|
16
|
+
def setup_mixin
|
17
|
+
assert_equal false, @alpha_setup_called
|
18
|
+
@alpha_setup_called = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown_mixin
|
22
|
+
assert_equal false, @alpha_teardown_called
|
23
|
+
assert_equal true, @beta_teardown_called
|
24
|
+
@alpha_teardown_called = true
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
module BetaSetup
|
30
|
+
include Greenbar::TestSetup
|
31
|
+
|
32
|
+
def setup_mixin
|
33
|
+
assert_equal true, @alpha_setup_called
|
34
|
+
assert_equal false, @beta_setup_called
|
35
|
+
@beta_setup_called = true
|
36
|
+
end
|
37
|
+
|
38
|
+
def teardown_mixin
|
39
|
+
assert_equal false, @beta_teardown_called
|
40
|
+
@beta_teardown_called = true
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class TestWithoutSetupAndTeardown
|
46
|
+
include Test::Unit::Assertions
|
47
|
+
include AlphaSetup
|
48
|
+
include BetaSetup
|
49
|
+
|
50
|
+
attr_reader :alpha_setup_called, :beta_setup_called, :alpha_teardown_called, :beta_teardown_called
|
51
|
+
|
52
|
+
def initialize
|
53
|
+
@alpha_setup_called = false
|
54
|
+
@beta_setup_called = false
|
55
|
+
@test_case_setup_called = false
|
56
|
+
@alpha_teardown_called = false
|
57
|
+
@beta_teardown_called = false
|
58
|
+
@test_case_teardown_called = false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class TestWithSetupAndTeardown
|
63
|
+
include Test::Unit::Assertions
|
64
|
+
include AlphaSetup
|
65
|
+
include BetaSetup
|
66
|
+
|
67
|
+
attr_reader :alpha_setup_called, :beta_setup_called, :test_case_setup_called,
|
68
|
+
:alpha_teardown_called, :beta_teardown_called, :test_case_teardown_called
|
69
|
+
|
70
|
+
def initialize
|
71
|
+
@alpha_setup_called = false
|
72
|
+
@beta_setup_called = false
|
73
|
+
@test_case_setup_called = false
|
74
|
+
@alpha_teardown_called = false
|
75
|
+
@beta_teardown_called = false
|
76
|
+
@test_case_teardown_called = false
|
77
|
+
end
|
78
|
+
|
79
|
+
def setup
|
80
|
+
assert_equal true, @alpha_setup_called
|
81
|
+
assert_equal true, @beta_setup_called
|
82
|
+
assert_equal false, @test_case_setup_called
|
83
|
+
@test_case_setup_called = true
|
84
|
+
end
|
85
|
+
|
86
|
+
def teardown
|
87
|
+
assert_equal false, @alpha_teardown_called
|
88
|
+
assert_equal false, @beta_teardown_called
|
89
|
+
assert_equal false, @test_case_teardown_called
|
90
|
+
@test_case_teardown_called = true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class TestSetupTest < Test::Unit::TestCase
|
95
|
+
|
96
|
+
def test_test_without_setup_and_teardown
|
97
|
+
test = TestWithoutSetupAndTeardown.new
|
98
|
+
|
99
|
+
test.setup
|
100
|
+
assert_equal true, test.alpha_setup_called
|
101
|
+
assert_equal true, test.beta_setup_called
|
102
|
+
|
103
|
+
test.teardown
|
104
|
+
assert_equal true, test.beta_teardown_called
|
105
|
+
assert_equal true, test.alpha_teardown_called
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_test_with_setup_and_teardown
|
109
|
+
test = TestWithSetupAndTeardown.new
|
110
|
+
|
111
|
+
test.setup
|
112
|
+
assert_equal true, test.alpha_setup_called
|
113
|
+
assert_equal true, test.beta_setup_called
|
114
|
+
assert_equal true, test.test_case_setup_called
|
115
|
+
|
116
|
+
test.teardown
|
117
|
+
assert_equal true, test.test_case_teardown_called
|
118
|
+
assert_equal true, test.beta_teardown_called
|
119
|
+
assert_equal true, test.alpha_teardown_called
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|