citrusbyte-contest 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,8 +6,7 @@ Contexts for Test::Unit.
6
6
  Description
7
7
  -----------
8
8
 
9
- Write declarative tests with nested contexts without performance
10
- penalties. Contest is less than 100 lines of code and gets the job done.
9
+ Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.
11
10
 
12
11
  Usage
13
12
  -----
@@ -20,6 +19,10 @@ Declare your tests as you would in RSpec or Shoulda:
20
19
  setup do
21
20
  @value = 1
22
21
  end
22
+
23
+ teardown do
24
+ @value = nil
25
+ end
23
26
 
24
27
  test "sample test" do
25
28
  assert_equal 1, @value
@@ -64,7 +67,7 @@ For your convenience, `context` is aliased as `describe` and `test` is aliased a
64
67
  end
65
68
  end
66
69
 
67
- You can run it normaly, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:
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:
68
71
 
69
72
  $ testrb my_test.rb -n test_yet_more_tests
70
73
 
@@ -1,34 +1,45 @@
1
1
  require "test/unit"
2
2
 
3
- # Test::Unit loads a default test if the suite is empty, and the only
4
- # purpose of that test is to fail. As having empty contexts is a common
5
- # practice, we decided to overwrite TestSuite#empty? in order to
6
- # allow them. Having a failure when no tests have been defined seems
7
- # counter-intuitive.
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.
8
7
  class Test::Unit::TestSuite
9
8
  def empty?
10
9
  false
11
10
  end
12
11
  end
13
12
 
14
- # We added setup, test and context as class methods, and the instance
15
- # method setup now iterates on the setup blocks. Note that all setup
16
- # blocks must be defined with the block syntax. Adding a setup instance
17
- # method defeats the purpose of this library.
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
18
  class Test::Unit::TestCase
19
19
  def self.setup(&block)
20
20
  setup_blocks << block
21
21
  end
22
22
 
23
+ def self.teardown(&block)
24
+ teardown_blocks << block
25
+ end
26
+
23
27
  def setup
24
28
  self.class.setup_blocks.each do |block|
25
29
  instance_eval(&block)
26
30
  end
27
31
  end
28
32
 
33
+ def teardown
34
+ self.class.teardown_blocks.each do |block|
35
+ instance_eval(&block)
36
+ end
37
+ end
38
+
29
39
  def self.context(name, &block)
30
40
  subclass = Class.new(self.superclass)
31
41
  subclass.setup_blocks.unshift(*setup_blocks)
42
+ subclass.teardown_blocks.unshift(*teardown_blocks)
32
43
  subclass.class_eval(&block)
33
44
  const_set(context_name(name), subclass)
34
45
  end
@@ -48,6 +59,10 @@ private
48
59
  @setup_blocks ||= []
49
60
  end
50
61
 
62
+ def self.teardown_blocks
63
+ @teardown_blocks ||= []
64
+ end
65
+
51
66
  def self.context_name(name)
52
67
  "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
53
68
  end
@@ -4,6 +4,10 @@ class FooTest < Test::Unit::TestCase
4
4
  setup do
5
5
  @value = 1
6
6
  end
7
+
8
+ teardown do
9
+ @value = nil
10
+ end
7
11
 
8
12
  test "truth" do
9
13
  assert_equal 1, @value
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: citrusbyte-contest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Janowski