mjfreshyfresh-contest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,116 @@
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
+ $ sudo gem install contest
82
+
83
+ If you want to use it with Rails, add this to config/environment.rb:
84
+
85
+ config.gem "contest"
86
+
87
+ Then you can vendor the gem:
88
+
89
+ rake gems:install
90
+ rake gems:unpack
91
+
92
+ License
93
+ -------
94
+
95
+ Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte
96
+
97
+ Permission is hereby granted, free of charge, to any person
98
+ obtaining a copy of this software and associated documentation
99
+ files (the "Software"), to deal in the Software without
100
+ restriction, including without limitation the rights to use,
101
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
102
+ copies of the Software, and to permit persons to whom the
103
+ Software is furnished to do so, subject to the following
104
+ conditions:
105
+
106
+ The above copyright notice and this permission notice shall be
107
+ included in all copies or substantial portions of the Software.
108
+
109
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
110
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
111
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
112
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
113
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
114
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
115
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
116
+ 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,69 @@
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) if block_given?
37
+ const_set(context_name(name.join(" ")), 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 :it, :test
46
+ alias_method :should, :test
47
+ alias_method :describe, :context
48
+ end
49
+
50
+ private
51
+
52
+ def self.context_name(name)
53
+ "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
54
+ end
55
+
56
+ def self.test_name(name)
57
+ "test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
58
+ end
59
+
60
+ def self.sanitize_name(name)
61
+ name.gsub(/\W+/, ' ').strip
62
+ end
63
+
64
+ def self.remove_tests(subclass)
65
+ subclass.public_instance_methods.grep(/^test_/).each do |meth|
66
+ subclass.send(:undef_method, meth.to_sym)
67
+ end
68
+ end
69
+ 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,130 @@
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 "context", "with multiple", "arguments", 123, FooTest do
23
+ should "run the test inside" do
24
+ assert_equal 1, @value
25
+ end
26
+ end
27
+
28
+ context "some context" do
29
+ setup do
30
+ @value += 1
31
+ end
32
+
33
+ test "another truth" do
34
+ assert_equal 2, @value
35
+ end
36
+
37
+ context "and a nested context" do
38
+ setup do
39
+ @value += 1
40
+ end
41
+
42
+ test "more" do
43
+ assert_equal 3, @value
44
+ end
45
+ end
46
+ end
47
+
48
+ context "some other context" do
49
+ setup do
50
+ @value += 1
51
+ end
52
+
53
+ test "yet another truth" do
54
+ assert_equal 2, @value
55
+ end
56
+ end
57
+
58
+ describe "context with should" do
59
+ setup do
60
+ @value += 1
61
+ end
62
+
63
+ should "yet another truth" do
64
+ assert_equal 2, @value
65
+ end
66
+ end
67
+ end
68
+
69
+ class BarTest < Test::Unit::TestCase
70
+ setup do
71
+ @value = 1
72
+ end
73
+
74
+ context "some context" do
75
+ setup do
76
+ @value += 1
77
+ end
78
+
79
+ test "another truth" do
80
+ assert_equal 2, @value
81
+ end
82
+
83
+ test "yet another truth" do
84
+ assert_equal 2, @value
85
+ end
86
+ end
87
+ end
88
+
89
+ class TestBaz < Test::Unit::TestCase
90
+ def foo
91
+ 42
92
+ end
93
+
94
+ def setup
95
+ @value = 1
96
+ super
97
+ end
98
+
99
+ context "some context" do
100
+ def setup
101
+ super
102
+ @value += 2
103
+ end
104
+
105
+ def bar
106
+ foo + 1
107
+ end
108
+
109
+ test "a helper" do
110
+ assert_equal 42, foo
111
+ assert_equal 3, @value
112
+ end
113
+
114
+ test "another helper" do
115
+ assert_equal 43, bar
116
+ end
117
+
118
+ context "another context" do
119
+ setup do
120
+ @value += 3
121
+ end
122
+
123
+ test "blah" do
124
+ assert_equal 6, @value
125
+ end
126
+ end
127
+ end
128
+
129
+ context "empty context"
130
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + "/../lib/contest"
2
+
3
+ class BaseTest < Test::Unit::TestCase
4
+ def setup
5
+ @order = []
6
+ @order << "Grandparent Setup"
7
+ end
8
+
9
+ def teardown
10
+ @order << "Grandparent Teardown"
11
+
12
+ assert_equal ["Grandparent Setup", "Parent Setup", "Child Setup", "Test Case", "Child Teardown", "Parent Teardown", "Grandparent Teardown"], @order
13
+ end
14
+ end
15
+
16
+ class MidLayerTest < BaseTest
17
+ setup { @order << "Parent Setup" }
18
+ teardown { @order << "Parent Teardown" }
19
+ end
20
+
21
+ class LeafTest < MidLayerTest
22
+ setup { @order << "Child Setup" }
23
+ teardown { @order << "Child Teardown" }
24
+
25
+ test "my actual test" do
26
+ @order << "Test Case"
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mjfreshyfresh-contest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/contest.rb
26
+ - README.markdown
27
+ - LICENSE
28
+ - Rakefile
29
+ - rails/init.rb
30
+ - test/all_test.rb
31
+ - test/setup_and_teardown_order_test.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/citrusbyte/contest
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Write more readable tests in Test::Unit with this tiny script.
60
+ test_files: []
61
+