yard-doctest 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/LICENSE.txt +1 -1
- data/README.md +23 -1
- data/features/yard-doctest.feature +21 -3
- data/lib/yard/doctest/example.rb +20 -27
- data/lib/yard/doctest/version.rb +1 -1
- data/lib/yard-doctest.rb +2 -2
- data/yard-doctest.gemspec +0 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 216ca29f83eef3428bc20ff8a2ca765b605e8428
|
4
|
+
data.tar.gz: 4c821cbbd5d8c861c365fb57d6defa9f99726002
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 831c02685ac564f579baeb4ecde9d6994e7ee09a34db02556bcac5f3ee799eda2c816905a524a659602d1316b5a3bc4c9b18a0a89f1502add3a9dce86e2a11db
|
7
|
+
data.tar.gz: 360322c342fb9d2b567c8d250968a3087b5a7f4ee2e5a4270ce2ee6de6161d8ad0fce80fb37557e502376ca9a9ca92884666bf9e4b6f6a628315118d8028f8d4
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# yard-doctest [![Gem Version](https://badge.fury.io/rb/yard-doctest.
|
1
|
+
# yard-doctest [![Gem Version](https://badge.fury.io/rb/yard-doctest.svg)](http://badge.fury.io/rb/yard-doctest) [![Build Status](https://travis-ci.org/p0deje/yard-doctest.png?branch=master)](https://travis-ci.org/p0deje/yard-doctest)
|
2
2
|
|
3
3
|
Have you ever wanted to turn your amazing code examples into something that really make sense, is always up-to-date and bullet-proof? Were looking at an amazing [Python doctest](https://docs.python.org/3/library/doctest.html)? Well, look no longer!
|
4
4
|
|
@@ -99,6 +99,8 @@ Next, you'll need to create test helper, which will be required before each of y
|
|
99
99
|
|
100
100
|
```bash
|
101
101
|
$ touch doctest_helper.rb
|
102
|
+
# or if you don't want to have it in root
|
103
|
+
$ touch support/doctest_helper.rb
|
102
104
|
```
|
103
105
|
|
104
106
|
```ruby
|
@@ -195,6 +197,22 @@ It is actually delegated to amazing [minitest](https://github.com/seattlerb/mini
|
|
195
197
|
|
196
198
|
## Advanced usage
|
197
199
|
|
200
|
+
### Exceptions
|
201
|
+
|
202
|
+
If you want to use example that raises exception, this can be achieved by specifying the correct expected value:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
class Calculator
|
206
|
+
# @example
|
207
|
+
# divide(1, 0) #=> raise ZeroDivisionError, "divided by 0"
|
208
|
+
def divide(one, two)
|
209
|
+
one / two
|
210
|
+
end
|
211
|
+
end
|
212
|
+
```
|
213
|
+
|
214
|
+
The comparison of raised exceptions is being done by string containing the class and message of exceptions. With that said, you have to use the same message in expected value as the one that is used in actual.
|
215
|
+
|
198
216
|
### Test helper
|
199
217
|
|
200
218
|
You can define any methods and instance variables in test helper and they will be available in examples.
|
@@ -302,6 +320,10 @@ end
|
|
302
320
|
$ bundle exec rake yard:doctest
|
303
321
|
```
|
304
322
|
|
323
|
+
## Is it really used?
|
324
|
+
|
325
|
+
Well, yeah. A great example of using yard-doctest is [watir-webdriver](https://github.com/watir/watir-webdriver).
|
326
|
+
|
305
327
|
## Testing
|
306
328
|
|
307
329
|
There are some system tests implemented with [Aruba](https://github.com/cucumber/aruba):
|
@@ -128,6 +128,22 @@ Feature: yard doctest
|
|
128
128
|
Actual: "2"
|
129
129
|
"""
|
130
130
|
|
131
|
+
Scenario: asserts exceptions
|
132
|
+
Given a file named "doctest_helper.rb" with:
|
133
|
+
"""
|
134
|
+
require 'app/app'
|
135
|
+
"""
|
136
|
+
And a file named "app/app.rb" with:
|
137
|
+
"""
|
138
|
+
# @example
|
139
|
+
# divide(1, 0) #=> raise ZeroDivisionError, "divided by 0"
|
140
|
+
def divide(one, two)
|
141
|
+
one / two
|
142
|
+
end
|
143
|
+
"""
|
144
|
+
When I run `bundle exec yard doctest`
|
145
|
+
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
146
|
+
|
131
147
|
Scenario Outline: properly handles different return values
|
132
148
|
Given a file named "doctest_helper.rb" with:
|
133
149
|
"""
|
@@ -428,9 +444,11 @@ Feature: yard doctest
|
|
428
444
|
"""
|
429
445
|
require 'app/app'
|
430
446
|
|
431
|
-
YARD::Doctest.
|
432
|
-
|
433
|
-
|
447
|
+
YARD::Doctest.configure do |doctest|
|
448
|
+
doctest.before { @flag = false }
|
449
|
+
doctest.after { @flag = true }
|
450
|
+
doctest.after_run { puts 'Run after all by minitest' }
|
451
|
+
end
|
434
452
|
"""
|
435
453
|
And a file named "app/app.rb" with:
|
436
454
|
"""
|
data/lib/yard/doctest/example.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'sourcify'
|
2
|
-
|
3
1
|
module YARD
|
4
2
|
module Doctest
|
5
3
|
class Example < ::Minitest::Spec
|
@@ -21,29 +19,25 @@ module YARD
|
|
21
19
|
|
22
20
|
Class.new(this.class).class_eval do
|
23
21
|
require 'minitest/autorun'
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
require 'support/doctest_helper'
|
22
|
+
|
23
|
+
%w[. support].each do |dir|
|
24
|
+
require "#{dir}/doctest_helper" if File.exist?("#{dir}/doctest_helper.rb")
|
28
25
|
end
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
return if YARD::Doctest.skips.any? { |skip| this.definition.include?(skip) }
|
28
|
+
describe this.definition do
|
29
|
+
register_hooks(this.definition, YARD::Doctest.hooks)
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
it this.name do
|
32
|
+
this.asserts.each do |assert|
|
33
|
+
expected, actual = assert[:expected], assert[:actual]
|
34
|
+
next if expected.empty?
|
38
35
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
raise error
|
45
|
-
end
|
46
|
-
end
|
36
|
+
begin
|
37
|
+
assert_equal evaluate(expected), evaluate(actual)
|
38
|
+
rescue Minitest::Assertion => error
|
39
|
+
add_filepath_to_backtrace(error, this.filepath)
|
40
|
+
raise error
|
47
41
|
end
|
48
42
|
end
|
49
43
|
end
|
@@ -54,8 +48,9 @@ module YARD
|
|
54
48
|
protected
|
55
49
|
|
56
50
|
def evaluate(code)
|
57
|
-
code = code.to_source(strip_enclosure: true) if code.is_a?(Proc)
|
58
51
|
context.eval(code)
|
52
|
+
rescue StandardError => error
|
53
|
+
"#<#{error.class}: #{error}>"
|
59
54
|
end
|
60
55
|
|
61
56
|
def context
|
@@ -64,7 +59,7 @@ module YARD
|
|
64
59
|
|
65
60
|
def add_filepath_to_backtrace(exception, filepath)
|
66
61
|
backtrace = exception.backtrace
|
67
|
-
line = backtrace.find { |l| l =~ %r
|
62
|
+
line = backtrace.find { |l| l =~ %r{lib/yard/doctest/example} }
|
68
63
|
index = backtrace.index(line)
|
69
64
|
backtrace = backtrace.insert(index, filepath)
|
70
65
|
exception.set_backtrace backtrace
|
@@ -75,12 +70,10 @@ module YARD
|
|
75
70
|
hooks.each do |hook|
|
76
71
|
if hook[:test]
|
77
72
|
# test-name hooks
|
78
|
-
if definition.include?(hook[:test])
|
79
|
-
send(type) { evaluate(hook[:block]) }
|
80
|
-
end
|
73
|
+
send(type, &hook[:block]) if definition.include?(hook[:test])
|
81
74
|
else
|
82
75
|
# global hooks
|
83
|
-
send(type
|
76
|
+
send(type, &hook[:block])
|
84
77
|
end
|
85
78
|
end
|
86
79
|
end
|
data/lib/yard/doctest/version.rb
CHANGED
data/lib/yard-doctest.rb
CHANGED
@@ -30,7 +30,7 @@ module YARD
|
|
30
30
|
# @param [Proc] blk
|
31
31
|
#
|
32
32
|
def before(test = nil, &blk)
|
33
|
-
hooks[:before] << {test: test, block: blk}
|
33
|
+
hooks[:before] << {test: test, block: blk}
|
34
34
|
end
|
35
35
|
|
36
36
|
#
|
@@ -43,7 +43,7 @@ module YARD
|
|
43
43
|
# @param [Proc] blk
|
44
44
|
#
|
45
45
|
def after(test = nil, &blk)
|
46
|
-
hooks[:after] << {test: test, block: blk}
|
46
|
+
hooks[:after] << {test: test, block: blk}
|
47
47
|
end
|
48
48
|
|
49
49
|
#
|
data/yard-doctest.gemspec
CHANGED
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.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rodionov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: sourcify
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: bundler
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|