yard-doctest 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eaf710b790298916a7621aa89f1ccfc8f958d4a8
4
- data.tar.gz: 3170484780f834944b8b2b23b4fd845d96b3f83b
3
+ metadata.gz: cd5ed8e592d798085c06707ac39cc89772b80bca
4
+ data.tar.gz: 026c3d35c873ddb3ee9c5847d5ca8d89cb46b599
5
5
  SHA512:
6
- metadata.gz: 39ad0fa507a041b04f5d040a5609f1269ac38c8780cd3e4868a5a4d608babeafb289857b09ada303ddd0ed414e34af82fde8b8a253483493c2f422d012448441
7
- data.tar.gz: 5a3d8827d8da6d84f690f51fb1374fb42171254b320a20db767bbbefe7b45cb355178331dbbb8c026fd826dc5c3f82833b2ba5ae18617d88e0b5687e8a26f461
6
+ metadata.gz: 98762b209916d0d4e5da1f62f30b5189cacb07ed56f3c066abb3f3b15f12db2b84f6d5b2123a7254ee914bb7e7cbd20ca75b88cf4f5b993c7bb9dd72808f599f
7
+ data.tar.gz: 695cb4f162d40bafa601b1736fe8c9d2f639e3094ffbab6b2a7cd128bec756698741433c87a8ec13db42481665a57b682f3899acb39982e6c4a773bcb49362db
data/README.md CHANGED
@@ -9,7 +9,7 @@ Meet `YARD::Doctest` - simple and magical gem, which automatically parses your `
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'yardoctest'
12
+ gem 'yard-doctest'
13
13
  ```
14
14
 
15
15
  And then execute:
data/lib/yard-doctest.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'yard'
2
2
  require 'minitest'
3
+ require 'minitest/spec'
3
4
 
4
5
  require 'yard/cli/doctest'
5
6
  require 'yard/doctest/example'
@@ -10,14 +11,34 @@ module YARD
10
11
  module Doctest
11
12
 
12
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
+ #
13
20
  def before(&blk)
14
21
  block_given? ? @before = blk : @before
15
22
  end
16
23
 
24
+ #
25
+ # Passed block called after each example and
26
+ # evaluated in the same context as example.
27
+ #
28
+ # @param [Proc] blk
29
+ #
17
30
  def after(&blk)
18
31
  block_given? ? @after = blk : @after
19
32
  end
20
33
 
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
+ #
21
42
  def after_run(&blk)
22
43
  Minitest.after_run &blk
23
44
  end
@@ -26,4 +47,5 @@ module YARD
26
47
  end # Doctest
27
48
  end # YARD
28
49
 
50
+
29
51
  YARD::CLI::CommandParser.commands[:doctest] = YARD::CLI::Doctest
@@ -6,6 +6,13 @@ module YARD
6
6
  'Doctests from @example tags'
7
7
  end
8
8
 
9
+ #
10
+ # Runs the command line, parsing arguments
11
+ # and generating tests.
12
+ #
13
+ # @param [Array<String>] args Switches are passed to minitest,
14
+ # everything else is treated as the list of directories/files or glob
15
+ #
9
16
  def run(*args)
10
17
  files = args.select { |arg| arg !~ /^-/ }
11
18
 
@@ -13,14 +20,8 @@ module YARD
13
20
  examples = parse_examples(files)
14
21
 
15
22
  add_pwd_to_path
16
- require_helper
17
23
 
18
- hooks = {}.tap do |hash|
19
- hash[:before] = YARD::Doctest.before if YARD::Doctest.before.is_a?(Proc)
20
- hash[:after] = YARD::Doctest.after if YARD::Doctest.after.is_a?(Proc)
21
- end
22
-
23
- generate_tests(examples, hooks)
24
+ generate_tests(examples)
24
25
  end
25
26
 
26
27
  private
@@ -45,11 +46,8 @@ module YARD
45
46
  registry.all.map { |object| object.tags(:example) }.flatten
46
47
  end
47
48
 
48
- def generate_tests(examples, hooks)
49
+ def generate_tests(examples)
49
50
  examples.each do |example|
50
- path = example.object.path
51
- file = "#{Dir.pwd}/#{example.object.files.first.join(':')}"
52
- name = example.name
53
51
  text = example.text
54
52
 
55
53
  text = text.gsub('# =>', '#=>')
@@ -69,7 +67,11 @@ module YARD
69
67
  end
70
68
  end
71
69
 
72
- YARD::Doctest::Example.new(path, file, name, asserts, hooks)
70
+ spec = YARD::Doctest::Example.new(example.name)
71
+ spec.definition = example.object.path
72
+ spec.filepath = "#{Dir.pwd}/#{example.object.files.first.join(':')}"
73
+ spec.asserts = asserts
74
+ spec.generate
73
75
  end
74
76
  end
75
77
 
@@ -77,10 +79,6 @@ module YARD
77
79
  $LOAD_PATH.unshift(Dir.pwd) unless $LOAD_PATH.include?(Dir.pwd)
78
80
  end
79
81
 
80
- def require_helper
81
- require 'yard-doctest_helper'
82
- end
83
-
84
82
  end # Doctest
85
83
  end # CLI
86
84
  end # YARD
@@ -2,44 +2,42 @@ require 'sourcify'
2
2
 
3
3
  module YARD
4
4
  module Doctest
5
- class Example
5
+ class Example < ::Minitest::Spec
6
+
7
+ # @return [String] namespace path of example (e.g. `Foo#bar`)
8
+ attr_accessor :definition
9
+
10
+ # @return [String] filepath to definition (e.g. `app/app.rb:10`)
11
+ attr_accessor :filepath
12
+
13
+ # @return [Array<Hash>] assertions to be done
14
+ attr_accessor :asserts
6
15
 
7
16
  #
8
- # There are a bunch of hacks happening here:
9
- #
10
- # 1. Everything is done within constructor.
11
- # 2. Context (binding) is shared between helper, example and hooks.
12
- # 3. Since hooks are blocks, they can't be passed to `#eval`,
13
- # so we translate them into string of Ruby code.
14
- # 4. Intercept exception backtrace and add example object definition path.
17
+ # Generates a spec and registers it to Minitest runner.
15
18
  #
19
+ def generate
20
+ this = self
16
21
 
17
- def initialize(path, file, name, asserts, hooks)
18
- Object.instance_eval do
19
- context = binding
20
-
22
+ Class.new(this.class).class_eval do
21
23
  require 'minitest/autorun'
22
- context.eval "require 'yard-doctest_helper'"
24
+ require 'yard-doctest_helper'
23
25
 
24
- describe path do
25
- before { context.eval(hooks[:before].to_source(strip_enclosure: true)) } if hooks[:before]
26
- after { context.eval(hooks[:after].to_source(strip_enclosure: true)) } if hooks[:after]
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)
27
29
 
28
- it name do
29
- asserts.each do |assert|
30
+ it this.name do
31
+ this.asserts.each do |assert|
30
32
  expected, actual = assert[:expected], assert[:actual]
31
33
  actual = context.eval(actual)
32
34
 
33
35
  unless expected.empty?
34
36
  begin
35
- assert_equal context.eval(expected), actual
36
- rescue Minitest::Assertion => e
37
- backtrace = e.backtrace
38
- example = backtrace.find { |trace| trace =~ %r(lib/yard/doctest/example) }
39
- example = backtrace.index(example)
40
- backtrace = backtrace.insert(example, file)
41
- e.set_backtrace backtrace
42
- raise e
37
+ assert_equal evaluate(expected), actual
38
+ rescue Minitest::Assertion => error
39
+ add_filepath_to_backtrace(error, this.filepath)
40
+ raise error
43
41
  end
44
42
  end
45
43
  end
@@ -48,6 +46,25 @@ module YARD
48
46
  end
49
47
  end
50
48
 
49
+ protected
50
+
51
+ def evaluate(code)
52
+ code = code.to_source(strip_enclosure: true) if code.is_a?(Proc)
53
+ context.eval(code)
54
+ end
55
+
56
+ def context
57
+ @binding ||= binding
58
+ end
59
+
60
+ def add_filepath_to_backtrace(exception, filepath)
61
+ backtrace = exception.backtrace
62
+ line = backtrace.find { |line| line =~ %r(lib/yard/doctest/example) }
63
+ index = backtrace.index(line)
64
+ backtrace = backtrace.insert(index, filepath)
65
+ exception.set_backtrace backtrace
66
+ end
67
+
51
68
  end # Example
52
69
  end # Doctest
53
70
  end # YARD
@@ -5,8 +5,13 @@ module YARD
5
5
  module Doctest
6
6
  class RakeTask < ::Rake::TaskLib
7
7
 
8
+ # @return [String] the name of the task
8
9
  attr_accessor :name
10
+
11
+ # @return [Array<String>] options to pass to test runner
9
12
  attr_accessor :doctest_opts
13
+
14
+ # @return [String] list of files/dirs separated with space or glob
10
15
  attr_accessor :pattern
11
16
 
12
17
  def initialize(name = 'yard:doctest')
@@ -1,5 +1,5 @@
1
1
  module YARD
2
2
  module Doctest
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
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.0
4
+ version: 0.1.1
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-16 00:00:00.000000000 Z
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard