contest 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 ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ Contest
2
+ =======
3
+
4
+ Contexts for Test::Unit.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.
10
+
11
+ Usage
12
+ -----
13
+
14
+ Declare your tests as you would in RSpec or Shoulda:
15
+
16
+ require 'contest'
17
+
18
+ class SomeTest < Test::Unit::TestCase
19
+ setup do
20
+ @value = 1
21
+ end
22
+
23
+ teardown do
24
+ @value = nil
25
+ end
26
+
27
+ test "sample test" do
28
+ assert_equal 1, @value
29
+ end
30
+
31
+ context "a context" do
32
+ setup do
33
+ @value += 1
34
+ end
35
+
36
+ test "more tests" do
37
+ assert_equal 2, @value
38
+ end
39
+
40
+ context "a nested context" do
41
+ setup do
42
+ @value += 1
43
+ end
44
+
45
+ test "yet more tests" do
46
+ assert_equal 3, @value
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ For your convenience, `context` is aliased as `describe` and `test` is aliased as `should`, so this is valid:
53
+
54
+ class SomeTest < Test::Unit::TestCase
55
+ setup do
56
+ @value = 1
57
+ end
58
+
59
+ describe "something" do
60
+ setup do
61
+ @value += 1
62
+ end
63
+
64
+ should "equal 2" do
65
+ assert_equal 2, @value
66
+ end
67
+ end
68
+ end
69
+
70
+ You can run it normally, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:
71
+
72
+ $ testrb my_test.rb -n test_yet_more_tests
73
+
74
+ Or with a regular expression:
75
+
76
+ $ testrb my_test.rb -n /yet_more_tests/
77
+
78
+ Installation
79
+ ------------
80
+
81
+ $ gem sources -a http://gems.github.com (you only have to do this once)
82
+ $ sudo gem install citrusbyte-contest
83
+
84
+ License
85
+ -------
86
+
87
+ Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte
88
+
89
+ Permission is hereby granted, free of charge, to any person
90
+ obtaining a copy of this software and associated documentation
91
+ files (the "Software"), to deal in the Software without
92
+ restriction, including without limitation the rights to use,
93
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
94
+ copies of the Software, and to permit persons to whom the
95
+ Software is furnished to do so, subject to the following
96
+ conditions:
97
+
98
+ The above copyright notice and this permission notice shall be
99
+ included in all copies or substantial portions of the Software.
100
+
101
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
102
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
103
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
104
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
105
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
106
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
107
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
108
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+ require 'rake/clean'
5
+
6
+ gem_spec_file = 'contest.gemspec'
7
+
8
+ gem_spec = eval(File.read(gem_spec_file)) rescue nil
9
+
10
+ task :default => :test
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = false
15
+ end
16
+
17
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
18
+ pkg.need_zip = false
19
+ pkg.need_tar = false
20
+ rm_f FileList['pkg/**/*.*']
21
+ end if gem_spec
22
+
23
+ desc "Generate the gemspec file."
24
+ task :gemspec do
25
+ require 'erb'
26
+
27
+ File.open(gem_spec_file, 'w') do |f|
28
+ f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
29
+ end
30
+ end
31
+
32
+ desc "Builds and installs the gem."
33
+ task :install => :repackage do
34
+ `sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
35
+ end
@@ -0,0 +1,68 @@
1
+ require "test/unit"
2
+
3
+ # Test::Unit loads a default test if the suite is empty, whose purpose is to
4
+ # fail. Since having empty contexts is a common practice, we decided to
5
+ # overwrite TestSuite#empty? in order to allow them. Having a failure when no
6
+ # tests have been defined seems counter-intuitive.
7
+ class Test::Unit::TestSuite
8
+ def empty?
9
+ false
10
+ end
11
+ end
12
+
13
+ # Contest adds +teardown+, +test+ and +context+ as class methods, and the
14
+ # instance methods +setup+ and +teardown+ now iterate on the corresponding
15
+ # blocks. Note that all setup and teardown blocks must be defined with the
16
+ # block syntax. Adding setup or teardown instance methods defeats the purpose
17
+ # of this library.
18
+ class Test::Unit::TestCase
19
+ def self.setup(&block)
20
+ define_method :setup do
21
+ super(&block)
22
+ instance_eval(&block)
23
+ end
24
+ end
25
+
26
+ def self.teardown(&block)
27
+ define_method :teardown do
28
+ instance_eval(&block)
29
+ super(&block)
30
+ end
31
+ end
32
+
33
+ def self.context(name, &block)
34
+ subclass = Class.new(self)
35
+ remove_tests(subclass)
36
+ subclass.class_eval(&block)
37
+ const_set(context_name(name), subclass)
38
+ end
39
+
40
+ def self.test(name, &block)
41
+ define_method(test_name(name), &block)
42
+ end
43
+
44
+ class << self
45
+ alias_method :should, :test
46
+ alias_method :describe, :context
47
+ end
48
+
49
+ private
50
+
51
+ def self.context_name(name)
52
+ "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
53
+ end
54
+
55
+ def self.test_name(name)
56
+ "test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
57
+ end
58
+
59
+ def self.sanitize_name(name)
60
+ name.gsub(/\W+/, ' ').strip
61
+ end
62
+
63
+ def self.remove_tests(subclass)
64
+ subclass.public_instance_methods.grep(/^test_/).each do |meth|
65
+ subclass.send(:undef_method, meth.to_sym)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,27 @@
1
+ # ActiveStupor defines its own idiom for the class-level setup method
2
+ # (using callback chains). This hack is to ensure that Contest users can
3
+ # still call the setup method with a block.
4
+ if RAILS_ENV == 'test'
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'contest')
6
+ require "active_support/test_case"
7
+
8
+ class Test::Unit::TestCase
9
+ class << self
10
+ alias contest_setup setup
11
+ end
12
+ end
13
+
14
+ class ActiveSupport::TestCase
15
+ class << self
16
+ alias activesupport_setup setup
17
+ end
18
+
19
+ def self.setup(*args, &block)
20
+ if args.empty?
21
+ contest_setup(&block)
22
+ else
23
+ activesupport_setup(*args)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,114 @@
1
+ require File.dirname(__FILE__) + "/../lib/contest"
2
+
3
+ class FooTest < Test::Unit::TestCase
4
+ setup do
5
+ @value = 1
6
+ end
7
+
8
+ teardown do
9
+ @value = nil
10
+ end
11
+
12
+ test "truth" do
13
+ assert_equal 1, @value
14
+ end
15
+
16
+ context "context's non-word characters " do
17
+ should "run the test inside" do
18
+ assert_equal 1, @value
19
+ end
20
+ end
21
+
22
+ context "some context" do
23
+ setup do
24
+ @value += 1
25
+ end
26
+
27
+ test "another truth" do
28
+ assert_equal 2, @value
29
+ end
30
+
31
+ context "and a nested context" do
32
+ setup do
33
+ @value += 1
34
+ end
35
+
36
+ test "more" do
37
+ assert_equal 3, @value
38
+ end
39
+ end
40
+ end
41
+
42
+ context "some other context" do
43
+ setup do
44
+ @value += 1
45
+ end
46
+
47
+ test "yet another truth" do
48
+ assert_equal 2, @value
49
+ end
50
+ end
51
+
52
+ describe "context with should" do
53
+ setup do
54
+ @value += 1
55
+ end
56
+
57
+ should "yet another truth" do
58
+ assert_equal 2, @value
59
+ end
60
+ end
61
+ end
62
+
63
+ class BarTest < Test::Unit::TestCase
64
+ setup do
65
+ @value = 1
66
+ end
67
+
68
+ context "some context" do
69
+ setup do
70
+ @value += 1
71
+ end
72
+
73
+ test "another truth" do
74
+ assert_equal 2, @value
75
+ end
76
+
77
+ test "another truth" do
78
+ assert_equal 2, @value
79
+ end
80
+ end
81
+ end
82
+
83
+ class TestBaz < Test::Unit::TestCase
84
+ def foo
85
+ 42
86
+ end
87
+
88
+ def setup
89
+ @value = 1
90
+ super
91
+ end
92
+
93
+ context "some context" do
94
+ def setup
95
+ super
96
+ @value += 2
97
+ end
98
+
99
+ test "a helper" do
100
+ assert_equal 42, foo
101
+ assert_equal 3, @value
102
+ end
103
+
104
+ context "another context" do
105
+ setup do
106
+ @value += 3
107
+ end
108
+
109
+ test "blah" do
110
+ assert_equal 6, @value
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + "/../lib/contest"
2
+
3
+ class Test::Unit::TestCase
4
+ def setup
5
+ puts "Grandparent Setup"
6
+ end
7
+ end
8
+
9
+ class Test::Unit::TestCase
10
+ def teardown
11
+ puts "Grandparent Teardown"
12
+ end
13
+ end
14
+
15
+ class MidLayerTest < Test::Unit::TestCase
16
+ setup { puts "Parent Setup" }
17
+ teardown { puts "Parent Teardown" }
18
+ end
19
+
20
+ class LeafTest < MidLayerTest
21
+ setup { puts "Child Setup" }
22
+ teardown { puts "Child Teardown" }
23
+
24
+ test "my actual test" do
25
+ puts "Test Case"
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Damian Janowski
8
+ - Michel Martens
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-16 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: damian.janowski@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/contest.rb
27
+ - README.markdown
28
+ - LICENSE
29
+ - Rakefile
30
+ - rails/init.rb
31
+ - test/all_test.rb
32
+ - test/setup_and_teardown_order.rb
33
+ has_rdoc: false
34
+ homepage: http://github.com/citrusbyte/contest
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.2
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Write more readable tests in Test::Unit with this tiny script.
61
+ test_files: []
62
+