test-spec 0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,22 +6,24 @@ module Test::Unit::UI # :nodoc:
6
6
  protected
7
7
  def setup_mediator
8
8
  @mediator = create_mediator(@suite)
9
- suite_name = @suite.to_s
10
- if ( @suite.kind_of?(Module) )
11
- suite_name = @suite.name
12
- end
13
9
  end
14
10
 
15
11
  def add_fault(fault)
16
- @faults << fault
17
- word = fault.class.name[/(.*::)?(.*)/, 2].upcase
18
- output_no_nl " (#{word} - #{@faults.size})"
12
+ if fault.kind_of? Test::Spec::Disabled
13
+ @disabled += 1
14
+ output_no_nl " (disabled)"
15
+ else
16
+ @faults << fault
17
+ word = fault.class.name[/(.*::)?(.*)/, 2].upcase
18
+ output_no_nl " (#{word} - #{@faults.size})"
19
+ end
19
20
  end
20
21
 
21
22
  def started(result)
22
23
  @result = result
23
24
  @context = nil
24
25
  @contexts = []
26
+ @disabled = 0
25
27
  indent 0
26
28
  end
27
29
 
@@ -37,7 +39,13 @@ module Test::Unit::UI # :nodoc:
37
39
  end
38
40
 
39
41
  def output_result
40
- r = "%d specifications (%d requirements), %d failures" % [
42
+ if @disabled > 0
43
+ disabled = ", #{@disabled} disabled"
44
+ else
45
+ disabled = ""
46
+ end
47
+
48
+ r = "%d specifications#{disabled} (%d requirements), %d failures" % [
41
49
  @result.run_count, @result.assertion_count, @result.failure_count]
42
50
  r << ", #{@result.error_count} errors" if @result.error_count > 0
43
51
  output r
@@ -1,5 +1,4 @@
1
- # commented coz Dr Nic magic works quite well for require!
2
- # require 'test/spec/dox'
1
+ require 'test/spec/dox'
3
2
 
4
3
  module Test::Unit::UI # :nodoc:
5
4
  module RDox # :nodoc:
@@ -1,5 +1,6 @@
1
1
  # Code adapted from rs, written by Eero Saynatkari.
2
2
  require 'fileutils'
3
+ require 'tmpdir'
3
4
 
4
5
  class Test::Spec::Should
5
6
  # Captures output from the IO given as
@@ -11,8 +12,8 @@ class Test::Spec::Should
11
12
  old_to = to.dup
12
13
 
13
14
  # Obtain a filehandle to replace (works with Readline)
14
- to.reopen File.open("/tmp/should_output_#{$$}", "w+")
15
-
15
+ to.reopen File.open(File.join(Dir.tmpdir, "should_output_#{$$}"), "w+")
16
+
16
17
  # Execute
17
18
  @object.call
18
19
 
@@ -0,0 +1,39 @@
1
+ require 'test/spec'
2
+
3
+ require 'test/spec/dox'
4
+
5
+ context "SpecDox" do
6
+ setup do
7
+ r = Test::Unit::UI::SpecDox::TestRunner.new(nil)
8
+ @unmangler = r.method(:unmangle)
9
+ end
10
+
11
+ specify "can unmangle Test::Unit names correctly" do
12
+ @unmangler["test_foo_bar(TestFoo)"].should.equal ["Foo", "foo bar"]
13
+ @unmangler["test_foo_bar(FooTest)"].should.equal ["Foo", "foo bar"]
14
+ @unmangler["test_he_he(Foo)"].should.equal ["Foo", "he he"]
15
+ @unmangler["test_heh(Foo)"].should.equal ["Foo", "heh"]
16
+
17
+ @unmangler["test_heh(Test::Unit::TC_Assertions)"].
18
+ should.equal ["Test::Unit::TC_Assertions", "heh"]
19
+
20
+ @unmangler["test_heh(Foo::Bar::Test)"].
21
+ should.equal ["Foo::Bar::Test", "heh"]
22
+ end
23
+
24
+ specify "can unmangle Test::Spec names correctly" do
25
+ @unmangler["test_spec {context} 007 [whee]()"].
26
+ should.equal ["context", "whee"]
27
+ @unmangler["test_spec {a bit longish context} 069 [and more text]()"].
28
+ should.equal ["a bit longish context", "and more text"]
29
+ @unmangler["test_spec {special chars !\"/&%$} 2 [special chars !\"/&%$]()"].
30
+ should.equal ["special chars !\"/&%$", "special chars !\"/&%$"]
31
+ @unmangler["test_spec {[]} 666666 [{}]()"].
32
+ should.equal ["[]", "{}"]
33
+ end
34
+
35
+ specify "has sensible fallbacks" do
36
+ @unmangler["weird"].should.equal [nil, nil]
37
+ end
38
+ end
39
+
@@ -0,0 +1,210 @@
1
+ # Adapted from flexmock (http://onestepback.org/software/flexmock).
2
+ #
3
+ # Copyright 2006 by Jim Weirich (jweirich@one.net).
4
+ # All rights reserved.
5
+
6
+ # Permission is granted for use, copying, modification, distribution,
7
+ # and distribution of modified versions of this work as long as the
8
+ # above copyright notice is included.
9
+
10
+
11
+ require 'test/spec'
12
+
13
+ begin
14
+ require 'flexmock'
15
+ rescue LoadError
16
+ context "flexmock" do
17
+ specify "can not be found. BAIL OUT!" do
18
+ end
19
+ end
20
+ else
21
+
22
+ context "flexmock" do
23
+ setup do
24
+ @mock = FlexMock.new
25
+ end
26
+
27
+ specify "should handle" do
28
+ args = nil
29
+ @mock.mock_handle(:hi) { |a, b| args = [a,b] }
30
+ @mock.hi(1,2)
31
+ args.should.equal [1,2]
32
+ end
33
+
34
+ specify "should handle without a block" do
35
+ lambda {
36
+ @mock.mock_handle(:blip)
37
+ @mock.blip
38
+ }.should.not.raise
39
+ end
40
+
41
+ specify "should handle with a block" do
42
+ called = false
43
+ @mock.mock_handle(:blip) { |block| block.call }
44
+ @mock.blip { called = true }
45
+ called.should.be true
46
+ end
47
+
48
+ specify "should have a return value" do
49
+ @mock.mock_handle(:blip) { 10 }
50
+ @mock.blip.should.equal 10
51
+ end
52
+
53
+ specify "should handle missing methods" do
54
+ ex = lambda {
55
+ @mock.not_defined
56
+ }.should.raise(NoMethodError)
57
+ ex.message.should.match(/not_defined/)
58
+ end
59
+
60
+ specify "should ignore missing methods" do
61
+ lambda {
62
+ @mock.mock_ignore_missing
63
+ @mock.blip
64
+ }.should.not.raise
65
+ end
66
+
67
+ specify "should count correctly" do
68
+ @mock.mock_handle(:blip, 3)
69
+ @mock.blip
70
+ @mock.blip
71
+ @mock.blip
72
+ @mock.mock_verify
73
+ end
74
+
75
+ specify "should raise on bad counts" do
76
+ @mock.mock_handle(:blip, 3)
77
+ @mock.blip
78
+ @mock.blip
79
+ lambda { @mock.mock_verify }.should.raise Test::Unit::AssertionFailedError
80
+ end
81
+
82
+ specify "should handle undetermined counts" do
83
+ FlexMock.use('fs') { |m|
84
+ m.mock_handle(:blip)
85
+ m.blip
86
+ m.blip
87
+ m.blip
88
+ }
89
+ end
90
+
91
+ specify "should handle zero counts" do
92
+ lambda {
93
+ FlexMock.use { |m|
94
+ m.mock_handle(:blip, 0)
95
+ m.blip
96
+ }
97
+ }.should.raise Test::Unit::AssertionFailedError
98
+ end
99
+
100
+ specify "should have file IO with use" do
101
+ file = FlexMock.use do |m|
102
+ filedata = ["line 1", "line 2"]
103
+ m.mock_handle(:gets, 3) { filedata.shift }
104
+ count_lines(m).should.equal 2
105
+ end
106
+ end
107
+
108
+ def count_lines(stream)
109
+ result = 0
110
+ while line = stream.gets
111
+ result += 1
112
+ end
113
+ result
114
+ end
115
+
116
+ specify "should have use" do
117
+ lambda {
118
+ FlexMock.use do |m|
119
+ m.mock_handle(:blip, 2)
120
+ m.blip
121
+ end
122
+ }.should.raise Test::Unit::AssertionFailedError
123
+ end
124
+
125
+ specify "should handle failures during use" do
126
+ ex = lambda {
127
+ FlexMock.use do |m|
128
+ m.mock_handle(:blip, 2)
129
+ xyz
130
+ end
131
+ }.should.raise NameError
132
+ ex.message.should.match(/undefined local variable or method/)
133
+ end
134
+
135
+ specify "should deal with sequential values" do
136
+ values = [1,4,9,16]
137
+ @mock.mock_handle(:get) { values.shift }
138
+ @mock.get.should.equal 1
139
+ @mock.get.should.equal 4
140
+ @mock.get.should.equal 9
141
+ @mock.get.should.equal 16
142
+ end
143
+
144
+ specify "respond_to? should return false for non handled methods" do
145
+ @mock.should.not.respond_to :blah
146
+ end
147
+
148
+ specify "respond_to? should return true for explicit methods" do
149
+ @mock.mock_handle(:xyz)
150
+ @mock.should.respond_to :xyz
151
+ end
152
+
153
+ specify "respond_to? should return true when ignoring_missing" do
154
+ @mock.mock_ignore_missing
155
+ @mock.should.respond_to :yada
156
+ end
157
+
158
+ specify "respond_to? should return true for missing_methods when should_ignore_missing" do
159
+ @mock.should_ignore_missing
160
+ @mock.should.respond_to :yada
161
+ end
162
+
163
+ specify "should raise error on unknown method proc" do
164
+ lambda {
165
+ @mock.method(:xyzzy)
166
+ }.should.raise NameError
167
+ end
168
+
169
+ specify "should return callable proc on method" do
170
+ got_it = false
171
+ @mock.mock_handle(:xyzzy) { got_it = true }
172
+ method_proc = @mock.method(:xyzzy)
173
+ method_proc.should.not.be.nil
174
+ method_proc.call
175
+ got_it.should.be true
176
+ end
177
+
178
+ specify "should return do nothing proc for missing methods" do
179
+ @mock.mock_ignore_missing
180
+ method_proc = @mock.method(:plugh)
181
+ method_proc.should.not.be.nil
182
+ lambda { method_proc.call }.should.not.raise
183
+ end
184
+ end
185
+
186
+
187
+ class TemperatureSampler
188
+ def initialize(sensor)
189
+ @sensor = sensor
190
+ end
191
+
192
+ def average_temp
193
+ total = (0...3).collect { @sensor.read_temperature }.inject { |i, s| i + s }
194
+ total / 3.0
195
+ end
196
+ end
197
+
198
+ context "flexmock" do
199
+ include FlexMock::TestCase
200
+
201
+ specify "works with test/spec" do
202
+ sensor = flexmock("temp")
203
+ sensor.should_receive(:read_temperature).times(3).and_return(10, 12, 14)
204
+
205
+ sampler = TemperatureSampler.new(sensor)
206
+ sampler.average_temp.should.equal 12
207
+ end
208
+ end
209
+
210
+ end # if not rescue LoadError
@@ -0,0 +1,118 @@
1
+ # Adapted from mocha (http://mocha.rubyforge.org/).
2
+ #
3
+ # Copyright (C) 2006 Revieworld Ltd.
4
+ #
5
+ # You may use, copy and redistribute this library under the same terms
6
+ # as Ruby itself (see www.ruby-lang.org/en/LICENSE.txt) or under the
7
+ # MIT license (see MIT-LICENSE file).
8
+
9
+ require 'test/spec'
10
+
11
+ $: << "/home/chris/src/mocha-0.3.2/lib"
12
+
13
+ begin
14
+ require 'mocha'
15
+ rescue LoadError
16
+ context "mocha" do
17
+ specify "can not be found. BAIL OUT!" do
18
+ end
19
+ end
20
+ else
21
+
22
+ context "mocha" do
23
+ specify "works with test/spec" do
24
+ object = mock()
25
+ object.expects(:expected_method).with(:p1, :p2).returns(:result)
26
+ object.expected_method(:p1, :p2).should.equal :result
27
+ end
28
+ end
29
+
30
+ class Enterprise
31
+ def initialize(dilithium)
32
+ @dilithium = dilithium
33
+ end
34
+
35
+ def go(warp_factor)
36
+ warp_factor.times { @dilithium.nuke(:anti_matter) }
37
+ end
38
+ end
39
+
40
+ context "mocha" do
41
+ specify "works with test/spec and Enterprise example" do
42
+ dilithium = mock()
43
+ dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
44
+ enterprise = Enterprise.new(dilithium)
45
+ enterprise.go(2)
46
+ end
47
+ end
48
+
49
+ end # if not rescue LoadError
50
+
51
+
52
+ begin
53
+ require 'stubba'
54
+ rescue LoadError
55
+ context "stubba" do
56
+ specify "can not be found. BAIL OUT!" do
57
+ end
58
+ end
59
+ else
60
+
61
+ class Order
62
+ attr_accessor :shipped_on
63
+
64
+ def total_cost
65
+ line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
66
+ end
67
+
68
+ def total_weight
69
+ line_items.inject(0) { |total, line_item| total + line_item.weight }
70
+ end
71
+
72
+ def shipping_cost
73
+ total_weight * 5 + 10
74
+ end
75
+
76
+ class << self
77
+
78
+ def find_all
79
+ # Database.connection.execute('select * from orders...
80
+ end
81
+
82
+ def number_shipped_since(date)
83
+ find_all.select { |order| order.shipped_on > date }.size
84
+ end
85
+
86
+ def unshipped_value
87
+ find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ context "stubba" do
95
+ specify "works with test/spec and instance method stubbing" do
96
+ order = Order.new
97
+ order.stubs(:total_weight).returns(10)
98
+ order.shipping_cost.should.equal 60
99
+ end
100
+
101
+ specify "works with test/spec and class method stubbing" do
102
+ now = Time.now; week_in_secs = 7 * 24 * 60 * 60
103
+ order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
104
+ order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
105
+ Order.stubs(:find_all).returns([order_1, order_2])
106
+ Order.number_shipped_since(now - 2 * week_in_secs).should.equal 1
107
+ end
108
+
109
+ specify "works with test/spec and global instance method stubbing" do
110
+ Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
111
+ Order.any_instance.stubs(:shipped_on).returns(nil)
112
+ Order.any_instance.stubs(:total_cost).returns(10)
113
+ Order.unshipped_value.should.equal 30
114
+ end
115
+ end
116
+
117
+ end # if not rescue LoadError
118
+
@@ -0,0 +1,26 @@
1
+ require 'test/spec'
2
+
3
+ context "Empty context" do
4
+ # should.not.raise
5
+ end
6
+
7
+ context "Outer context" do
8
+ context "Inner context" do
9
+ specify "is nested" do
10
+ end
11
+ specify "has multiple empty specifications" do
12
+ end
13
+ end
14
+ context "Second Inner context" do
15
+ context "Inmost context" do
16
+ specify "works too!" do
17
+ end
18
+ specify "whoo!" do
19
+ end
20
+ end
21
+ specify "is indented properly" do
22
+ end
23
+ specify "still runs in order of definition" do
24
+ end
25
+ end
26
+ end