GUnit 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Greg Sterndale
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = GUnit
2
+
3
+ Unit Test. Gangsta style.
4
+
5
+ GUnit is a fresh new XUnit Test implementation, poppin' a cap in the ass of TestUnit.
6
+
7
+ Just playin'. TestUnit is our boy.
8
+
9
+ == Rolling with the following features:
10
+ * Nested contexts
11
+ * Custom macros *soon*
12
+ * Setup, teardown and *soon* exercise
13
+ * Todos
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 Greg Sterndale. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "GUnit"
8
+ gem.summary = %Q{XUnit Test. Gangsta style.}
9
+ gem.description = %Q{GUnit is a fresh new XUnit Test implementation, poppin' a cap in the ass of TestUnit. Just playin'. TestUnit is our boy.}
10
+ gem.email = "gsterndale@gmail.com"
11
+ gem.homepage = "http://github.com/gsterndale/gunit"
12
+ gem.authors = ["Greg Sterndale"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "GUnit #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
data/gunit.gemspec ADDED
@@ -0,0 +1,86 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{GUnit}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Greg Sterndale"]
12
+ s.date = %q{2010-01-22}
13
+ s.description = %q{GUnit is a fresh new XUnit Test implementation, poppin' a cap in the ass of TestUnit. Just playin'. TestUnit is our boy.}
14
+ s.email = %q{gsterndale@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "gunit.gemspec",
27
+ "lib/gunit.rb",
28
+ "lib/gunit/assertions.rb",
29
+ "lib/gunit/context.rb",
30
+ "lib/gunit/exception_response.rb",
31
+ "lib/gunit/exercise.rb",
32
+ "lib/gunit/fail_response.rb",
33
+ "lib/gunit/pass_response.rb",
34
+ "lib/gunit/proc_extensions.rb",
35
+ "lib/gunit/setup.rb",
36
+ "lib/gunit/teardown.rb",
37
+ "lib/gunit/test_case.rb",
38
+ "lib/gunit/test_response.rb",
39
+ "lib/gunit/test_runner.rb",
40
+ "lib/gunit/test_suite.rb",
41
+ "lib/gunit/to_do_response.rb",
42
+ "lib/gunit/verification.rb",
43
+ "test/integration/foo_test.rb",
44
+ "test/test_helper.rb",
45
+ "test/unit/assertions_test.rb",
46
+ "test/unit/context_test.rb",
47
+ "test/unit/fail_response_test.rb",
48
+ "test/unit/proc_extensions_test.rb",
49
+ "test/unit/setup_test.rb",
50
+ "test/unit/teardown_test.rb",
51
+ "test/unit/test_case_test.rb",
52
+ "test/unit/test_runner_test.rb",
53
+ "test/unit/test_suite_test.rb",
54
+ "test/unit/verification_test.rb"
55
+ ]
56
+ s.homepage = %q{http://github.com/gsterndale/gunit}
57
+ s.rdoc_options = ["--charset=UTF-8"]
58
+ s.require_paths = ["lib"]
59
+ s.rubygems_version = %q{1.3.5}
60
+ s.summary = %q{XUnit Test. Gangsta style.}
61
+ s.test_files = [
62
+ "test/integration/foo_test.rb",
63
+ "test/test_helper.rb",
64
+ "test/unit/assertions_test.rb",
65
+ "test/unit/context_test.rb",
66
+ "test/unit/fail_response_test.rb",
67
+ "test/unit/proc_extensions_test.rb",
68
+ "test/unit/setup_test.rb",
69
+ "test/unit/teardown_test.rb",
70
+ "test/unit/test_case_test.rb",
71
+ "test/unit/test_runner_test.rb",
72
+ "test/unit/test_suite_test.rb",
73
+ "test/unit/verification_test.rb"
74
+ ]
75
+
76
+ if s.respond_to? :specification_version then
77
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
78
+ s.specification_version = 3
79
+
80
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
81
+ else
82
+ end
83
+ else
84
+ end
85
+ end
86
+
@@ -0,0 +1,84 @@
1
+ module GUnit
2
+
3
+ class AssertionFailure < StandardError
4
+ end
5
+
6
+ module Assertions
7
+
8
+ def assert(*args, &blk)
9
+ expected = true
10
+
11
+ if blk
12
+ actual = blk
13
+ message = args[0]
14
+ else
15
+ actual = args[0]
16
+ message = args[1]
17
+ end
18
+
19
+ actual = actual.call if actual.is_a?(Proc)
20
+
21
+ unless actual == expected
22
+ message ||= "#{actual.to_s} != #{expected.to_s}"
23
+ raise GUnit::AssertionFailure.new(message)
24
+ end
25
+
26
+ true
27
+ end
28
+
29
+ def assert_equal(*args, &blk)
30
+ expected = args[0]
31
+
32
+ if blk
33
+ actual = blk
34
+ message = args[1]
35
+ else
36
+ actual = args[1]
37
+ message = args[2]
38
+ end
39
+
40
+ actual = actual.call if actual.is_a?(Proc)
41
+
42
+ message ||= "#{actual.to_s} != #{expected.to_s}"
43
+
44
+ assert expected == actual, message
45
+ end
46
+
47
+ def assert_raises(*args, &blk)
48
+ expected = args[0]
49
+ message = args[1]
50
+
51
+ begin
52
+ blk.call
53
+ rescue Exception => e
54
+ actual = e
55
+ end
56
+
57
+ bool = case
58
+ when actual.nil?
59
+ false
60
+ when expected.is_a?(String)
61
+ actual.to_s == expected
62
+ when expected.is_a?(Class)
63
+ actual.is_a?(expected)
64
+ when expected.nil?
65
+ !actual.nil?
66
+ else
67
+ actual == expected
68
+ end
69
+
70
+ message ||= case
71
+ when !expected.nil? && !actual.nil?
72
+ "Expected #{expected.to_s} to be raised, but got #{actual.to_s}"
73
+ when !expected.nil? && actual.nil?
74
+ "Expected #{expected.to_s} to be raised, but nothing raised"
75
+ else
76
+ "Nothing raised"
77
+ end
78
+
79
+ assert bool, message
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,46 @@
1
+ module GUnit
2
+ # Context instance has message, setups, exercises, teardowns, and parent (another Context)
3
+ class Context
4
+ attr_accessor :task, :parent, :message, :setups, :teardowns
5
+
6
+ # Context.new("my message")
7
+ # Context.new("my message") do
8
+ # setup { @foo = "bar" }
9
+ # verify { true }
10
+ # end
11
+ # Context.new do
12
+ # setup { @foo = "bar" }
13
+ # verify { true }
14
+ # end
15
+ def initialize(*args, &blk)
16
+ self.message = args[0] || ''
17
+ self.setups = []
18
+ self.teardowns = []
19
+ self.task = blk if blk
20
+ end
21
+
22
+ def run(binding=self)
23
+ if @task.is_a?(Proc)
24
+ bound_task = @task.bind(binding)
25
+ bound_task.call
26
+ end
27
+ return true
28
+ end
29
+
30
+ def all_message
31
+ parent_message = self.parent.message if self.parent
32
+ parent_message = nil if parent_message == ''
33
+ [parent_message, @message].compact.join(' ')
34
+ end
35
+
36
+ def all_setups
37
+ (self.parent ? self.parent.setups : []) + @setups
38
+ end
39
+
40
+ def all_teardowns
41
+ (self.parent ? self.parent.teardowns : []) + @teardowns
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,6 @@
1
+ module GUnit
2
+
3
+ class ExceptionResponse < TestResponse
4
+ end
5
+
6
+ end
File without changes
@@ -0,0 +1,19 @@
1
+ module GUnit
2
+
3
+ class FailResponse < TestResponse
4
+
5
+ DEFAULT_MESSAGE = 'Fail'
6
+
7
+ attr_accessor :message
8
+
9
+ # FailResponse.new("my message")
10
+ # Verification.new()
11
+ def initialize(msg=DEFAULT_MESSAGE)
12
+ self.message = msg
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+
@@ -0,0 +1,8 @@
1
+ module GUnit
2
+
3
+ class PassResponse < TestResponse
4
+ end
5
+
6
+ end
7
+
8
+
@@ -0,0 +1,14 @@
1
+ # Stolen straight from ActiveSupport
2
+
3
+ class Proc #:nodoc:
4
+ def bind(object)
5
+ block, time = self, Time.now
6
+ (class << object; self end).class_eval do
7
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
8
+ define_method(method_name, &block)
9
+ method = instance_method(method_name)
10
+ remove_method(method_name)
11
+ method
12
+ end.bind(object)
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ module GUnit
2
+
3
+ class Setup
4
+ attr_writer :message
5
+ attr_accessor :task
6
+
7
+ # Setup.new("my message")
8
+ # Setup.new("my message") { @foo = "bar" }
9
+ # Setup.new() { @foo = "bar" }
10
+ def initialize(*args, &blk)
11
+ self.message = args[0]
12
+ self.task = blk if blk
13
+ end
14
+
15
+ def run(binding=self)
16
+ begin
17
+ if @task.is_a?(Proc)
18
+ bound_task = @task.bind(binding)
19
+ bound_task.call
20
+ end
21
+ return true
22
+ rescue GUnit::AssertionFailure => e
23
+ FailResponse.new
24
+ rescue ::StandardError => e
25
+ ExceptionResponse.new
26
+ end
27
+ end
28
+
29
+ def message
30
+ @message || default_message
31
+ end
32
+
33
+ def default_message
34
+ "Setup"
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+
@@ -0,0 +1,41 @@
1
+ module GUnit
2
+
3
+ class Teardown
4
+ attr_writer :message
5
+ attr_accessor :task
6
+
7
+ # Teardown.new("my message")
8
+ # Teardown.new("my message") { @foo = "bar" }
9
+ # Teardown.new() { @foo = "bar" }
10
+ def initialize(*args, &blk)
11
+ self.message = args[0]
12
+ self.task = blk if blk
13
+ end
14
+
15
+ def run(binding=self)
16
+ begin
17
+ if @task.is_a?(Proc)
18
+ bound_task = @task.bind(binding)
19
+ bound_task.call
20
+ end
21
+ return true
22
+ rescue GUnit::AssertionFailure => e
23
+ FailResponse.new
24
+ rescue ::StandardError => e
25
+ ExceptionResponse.new
26
+ end
27
+ end
28
+
29
+ def message
30
+ @message || default_message
31
+ end
32
+
33
+ def default_message
34
+ "Teardown"
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+
@@ -0,0 +1,136 @@
1
+ module GUnit
2
+
3
+ # Acts as a TestSuite Factory
4
+ # Creates a TestCase Object for each Test Method
5
+ # Adds all TestCase Objects onto a TestSuite Object
6
+ # The TestSuite Object will be used by a TestRunner
7
+
8
+ # Each TestCase Object of the TestCase Class have a way to determine which Test Method it should invoke.
9
+ # Pluggable Behavior is the most common way to do this.
10
+ # The constructor of the TestCase Class takes the name of the method to be invoked as a parameter and store this name in an instance variable.
11
+ # When the Test Runner invokes the run method on the TestCase Object, it uses reflection to find and invoke the method whose name is in the instance variable.
12
+
13
+ # count, run, display
14
+ class TestCase
15
+
16
+ include Assertions
17
+
18
+ TEST_METHOD_PREFIX = 'test'
19
+
20
+ attr_accessor :method_name, :context
21
+
22
+ @@method_count = 0
23
+ @@context_stack = [ GUnit::Context.new ]
24
+ @@test_method_contexts = {}
25
+
26
+ def initialize(method=nil)
27
+ self.method_name = method
28
+ end
29
+
30
+ def context
31
+ self.class.context_for_method(self.method_name)
32
+ end
33
+
34
+ def run
35
+ self.run_setups
36
+ response = self.send(self.method_name.to_sym)
37
+ self.run_teardowns
38
+ response
39
+ end
40
+
41
+ def self.suite
42
+ # Create an new instance of self.class for each test method and add them to a TestSuite Object
43
+ test_suite = TestSuite.new
44
+ test_methods.each do |test_method|
45
+ test_suite.tests << new(test_method)
46
+ end
47
+ test_suite
48
+ end
49
+
50
+ def self.test_methods(prefix=TEST_METHOD_PREFIX)
51
+ method_names = instance_methods.find_all{|method| method =~ /\A#{prefix}/ && ! GUnit::TestCase.instance_methods.include?(method) }
52
+ method_names.map!{|m| m.to_sym }
53
+ end
54
+
55
+ def self.context_for_method(method_name)
56
+ @@test_method_contexts[method_name] || @@context_stack.first
57
+ end
58
+
59
+ def self.context(*args, &blk)
60
+ new_context = if blk
61
+ GUnit::Context.new(args.first, &blk)
62
+ else
63
+ GUnit::Context.new(args.first)
64
+ end
65
+ new_context.parent = current_context
66
+ @@context_stack << new_context
67
+ current_context.run(self)
68
+ @@context_stack.pop
69
+ end
70
+
71
+ def self.setup(*args, &blk)
72
+ setup = if blk
73
+ GUnit::Setup.new(args.first, &blk)
74
+ else
75
+ GUnit::Setup.new(args.first)
76
+ end
77
+ current_context.setups << setup
78
+ end
79
+
80
+ def self.teardown(*args, &blk)
81
+ teardown = if blk
82
+ GUnit::Teardown.new(args.first, &blk)
83
+ else
84
+ GUnit::Teardown.new(args.first)
85
+ end
86
+ current_context.teardowns << teardown
87
+ end
88
+
89
+ def self.verify(*args, &blk)
90
+ test_method_name = message_to_unique_test_method_name(args.first)
91
+ define_method(test_method_name) do
92
+ verification = if blk
93
+ GUnit::Verification.new(args.first, &blk)
94
+ else
95
+ GUnit::Verification.new(args.first)
96
+ end
97
+ verification.run(self)
98
+ end
99
+ @@method_count += 1
100
+ @@test_method_contexts[test_method_name] = current_context
101
+ test_method_name
102
+ end
103
+
104
+ def self.message_to_test_method_name(msg)
105
+ if msg && msg != ''
106
+ [TEST_METHOD_PREFIX, msg.downcase.gsub(/ |@/,'_')].join('_').to_sym
107
+ else
108
+ TEST_METHOD_PREFIX.to_sym
109
+ end
110
+ end
111
+
112
+ protected
113
+
114
+ def run_setups
115
+ self.context.setups.each {|s| s.run(self) }
116
+ end
117
+
118
+ def run_teardowns
119
+ self.context.teardowns.reverse.each {|t| t.run(self) } if self.context
120
+ end
121
+
122
+ def self.current_context
123
+ @@context_stack.last
124
+ end
125
+
126
+ def self.message_to_unique_test_method_name(msg)
127
+ name = message_to_test_method_name(msg)
128
+ if method_defined?(name)
129
+ name = [name.to_s, (@@method_count+1).to_s].join('_').to_sym
130
+ end
131
+ name
132
+ end
133
+
134
+ end
135
+
136
+ end
@@ -0,0 +1,8 @@
1
+ module GUnit
2
+
3
+ class TestResponse
4
+ end
5
+
6
+ end
7
+
8
+