test-belt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,96 @@
1
+ = TestBelt
2
+
3
+ == Description
4
+
5
+ This is my Ruby testing "tool belt". It packages up the most common testing tools and paradigms I use. It is opinionated and custom to how I like to test.
6
+
7
+ == Installation
8
+
9
+ gem install test-belt
10
+
11
+ TestBelt only impacts your testing environment or setup and is designed only for testing scenarios. Here are a few ways of bringing it in to your tool:
12
+ * As a development dependency for a gem:
13
+ Gem::Specification.new do |s|
14
+ # your spec stuff ...
15
+
16
+ s.add_development_dependency("test-belt")
17
+ end
18
+
19
+ * As a Gemfile test dependency:
20
+ source 'http://rubygems.org'
21
+
22
+ # other gem dependencies ...
23
+
24
+ group :test do
25
+ gem 'test-belt'
26
+
27
+ # other testing gem dependencies ...
28
+ end
29
+
30
+ == Test Helper
31
+ Using the TestBelt test helper loads in the testing libraries I like and use. In your test files or a common test helper file, require in the helper:
32
+ require 'test_belt/helper'
33
+
34
+ This will give you the following:
35
+
36
+ === Test::Unit
37
+ just write Test::Unit tests with assertions
38
+
39
+ === Leftright
40
+ https://github.com/jordi/leftright
41
+
42
+ === Shoulda
43
+ https://github.com/thoughtbot/shoulda
44
+
45
+ === Shoulda enhancements
46
+ I've added some handy macros and extensions to some things Shoulda does. None of these are required so just use them as you see fit. Here's a sampling of what is added:
47
+ * Class interface macros:
48
+ ** should_have_class_methods
49
+ ** should_have_readers
50
+ ** etc
51
+ * Files macros:
52
+ ** should_have_files
53
+ ** should_have_directories
54
+ ** etc
55
+ * Context macros:
56
+ ** before/after aliases for setup/teardown
57
+ ** etc
58
+
59
+ I use most of these helpers in testing this gem. To see examples of these in action, peruse this gem's test files and run the test suite with:
60
+ $ rake test
61
+
62
+ == Rake tasks for running tests
63
+ TestBelt can provide an automatic set of rake tasks for testing subsets of your code base. These tasks are defined based on the structure of your test files. To use this first add this to your Rakefile:
64
+
65
+ require 'rubygems'
66
+ require 'test_belt/rake_tasks'
67
+ TestBelt::RakeTasks.for :test # this assumes your test files are located in /test
68
+
69
+ To see what this gives you:
70
+
71
+ $ rake -T
72
+
73
+ == License
74
+
75
+ Copyright (c) 2010 Kelly D. Redding
76
+
77
+ Permission is hereby granted, free of charge, to any person
78
+ obtaining a copy of this software and associated documentation
79
+ files (the "Software"), to deal in the Software without
80
+ restriction, including without limitation the rights to use,
81
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
82
+ copies of the Software, and to permit persons to whom the
83
+ Software is furnished to do so, subject to the following
84
+ conditions:
85
+
86
+ The above copyright notice and this permission notice shall be
87
+ included in all copies or substantial portions of the Software.
88
+
89
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
90
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
91
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
92
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
93
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
94
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
95
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
96
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'lib/test_belt/version'
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'test-belt'
8
+ s.version = TestBelt::Version.to_s
9
+ s.has_rdoc = true
10
+ s.extra_rdoc_files = %w(README.rdoc)
11
+ s.rdoc_options = %w(--main README.rdoc)
12
+ s.summary = "A library for using testing tools I like - my Ruby testing toolbelt."
13
+ s.author = 'Kelly Redding'
14
+ s.email = 'kelly@kelredd.com'
15
+ s.homepage = 'http://github.com/kelredd/test-belt'
16
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
17
+ # s.executables = ['test-belt']
18
+
19
+ s.add_development_dependency("shoulda", [">= 2.10.0"])
20
+ s.add_development_dependency("leftright", [">= 0.0.6"])
21
+ s.add_development_dependency("kelredd-useful", [">= 0.4.0"])
22
+ s.add_development_dependency("kelredd-simple-gem", [">= 0.7.0"])
23
+
24
+ # s.add_dependency("gem-name", [">= 0"])
25
+ end
26
+
27
+ Rake::GemPackageTask.new(spec) do |pkg|
28
+ pkg.gem_spec = spec
29
+ end
30
+
31
+ desc 'Generate the gemspec for this gem'
32
+ task :gemspec do
33
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
34
+ File.open(file, 'w') {|f| f << spec.to_ruby }
35
+ puts "Created gemspec: #{file}"
36
+ end
37
+
38
+ task :default => :gem
39
+
40
+ require 'lib/test_belt/rake_tasks'
41
+ TestBelt::RakeTasks.for :test
42
+
data/lib/test_belt.rb ADDED
@@ -0,0 +1,2 @@
1
+ module TestBelt
2
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'leftright'
4
+ require 'shoulda'
5
+ require 'test_belt/shoulda_macros'
@@ -0,0 +1,84 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ module TestBelt; end
5
+ module TestBelt::RakeTasks
6
+
7
+ FILE_SUFFIX = "_test.rb"
8
+
9
+ class TestTask < Rake::TaskLib
10
+ attr_accessor :name, :description, :test_files
11
+
12
+ # Create a testing task.
13
+ def initialize(name=:test)
14
+ @name = name
15
+ @description = "Run tests" + (@name==:test ? "" : " for #{@name}")
16
+ @test_files = []
17
+ yield self if block_given?
18
+ end
19
+
20
+ # Define the rake task to run this test suite
21
+ def to_task
22
+ desc @description
23
+ task @name do
24
+ RakeFileUtils.verbose(true) { ruby "\"#{rake_loader}\" " + file_list }
25
+ end
26
+ end
27
+
28
+ def file_list # :nodoc:
29
+ @test_files.collect{|f| "\"#{f}\""}.join(' ')
30
+ end
31
+
32
+ protected
33
+
34
+ def rake_loader # :nodoc:
35
+ find_file('rake/rake_test_loader') or
36
+ fail "unable to find rake test loader"
37
+ end
38
+
39
+ def find_file(fn) # :nodoc:
40
+ $LOAD_PATH.each do |path|
41
+ file_path = File.join(path, "#{fn}.rb")
42
+ return file_path if File.exist? file_path
43
+ end
44
+ nil
45
+ end
46
+ end
47
+
48
+ class << self
49
+ def for(test_namespace = :test)
50
+ self.to_tasks(test_namespace.to_s)
51
+ end
52
+
53
+ def to_tasks(path)
54
+ suite_name = File.basename(path)
55
+
56
+ # define a rake test task for all top-level test files at this path
57
+ if !Dir.glob(File.join(path, "**/*#{FILE_SUFFIX}")).empty?
58
+ TestTask.new(suite_name.to_sym) do |t|
59
+ file_location = suite_name == path ? '' : " for #{File.join(path.split(File::SEPARATOR)[1..-1])}"
60
+ t.description = "Run all tests#{file_location}"
61
+ t.test_files = FileList["#{path}/**/*#{FILE_SUFFIX}"]
62
+ end.to_task
63
+ end
64
+
65
+ namespace suite_name.to_s do
66
+ # define rake test tasks for each top-level test file individually
67
+ Dir.glob(File.join(path, "*#{FILE_SUFFIX}")).each do |test_file|
68
+ test_name = File.basename(test_file, FILE_SUFFIX)
69
+ TestTask.new(test_name.to_sym) do |t|
70
+ t.description = "Run tests for #{[path.split(File::SEPARATOR), test_name].flatten[1..-1].join(':')}"
71
+ t.test_files = FileList[test_file]
72
+ end.to_task
73
+ end
74
+
75
+ # recursively define rake test tasks for each file
76
+ # in each top-level directory
77
+ Dir.glob(File.join(path, "*/")).each do |test_dir|
78
+ self.to_tasks(test_dir)
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,9 @@
1
+ require 'shoulda'
2
+
3
+ module TestBelt; end
4
+ module TestBelt::ShouldaMacros; end
5
+
6
+ require 'test_belt/shoulda_macros/classes'
7
+ require 'test_belt/shoulda_macros/context'
8
+ require 'test_belt/shoulda_macros/files'
9
+
@@ -0,0 +1,61 @@
1
+ require 'useful/ruby_extensions/string' unless String.new.respond_to?(:constantize)
2
+
3
+ module TestBelt; end
4
+ module TestBelt::ShouldaMacros; end
5
+ module TestBelt::ShouldaMacros::Classes
6
+
7
+ # Ripped from Shoulda::ActiveRecord::Macros
8
+ def should_have_class_methods(*methods)
9
+ get_options!(methods)
10
+ methods.each do |method|
11
+ should "respond to class method ##{method}" do
12
+ klass = construct_subject.class
13
+ assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
14
+ end
15
+ end
16
+ end
17
+ alias_method :should_have_class_method, :should_have_class_methods
18
+ protected :should_have_class_methods, :should_have_class_method
19
+
20
+
21
+ # Ripped from Shoulda::ActiveRecord::Macros
22
+ def should_have_instance_methods(*methods)
23
+ get_options!(methods)
24
+ methods.each do |method|
25
+ should "respond to instance method ##{method}" do
26
+ the_subject = construct_subject
27
+ assert_respond_to(the_subject, method, "#{the_subject.class.name} does not have instance method #{method}")
28
+ end
29
+ end
30
+ end
31
+ alias_method :should_have_instance_method, :should_have_instance_methods
32
+ protected :should_have_instance_methods, :should_have_instance_method
33
+
34
+
35
+ def should_have_readers(*readers)
36
+ get_options!(readers)
37
+ should_have_instance_methods *readers
38
+ end
39
+ alias_method :should_have_reader, :should_have_readers
40
+ protected :should_have_readers, :should_have_reader
41
+
42
+
43
+ def should_have_writers(*writers)
44
+ get_options!(writers)
45
+ should_have_instance_methods *(writers.collect{|w| "#{w}="})
46
+ end
47
+ alias_method :should_have_writer, :should_have_writers
48
+ protected :should_have_writers, :should_have_writer
49
+
50
+
51
+ def should_have_accessors(*accessors)
52
+ get_options!(accessors)
53
+ should_have_instance_methods *accessors
54
+ should_have_instance_methods *(accessors.collect{|a| "#{a}="})
55
+ end
56
+ alias_method :should_have_accessor, :should_have_accessors
57
+ protected :should_have_accessors, :should_have_accessor
58
+
59
+ end
60
+
61
+ Test::Unit::TestCase.extend(TestBelt::ShouldaMacros::Classes) if defined? Test::Unit::TestCase
@@ -0,0 +1,25 @@
1
+ module TestBelt; end
2
+ module TestBelt::ShouldaMacros; end
3
+ module TestBelt::ShouldaMacros::Context
4
+ end
5
+
6
+
7
+ if defined? Shoulda::Context
8
+ module Shoulda
9
+ class Context
10
+
11
+ alias_method :before, :setup
12
+ alias_method :after, :teardown
13
+
14
+ # TODO: override described_type to use string blessed with constantize method
15
+ # alias_method(:orig_described_type, :described_type) rescue NameError
16
+ # def described_type
17
+ # self.name.gsub(/Test$/, '').constantize
18
+ # end
19
+ # protected :described_type
20
+
21
+ end
22
+ end
23
+
24
+ Shoulda::Context.extend(TestBelt::ShouldaMacros::Context)
25
+ end
@@ -0,0 +1,31 @@
1
+ require 'useful/ruby_extensions/string' unless String.new.respond_to?(:constantize)
2
+
3
+ module TestBelt; end
4
+ module TestBelt::ShouldaMacros; end
5
+ module TestBelt::ShouldaMacros::Files
6
+
7
+ def should_have_files(*files)
8
+ the_files = files.flatten
9
+ if the_files.empty?
10
+ should "have @root_path" do
11
+ assert @root_path, "the variable @root_path is not defined"
12
+ assert File.exists?(@root_path), "'#{@root_path}' does not exist"
13
+ end
14
+ else
15
+ the_files.each do |file|
16
+ should "have the file '#{file}' in @root_path" do
17
+ assert @root_path, "the variable @root_path is not defined"
18
+ assert File.exists?(File.join(@root_path, file)), "'#{file}' does not exist in '#{@root_path}'"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ protected :should_have_files
24
+
25
+ alias_method :should_have_file, :should_have_files
26
+ alias_method :should_have_directories, :should_have_files
27
+ alias_method :should_have_directory, :should_have_files
28
+
29
+ end
30
+
31
+ Test::Unit::TestCase.extend(TestBelt::ShouldaMacros::Files) if defined? Test::Unit::TestCase
@@ -0,0 +1,71 @@
1
+ require 'test/unit'
2
+ require 'test_belt/test_unit/runner'
3
+
4
+ module Test::Unit
5
+
6
+ class TestCase
7
+
8
+ class << self
9
+
10
+ def _testbelt_suite_callbacks
11
+ @_testbelt_suite_callbacks ||= {
12
+ :started => [],
13
+ :finished => []
14
+ }
15
+ end
16
+
17
+ def _testbelt_testcase_callbacks
18
+ @_testbelt_testcase_callbacks ||= {
19
+ :setup => [],
20
+ :teardown => []
21
+ }
22
+ end
23
+
24
+ protected
25
+
26
+ # Suite level callbacks
27
+ def suite_started(&block)
28
+ ::Test::Unit::TestCase._testbelt_suite_callbacks[:started] << block
29
+ end
30
+ alias :on_suite_started :suite_started
31
+
32
+ def suite_finished(&block)
33
+ ::Test::Unit::TestCase._testbelt_suite_callbacks[:finished] << block
34
+ end
35
+ alias :on_suite_finished :suite_finished
36
+
37
+ # TestCase level callbacks
38
+ def setup_once(&block)
39
+ _testbelt_testcase_callbacks[:setup] << block
40
+ end
41
+ alias :before_once :setup_once
42
+
43
+ def teardown_once(&block)
44
+ _testbelt_testcase_callbacks[:teardown] << block
45
+ end
46
+ alias :after_once :teardown_once
47
+
48
+ end
49
+ end
50
+
51
+
52
+ # override the TestSuite with TestCase callbacks
53
+ class TestSuite
54
+ alias_method :run_without_testbelt_callbacks, :run
55
+
56
+ def run(*args, &block) # :nodoc:
57
+ if !tests.empty? && (testclass = tests.first).kind_of?(::Test::Unit::TestCase)
58
+ tests.first.class._testbelt_testcase_callbacks[:setup].each do |callback|
59
+ callback.call
60
+ end
61
+ end
62
+ run_without_testbelt_callbacks *args, &block
63
+ if !tests.empty? && (testclass = tests.first).kind_of?(::Test::Unit::TestCase)
64
+ tests.first.class._testbelt_testcase_callbacks[:teardown].reverse.each do |callback|
65
+ callback.call
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,48 @@
1
+ require 'test/unit'
2
+ require 'test/unit/ui/console/testrunner'
3
+
4
+ module TestBelt; end
5
+ module TestBelt::TestUnit
6
+
7
+ if defined? ::LeftRight::Runner
8
+ class Runner < ::LeftRight::Runner; end
9
+ else
10
+ class Runner < ::Test::Unit::UI::Console::TestRunner; end
11
+ end
12
+
13
+ class Runner
14
+ def started(*args)
15
+ super
16
+ if ::Test::Unit::TestCase.respond_to?("_testbelt_suite_callbacks")
17
+ ::Test::Unit::TestCase._testbelt_suite_callbacks[:started].each do |callback|
18
+ callback.call
19
+ end
20
+ end
21
+ end
22
+
23
+ def finished(*args)
24
+ if ::Test::Unit::TestCase.respond_to?("_testbelt_suite_callbacks")
25
+ ::Test::Unit::TestCase._testbelt_suite_callbacks[:finished].reverse.each do |callback|
26
+ callback.call
27
+ end
28
+ end
29
+ super
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ module Test::Unit
36
+
37
+ # override the AutoRunner's runner to use TestBelt's
38
+ # with callback for suite started/finished
39
+ class AutoRunner
40
+ alias_method :initialize_without_testbelt_runner, :initialize
41
+
42
+ def initialize(*args)
43
+ initialize_without_testbelt_runner *args
44
+ @runner = lambda { |r| ::TestBelt::TestUnit::Runner }
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,13 @@
1
+ module TestBelt
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test-belt
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kelly Redding
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-06 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 39
30
+ segments:
31
+ - 2
32
+ - 10
33
+ - 0
34
+ version: 2.10.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: leftright
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 0
48
+ - 0
49
+ - 6
50
+ version: 0.0.6
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: kelredd-useful
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 15
62
+ segments:
63
+ - 0
64
+ - 4
65
+ - 0
66
+ version: 0.4.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: kelredd-simple-gem
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ - 7
81
+ - 0
82
+ version: 0.7.0
83
+ type: :development
84
+ version_requirements: *id004
85
+ description:
86
+ email: kelly@kelredd.com
87
+ executables: []
88
+
89
+ extensions: []
90
+
91
+ extra_rdoc_files:
92
+ - README.rdoc
93
+ files:
94
+ - README.rdoc
95
+ - Rakefile
96
+ - lib/test_belt/helper.rb
97
+ - lib/test_belt/rake_tasks.rb
98
+ - lib/test_belt/shoulda_macros/classes.rb
99
+ - lib/test_belt/shoulda_macros/context.rb
100
+ - lib/test_belt/shoulda_macros/files.rb
101
+ - lib/test_belt/shoulda_macros.rb
102
+ - lib/test_belt/test_unit/context.rb
103
+ - lib/test_belt/test_unit/runner.rb
104
+ - lib/test_belt/version.rb
105
+ - lib/test_belt.rb
106
+ has_rdoc: true
107
+ homepage: http://github.com/kelredd/test-belt
108
+ licenses: []
109
+
110
+ post_install_message:
111
+ rdoc_options:
112
+ - --main
113
+ - README.rdoc
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ requirements: []
135
+
136
+ rubyforge_project:
137
+ rubygems_version: 1.3.7
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: A library for using testing tools I like - my Ruby testing toolbelt.
141
+ test_files: []
142
+