running_man 0.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 +21 -0
- data/README.md +89 -0
- data/Rakefile +146 -0
- data/lib/running_man/active_record_block.rb +50 -0
- data/lib/running_man/block.rb +80 -0
- data/lib/running_man.rb +45 -0
- data/running_man.gemspec +58 -0
- data/test/running_man/active_record_block_test.rb +64 -0
- data/test/running_man/block_helper_test.rb +25 -0
- data/test/running_man/block_test.rb +30 -0
- data/test/test_helper.rb +21 -0
- metadata +67 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) Rick Olson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# RunningMan
|
2
|
+
|
3
|
+
Provides a simple way of setting up setup/teardown blocks that execute just
|
4
|
+
once for the entire test case.
|
5
|
+
|
6
|
+
class MyTest < Test::Unit::TestCase
|
7
|
+
class << self
|
8
|
+
attr_accessor :block
|
9
|
+
end
|
10
|
+
|
11
|
+
self.block = RunningMan::Block.new do
|
12
|
+
# something expensive
|
13
|
+
end
|
14
|
+
|
15
|
+
def setup
|
16
|
+
self.class.block.run(self)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
This looks much better in something like ActiveSupport::TestCase, where a
|
21
|
+
`#setup` method takes a block.
|
22
|
+
|
23
|
+
class MyTest < ActiveSupport::TestCase
|
24
|
+
block = RunningMan::Block.new do
|
25
|
+
# something expensive
|
26
|
+
end
|
27
|
+
|
28
|
+
setup { block.run(self) }
|
29
|
+
end
|
30
|
+
|
31
|
+
You can also extend your test case class with helper methods to make this
|
32
|
+
look nicer.
|
33
|
+
|
34
|
+
RunningMan.setup_on ActiveSupport::TestCase
|
35
|
+
class MyTest < ActiveSupport::TestCase
|
36
|
+
setup_once do
|
37
|
+
# something expensive
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
## ActiveRecord!
|
42
|
+
|
43
|
+
The use case for RunningMan is for database testing in my Rails apps.
|
44
|
+
RunningMan gives me a setup where on each test case, we:
|
45
|
+
|
46
|
+
1. Clear the database
|
47
|
+
2. Load the fixtures
|
48
|
+
3. Run the tests
|
49
|
+
|
50
|
+
It's not entirely unlike Rails' default fixture behavior, but there are a few
|
51
|
+
important subtleties:
|
52
|
+
|
53
|
+
1. Fixtures are not loaded into the DB for each test - instead they
|
54
|
+
are loaded once for each test class and shared amongst the
|
55
|
+
tests.
|
56
|
+
2. Each test class has its own set of fixtures. Adding or removing
|
57
|
+
fixtures to a test class will not break other tests in strange
|
58
|
+
and mysterious ways.
|
59
|
+
|
60
|
+
This has been tested on Ruby 1.8.7/ActiveRecord 2.2 and
|
61
|
+
Ruby 1.9/ActiveRecord 3.0 beta 3.
|
62
|
+
|
63
|
+
RunningMan.setup_on ActiveSupport::TestCase, :ActiveRecordBlock
|
64
|
+
class MyTest < ActiveSupport::TestCase
|
65
|
+
fixtures do
|
66
|
+
@post = Post.make # <3 Machinist
|
67
|
+
end
|
68
|
+
|
69
|
+
test "check something on post" do
|
70
|
+
assert_equal 'foo', @post.title
|
71
|
+
end
|
72
|
+
|
73
|
+
test "delete post" do
|
74
|
+
@post.destroy
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
## Note on Patches/Pull Requests
|
79
|
+
|
80
|
+
* Fork the project.
|
81
|
+
* Make your feature addition or bug fix.
|
82
|
+
* Add tests for it. This is important so I don’t break it in a future version
|
83
|
+
unintentionally.
|
84
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have
|
85
|
+
your own version, that is fine but bump version in a commit by itself I can
|
86
|
+
ignore when I pull)
|
87
|
+
* Send me a pull request. Bonus points for topic branches.
|
88
|
+
|
89
|
+
Copyright © 2010 rick. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/testtask'
|
49
|
+
Rake::TestTask.new(:test) do |test|
|
50
|
+
test.libs << 'lib' << 'test'
|
51
|
+
test.pattern = 'test/**/*_test.rb'
|
52
|
+
test.verbose = true
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Generate RCov test coverage and open in your browser"
|
56
|
+
task :coverage do
|
57
|
+
require 'rcov'
|
58
|
+
sh "rm -fr coverage"
|
59
|
+
sh "rcov test/test_*.rb"
|
60
|
+
sh "open coverage/index.html"
|
61
|
+
end
|
62
|
+
|
63
|
+
require 'rake/rdoctask'
|
64
|
+
Rake::RDocTask.new do |rdoc|
|
65
|
+
rdoc.rdoc_dir = 'rdoc'
|
66
|
+
rdoc.title = "#{name} #{version}"
|
67
|
+
rdoc.rdoc_files.include('README*')
|
68
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Open an irb session preloaded with this library"
|
72
|
+
task :console do
|
73
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
74
|
+
end
|
75
|
+
|
76
|
+
#############################################################################
|
77
|
+
#
|
78
|
+
# Custom tasks (add your own tasks here)
|
79
|
+
#
|
80
|
+
#############################################################################
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
#############################################################################
|
85
|
+
#
|
86
|
+
# Packaging tasks
|
87
|
+
#
|
88
|
+
#############################################################################
|
89
|
+
|
90
|
+
task :release => :build do
|
91
|
+
unless `git branch` =~ /^\* master$/
|
92
|
+
puts "You must be on the master branch to release!"
|
93
|
+
exit!
|
94
|
+
end
|
95
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
96
|
+
sh "git tag v#{version}"
|
97
|
+
sh "git push origin master"
|
98
|
+
sh "git push v#{version}"
|
99
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
100
|
+
end
|
101
|
+
|
102
|
+
task :build => :gemspec do
|
103
|
+
sh "mkdir -p pkg"
|
104
|
+
sh "gem build #{gemspec_file}"
|
105
|
+
sh "mv #{gem_file} pkg"
|
106
|
+
end
|
107
|
+
|
108
|
+
task :gemspec => :validate do
|
109
|
+
# read spec file and split out manifest section
|
110
|
+
spec = File.read(gemspec_file)
|
111
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
112
|
+
|
113
|
+
# replace name version and date
|
114
|
+
replace_header(head, :name)
|
115
|
+
replace_header(head, :version)
|
116
|
+
replace_header(head, :date)
|
117
|
+
#comment this out if your rubyforge_project has a different name
|
118
|
+
replace_header(head, :rubyforge_project)
|
119
|
+
|
120
|
+
# determine file list from git ls-files
|
121
|
+
files = `git ls-files`.
|
122
|
+
split("\n").
|
123
|
+
sort.
|
124
|
+
reject { |file| file =~ /^\./ }.
|
125
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
126
|
+
map { |file| " #{file}" }.
|
127
|
+
join("\n")
|
128
|
+
|
129
|
+
# piece file back together and write
|
130
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
131
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
132
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
133
|
+
puts "Updated #{gemspec_file}"
|
134
|
+
end
|
135
|
+
|
136
|
+
task :validate do
|
137
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
138
|
+
unless libfiles.empty?
|
139
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
140
|
+
exit!
|
141
|
+
end
|
142
|
+
unless Dir['VERSION*'].empty?
|
143
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
144
|
+
exit!
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module RunningMan
|
2
|
+
# Allow simple setup:
|
3
|
+
#
|
4
|
+
# RunningMan.setup_on Test::Unit::TestCase, :ActiveRecordBlock
|
5
|
+
#
|
6
|
+
# See README for instructions on use.
|
7
|
+
class ActiveRecordBlock < Block
|
8
|
+
module TestClassMethods
|
9
|
+
def fixtures(&block)
|
10
|
+
test_block = RunningMan::ActiveRecordBlock.new(block)
|
11
|
+
setup do
|
12
|
+
test_block.run(self)
|
13
|
+
|
14
|
+
# Open a new transaction before running any test.
|
15
|
+
ActiveRecord::Base.connection.increment_open_transactions
|
16
|
+
ActiveRecord::Base.connection.begin_db_transaction
|
17
|
+
end
|
18
|
+
|
19
|
+
teardown do
|
20
|
+
# Rollback our transaction, returning our fixtures to a pristine
|
21
|
+
# state.
|
22
|
+
ActiveRecord::Base.connection.rollback_db_transaction
|
23
|
+
ActiveRecord::Base.connection.decrement_open_transactions
|
24
|
+
ActiveRecord::Base.clear_active_connections!
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Clear the database before running the block.
|
30
|
+
def run_once(binding)
|
31
|
+
clear_database
|
32
|
+
super
|
33
|
+
end
|
34
|
+
|
35
|
+
# reload any AR instances
|
36
|
+
def set_ivar(binding, ivar, value)
|
37
|
+
if value.class.respond_to?(:find)
|
38
|
+
value = value.class.find(value.id)
|
39
|
+
end
|
40
|
+
super(binding, ivar, value)
|
41
|
+
end
|
42
|
+
|
43
|
+
def clear_database
|
44
|
+
conn = ActiveRecord::Base.connection
|
45
|
+
conn.tables.each do |table|
|
46
|
+
conn.delete "DELETE FROM #{table}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module RunningMan
|
2
|
+
class Block
|
3
|
+
module TestClassMethods
|
4
|
+
# Runs the given block
|
5
|
+
def setup_once(&block)
|
6
|
+
test_block = RunningMan::Block.new(block)
|
7
|
+
setup { test_block.run(self) }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# block_arg - Optional Proc of code that runs only once for the test case.
|
12
|
+
# &block - The default Proc of code that runs only once. Falls back to
|
13
|
+
# block_arg if provided.
|
14
|
+
#
|
15
|
+
# Returns RunningMan::Block instance.
|
16
|
+
def initialize(block_arg = nil, &block)
|
17
|
+
@block = block || block_arg
|
18
|
+
@run = false
|
19
|
+
@ivars = {}
|
20
|
+
if !@block
|
21
|
+
raise ArgumentError, "needs a block."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: This is what is run in the test/unit callback. #run_once is
|
26
|
+
# called only the first time, and #run_always is always called.
|
27
|
+
#
|
28
|
+
# binding - Object that is running the test (usually a Test::Unit::TestCase).
|
29
|
+
#
|
30
|
+
# Returns nothing.
|
31
|
+
def run(binding)
|
32
|
+
if !run_once?
|
33
|
+
@run = true
|
34
|
+
run_once(binding)
|
35
|
+
end
|
36
|
+
run_always(binding)
|
37
|
+
end
|
38
|
+
|
39
|
+
# This runs the block and stores any new instance variables that were set.
|
40
|
+
#
|
41
|
+
# binding - The same Object that is given to #run.
|
42
|
+
#
|
43
|
+
# Returns nothing.
|
44
|
+
def run_once(binding)
|
45
|
+
@ivars.clear
|
46
|
+
before = instance_variables
|
47
|
+
instance_eval(&@block)
|
48
|
+
(instance_variables - before).each do |ivar|
|
49
|
+
@ivars[ivar] = instance_variable_get(ivar)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# This sets the instance variables set from #run_once on the test case.
|
54
|
+
#
|
55
|
+
# binding - The same Object that is given to #run.
|
56
|
+
#
|
57
|
+
# Returns nothing.
|
58
|
+
def run_always(binding)
|
59
|
+
@ivars.each do |ivar, value|
|
60
|
+
set_ivar(binding, ivar, value)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Sets the given instance variable to the test case.
|
65
|
+
#
|
66
|
+
# binding - The same Object that is given to #run.
|
67
|
+
# ivar - String name of the instance variable to set.
|
68
|
+
# value - The Object value of the instance variable.
|
69
|
+
def set_ivar(binding, ivar, value)
|
70
|
+
binding.instance_variable_set(ivar, value)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Determines whether #run_once has already been called.
|
74
|
+
#
|
75
|
+
# Returns a Boolean.
|
76
|
+
def run_once?
|
77
|
+
!!@run
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/running_man.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module RunningMan
|
4
|
+
VERSION = '0.1'
|
5
|
+
|
6
|
+
# Public: Sets up any helper class methods in TestClassMethods on the
|
7
|
+
# specified test case class.
|
8
|
+
#
|
9
|
+
# Examples
|
10
|
+
#
|
11
|
+
# # extends test/unit with RunningMan::Block::TestClassMethods
|
12
|
+
# RunningMan::Block.setup_on Test::Unit::TestCase
|
13
|
+
#
|
14
|
+
# # extends ActiveSupport::TestCase
|
15
|
+
# RunningMan::Block.setup_on ActiveSupport::TestCase
|
16
|
+
#
|
17
|
+
# # extends test/unit with RunningMan::Block::TestClassMethods and
|
18
|
+
# # RunningMan::FooBlock::TestClassMethods
|
19
|
+
# RunningMan::Block.setup_on Test::Unit::TestCase, :FooBlock
|
20
|
+
#
|
21
|
+
# # extends test/unit with RunningMan::Block::TestClassMethods and
|
22
|
+
# # MyBlock::TestClassMethods
|
23
|
+
# RunningMan::Block.setup_on Test::Unit::TestCase, MyBlock
|
24
|
+
#
|
25
|
+
# source - The class to extend. Usually Test::Unit::TestCase.
|
26
|
+
# *klasses - Optional Array of RunningMan::Block subclasses or Symbols.
|
27
|
+
#
|
28
|
+
# Returns nothing.
|
29
|
+
def self.setup_on(source, *klasses)
|
30
|
+
klasses.unshift(Block)
|
31
|
+
klasses.uniq!
|
32
|
+
klasses.each do |klass|
|
33
|
+
if klass.is_a?(Symbol)
|
34
|
+
klass = RunningMan.const_get(klass)
|
35
|
+
end
|
36
|
+
if klass.const_defined?(:TestClassMethods)
|
37
|
+
source.extend klass.const_get(:TestClassMethods)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
autoload :ActiveRecordBlock, 'running_man/active_record_block'
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'running_man/block'
|
data/running_man.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'running_man'
|
16
|
+
s.version = '0.1'
|
17
|
+
s.date = '2010-05-01'
|
18
|
+
s.rubyforge_project = 'running_man'
|
19
|
+
|
20
|
+
## Make sure your summary is short. The description may be as long
|
21
|
+
## as you like.
|
22
|
+
s.summary = "Simple class for test/unit setup blocks that run just once."
|
23
|
+
s.description = "Simple class for test/unit setup blocks that run just once."
|
24
|
+
|
25
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
26
|
+
## better to set the email to an email list or something. If you don't have
|
27
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
28
|
+
s.authors = ["rick"]
|
29
|
+
s.email = 'technoweenie@gmail.com'
|
30
|
+
s.homepage = 'http://techno-weenie.net'
|
31
|
+
|
32
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
33
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
34
|
+
s.require_paths = %w[lib]
|
35
|
+
|
36
|
+
## Leave this section as-is. It will be automatically generated from the
|
37
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
38
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
39
|
+
# = MANIFEST =
|
40
|
+
s.files = %w[
|
41
|
+
LICENSE
|
42
|
+
README.md
|
43
|
+
Rakefile
|
44
|
+
lib/running_man.rb
|
45
|
+
lib/running_man/active_record_block.rb
|
46
|
+
lib/running_man/block.rb
|
47
|
+
running_man.gemspec
|
48
|
+
test/running_man/active_record_block_test.rb
|
49
|
+
test/running_man/block_helper_test.rb
|
50
|
+
test/running_man/block_test.rb
|
51
|
+
test/test_helper.rb
|
52
|
+
]
|
53
|
+
# = MANIFEST =
|
54
|
+
|
55
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
56
|
+
## matches what you actually use.
|
57
|
+
s.test_files = s.files.select { |path| path =~ /^test\/.*_test\.rb/ }
|
58
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
|
+
|
3
|
+
begin
|
4
|
+
RunningMan::ActiveRecordBlock
|
5
|
+
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/version'
|
8
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
9
|
+
ActiveRecord::Base.connection.create_table :test_models do |t|
|
10
|
+
t.string :name
|
11
|
+
end
|
12
|
+
|
13
|
+
class ActiveRecordBlockTest < Test::Unit::TestCase
|
14
|
+
class TestModel < ActiveRecord::Base
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :block_calls
|
19
|
+
end
|
20
|
+
self.block_calls = 0
|
21
|
+
|
22
|
+
fixtures do
|
23
|
+
ActiveRecordBlockTest.block_calls += 1
|
24
|
+
@test = ActiveRecordBlockTest::TestModel.create! :name => 'foo'
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_sets_ivar_from_block
|
28
|
+
check_values
|
29
|
+
assert_equal 'foo', @test.name
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_rollbacks
|
33
|
+
check_values
|
34
|
+
@test.update_attribute :name, 'bar'
|
35
|
+
TestModel.create!
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_rollbacks_2
|
39
|
+
check_values
|
40
|
+
@test.update_attribute :name, 'bar'
|
41
|
+
TestModel.create!
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_rollbacks_3
|
45
|
+
check_values
|
46
|
+
@test.update_attribute :name, 'bar'
|
47
|
+
TestModel.create!
|
48
|
+
end
|
49
|
+
|
50
|
+
# since the primitive #setup and #teardown blocks only allow one set per
|
51
|
+
# class, do the checks here
|
52
|
+
def check_values
|
53
|
+
assert_equal 'foo', @test.name
|
54
|
+
assert_equal 1, TestModel.count
|
55
|
+
assert_equal 1, self.class.block_calls
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
puts "Running ActiveRecord v#{ActiveRecord::VERSION::STRING} tests..."
|
60
|
+
|
61
|
+
rescue LoadError
|
62
|
+
puts $!
|
63
|
+
puts "skipping ActiveRecord tests... gem install activerecord sqlite3-ruby"
|
64
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
|
+
|
3
|
+
class BlockHelperTest < Test::Unit::TestCase
|
4
|
+
class << self
|
5
|
+
attr_accessor :block_calls
|
6
|
+
end
|
7
|
+
self.block_calls = 0
|
8
|
+
|
9
|
+
setup_once do
|
10
|
+
@a = 1
|
11
|
+
BlockHelperTest.block_calls += 1
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
assert_equal 1, self.class.block_calls
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_sets_ivar_from_block
|
19
|
+
assert_equal 1, @a
|
20
|
+
end
|
21
|
+
|
22
|
+
# checks that block_calls is not incremented again
|
23
|
+
def test_again
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
|
+
|
3
|
+
class BlockTest < Test::Unit::TestCase
|
4
|
+
class << self
|
5
|
+
attr_accessor :block, :block_calls
|
6
|
+
end
|
7
|
+
|
8
|
+
self.block_calls = 0
|
9
|
+
self.block = RunningMan::Block.new do
|
10
|
+
@a = 1
|
11
|
+
BlockTest.block_calls += 1
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@b = 2
|
16
|
+
self.class.block.run(self)
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
assert_equal 1, self.class.block_calls
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_sets_ivar_from_block
|
24
|
+
assert_equal 1, @a
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_sets_ivar_from_setup
|
28
|
+
assert_equal 2, @b
|
29
|
+
end
|
30
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'running_man'
|
4
|
+
|
5
|
+
RunningMan.setup_on Test::Unit::TestCase, :ActiveRecordBlock
|
6
|
+
|
7
|
+
class Test::Unit::TestCase
|
8
|
+
def self.setup(&block)
|
9
|
+
define_method(:setup, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.teardown(&block)
|
13
|
+
define_method(:teardown, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'ruby-debug'
|
19
|
+
Debugger.start
|
20
|
+
rescue LoadError
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: running_man
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rick
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-05-01 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Simple class for test/unit setup blocks that run just once.
|
17
|
+
email: technoweenie@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/running_man.rb
|
29
|
+
- lib/running_man/active_record_block.rb
|
30
|
+
- lib/running_man/block.rb
|
31
|
+
- running_man.gemspec
|
32
|
+
- test/running_man/active_record_block_test.rb
|
33
|
+
- test/running_man/block_helper_test.rb
|
34
|
+
- test/running_man/block_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://techno-weenie.net
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: running_man
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Simple class for test/unit setup blocks that run just once.
|
64
|
+
test_files:
|
65
|
+
- test/running_man/active_record_block_test.rb
|
66
|
+
- test/running_man/block_helper_test.rb
|
67
|
+
- test/running_man/block_test.rb
|