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.
@@ -0,0 +1,62 @@
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
+ require 'greenbar/ClassMethodSetup'
13
+ require 'date'
14
+
15
+ class Date; end
16
+ #:startdoc:
17
+ #
18
+ # ===Purpose
19
+ #
20
+ # To control time at the date level. DateSetup lets you
21
+ # control what day Date.today thinks it is.
22
+ #
23
+ # ===Usage
24
+ #
25
+ # 1. Include Greenbar::DateSetup in your test case.
26
+ # 2. Use Date.today= to define the date you want to be 'today'
27
+ #
28
+ # You may change 'today' as often as you want.
29
+ #
30
+ # ===Example
31
+ #
32
+ # :include: doc/examples/DateSetup.rb
33
+ #
34
+ # ===Additional definitions
35
+ # Date.today=(date)
36
+ #
37
+ # sets the Date to returned as 'today' for Date.today
38
+ # Must be called before Date.today if DateSetup is being used.
39
+ #
40
+
41
+ module Greenbar::DateSetup
42
+ include Greenbar::TestSetup
43
+ include Greenbar::ClassMethodSetup
44
+
45
+ def setup_mixin #:nodoc:
46
+ replace_class_method(Date, :today) {
47
+ raise "Must use Date.today= when using DateSetup." unless defined? @today
48
+ @today
49
+ }
50
+
51
+ def Date.today=(date) #:nodoc:
52
+ @today = date
53
+ end
54
+ end
55
+
56
+ def teardown_mixin #:nodoc:
57
+ class << Date
58
+ remove_method :today=
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,51 @@
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
+ # :startdoc:
14
+ #
15
+ # ===Purpose
16
+ #
17
+ # Save and restore The Environment (ENV) automatically. If you've got a
18
+ # piece of code that depends on environment variables, then you'll want
19
+ # this to do good unit testing. The variable needs to be set to make
20
+ # sure the test passes, but you want to restore everything to a consistent
21
+ # state when the test ends.
22
+ #
23
+ # ===Usage
24
+ #
25
+ # * Include Greenbar::EnvSetup in your test case.
26
+ # * Define any ENV settings you need.
27
+ #
28
+ # You may now modify the ENV freely, confident that it will be restored to
29
+ # it's starting configuration.
30
+ #
31
+ # ===Example
32
+ #
33
+ # Let's say you've got an application, and it needs to know find it's
34
+ # configuration file. To do this, you want it to look in the directory
35
+ # specified in the environment variable APP_HOME.
36
+ #
37
+ # :include: doc/examples/EnvSetup.rb
38
+ #
39
+ module Greenbar::EnvSetup
40
+
41
+ include Greenbar::TestSetup
42
+
43
+ def setup_mixin #:nodoc:
44
+ @env_mixin_backup = ENV.to_hash
45
+ ENV.clear
46
+ end
47
+
48
+ def teardown_mixin #:nodoc:
49
+ ENV.replace @env_mixin_backup
50
+ end
51
+ end
@@ -0,0 +1,57 @@
1
+ #:stopdoc:
2
+ # Copyright 2006 David Corbin <dcorbin@users.sourceforge.net>, All Rights Reserved
3
+ #
4
+ # This software may be copied and used only in accordance with the
5
+ # terms provided in the include LICENSE file. If no such file is included
6
+ # then please contact the authors of the Greenbar project at
7
+ # http://rubyforge.org/projects/greenbar/
8
+ if defined? RAILS_ROOT
9
+ require 'test/unit/testcase'
10
+ #:startdoc:
11
+ module Greenbar
12
+ #
13
+ # ===Purpose
14
+ #
15
+ # Make RubyOnRails compatible with Greenbar::TestSetup
16
+ #
17
+ # Unfortunately, RubyOnRails wants to replace TestCase setup/teardown methods, just like
18
+ # Greenbar::TestSetup. And it does it in a 'very selfish' way.
19
+ # This module makes it possible to use RubyOnRails fixtures and any Greenbar TestSetup
20
+ # in the same test case.
21
+ #
22
+ # ===Usage
23
+ #
24
+ # * Simply add the following line to the end of your 'test_helper.rb' file
25
+ #
26
+ # require_gem 'greenbar'
27
+ #
28
+ # That's it. Do that, and then you can freely include the various TestSetups
29
+ #
30
+ module RailsSetup
31
+ include Greenbar::TestSetup
32
+ def setup_mixin #:nodoc:
33
+ setup_with_fixtures
34
+ end
35
+
36
+ def teardown_mixin#:nodoc:
37
+ teardown_with_fixtures
38
+ end
39
+ end
40
+
41
+ end
42
+ #:stopdoc:
43
+
44
+ class Test::Unit::TestCase; end
45
+
46
+ class << Test::Unit::TestCase
47
+ alias_method :rails_fixtures, :fixtures
48
+ end
49
+
50
+ module Test::Unit
51
+ def TestCase.fixtures *args
52
+ include Greenbar::RailsSetup
53
+ rails_fixtures(*args)
54
+ end
55
+ end
56
+ #:startdoc:
57
+ end
@@ -0,0 +1,82 @@
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
+ #TODO - use ClassMethodSetup?
13
+
14
+ #:startdoc:#
15
+ #===Purpose
16
+ #
17
+ # Provides a way to control random numbers generated from Kernel#rand
18
+ #
19
+ #===Usage
20
+ #
21
+ # * Include Greenbar::RandSetup in your test case.
22
+ # * Seed sequences for each 'max' used in the code under test. Use seed_rand_sequence
23
+ #
24
+ # Kernel#rand is called to generate random numbers, and it takes a single argument, 'max'
25
+ # If you're picking a random color from a collection of eight, you might seay
26
+ # color = COLORS[rand(8)]
27
+ # Unfortunately, if you're really getting random numbers back, it's very difficult
28
+ # to write a test. So, you might add this call to your test case.
29
+ # seed_rand_sequence 8, [2, 4]
30
+ # If you call it a third time, it will return 2 again, as the sequence is repeated automatically.
31
+ #
32
+ #===Example
33
+ #
34
+ #
35
+ # :include: doc/examples/RandSetup.rb
36
+ #
37
+ #===Additional definitions
38
+ # Kernel#seed_rand_sequence=(max, sequence)
39
+ #
40
+ # Initializes a re-usable sequence for calls to Kernel#rand with max.
41
+ # The first element of the array is returned on the first call to rand(max),
42
+ # and the sequence is rotated, appending this return to the end.
43
+ #
44
+
45
+ module Greenbar::RandSetup
46
+
47
+ #:stopdoc:
48
+ include Greenbar::TestSetup
49
+
50
+ def setup_mixin
51
+ Kernel.class_eval do
52
+
53
+ alias :rand_saved_by_RandSetup :rand
54
+
55
+ def rand(max=0)
56
+ @@rand_sequences ||= {}
57
+ raise "Must use seed_rand_sequence(#{max}) before using rand(#{max})." unless @@rand_sequences.has_key? max
58
+ @@rand_sequences[max] << @@rand_sequences[max].shift
59
+ @@rand_sequences[max][-1]
60
+ end
61
+
62
+ def seed_rand_sequence(max, sequence)
63
+ array = sequence.to_a
64
+ raise "Must provide a non-empty sequence." if array.empty?
65
+ @@rand_sequences ||= {}
66
+ @@rand_sequences[max] = array
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ def teardown_mixin
74
+ Kernel.class_eval do
75
+ alias :rand :rand_saved_by_RandSetup
76
+ remove_method :rand_saved_by_RandSetup
77
+ remove_method :seed_rand_sequence
78
+ @@rand_sequences = nil
79
+ end
80
+ end
81
+ #:startdoc:#
82
+ end
@@ -0,0 +1,105 @@
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
+ module Greenbar
12
+ #:startdoc:
13
+ #
14
+ # === Purpose
15
+ #
16
+ # Provides a way to aggregate reusable setup and teardown methods in a test case, that
17
+ # are called automatically, just like the setup and teardown methods that are directly
18
+ # in a test case.
19
+ #
20
+ # === Usage
21
+ #
22
+ # * Create a new module, and mix-in Greenbar::TestSetup.
23
+ # * Define methods setup_mixin and teardown_mixin to do whatever you need.
24
+ # * Include your module in your test case.
25
+ #
26
+ # It is not necessary to call any methods. The mere act of including the module
27
+ # will cause it to be hooked in correctly.
28
+ #
29
+ # You may include as many TestSetup-based modules as you like in your test cases, and
30
+ # it all works.
31
+ #
32
+ # === Example
33
+ #
34
+ # The following code shows an example where you might want to control
35
+ # the state of a global variable, but only for the duration of a particular
36
+ # test case. Note: we all know global data is evil, *and* a bad programming
37
+ # practice but it makes for a convenient example.
38
+ #
39
+ # :include: doc/examples/TestSetup.rb
40
+ #
41
+ module Greenbar::TestSetup
42
+ def setup #:nodoc:
43
+ setup_mixins
44
+ end
45
+
46
+ def teardown #:nodoc:
47
+ teardown_mixins
48
+ end
49
+
50
+ def setup_mixins #:nodoc:
51
+ self.class.included_test_mixins.reverse_each {|testSetup| testSetup.call_instance_method :setup_mixin, self}
52
+ end
53
+
54
+ def teardown_mixins #:nodoc:
55
+ self.class.included_test_mixins.each {|testSetup| testSetup.call_instance_method :teardown_mixin, self}
56
+ end
57
+
58
+ def self.included(fixtureModule) #:nodoc:
59
+ fixtureModule.extend SetupModuleExtension
60
+ end
61
+
62
+ module SetupModuleExtension #:nodoc:
63
+
64
+ def included(aClass)
65
+ aClass.extend TestCaseClassExtension
66
+ end
67
+
68
+ def call_instance_method(symbol, testCase)
69
+ method = instance_method(symbol).bind(testCase)
70
+ method.call
71
+ end
72
+
73
+ end
74
+
75
+ module TestCaseClassExtension #:nodoc:
76
+
77
+ def method_added(symbol)
78
+ case symbol
79
+ when :setup
80
+ unless method_defined?(:setup_without_mixins)
81
+ alias_method :setup_without_mixins, :setup
82
+ define_method :setup do
83
+ send :setup_mixins
84
+ send :setup_without_mixins
85
+ end
86
+ end
87
+ when :teardown
88
+ unless method_defined?(:teardown_without_mixins)
89
+ alias_method :teardown_without_mixins, :teardown
90
+ define_method :teardown do
91
+ send :teardown_without_mixins
92
+ send :teardown_mixins
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ def included_test_mixins
99
+ ancestors.find_all {|aModule| aModule != self && aModule < Greenbar::TestSetup}
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,66 @@
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
+ require 'greenbar/ClassMethodSetup'
13
+
14
+ class Time; end
15
+ #:startdoc:
16
+ #
17
+ # ===Purpose
18
+ #
19
+ # To control time. What could be more useful than that? TimeSetup lets you
20
+ # control what time Time.new thinks it is.
21
+ #
22
+ # ===Usage
23
+ #
24
+ # 1. Include Greenbar::TimeSetup in your test case.
25
+ # 2. Use Time.now= to define the time you want to be 'now'
26
+ #
27
+ # You may change 'now' as often as you want.
28
+ #
29
+ # ===Example
30
+ #
31
+ # :include: doc/examples/TimeSetup.rb
32
+ #
33
+ # ===Additional definitions
34
+ # Time.now=(time)
35
+ #
36
+ # sets the Time to returned as 'now' for Time.new and Time.now
37
+ # Must be called before Time.new or Time.now if TimeSetup is being used.
38
+ #
39
+ module Greenbar::TimeSetup
40
+ #:stopdoc:
41
+ include Greenbar::TestSetup
42
+ include Greenbar::ClassMethodSetup
43
+ #:startdoc:
44
+
45
+ def setup_mixin #:nodoc:
46
+ replace_class_method(Time, :now) {
47
+ raise "Must use Time.now= when using TimeSetup." unless @now
48
+ @now
49
+ }
50
+
51
+ replace_class_method(Time, :new) {
52
+ Time.now
53
+ }
54
+
55
+ def Time.now=(time) #:nodoc:
56
+ @now = time
57
+ end
58
+ end
59
+
60
+ def teardown_mixin #:nodoc:
61
+ class << Time
62
+ remove_method :now=
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,102 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+
7
+ GREENBAR_VERSION = '0.1.1'
8
+ WEB_BUILD="web/build"
9
+
10
+ desc "Default Task"
11
+ task :default => :unitTest
12
+
13
+ desc "Build website"
14
+ task :build_web => [:clean_web_build, :rdoc] do
15
+ builder = WebBuilder.new(WEB_BUILD, 'web')
16
+ builder.generate_pages
17
+ builder.build_page "#{Dir.pwd}/LICENSE", "license.html"
18
+
19
+ dest = "#{WEB_BUILD}/documentation"
20
+ mkdir_p dest
21
+ cp_r 'html/.', dest
22
+ end
23
+
24
+ task :clean_web_build do
25
+ rm_rf WEB_BUILD
26
+ rm_rf 'html'
27
+ mkdir_p WEB_BUILD
28
+ end
29
+
30
+
31
+ desc "Run unit tests"
32
+ Rake::TestTask.new(:unitTest) {|unitTest|
33
+ unitTest.test_files = FileList['test/allTests.rb']
34
+ unitTest.warning = false
35
+ unitTest.verbose = true
36
+ }
37
+
38
+ desc "Run examples"
39
+ Rake::TestTask.new(:examples) {|unitTest|
40
+ unitTest.test_files = FileList['doc/examples/**.rb']
41
+ unitTest.libs << 'lib'
42
+ unitTest.warning = false
43
+ unitTest.verbose = true
44
+ }
45
+
46
+ rdoc = Rake::RDocTask.new("rdoc") {|rdoc|
47
+ rdoc.rdoc_dir = 'html'
48
+ rdoc.template = 'jamis'
49
+ rdoc.title = "Greenbar -- Test::Unit Tools"
50
+ rdoc.options << '--line-numbers' << '--inline-source'
51
+ rdoc.main = 'README'
52
+ rdoc.rdoc_files.include('README')
53
+ rdoc.rdoc_files.include('LICENSE')
54
+ rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
55
+ }
56
+
57
+ FILES_TO_PACKAGE = FileList[
58
+ 'lib/**/*.rb',
59
+ 'test/**/*.rb',
60
+ 'doc/**/*.rb',
61
+ '[A-Z]*',
62
+ 'rakefile.rb',
63
+ ]
64
+
65
+ if ! defined?(Gem)
66
+ puts "Package Target requires RubyGEMs"
67
+ else
68
+ gemSpec = Gem::Specification.new {|spec|
69
+
70
+ spec.name = 'greenbar'
71
+ spec.version = GREENBAR_VERSION
72
+ spec.summary = "Test::Unit tools."
73
+ spec.description = <<-EOF
74
+ Greenbar is a collection of aids for testing with Test::Unit' <<
75
+ EOF
76
+ spec.authors = "David Corbin, Kiel Hodges"
77
+ spec.homepage = "http://greenbar.rubyforge.org"
78
+ spec.rubyforge_project = "greenbar"
79
+
80
+ spec.files = FILES_TO_PACKAGE.to_a
81
+ spec.test_file = 'test/allTests.rb'
82
+
83
+ spec.require_path = 'lib'
84
+ spec.autorequire = 'greenbar.rb'
85
+
86
+ spec.has_rdoc = true
87
+ spec.extra_rdoc_files = rdoc.rdoc_files.reject {|filename| filename =~ /\.rb$/ }.to_a << 'README'
88
+ spec.rdoc_options <<
89
+ '--title' << 'Greenbar - Aids for Testing with Test::Unit' <<
90
+ '--main' << 'README' <<
91
+ '--line-numbers' << '--inline-source'
92
+
93
+ # spec.signing_key = 'gem-private_key.pem'
94
+ # spec.cert_chain = ['gem-public_cert.pem']
95
+ }
96
+
97
+ Rake::GemPackageTask.new(gemSpec) {|package|
98
+ package.need_tar = true
99
+ }
100
+ end
101
+
102
+