dkubb-yardstick 0.0.1 → 0.1.0

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.
@@ -0,0 +1,247 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.join('..', '..', 'spec_helper')
3
+
4
+ describe Yardstick::MeasurementSet do
5
+ before do
6
+ YARD.parse_string('def test; end')
7
+
8
+ @description = 'test measurement'
9
+ @docstring = YARD::Registry.all(:method).first.docstring
10
+
11
+ @measurement = Yardstick::Measurement.new(@description, @docstring) { true }
12
+ @measurements = Yardstick::MeasurementSet.new
13
+ end
14
+
15
+ it 'should be Enumerable' do
16
+ Yardstick::MeasurementSet.new.should be_kind_of(Enumerable)
17
+ end
18
+
19
+ describe '.new' do
20
+ describe 'with no arguments' do
21
+ before do
22
+ @measurements = Yardstick::MeasurementSet.new
23
+ end
24
+
25
+ it 'should return a MeasurementSet' do
26
+ @measurements.should be_kind_of(Yardstick::MeasurementSet)
27
+ end
28
+
29
+ it 'should be empty' do
30
+ @measurements.should be_empty
31
+ end
32
+ end
33
+
34
+ describe 'with Measurements' do
35
+ before do
36
+ @measurements = Yardstick::MeasurementSet.new([ @measurement ])
37
+ end
38
+
39
+ it 'should return a MeasurementSet' do
40
+ @measurements.should be_kind_of(Yardstick::MeasurementSet)
41
+ end
42
+
43
+ it 'should include the Measurements' do
44
+ @measurements.should include(@measurement)
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#<<' do
50
+ describe 'with a new Measurement' do
51
+ before do
52
+ @response = @measurements << @measurement
53
+ end
54
+
55
+ it 'should return self' do
56
+ @response.should be_equal(@measurements)
57
+ end
58
+
59
+ it 'should append the Measurement' do
60
+ @measurements.to_a.last.should equal(@measurement)
61
+ end
62
+ end
63
+
64
+ describe 'with an equivalent Measurement' do
65
+ before do
66
+ @measurements << @measurement
67
+ @measurements.to_a.should == [ @measurement ]
68
+
69
+ @response = @measurements << Yardstick::Measurement.new(@description, @docstring) { true }
70
+ end
71
+
72
+ it 'should return self' do
73
+ @response.should be_equal(@measurements)
74
+ end
75
+
76
+ it 'should not append the Measurement again' do
77
+ @measurements.to_a.should == [ @measurement ]
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#merge' do
83
+ before do
84
+ @other = Yardstick::MeasurementSet.new
85
+ @other << @measurement
86
+
87
+ @response = @measurements.merge(@other)
88
+ end
89
+
90
+ it 'should return self' do
91
+ @response.should be_equal(@measurements)
92
+ end
93
+
94
+ it 'should merge the other Measurements' do
95
+ @measurements.should include(*@other)
96
+ end
97
+ end
98
+
99
+ describe '#each' do
100
+ before do
101
+ @measurements << @measurement
102
+
103
+ @yield = []
104
+
105
+ @response = @measurements.each { |*args| @yield << args }
106
+ end
107
+
108
+ it 'should return self' do
109
+ @response.should be_equal(@measurements)
110
+ end
111
+
112
+ it 'should yield measurements' do
113
+ @yield.should eql([ [ @measurement ] ])
114
+ end
115
+ end
116
+
117
+ describe '#empty?' do
118
+ describe 'when there are no measurements' do
119
+ it 'should return true' do
120
+ @measurements.empty?.should be_true
121
+ end
122
+ end
123
+
124
+ describe 'when there are measurements' do
125
+ before do
126
+ @measurements << @measurement
127
+ end
128
+
129
+ it 'should return false' do
130
+ @measurements.empty?.should be_false
131
+ end
132
+ end
133
+ end
134
+
135
+ describe '#include?' do
136
+ describe 'when provided an included measurement' do
137
+ before do
138
+ @measurements << @measurement
139
+
140
+ @response = @measurements.include?(@measurement)
141
+ end
142
+
143
+ it 'should return true' do
144
+ @response.should be_true
145
+ end
146
+ end
147
+
148
+ describe 'when provided an excluded measurement' do
149
+ before do
150
+ @response = @measurements.include?(@measurement)
151
+ end
152
+
153
+ it 'should return false' do
154
+ @response.should be_false
155
+ end
156
+ end
157
+ end
158
+
159
+ describe '#index' do
160
+ describe 'when provided an included measurement' do
161
+ before do
162
+ @measurements << @measurement
163
+
164
+ @response = @measurements.index(@measurement)
165
+ end
166
+
167
+ it 'should return the index' do
168
+ @response.should eql(0)
169
+ end
170
+ end
171
+
172
+ describe 'when provided an excluded measurement' do
173
+ before do
174
+ @response = @measurements.index(@measurement)
175
+ end
176
+
177
+ it 'should return nil' do
178
+ @response.should == nil
179
+ end
180
+ end
181
+ end
182
+
183
+ describe '#total' do
184
+ before do
185
+ @measurements << @measurement
186
+ end
187
+
188
+ it 'should return the number of total measurements' do
189
+ @measurements.total.should eql(1)
190
+ end
191
+ end
192
+
193
+ describe '#successful' do
194
+ before do
195
+ @measurements << @measurement
196
+ end
197
+
198
+ it 'should return the number of successful measurements' do
199
+ @measurements.successful.should eql(1)
200
+ end
201
+ end
202
+
203
+ describe '#failed' do
204
+ before do
205
+ @measurements << @measurement
206
+ end
207
+
208
+ it 'should return the number of failed measurements' do
209
+ @measurements.failed.should eql(0)
210
+ end
211
+ end
212
+
213
+ describe '#coverage' do
214
+ describe 'when there are no measurements' do
215
+ it 'should return 0' do
216
+ @measurements.coverage.should eql(0)
217
+ end
218
+ end
219
+
220
+ describe 'when there are measurements' do
221
+ before do
222
+ @response = @measurements << @measurement
223
+ end
224
+
225
+ it 'should return a Rational' do
226
+ @measurements.coverage.should be_kind_of(Rational)
227
+ end
228
+
229
+ it 'should return the expected value' do
230
+ @measurements.coverage.should == 1
231
+ end
232
+ end
233
+ end
234
+
235
+ describe '#warn' do
236
+ before do
237
+ @measurements << Yardstick::Measurement.new(@description, @docstring) { false }
238
+
239
+ capture_stderr { @measurements.warn }
240
+ end
241
+
242
+ it 'should output the summary' do
243
+ @output.should == "(stdin):1: #test: test measurement\n" \
244
+ "\nCoverage: 0.0% Success: 0 Failed: 1 Total: 1\n"
245
+ end
246
+ end
247
+ end
@@ -2,55 +2,61 @@ require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path.join('..', '..', 'spec_helper')
3
3
 
4
4
  shared_examples_for 'measurement is successful' do
5
- before :all do
5
+ before do
6
6
  @measurement = Yardstick::Measurement.new('successful', @docstring) { true }
7
7
  end
8
8
  end
9
9
 
10
10
  shared_examples_for 'measurement is skipped' do
11
- before :all do
11
+ before do
12
12
  @measurement = Yardstick::Measurement.new('skipped', @docstring) { skip }
13
13
  end
14
14
  end
15
15
 
16
16
  shared_examples_for 'measurement is not successful' do
17
- before :all do
17
+ before do
18
18
  @measurement = Yardstick::Measurement.new('not successful', @docstring) { false }
19
19
  end
20
20
  end
21
21
 
22
22
  shared_examples_for 'measurement is not implemented' do
23
- before :all do
23
+ before do
24
24
  @measurement = Yardstick::Measurement.new('not implemented', @docstring) { todo }
25
25
  end
26
26
  end
27
27
 
28
28
  shared_examples_for 'measurement is warned' do
29
- before :all do
30
- $stderr = StringIO.new
31
- @response = @measurement.warn
32
- $stderr.rewind
33
- @output = $stderr.read
34
- end
35
-
36
- it 'should return nil' do
37
- @response.should be_nil
29
+ before do
30
+ capture_stderr { @measurement.warn }
38
31
  end
39
32
  end
40
33
 
41
34
  describe Yardstick::Measurement do
42
- before :all do
35
+ before do
43
36
  YARD.parse_string('def test; end')
37
+
44
38
  @docstring = YARD::Registry.all(:method).first.docstring
45
39
  end
46
40
 
47
41
  describe '.new' do
48
- before :all do
49
- @response = Yardstick::Measurement.new('test measurement', @docstring) { true }
42
+ before do
43
+ @measurement = Yardstick::Measurement.new('test measurement', @docstring) { true }
50
44
  end
51
45
 
52
46
  it 'should return a Measurement' do
53
- @response.should be_kind_of(Yardstick::Measurement)
47
+ @measurement.should be_kind_of(Yardstick::Measurement)
48
+ end
49
+ end
50
+
51
+ describe '#description' do
52
+ before do
53
+ @description = 'test measurement'
54
+
55
+ @measurement = Yardstick::Measurement.new(@description, @docstring) { true }
56
+ end
57
+
58
+ it 'should return the expected description' do
59
+ @measurement.description.should equal(@description)
54
60
  end
55
61
  end
56
62
 
@@ -1,6 +1,356 @@
1
1
  require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path.join('..', '..', 'spec_helper')
3
3
 
4
+ shared_examples_for 'method is measured' do
5
+ before do
6
+ @measurements = docstring.measure
7
+ end
8
+
9
+ it 'should return a MeasurementSet' do
10
+ @measurements.should be_kind_of(Yardstick::MeasurementSet)
11
+ end
12
+ end
13
+
14
+ shared_examples_for 'a valid method' do
15
+ before do
16
+ YARD.parse_string(<<-RUBY)
17
+ # This is a method summary that is the maximum --- exactly 80 characters in length
18
+ #
19
+ # @example
20
+ # test('Hello World') # => nil
21
+ #
22
+ # @param [#to_str] value
23
+ # the value
24
+ #
25
+ # @return [nil]
26
+ # returns nil
27
+ #
28
+ # @api public
29
+ def test(value)
30
+ end
31
+ RUBY
32
+ end
33
+ end
34
+
4
35
  describe Yardstick::Method do
5
- it 'should be awesome'
36
+ def docstring
37
+ YARD::Registry.all(:method).first.docstring
38
+ end
39
+
40
+ describe '#measure' do
41
+ describe 'with a method summary' do
42
+ it_should_behave_like 'a valid method'
43
+ it_should_behave_like 'method is measured'
44
+
45
+ it 'should have a correct measurement' do
46
+ @measurements.detect { |measurement| measurement.description == 'The method summary should be specified' }.should be_ok
47
+ end
48
+ end
49
+
50
+ describe 'without a method summary' do
51
+ before do
52
+ YARD.parse_string('def test(value); end')
53
+ end
54
+
55
+ it_should_behave_like 'method is measured'
56
+
57
+ it 'should have an incorrect measurement' do
58
+ @measurements.detect { |measurement| measurement.description == 'The method summary should be specified' }.should_not be_ok
59
+ end
60
+ end
61
+
62
+ describe 'with a method summary that is 80 characters in length' do
63
+ it_should_behave_like 'a valid method'
64
+ it_should_behave_like 'method is measured'
65
+
66
+ it 'should have a correct measurement' do
67
+ @measurements.detect { |measurement| measurement.description == 'The method summary should be less than 80 characters in length' }.should be_ok
68
+ end
69
+ end
70
+
71
+ describe 'with a method summary that is 81 characters in length' do
72
+ before do
73
+ YARD.parse_string(<<-RUBY)
74
+ # This is a method summary greater than the maximum - it is 81 characters in length
75
+ def test(value)
76
+ end
77
+ RUBY
78
+ end
79
+
80
+ it_should_behave_like 'method is measured'
81
+
82
+ it 'should have an incorrect measurement' do
83
+ @measurements.detect { |measurement| measurement.description == 'The method summary should be less than 80 characters in length' }.should_not be_ok
84
+ end
85
+ end
86
+
87
+ describe 'with a method summary that does not end in a period' do
88
+ it_should_behave_like 'a valid method'
89
+ it_should_behave_like 'method is measured'
90
+
91
+ it 'should have a correct measurement' do
92
+ @measurements.detect { |measurement| measurement.description == 'The method summary should not end in a period' }.should be_ok
93
+ end
94
+ end
95
+
96
+ describe 'with a method summary that does end in a period' do
97
+ before do
98
+ YARD.parse_string(<<-RUBY)
99
+ # This method summary ends in a period.
100
+ def test(value)
101
+ end
102
+ RUBY
103
+ end
104
+
105
+ it_should_behave_like 'method is measured'
106
+
107
+ it 'should have an incorrect measurement' do
108
+ @measurements.detect { |measurement| measurement.description == 'The method summary should not end in a period' }.should_not be_ok
109
+ end
110
+ end
111
+
112
+ describe 'with a method summary that is on one line' do
113
+ it_should_behave_like 'a valid method'
114
+ it_should_behave_like 'method is measured'
115
+
116
+ it 'should have a correct measurement' do
117
+ @measurements.detect { |measurement| measurement.description == 'The method summary should be a single line' }.should be_ok
118
+ end
119
+ end
120
+
121
+ describe 'with a method summary that is not on one line' do
122
+ before do
123
+ YARD.parse_string(<<-RUBY)
124
+ # This method summary
125
+ # is on two lines
126
+ def test(value)
127
+ end
128
+ RUBY
129
+ end
130
+
131
+ it_should_behave_like 'method is measured'
132
+
133
+ it 'should have an incorrect measurement' do
134
+ @measurements.detect { |measurement| measurement.description == 'The method summary should be a single line' }.should_not be_ok
135
+ end
136
+ end
137
+
138
+ describe 'with a method that has an @example tag' do
139
+ it_should_behave_like 'a valid method'
140
+ it_should_behave_like 'method is measured'
141
+
142
+ it 'should have a correct measurement' do
143
+ @measurements.detect { |measurement| measurement.description == 'The public/semipublic method should have an example specified' }.should be_ok
144
+ end
145
+ end
146
+
147
+ describe 'with a method that is private' do
148
+ before do
149
+ YARD.parse_string(<<-RUBY)
150
+ # @api private
151
+ def test(value)
152
+ end
153
+ RUBY
154
+ end
155
+
156
+ it_should_behave_like 'method is measured'
157
+
158
+ it 'should be skipped' do
159
+ @measurements.detect { |measurement| measurement.description == 'The public/semipublic method should have an example specified' }.should be_skip
160
+ end
161
+ end
162
+
163
+ describe 'with a method that does not have an @example tag, and has an undefined @return tag' do
164
+ before do
165
+ YARD.parse_string(<<-RUBY)
166
+ # @return [undefined]
167
+ #
168
+ # @api public
169
+ def test(value)
170
+ end
171
+ RUBY
172
+ end
173
+
174
+ it_should_behave_like 'method is measured'
175
+
176
+ it 'should be skipped' do
177
+ @measurements.detect { |measurement| measurement.description == 'The public/semipublic method should have an example specified' }.should be_skip
178
+ end
179
+ end
180
+
181
+ describe 'with a method that has an @api tag' do
182
+ it_should_behave_like 'a valid method'
183
+ it_should_behave_like 'method is measured'
184
+
185
+ it 'should have a correct measurement' do
186
+ @measurements.detect { |measurement| measurement.description == 'The @api tag should be specified' }.should be_ok
187
+ end
188
+ end
189
+
190
+ describe 'with a method that does not have an @api tag' do
191
+ before do
192
+ YARD.parse_string('def test(value); end')
193
+ end
194
+
195
+ it_should_behave_like 'method is measured'
196
+
197
+ it 'should have an incorrect measurement' do
198
+ @measurements.detect { |measurement| measurement.description == 'The @api tag should be specified' }.should_not be_ok
199
+ end
200
+ end
201
+
202
+ describe 'with a method that has a public @api tag' do
203
+ it_should_behave_like 'a valid method'
204
+ it_should_behave_like 'method is measured'
205
+
206
+ it 'should have a correct measurement' do
207
+ @measurements.detect { |measurement| measurement.description == 'The @api tag must be either public, semipublic or private' }.should be_ok
208
+ end
209
+ end
210
+
211
+ describe 'with a method that has an invalid @api tag' do
212
+ before do
213
+ YARD.parse_string(<<-RUBY)
214
+ # @api invalid
215
+ def test(value)
216
+ end
217
+ RUBY
218
+ end
219
+
220
+ it_should_behave_like 'method is measured'
221
+
222
+ it 'should have an incorrect measurement' do
223
+ @measurements.detect { |measurement| measurement.description == 'The @api tag must be either public, semipublic or private' }.should_not be_ok
224
+ end
225
+ end
226
+
227
+ describe 'with a protected method and a semipublic @api tag' do
228
+ before do
229
+ YARD.parse_string(<<-RUBY)
230
+ protected
231
+
232
+ # @api semipublic
233
+ def test(value)
234
+ end
235
+ RUBY
236
+ end
237
+
238
+ it_should_behave_like 'method is measured'
239
+
240
+ it 'should have a correct measurement' do
241
+ @measurements.detect { |measurement| measurement.description == 'A method with protected visibility must have an @api tag of semipublic or private' }.should be_ok
242
+ end
243
+ end
244
+
245
+ describe 'with a protected method and a private @api tag' do
246
+ before do
247
+ YARD.parse_string(<<-RUBY)
248
+ protected
249
+
250
+ # @api private
251
+ def test(value)
252
+ end
253
+ RUBY
254
+ end
255
+
256
+ it_should_behave_like 'method is measured'
257
+
258
+ it 'should have a correct measurement' do
259
+ @measurements.detect { |measurement| measurement.description == 'A method with protected visibility must have an @api tag of semipublic or private' }.should be_ok
260
+ end
261
+ end
262
+
263
+ describe 'with a protected method and a public @api tag' do
264
+ before do
265
+ YARD.parse_string(<<-RUBY)
266
+ protected
267
+
268
+ # @api public
269
+ def test(value)
270
+ end
271
+ RUBY
272
+ end
273
+
274
+ it_should_behave_like 'method is measured'
275
+
276
+ it 'should have an incorrect measurement' do
277
+ @measurements.detect { |measurement| measurement.description == 'A method with protected visibility must have an @api tag of semipublic or private' }.should_not be_ok
278
+ end
279
+ end
280
+
281
+ describe 'with a private method and a private @api tag' do
282
+ before do
283
+ YARD.parse_string(<<-RUBY)
284
+ private
285
+
286
+ # @api private
287
+ def test(value)
288
+ end
289
+ RUBY
290
+ end
291
+
292
+ it_should_behave_like 'method is measured'
293
+
294
+ it 'should have a correct measurement' do
295
+ @measurements.detect { |measurement| measurement.description == 'A method with private visibility must have an @api tag of private' }.should be_ok
296
+ end
297
+ end
298
+
299
+ describe 'with a private method and a public @api tag' do
300
+ before do
301
+ YARD.parse_string(<<-RUBY)
302
+ private
303
+
304
+ # @api public
305
+ def test(value)
306
+ end
307
+ RUBY
308
+ end
309
+
310
+ it_should_behave_like 'method is measured'
311
+
312
+ it 'should have an incorrect measurement' do
313
+ @measurements.detect { |measurement| measurement.description == 'A method with private visibility must have an @api tag of private' }.should_not be_ok
314
+ end
315
+ end
316
+
317
+ describe 'with a private method and a semipublic @api tag' do
318
+ before do
319
+ YARD.parse_string(<<-RUBY)
320
+ private
321
+
322
+ # @api semipublic
323
+ def test(value)
324
+ end
325
+ RUBY
326
+ end
327
+
328
+ it_should_behave_like 'method is measured'
329
+
330
+ it 'should have an incorrect measurement' do
331
+ @measurements.detect { |measurement| measurement.description == 'A method with private visibility must have an @api tag of private' }.should_not be_ok
332
+ end
333
+ end
334
+
335
+ describe 'with a method that has a @return tag' do
336
+ it_should_behave_like 'a valid method'
337
+ it_should_behave_like 'method is measured'
338
+
339
+ it 'should have a correct measurement' do
340
+ @measurements.detect { |measurement| measurement.description == 'The @return tag should be specified' }.should be_ok
341
+ end
342
+ end
343
+
344
+ describe 'with a method that does not have a @return tag' do
345
+ before do
346
+ YARD.parse_string('def test(value); end')
347
+ end
348
+
349
+ it_should_behave_like 'method is measured'
350
+
351
+ it 'should have an incorrect measurement' do
352
+ @measurements.detect { |measurement| measurement.description == 'The @return tag should be specified' }.should_not be_ok
353
+ end
354
+ end
355
+ end
6
356
  end