yard-rspec_examples 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 601f363204ebbf3d59050eab358312982cefc6fa
4
+ data.tar.gz: 68010e9a097bed8711e97f487654be151c63f515
5
+ SHA512:
6
+ metadata.gz: 59af946589cc352e1e6c001b1fc7b579772682680cbedf281d5b5828573a52d510430ebe5a02c7ec8ee0b098a570fcb5f569d6e609cd7616b5369a36b56d8620
7
+ data.tar.gz: 38d4807e9bf351ba80f957351883948345f439c62c167bcb4d1556eb738dd51c41a6b64cc5e11cda0d036b4b616e62d52816f1e9adefd8fee62d4cf75faf5fb6
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ lib/**/*.rb
2
+ spec/**/*.rb
3
+ -
4
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ doc
2
+ .yardoc
3
+ Gemfile.lock
4
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --plugin yard-rspec_examples
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "rspec"
7
+ gem "yard"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yard-rspec_examples (0.0.1)
5
+ yard
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ rspec (3.4.0)
12
+ rspec-core (~> 3.4.0)
13
+ rspec-expectations (~> 3.4.0)
14
+ rspec-mocks (~> 3.4.0)
15
+ rspec-core (3.4.1)
16
+ rspec-support (~> 3.4.0)
17
+ rspec-expectations (3.4.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.4.0)
20
+ rspec-mocks (3.4.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.4.0)
23
+ rspec-support (3.4.1)
24
+ yard (0.8.7.6)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ rspec
31
+ yard
32
+ yard-rspec_examples!
33
+
34
+ BUNDLED WITH
35
+ 1.10.6
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jose Fernandez (magec)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,75 @@
1
+ = yard-rspec_examples
2
+
3
+ Yard plugin to include examples in the generated doc through rspec
4
+
5
+ == Installation
6
+
7
+ In order to install it you just have to install it gem from gemcutter
8
+
9
+ gem install yard-rspec_examples
10
+
11
+ == Description
12
+
13
+ This plugin simply includes an example tag and recreates it with the source code that corresponds
14
+ to the first example included in the describe blocks below.
15
+ This have to be defined inside a describe block called "#method_name" that, in turn,
16
+ has to be defined in a describe block with the class name, i.e. the regular way people use rspec when testing objects.
17
+
18
+ So, for example,
19
+
20
+ Given this source code:
21
+
22
+ class Foo
23
+ ##
24
+ # This is a foo method with an example
25
+ # @rspec_example
26
+ def foo_method
27
+ end
28
+ end
29
+
30
+ And this spec:
31
+
32
+ describe Foo do
33
+ describe "#foo_method" do
34
+ it "works this way" do
35
+ this = "is"
36
+ # The code that will be included in the example
37
+ end
38
+ end
39
+ end
40
+
41
+ A result like this is to be expected
42
+ class Foo
43
+ ##
44
+ # This is a foo method with an example
45
+ # @example
46
+ # this = "is"
47
+ # # The code that will be included in the example
48
+ def foo_method
49
+ end
50
+ end
51
+
52
+ Another usage is to pass the example's name to the tag:
53
+
54
+ class Foo
55
+ ##
56
+ # This is a foo method with an example
57
+ # @rspec_example works this way
58
+ def foo_method
59
+ end
60
+ end
61
+
62
+
63
+
64
+
65
+ == Using it
66
+
67
+ In order to use it, you just have to add the --plugin param plus yours
68
+
69
+ yardoc --plugin yar-rspec_examples [OPTS]
70
+
71
+
72
+ == Copyright
73
+ Copyright (c) 2011 Jose Fernandez (magec). See LICENSE.txt for
74
+ further details.
75
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ require 'rspec/core/rake_task'
11
+
12
+ RSpec::Core::RakeTask.new(:spec) do |t|
13
+ t.rspec_opts = "--color"
14
+ end
15
+
16
+ require 'rubygems'
17
+
18
+ require 'rake'
@@ -0,0 +1,36 @@
1
+ module YARD
2
+ module RSpecExamples
3
+ # Handler used to inspect the rspec describe call
4
+ # used, to catch the class describe declarations and method declarations
5
+ class RSpecDescribeHandler < YARD::Handlers::Ruby::Base
6
+
7
+ handles method_call(:describe)
8
+
9
+ def process
10
+ param = statement.parameters.first
11
+
12
+ subject = nil
13
+
14
+ # A class is being declared
15
+ if param.type == :var_ref && param.children.first.type == :const
16
+ subject = param.children.first.source
17
+ end
18
+
19
+ # A class is being declared (with a nested name)
20
+ if param.type == :const_path_ref
21
+ subject = param.children.map { |i| i.source }.join("::")
22
+ end
23
+
24
+ # A method is being declared
25
+ if param.type == :string_literal && param.source =~ /^"#/
26
+ subject = param.source.delete('"|\ ')
27
+ end
28
+
29
+ ParserTrace.path.push(subject.dup) if subject
30
+ parse_block(statement.last.last)
31
+ ParserTrace.path.pop if subject
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,16 @@
1
+ module YARD
2
+ module RSpecExamples
3
+ # Handler that will set up the method <-> example map
4
+ class ItHandler < YARD::Handlers::Ruby::Base
5
+
6
+ handles method_call(:it)
7
+
8
+ def process
9
+ it_description = statement.parameters.source[1...-1]
10
+ rspec_path_string = ParserTrace.path.dup.push(it_description).join(' ')
11
+ #TODO: indentation depends on level of test indents.
12
+ RSpecExampleRegistry.example_map[rspec_path_string] = statement.block.last.source.gsub("\n ", "\n")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,34 @@
1
+ module YARD
2
+ module RSpecExamples
3
+ class ModuleHandler < YARD::Handlers::Ruby::Base
4
+ handles :module
5
+
6
+ def process
7
+ # param = statement.parameters.first
8
+ #
9
+ # subject = nil
10
+ #
11
+ # # A class is being declared
12
+ # if param.type == :var_ref && param.children.first.type == :const
13
+ # subject = param.children.first.source
14
+ # end
15
+ #
16
+ # # A class is being declared (with a nested name)
17
+ # if param.type == :const_path_ref
18
+ # subject = param.children.map { |i| i.source }.join("::")
19
+ # end
20
+ #
21
+ # # A method is being declared
22
+ # if param.type == :string_literal && param.source =~ /^"#/
23
+ # subject = param.source.delete('"|\ ')
24
+ # end
25
+ #
26
+ #TODO: probably thave to strip namespace from the module
27
+ ParserTrace.path.push(statement.module_name.path)
28
+ parse_block(statement.last.last)
29
+ ParserTrace.path.pop
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ module YARD
2
+ module RSpecExamples
3
+ class ParserTrace
4
+ def self.path
5
+ @path ||= []
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,5 @@
1
+ module YARD
2
+ module RSpecExamples
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'yard'
3
+ require 'yard/rspec_examples/it_handler'
4
+ require 'yard/rspec_examples/module_handler'
5
+ require 'yard/rspec_examples/describe_handler'
6
+ require 'yard/rspec_examples/parser_trace'
7
+
8
+ ##
9
+ # This class will hold a map that associates the method descriptions with its source code
10
+ class RSpecExampleRegistry
11
+ def self.example_map
12
+ @example_map ||= {}
13
+ end
14
+ end
15
+
16
+ module YARD
17
+ module Tags
18
+ ##
19
+ # A new type of tags has to be used cause we need to intercept the
20
+ # text call and generate its contents (in generation time, after everything was parsed)
21
+ class RSpecExampleTag < Tag
22
+ def text
23
+ # for now, we just handle case where describes are derived from the context
24
+ example_description = rspec_description(object).gsub(/^::/,'').gsub(/ ::/, ' ')
25
+ example_description += " #{name}" unless name.empty?
26
+ key = RSpecExampleRegistry.example_map.keys.detect{|k| k.include?(example_description)}
27
+ puts "[warn] Could not find example '#{example_description}'" unless key
28
+ @text = RSpecExampleRegistry.example_map[key]
29
+ end
30
+
31
+ private
32
+ def rspec_description(object)
33
+ case object
34
+ when CodeObjects::RootObject
35
+ ""
36
+ when CodeObjects::MethodObject
37
+ "#{rspec_description(object.namespace)} ##{object.name}"
38
+ when CodeObjects::ClassObject
39
+ "#{rspec_description(object.namespace)}::#{object.name}"
40
+ when CodeObjects::ModuleObject
41
+ "#{rspec_description(object.namespace)}#{object.name} "
42
+ end
43
+ end
44
+ end
45
+
46
+ class DefaultFactory
47
+ def parse_tag_with_rspec_example_tag(tagname, text)
48
+ RSpecExampleTag.new(:example, "", [], text)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ YARD::Tags::Library.define_tag("Rspec Example", :rspec_example, :with_rspec_example_tag)
@@ -0,0 +1,37 @@
1
+ class Foo
2
+ ##
3
+ # This is a foo method with an example
4
+ # @rspec_example
5
+ # @rspec_example also supports this
6
+ def foo_method
7
+ end
8
+ end
9
+
10
+ class Foo
11
+ class Bar
12
+ ##
13
+ # This is a class nested example
14
+ # @rspec_example
15
+ def bar_method
16
+ end
17
+ end
18
+ end
19
+
20
+ module My
21
+ class Nested
22
+ class Class
23
+ ##
24
+ # This is a module/class nested example
25
+ # @rspec_example
26
+ def nested_method
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ class InvalidClass
33
+ # tada
34
+ # @rspec_example invalid
35
+ def some_method
36
+ end
37
+ end
@@ -0,0 +1,6 @@
1
+ require 'yard-rspec_examples'
2
+ require 'fixtures/example_file'
3
+
4
+ RSpec.configure do |config|
5
+
6
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+ require 'yard'
3
+ EXAMPLE_DOC_DIR=[File.expand_path(File.dirname(__FILE__) + "/fixtures/*.rb"), File.expand_path(File.dirname(__FILE__) + "/*.rb")]
4
+
5
+ OPTIONS = {
6
+ :format => :html,
7
+ :template => :default,
8
+ :markup => :rdoc,
9
+ :serializer => YARD::Serializers::FileSystemSerializer.new,
10
+ :default_return => "Object",
11
+ :hide_void_return => false,
12
+ :no_highlight => false,
13
+ :files => []
14
+ }
15
+
16
+ describe "Using the rspec_example tag in the method comment" do
17
+ before(:each) do
18
+ YARD.parse(EXAMPLE_DOC_DIR)
19
+ foo = YARD::Registry.all(:class).find { |i| i.to_s == "Foo" }
20
+ @class_foo_output = YARD::Templates::Engine.render(OPTIONS.merge(:object => foo))
21
+ bar = YARD::Registry.all(:class).find { |i| i.to_s == "Foo::Bar" }
22
+ @class_bar_output = YARD::Templates::Engine.render(OPTIONS.merge(:object => bar))
23
+ nested_class = YARD::Registry.all(:class).find { |i| i.to_s == "My::Nested::Class" }
24
+ @nested_class_output = YARD::Templates::Engine.render(OPTIONS.merge(:object => nested_class))
25
+ end
26
+
27
+ it "displays warning on unfound example" do
28
+ expect do
29
+ invalid_class = YARD::Registry.all(:class).find { |i| i.to_s == "InvalidClass" }
30
+ @invalid_class_output = YARD::Templates::Engine.render(OPTIONS.merge(:object => invalid_class))
31
+ end.to output("[warn] Could not find example 'InvalidClass #some_method invalid'\n").to_stdout
32
+ end
33
+
34
+ it "creates an example tag in the registry" do
35
+ expect(@class_foo_output).to match /Examples:/
36
+ end
37
+
38
+ it "outputs the source code of the example of a method that reads \"works this way\" " do
39
+ expect(@class_foo_output).to match /this/
40
+ end
41
+
42
+ it "includes the comments in the example as well" do
43
+ # TODO: comments as first line get stripped away right now.
44
+ expect(@class_foo_output).to match /With some comments/
45
+ end
46
+
47
+ it "works with nested classes" do
48
+ expect(@class_bar_output).to match /nested_class/
49
+ end
50
+
51
+ it "works with module/class/classs nestes classes" do
52
+ expect(@nested_class_output).to match /super_nested_class/
53
+ end
54
+
55
+ it "includes named examples" do
56
+ expect(@class_foo_output).to include CGI.escape_html("comment for 'also support this' it")
57
+ end
58
+ end
59
+
60
+ # A bit of a hack, this is the spec that is going to be used as fixture for the plugin
61
+ describe Foo do
62
+ describe "#foo_method" do
63
+ it "works this way" do
64
+ this = "is"
65
+ just = "example code"
66
+ # With some comments
67
+ [1, 2, 3].each do |i|
68
+ i
69
+ end
70
+ end
71
+
72
+ it "also supports this" do
73
+ 0 + 0
74
+ # comment for 'also support this' it
75
+ 1 + 2
76
+ # kilo
77
+ 4 + 3
78
+ end
79
+ end
80
+ end
81
+
82
+
83
+ describe Foo::Bar do
84
+ describe "#bar_method" do
85
+ it "works this way" do
86
+ nested_class = nil
87
+ end
88
+ end
89
+ end
90
+
91
+ module My
92
+ describe Nested::Class do
93
+ describe "#nested_method" do
94
+ it "works this way" do
95
+ super_nested_class = nil
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'yard/rspec_examples/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'yard-rspec_examples'
6
+ s.version = YARD::RSpecExamples::VERSION
7
+ s.authors = [ 'Bert Bruynooghe', 'Jose Fernandez (magec)' ]
8
+ s.email = [ 'info@up-nxt.com' ]
9
+ s.homepage = 'https://github.com/UP-nxt/yard-rspec_examples'
10
+ s.summary = 'Yard plugin to include RSpec examples in documentation'
11
+ s.description = 'Yard plugin to include RSpec examples in documentation'
12
+
13
+ s.files = `git ls-files`.split($/)
14
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+ s.require_paths = [ 'lib' ]
17
+
18
+ s.add_dependency 'yard'
19
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yard-rspec_examples
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bert Bruynooghe
8
+ - Jose Fernandez (magec)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-01-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ description: Yard plugin to include RSpec examples in documentation
29
+ email:
30
+ - info@up-nxt.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".document"
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".yardopts"
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.rdoc
43
+ - Rakefile
44
+ - lib/yard-rspec_examples.rb
45
+ - lib/yard/rspec_examples/describe_handler.rb
46
+ - lib/yard/rspec_examples/it_handler.rb
47
+ - lib/yard/rspec_examples/module_handler.rb
48
+ - lib/yard/rspec_examples/parser_trace.rb
49
+ - lib/yard/rspec_examples/version.rb
50
+ - spec/fixtures/example_file.rb
51
+ - spec/spec_helper.rb
52
+ - spec/yard-rspec_examples_spec.rb
53
+ - yard-rspec_examples.gemspec
54
+ homepage: https://github.com/UP-nxt/yard-rspec_examples
55
+ licenses: []
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.4.5
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Yard plugin to include RSpec examples in documentation
77
+ test_files:
78
+ - spec/fixtures/example_file.rb
79
+ - spec/spec_helper.rb
80
+ - spec/yard-rspec_examples_spec.rb
81
+ has_rdoc: