Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more spec organizations and 1.8.6 fix #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions example/example_code.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Run `yardoc -e ../lib/yard-rspec example_code.rb`

# Run `yardoc -e ./lib/yard-rspec.rb ./example/example_code.rb`

class String
# Pig latin of a String
def pig_latin
self[1..-1] + self[0] + "ay"
def pig_latin(switch = true)
"#{self[1..-1]}#{self[0, 1]}ay"
end
end

Expand All @@ -15,10 +14,31 @@ def pig_latin
describe '#pig_latin' do
it "should be a pig!" do
"hello".pig_latin.should == "ellohay"
end
end

context "on an empty string" do
it "should fail to be a pig!" do
"".pig_latin.should == ""
end
end

it "should fail to be a pig!" do
"hello".pig_latin.should == "hello"
it "works with RSpec metadata", :see => "yep" do
"xyz".pig_latin.length.should == "xyz".length + 2
end

it "works on multi-lingual strings" # pending
end
end

context "under circumstances" do
describe "#pig_latin(true)" do
subject { %w(i can count on you).map(&:pig_latin).join(" ") }

its(:length) { should == 28 }
its(:"length") { should == 28 }
its('length') { should == 28 }
its "length" do; should == 28 end

specify { should == "iay ancay ountcay onay ouyay" }
end
end
end
111 changes: 94 additions & 17 deletions lib/yard-rspec/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,108 @@ class RSpecDescribeHandler < YARD::Handlers::Ruby::Base
handles method_call(:describe)

def process
objname = statement.parameters.first.jump(:string_content).source
if statement.parameters[1]
src = statement.parameters[1].jump(:string_content).source
objname += (src[0] == "#" ? "" : "::") + src
describes = statement.parameters.first.jump(:string_content).source

# Remove the argument list from describe "#method(a, b, &c)"
if arguments = describes[/[#.](?:.+)(\([^)]*\))$/, 1]
describes = describes[0, describes.length - arguments.length]
end

unless owner.is_a?(Hash)
pwner = Hash[describes: describes, context: ""]
parse_block(statement.last.last, owner: pwner)
else
describes = owner[:describes] + describes
pwner = owner.merge(describes: describes)
parse_block(statement.last.last, owner: pwner)
end
rescue YARD::Handlers::NamespaceMissingError
end
end

class RSpecContextHandler < YARD::Handlers::Ruby::Base
handles method_call(:context)

def process
if owner.is_a?(Hash)
context = statement.parameters.first.jump(:string_content).source
context = owner[:context] + context + " "

parse_block(statement.last.last, owner: owner.merge(context: context))
end
obj = {:spec => owner ? (owner[:spec] || "") : ""}
obj[:spec] += objname
parse_block(statement.last.last, owner: obj)
rescue YARD::Handlers::NamespaceMissingError
end
end

class RSpecItHandler < YARD::Handlers::Ruby::Base
handles method_call(:it)
handles method_call(:specify)

def process
return if owner.nil?
obj = P(owner[:spec])
return if obj.is_a?(Proxy)

(obj[:specifications] ||= []) << {
name: statement.parameters.first.jump(:string_content).source,
file: statement.file,
line: statement.line,
source: statement.last.last.source.chomp
}
return unless owner.is_a?(Hash)
return unless owner[:describes]

node = YARD::Registry.resolve(nil, owner[:describes], true)
spec = if statement.parameters
statement.parameters.first.jump(:string_content).source
else
"untitled spec"
end

unless node
log.warn "Unknown node #{owner[:describes]} for spec defined in #{statement.file} near line #{statement.line}"
# statement.file
# statement.line
# owner[:describes]
return
end

if last = statement.last.last
source = last.source.strip
else
source = ""
end

(node[:specifications] ||= []) << \
Hash[ name: owner[:context] + spec,
file: statement.file,
line: statement.line,
source: source ]
end
end

class RSpecItsHandler < YARD::Handlers::Ruby::Base
handles method_call(:its)

def process
return unless owner.is_a?(Hash)
return unless owner[:describes]

node = YARD::Registry.resolve(nil, owner[:describes], true)
property = statement.parameters.first.join(" ")
should = statement.last.last.source[/should(?:_not)? (.+)/, 1]

spec = if property and should
spec = "#{property} #{should}"
else
"untitled spec"
end

unless node
log.warn "Unknown node #{owner[:describes]} for spec defined in #{statement.file} near line #{statement.line}"
return
end

if last = statement.last.last
source = last.source.strip
else
source = ""
end

(node[:specifications] ||= []) << \
Hash[ name: owner[:context] + spec,
file: statement.file,
line: statement.line,
source: source ]
end
end
103 changes: 87 additions & 16 deletions lib/yard-rspec/legacy.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,102 @@
class LegacyRSpecDescribeHandler < YARD::Handlers::Ruby::Legacy::Base
MATCH = /\Adescribe\s+(.+?)\s+(do|\{)/
MATCH = /\Adescribe\s+(.+)\s+(?:do|\{)/
handles MATCH

def process
objname = statement.tokens.to_s[MATCH, 1].gsub(/["']/, '')
obj = {:spec => owner ? (owner[:spec] || "") : ""}
obj[:spec] += objname
parse_block :owner => obj
describes = statement.tokens.to_s[MATCH, 1].gsub(/["']/, "")

# Remove the argument list from describe "#method(a, b, &c)"
if arguments = describes[/[#.](?:.+?)(\([^)]*\))$/, 1]
describes = describes[0, describes.length - arguments.length]
end

unless owner.is_a?(Hash)
pwner = Hash[:describes => describes, :context => ""]
parse_block(:owner => pwner)
else
describes = owner[:describes] + describes
pwner = owner.merge(:describes => describes)
parse_block(:owner => pwner)
end
rescue YARD::Handlers::NamespaceMissingError
end
end

class LegacySpecContextHandler < YARD::Handlers::Ruby::Legacy::Base
MATCH = /\Acontext\s+(['"])(.+)\1(?:.*?)\s+(?:do|\{)/
handles MATCH

def process
if owner.is_a?(Hash)
context = statement.tokens.to_s[MATCH, 2]
context = owner[:context] + context + " "

parse_block(:owner => owner.merge(:context => context))
end
end
end

class LegacyRSpecItHandler < YARD::Handlers::Ruby::Legacy::Base
MATCH = /\Ait\s+['"](.+?)['"]\s+(do|\{)/
MATCH = /\A(?:it|specify)\s+(['"])(.+?)\1.*?(?:(\s+(?:do|\{))|$)/

handles MATCH
handles /\A(?:it|specify)\s+.*?(?:do|\{)/

def process
return if owner.nil?
obj = P(owner[:spec])
return if obj.is_a?(Proxy)

(obj[:specifications] ||= []) << {
:name => statement.tokens.to_s[MATCH, 1],
:file => parser.file,
:line => statement.line,
:source => statement.block.to_s
}
return unless owner.is_a?(Hash)
return unless owner[:describes]

node = YARD::Registry.resolve(nil, owner[:describes], true)
spec = statement.tokens.to_s[MATCH, 2] || "untitled spec"

unless node
log.warn "Unknown node #{owner[:describes]} for spec defined in #{parser.file} near line #{statement.line}"
return
end

source = statement.block.to_s.strip

(node[:specifications] ||= []) << \
Hash[ :name => owner[:context] + spec,
:file => parser.file,
:line => statement.line,
:source => source ]
end
end

class LegacyRSpecItsHandler < YARD::Handlers::Ruby::Legacy::Base
# MATCH = /\Aits\s*(\(?)\s*[:]?(['"]?)(.+)\2\1(?:(\s+(?:do|\{))|$)/
# handles MATCH

MATCH = /\Aits\s*([(]?)\s*:?(['"]?)([^'")]+)\2/
handles MATCH

def process
return unless owner.is_a?(Hash)
return unless owner[:describes]

node = YARD::Registry.resolve(nil, owner[:describes], true)

property = statement.tokens.to_s[MATCH, 3]
should = statement.block.to_s[/should(?:_not)?\s+(.+)/, 1]

spec = if property and should
"#{property} #{should}"
else
"untitled spec"
end

unless node
log.warn "Unknown node #{owner[:describes]} for spec defined in #{parser.file} near line #{statement.line}"
return
end

source = statement.block.to_s.strip

(node[:specifications] ||= []) << \
Hash[ :name => owner[:context] + spec,
:file => parser.file,
:line => statement.line,
:source => source ]
end
end
8 changes: 3 additions & 5 deletions templates/default/method_details/html/specs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
<td>
<pre class="lines">


<%= spec[:source].split("\n").size.times.to_a.map {|i| spec[:line] + i }.join("\n") %></pre>
<%= (1..spec[:source].split("\n").size).map {|i| spec[:line] + i }.join("\n") %></pre>
</td>
<td>
<pre class="code"><span class="info file"># File '<%= h spec[:file] %>', line <%= spec[:line] %></span>

<%= html_syntax_highlight format_source(spec[:source]) %></pre>
<% if spec[:source] =~ /\S/ %><%= html_syntax_highlight format_source(spec[:source]) %></pre><% end %>
</td>
</tr>
</table>
Expand All @@ -25,4 +23,4 @@
<% end %>
</ul>
</div>
<% end %>
<% end %>