yard-doctest 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd5ed8e592d798085c06707ac39cc89772b80bca
4
- data.tar.gz: 026c3d35c873ddb3ee9c5847d5ca8d89cb46b599
3
+ metadata.gz: ca18f61aa4d2ab189d9e0edb48011a7423bf7a3c
4
+ data.tar.gz: 0a84c6dd74503b1abcfe4b5b494725f6af6800ec
5
5
  SHA512:
6
- metadata.gz: 98762b209916d0d4e5da1f62f30b5189cacb07ed56f3c066abb3f3b15f12db2b84f6d5b2123a7254ee914bb7e7cbd20ca75b88cf4f5b993c7bb9dd72808f599f
7
- data.tar.gz: 695cb4f162d40bafa601b1736fe8c9d2f639e3094ffbab6b2a7cd128bec756698741433c87a8ec13db42481665a57b682f3899acb39982e6c4a773bcb49362db
6
+ metadata.gz: 3ff564c3d96544f560d361c385330967844c19d7de10bca805673261b3f36ceb48dcf29b57e24efe83533ff1e360d9da15a4dd61967316ef13a42accdfa14d61
7
+ data.tar.gz: 63d7ab908736488e14cd50ecbdde1c272d8f25399b00b5b3d340afbe6b3ccc1c65d01ff1bd64542f311e6eb195b261f2e8eeb89b2f773efd2d4a4ffa06421e02
data/README.md CHANGED
@@ -98,11 +98,11 @@ $ bundle exec yard config -a autoload_plugins yard-doctest
98
98
  Next, you'll need to create test helper, which will be required before each of your test. Think about it as `spec_helper.rb` in RSpec or `env.rb` in Cucumber. You should require everything necessary for your examples to run there.
99
99
 
100
100
  ```bash
101
- $ touch yard-doctest_helper.rb
101
+ $ touch doctest_helper.rb
102
102
  ```
103
103
 
104
104
  ```ruby
105
- # yard-doctest_helper.rb
105
+ # doctest_helper.rb
106
106
  require 'lib/cat'
107
107
  require 'lib/dog'
108
108
  ```
@@ -195,6 +195,8 @@ It is actually delegated to amazing [minitest](https://github.com/seattlerb/mini
195
195
 
196
196
  ## Advanced usage
197
197
 
198
+ ### Test helper
199
+
198
200
  You can define any methods and instance variables in test helper and they will be available in examples.
199
201
 
200
202
  For example, if we change the examples for `Cat#can_hunt_dogs?` like that:
@@ -224,7 +226,7 @@ NameError: undefined local variable or method `cat' for Object:Class
224
226
  If you don't want to create new instance of class each time (or include module if you're testing it), you can fix this by defining a method in test helper:
225
227
 
226
228
  ```ruby
227
- # yard-doctest_helper.rb
229
+ # doctest_helper.rb
228
230
  require 'lib/cat'
229
231
  require 'lib/dog'
230
232
 
@@ -233,26 +235,58 @@ def cat
233
235
  end
234
236
  ```
235
237
 
238
+ ### Hooks
239
+
236
240
  In case you need to do some preparations/cleanup between tests, hooks are at your service to be defined in test helper:
237
241
 
238
242
  ```ruby
239
- YARD::Doctest.before do
240
- # this is called before each example and
241
- # evaluated in the same context as example
242
- # (i.e. has access to the same instance variables)
243
+ YARD::Doctest.configure do |doctest|
244
+ doctest.before do
245
+ # this is called before each example and
246
+ # evaluated in the same context as example
247
+ # (i.e. has access to the same instance variables)
248
+ end
249
+
250
+ doctest.after do
251
+ # same as `before`, but runs after each example
252
+ end
253
+
254
+ doctest.after_run do
255
+ # runs after all the examples and
256
+ # has different context
257
+ # (i.e. no access to instance variables)
258
+ end
243
259
  end
260
+ ```
261
+
262
+ There is also a way to limit hooks to specific tests based on class/method name:
244
263
 
245
- YARD::Doctest.after do
246
- # same as `before`, but runs after each example
264
+ ```ruby
265
+ YARD::Doctest.configure do |doctest|
266
+ doctest.before('MyClass') do
267
+ # this will only be called for doctests of `MyClass` class
268
+ # and all its methods (i.e. `MyClass.foo`, `MyClass#bar`)
269
+ end
270
+
271
+ doctest.after('MyClass#foo') do
272
+ # this will only be called for doctests of `MyClass#foo`
273
+ end
247
274
  end
275
+ ```
248
276
 
249
- YARD::Doctest.after_run do
250
- # runs after all the examples and
251
- # has different context
252
- # (i.e. no access to instance variables)
277
+ ### Skip
278
+
279
+ You can skip running some of the tests:
280
+
281
+ ```ruby
282
+ YARD::Doctest.configure do |doctest|
283
+ doctest.skip 'MyClass' # will skip doctests for `MyClass` and all its methods
284
+ doctest.skip 'MyClass#foo' # will skip doctests for `MyClass#foo`
253
285
  end
254
286
  ```
255
287
 
288
+ ### Rake
289
+
256
290
  There is also a Rake task for you:
257
291
 
258
292
  ```ruby
@@ -11,7 +11,7 @@ Feature: yard doctest
11
11
  Then the output should contain "doctest Doctests from @example tags"
12
12
 
13
13
  Scenario: looks for files in app/lib directories by default
14
- Given a file named "yard-doctest_helper.rb" with:
14
+ Given a file named "doctest_helper.rb" with:
15
15
  """
16
16
  require 'app/app'
17
17
  require 'lib/lib'
@@ -36,7 +36,7 @@ Feature: yard doctest
36
36
  Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
37
37
 
38
38
  Scenario Outline: looks for files only in passed glob
39
- Given a file named "yard-doctest_helper.rb" with:
39
+ Given a file named "doctest_helper.rb" with:
40
40
  """
41
41
  require 'app/app'
42
42
  require 'lib/lib'
@@ -68,7 +68,7 @@ Feature: yard doctest
68
68
  | app/app.rb |
69
69
 
70
70
  Scenario: generates test names from unit name
71
- Given a file named "yard-doctest_helper.rb" with:
71
+ Given a file named "doctest_helper.rb" with:
72
72
  """
73
73
  require 'app/app'
74
74
  """
@@ -109,7 +109,7 @@ Feature: yard doctest
109
109
  And the output should contain "B#div"
110
110
 
111
111
  Scenario: asserts using equality
112
- Given a file named "yard-doctest_helper.rb" with:
112
+ Given a file named "doctest_helper.rb" with:
113
113
  """
114
114
  require 'app/app'
115
115
  """
@@ -129,7 +129,7 @@ Feature: yard doctest
129
129
  """
130
130
 
131
131
  Scenario Outline: properly handles different return values
132
- Given a file named "yard-doctest_helper.rb" with:
132
+ Given a file named "doctest_helper.rb" with:
133
133
  """
134
134
  require 'app/app'
135
135
  """
@@ -159,7 +159,7 @@ Feature: yard doctest
159
159
  | 1.0 |
160
160
 
161
161
  Scenario: handles multiple @example tags
162
- Given a file named "yard-doctest_helper.rb" with:
162
+ Given a file named "doctest_helper.rb" with:
163
163
  """
164
164
  require 'app/app'
165
165
  """
@@ -177,7 +177,7 @@ Feature: yard doctest
177
177
  Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
178
178
 
179
179
  Scenario: handles multiple return comments
180
- Given a file named "yard-doctest_helper.rb" with:
180
+ Given a file named "doctest_helper.rb" with:
181
181
  """
182
182
  require 'app/app'
183
183
  """
@@ -195,7 +195,7 @@ Feature: yard doctest
195
195
  Then the output should contain "1 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
196
196
 
197
197
  Scenario: runs @example tags without return comment
198
- Given a file named "yard-doctest_helper.rb" with:
198
+ Given a file named "doctest_helper.rb" with:
199
199
  """
200
200
  require 'app/app'
201
201
  """
@@ -211,7 +211,7 @@ Feature: yard doctest
211
211
  Then the output should contain "1 runs, 0 assertions, 0 failures, 0 errors, 0 skips"
212
212
 
213
213
  Scenario: handles `# =>` return comment
214
- Given a file named "yard-doctest_helper.rb" with:
214
+ Given a file named "doctest_helper.rb" with:
215
215
  """
216
216
  require 'app/app'
217
217
  """
@@ -227,7 +227,7 @@ Feature: yard doctest
227
227
  Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
228
228
 
229
229
  Scenario: handles return comment on newline
230
- Given a file named "yard-doctest_helper.rb" with:
230
+ Given a file named "doctest_helper.rb" with:
231
231
  """
232
232
  require 'app/app'
233
233
  """
@@ -244,7 +244,7 @@ Feature: yard doctest
244
244
  Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
245
245
 
246
246
  Scenario: handles multiple lines
247
- Given a file named "yard-doctest_helper.rb" with:
247
+ Given a file named "doctest_helper.rb" with:
248
248
  """
249
249
  require 'app/app'
250
250
  """
@@ -263,7 +263,7 @@ Feature: yard doctest
263
263
  Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
264
264
 
265
265
  Scenario: names test with example title when it's present
266
- Given a file named "yard-doctest_helper.rb" with:
266
+ Given a file named "doctest_helper.rb" with:
267
267
  """
268
268
  require 'app/app'
269
269
  """
@@ -279,7 +279,7 @@ Feature: yard doctest
279
279
  Then the output should contain "#sum#test_0001_sums two numbers"
280
280
 
281
281
  Scenario: doesn't name test when title is not present
282
- Given a file named "yard-doctest_helper.rb" with:
282
+ Given a file named "doctest_helper.rb" with:
283
283
  """
284
284
  require 'app/app'
285
285
  """
@@ -295,7 +295,7 @@ Feature: yard doctest
295
295
  Then the output should contain "#sum#test_0001_"
296
296
 
297
297
  Scenario: adds unit definition to backtrace on failures
298
- Given a file named "yard-doctest_helper.rb" with:
298
+ Given a file named "doctest_helper.rb" with:
299
299
  """
300
300
  require 'app/app'
301
301
  """
@@ -311,7 +311,7 @@ Feature: yard doctest
311
311
  Then the output should contain "app/app.rb:3"
312
312
 
313
313
  Scenario: has rake task to run the tests
314
- Given a file named "yard-doctest_helper.rb" with:
314
+ Given a file named "doctest_helper.rb" with:
315
315
  """
316
316
  require 'app/app'
317
317
  """
@@ -335,7 +335,7 @@ Feature: yard doctest
335
335
  Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
336
336
 
337
337
  Scenario: requires doctest helper
338
- Given a file named "yard-doctest_helper.rb" with:
338
+ Given a file named "doctest_helper.rb" with:
339
339
  """
340
340
  require 'app/app'
341
341
 
@@ -359,7 +359,7 @@ Feature: yard doctest
359
359
  Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
360
360
 
361
361
  Scenario: shares binding between asserts
362
- Given a file named "yard-doctest_helper.rb" with:
362
+ Given a file named "doctest_helper.rb" with:
363
363
  """
364
364
  require 'app/app'
365
365
  """
@@ -378,7 +378,7 @@ Feature: yard doctest
378
378
  Then the output should contain "1 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
379
379
 
380
380
  Scenario: does not share binding between examples
381
- Given a file named "yard-doctest_helper.rb" with:
381
+ Given a file named "doctest_helper.rb" with:
382
382
  """
383
383
  require 'app/app'
384
384
  """
@@ -397,23 +397,67 @@ Feature: yard doctest
397
397
  When I run `bundle exec yard doctest`
398
398
  Then the output should contain "NameError: undefined local variable or method `a'"
399
399
 
400
- Scenario: supports hooks
401
- Given a file named "yard-doctest_helper.rb" with:
400
+ Scenario: supports global hooks
401
+ Given a file named "doctest_helper.rb" with:
402
402
  """
403
403
  require 'app/app'
404
404
 
405
- @flag = true
405
+ YARD::Doctest.before { @flag = false }
406
+ YARD::Doctest.after { @flag = true }
407
+ YARD::Doctest.after_run { puts 'Run after all by minitest' }
408
+ """
409
+ And a file named "app/app.rb" with:
410
+ """
411
+ # @example
412
+ # flag #=> false
413
+ def flag
414
+ @flag
415
+ end
416
+ """
417
+ When I run `bundle exec yard doctest`
418
+ Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
419
+ And the output should contain "Run after all by minitest"
420
+
421
+ Scenario: supports test-name hooks
422
+ Given a file named "doctest_helper.rb" with:
423
+ """
424
+ require 'app/app'
406
425
 
407
426
  YARD::Doctest.before do
427
+ @flag = true
428
+ @foo = true
429
+ end
430
+
431
+ YARD::Doctest.before('#flag') do
408
432
  @flag = false
433
+ @foo = false
434
+ end
435
+ """
436
+ And a file named "app/app.rb" with:
437
+ """
438
+ # @example
439
+ # flag #=> false
440
+ def flag
441
+ @flag && @foo
409
442
  end
410
443
 
411
- YARD::Doctest.after do
412
- @flag = true
444
+ # @example
445
+ # foo #=> true
446
+ def foo
447
+ @foo && @flag
413
448
  end
449
+ """
450
+ When I run `bundle exec yard doctest`
451
+ Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
414
452
 
415
- YARD::Doctest.after_run do
416
- puts 'Run after all by minitest'
453
+ Scenario: can skip tests
454
+ Given a file named "doctest_helper.rb" with:
455
+ """
456
+ require 'app/app'
457
+
458
+ YARD::Doctest.configure do |doctest|
459
+ doctest.skip '#flag'
460
+ doctest.skip 'A.foo'
417
461
  end
418
462
  """
419
463
  And a file named "app/app.rb" with:
@@ -423,7 +467,14 @@ Feature: yard doctest
423
467
  def flag
424
468
  @flag
425
469
  end
470
+
471
+ class A
472
+ # @example
473
+ # A.foo => true
474
+ def self.foo
475
+ true
476
+ end
477
+ end
426
478
  """
427
479
  When I run `bundle exec yard doctest`
428
- Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
429
- And the output should contain "Run after all by minitest"
480
+ Then the output should contain "0 runs, 0 assertions, 0 failures, 0 errors, 0 skips"
data/lib/yard-doctest.rb CHANGED
@@ -9,38 +9,79 @@ require 'yard/doctest/version'
9
9
 
10
10
  module YARD
11
11
  module Doctest
12
+ extend self
12
13
 
13
- class << self
14
- #
15
- # Passed block called before each example and
16
- # evaluated in the same context as example.
17
- #
18
- # @param [Proc] blk
19
- #
20
- def before(&blk)
21
- block_given? ? @before = blk : @before
22
- end
14
+ #
15
+ # Configures YARD doctest.
16
+ #
17
+ # @yield [self]
18
+ #
19
+ def configure
20
+ yield self
21
+ end
23
22
 
24
- #
25
- # Passed block called after each example and
26
- # evaluated in the same context as example.
27
- #
28
- # @param [Proc] blk
29
- #
30
- def after(&blk)
31
- block_given? ? @after = blk : @after
32
- end
23
+ #
24
+ # Passed block called before each example
25
+ # or specific tests based on passed name.
26
+ #
27
+ # It is evaluated in the same context as example.
28
+ #
29
+ # @param [String] test
30
+ # @param [Proc] blk
31
+ #
32
+ def before(test = nil, &blk)
33
+ hooks[:before] << {test: test, block: blk} if block_given?
34
+ end
35
+
36
+ #
37
+ # Passed block called after each example
38
+ # or specific tests based on passed name.
39
+ #
40
+ # It is evaluated in the same context as example.
41
+ #
42
+ # @param [String] test
43
+ # @param [Proc] blk
44
+ #
45
+ def after(test = nil, &blk)
46
+ hooks[:after] << {test: test, block: blk} if block_given?
47
+ end
48
+
49
+ #
50
+ # Passed block called after all examples and
51
+ # evaluated in the different context from examples.
52
+ #
53
+ # It actually just sends block to `Minitest.after_run`.
54
+ #
55
+ # @param [Proc] blk
56
+ #
57
+ def after_run(&blk)
58
+ Minitest.after_run &blk
59
+ end
60
+
61
+ #
62
+ # Adds definition of test to be skipped.
63
+ #
64
+ # @param [Array<String>] test
65
+ #
66
+ def skip(test)
67
+ skips << test
68
+ end
69
+
70
+ #
71
+ # Array of tests to be skipped.
72
+ # @api private
73
+ #
74
+ def skips
75
+ @skips ||= []
76
+ end
33
77
 
34
- #
35
- # Passed block called after all examples and
36
- # evaluated in the different context from examples.
37
- #
38
- # It actually just sends block to `Minitest.after_run`.
39
- #
40
- # @param [Proc] blk
41
- #
42
- def after_run(&blk)
43
- Minitest.after_run &blk
78
+ #
79
+ # Returns hash with arrays of before/after hooks.
80
+ # @api private
81
+ #
82
+ def hooks
83
+ @hooks ||= {}.tap do |hash|
84
+ hash[:before], hash[:after] = [], []
44
85
  end
45
86
  end
46
87
 
@@ -21,23 +21,24 @@ module YARD
21
21
 
22
22
  Class.new(this.class).class_eval do
23
23
  require 'minitest/autorun'
24
- require 'yard-doctest_helper'
24
+ require 'doctest_helper'
25
25
 
26
- describe this.definition do
27
- before { evaluate YARD::Doctest.before } if YARD::Doctest.before.is_a?(Proc)
28
- after { evaluate YARD::Doctest.after } if YARD::Doctest.after.is_a?(Proc)
26
+ unless YARD::Doctest.skips.any? { |skip| this.definition.include?(skip) }
27
+ describe this.definition do
28
+ register_hooks(this.definition, YARD::Doctest.hooks)
29
29
 
30
- it this.name do
31
- this.asserts.each do |assert|
32
- expected, actual = assert[:expected], assert[:actual]
33
- actual = context.eval(actual)
30
+ it this.name do
31
+ this.asserts.each do |assert|
32
+ expected, actual = assert[:expected], assert[:actual]
33
+ actual = context.eval(actual)
34
34
 
35
- unless expected.empty?
36
- begin
37
- assert_equal evaluate(expected), actual
38
- rescue Minitest::Assertion => error
39
- add_filepath_to_backtrace(error, this.filepath)
40
- raise error
35
+ unless expected.empty?
36
+ begin
37
+ assert_equal evaluate(expected), actual
38
+ rescue Minitest::Assertion => error
39
+ add_filepath_to_backtrace(error, this.filepath)
40
+ raise error
41
+ end
41
42
  end
42
43
  end
43
44
  end
@@ -59,12 +60,28 @@ module YARD
59
60
 
60
61
  def add_filepath_to_backtrace(exception, filepath)
61
62
  backtrace = exception.backtrace
62
- line = backtrace.find { |line| line =~ %r(lib/yard/doctest/example) }
63
+ line = backtrace.find { |l| l =~ %r(lib/yard/doctest/example) }
63
64
  index = backtrace.index(line)
64
65
  backtrace = backtrace.insert(index, filepath)
65
66
  exception.set_backtrace backtrace
66
67
  end
67
68
 
69
+ def self.register_hooks(definition, all_hooks)
70
+ all_hooks.each do |type, hooks|
71
+ hooks.each do |hook|
72
+ if hook[:test]
73
+ # test-name hooks
74
+ if definition.include?(hook[:test])
75
+ send(type) { evaluate(hook[:block]) }
76
+ end
77
+ else
78
+ # global hooks
79
+ send(type) { evaluate(hook[:block]) }
80
+ end
81
+ end
82
+ end
83
+ end
84
+
68
85
  end # Example
69
86
  end # Doctest
70
87
  end # YARD
@@ -1,5 +1,5 @@
1
1
  module YARD
2
2
  module Doctest
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yard-doctest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rodionov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-17 00:00:00.000000000 Z
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard