test_unit-given 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -24
- data/Rakefile +1 -0
- data/lib/test/unit/given/simple.rb +27 -0
- data/lib/test/unit/given/version.rb +1 -1
- data/test/test_simple_given.rb +9 -0
- metadata +8 -11
- data/lib/test/unit/given/strict.rb +0 -34
- data/test/test_strict_given.rb +0 -54
data/README.rdoc
CHANGED
@@ -81,27 +81,8 @@ If you don't want to extend our base class, you can mix in the features explicit
|
|
81
81
|
}
|
82
82
|
end
|
83
83
|
|
84
|
-
You can also strictly enforce the use in your tests:
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
class CircleTest < Test::Unit::TestCase
|
89
|
-
include Test::Unit::Given::Strict
|
90
|
-
include Test::Unit::Given::TestThat
|
91
|
-
|
92
|
-
test_that {
|
93
|
-
@circle = Circle.new(10)
|
94
|
-
When {
|
95
|
-
# this causes an error, because there's no Given
|
96
|
-
@area = @circle.area
|
97
|
-
}
|
98
|
-
Then {
|
99
|
-
assert_equal 314,@area
|
100
|
-
}
|
101
|
-
}
|
102
|
-
end
|
103
|
-
|
104
|
-
Finally, you can re-use blocks:
|
85
|
+
Finally, you can re-use blocks, and use And to create richer expressions:
|
105
86
|
|
106
87
|
class CircleTest < Test::Unit::Given::TestCase
|
107
88
|
|
@@ -120,14 +101,18 @@ Finally, you can re-use blocks:
|
|
120
101
|
test_that {
|
121
102
|
Given circle_with_radius(10)
|
122
103
|
When get_radius
|
104
|
+
And {
|
105
|
+
@diameter = @circle.diameter
|
106
|
+
}
|
123
107
|
Then area_should_be(314)
|
108
|
+
And {
|
109
|
+
assert_equal 20,@diameter
|
110
|
+
}
|
124
111
|
}
|
125
112
|
end
|
126
113
|
|
127
114
|
=== What about block-based assertions, like +assert_raises+
|
128
115
|
|
129
|
-
You can use the non-strict version like so:
|
130
|
-
|
131
116
|
class CircleTest < Test::Unit::Given::TestCase
|
132
117
|
|
133
118
|
test_that "there is no diameter method" do
|
@@ -144,8 +129,6 @@ You can use the non-strict version like so:
|
|
144
129
|
end
|
145
130
|
end
|
146
131
|
|
147
|
-
This won't work in strict mode for now.
|
148
|
-
|
149
132
|
== WTF? Why?
|
150
133
|
|
151
134
|
Just because you're using Test::Unit doesn't mean you can't write fluent, easy to understand tests.
|
data/Rakefile
CHANGED
@@ -67,6 +67,33 @@ module Test
|
|
67
67
|
Given(existing_block,&block)
|
68
68
|
end
|
69
69
|
|
70
|
+
# Public: Continue the previous Given/When/Then in a new block. The reason
|
71
|
+
# you might do this is if you wanted to use the existing block form of a
|
72
|
+
# Given/When/Then, but also need to do something custom. This allows you to
|
73
|
+
# write your test more fluently.
|
74
|
+
#
|
75
|
+
# existing_block - a callable object (e.g. a Proc) that will be called immediately
|
76
|
+
# by this Then. If nil, &block is expected to be passed
|
77
|
+
# block - a block given to this call that will be executed immediately
|
78
|
+
# by this Then. If existing_block is non-nil, this is ignored
|
79
|
+
#
|
80
|
+
# Examples
|
81
|
+
#
|
82
|
+
# then_block = Then {
|
83
|
+
# assert_equal "bar",@foo
|
84
|
+
# }
|
85
|
+
# # => executes block and returns it
|
86
|
+
# Then then_block
|
87
|
+
# And {
|
88
|
+
# assert_equal, "quux",@blah
|
89
|
+
# }
|
90
|
+
# # => executes the block again
|
91
|
+
#
|
92
|
+
# Returns the block that was executed
|
93
|
+
def And(existing_block=nil,&block)
|
94
|
+
Given(existing_block,&block)
|
95
|
+
end
|
96
|
+
|
70
97
|
private
|
71
98
|
|
72
99
|
def call_block_param_or_block_given(existing_block,&block)
|
data/test/test_simple_given.rb
CHANGED
@@ -7,12 +7,21 @@ class TestSimpleGiven < Test::Unit::Given::TestCase
|
|
7
7
|
Given {
|
8
8
|
@x = nil
|
9
9
|
}
|
10
|
+
And {
|
11
|
+
@y = nil
|
12
|
+
}
|
10
13
|
When {
|
11
14
|
@x = 4
|
12
15
|
}
|
16
|
+
And {
|
17
|
+
@y = 10
|
18
|
+
}
|
13
19
|
Then {
|
14
20
|
assert_equal 4,@x
|
15
21
|
}
|
22
|
+
And {
|
23
|
+
assert_equal 10,@y
|
24
|
+
}
|
16
25
|
end
|
17
26
|
|
18
27
|
def test_cannot_use_locals
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_unit-given
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdoc
|
16
|
-
requirement: &
|
16
|
+
requirement: &70295687955360 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70295687955360
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70295687954940 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70295687954940
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: simplecov
|
38
|
-
requirement: &
|
38
|
+
requirement: &70295687954520 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70295687954520
|
47
47
|
description: We don't need no stinkin' RSpec! Get all the fluency you want in your
|
48
48
|
Test::Unit tests, with no magic required, using straight Ruby syntax
|
49
49
|
email:
|
@@ -60,13 +60,11 @@ files:
|
|
60
60
|
- Rakefile
|
61
61
|
- lib/test/unit/given.rb
|
62
62
|
- lib/test/unit/given/simple.rb
|
63
|
-
- lib/test/unit/given/strict.rb
|
64
63
|
- lib/test/unit/given/test_case.rb
|
65
64
|
- lib/test/unit/given/test_that.rb
|
66
65
|
- lib/test/unit/given/version.rb
|
67
66
|
- test/bootstrap.rb
|
68
67
|
- test/test_simple_given.rb
|
69
|
-
- test/test_strict_given.rb
|
70
68
|
- test/test_test_that.rb
|
71
69
|
- test_unit-given.gemspec
|
72
70
|
homepage: ''
|
@@ -96,5 +94,4 @@ summary: Use Given/When/Then in your Test::Unit tests
|
|
96
94
|
test_files:
|
97
95
|
- test/bootstrap.rb
|
98
96
|
- test/test_simple_given.rb
|
99
|
-
- test/test_strict_given.rb
|
100
97
|
- test/test_test_that.rb
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Test
|
2
|
-
module Unit
|
3
|
-
module Given
|
4
|
-
# A strict Given/When/Then that will generate errors if you
|
5
|
-
# omit any one of these in your test.
|
6
|
-
module Strict
|
7
|
-
include Simple
|
8
|
-
|
9
|
-
def setup
|
10
|
-
@__next = :given
|
11
|
-
end
|
12
|
-
|
13
|
-
# Set up the conditions for the test, see Test::Unit::Given::Simple::Given
|
14
|
-
def Given(existing_block=nil,&block)
|
15
|
-
@__next = :when
|
16
|
-
call_block_param_or_block_given(existing_block,&block)
|
17
|
-
end
|
18
|
-
# Execute the code under test, failing if no Given was called first, see Test::Unit::Given::Simple::When
|
19
|
-
def When(existing_block=nil,&block)
|
20
|
-
raise "No Given block?" unless @__next == :when
|
21
|
-
@__next = :then
|
22
|
-
call_block_param_or_block_given(existing_block,&block)
|
23
|
-
end
|
24
|
-
# Verify the results of the test, failing if no When was called first, see Test::Unit::Given::Simple::Then
|
25
|
-
def Then(existing_block=nil,&block)
|
26
|
-
raise "No When block?" unless @__next == :then
|
27
|
-
@__next = :given
|
28
|
-
call_block_param_or_block_given(existing_block,&block)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
data/test/test_strict_given.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'test/unit/given/strict'
|
3
|
-
|
4
|
-
class TestStrictGiven < Test::Unit::TestCase
|
5
|
-
include Test::Unit::Given::Strict
|
6
|
-
|
7
|
-
def test_wrong_order_then_first
|
8
|
-
assert_raises RuntimeError do
|
9
|
-
Then {
|
10
|
-
@x = 4
|
11
|
-
}
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_basics
|
16
|
-
Given {
|
17
|
-
@x = nil
|
18
|
-
}
|
19
|
-
When {
|
20
|
-
@x = 4
|
21
|
-
}
|
22
|
-
Then {
|
23
|
-
assert_equal 4,@x
|
24
|
-
}
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_wrong_order
|
28
|
-
assert_raises RuntimeError do
|
29
|
-
When {
|
30
|
-
@x = 4
|
31
|
-
}
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_can_reuse_blocks
|
36
|
-
invocations = 0
|
37
|
-
x_is_nil = Given {
|
38
|
-
@x = nil
|
39
|
-
invocations += 1
|
40
|
-
}
|
41
|
-
x_is_assigned_to_four = When {
|
42
|
-
@x = 4
|
43
|
-
invocations += 1
|
44
|
-
}
|
45
|
-
x_should_be_four = Then {
|
46
|
-
assert_equal 4,@x
|
47
|
-
invocations += 1
|
48
|
-
}
|
49
|
-
Given x_is_nil
|
50
|
-
When x_is_assigned_to_four
|
51
|
-
Then x_should_be_four
|
52
|
-
assert_equal 6,invocations
|
53
|
-
end
|
54
|
-
end
|