Skip to content

Commit

Permalink
Update specs to allow toggling on and off of LEGACY environment varia…
Browse files Browse the repository at this point in the history
…ble for legacy tests in 1.8/1.9

Remove HAVE_RIPPER and LEGACY_PARSER from main YARD code, only use in specs.
  • Loading branch information
lsegal committed Feb 26, 2011
1 parent 1ee355e commit bdd8a8f
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 67 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ end

begin
hide = '_spec\.rb$,spec_helper\.rb$,ruby_lex\.rb$,autoload\.rb$'
if HAVE_RIPPER
hide += ',legacy\/.+_handler,html_syntax_highlight_helper18\.rb$'
if YARD::Parser::SourceParser.parser_type == :ruby
hide += ',legacy\/.+_handler'
else
hide += ',ruby_parser\.rb$,ast_node\.rb$,handlers\/ruby\/[^\/]+\.rb$,html_syntax_highlight_helper\.rb$'
hide += ',ruby_parser\.rb$,ast_node\.rb$,handlers\/ruby\/[^\/]+\.rb$'
end

require 'rspec'
Expand Down
8 changes: 0 additions & 8 deletions lib/yard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ def self.load_plugins; YARD::Config.load_plugins end
# Keep track of Ruby version for compatibility code
RUBY19, RUBY18 = *(RUBY_VERSION >= "1.9.1" ? [true, false] : [false, true])

begin
require 'ripper'
rescue LoadError
ensure
HAVE_RIPPER = defined?(::Ripper) ? true : false
LEGACY_PARSER = !HAVE_RIPPER
end

# Whether or not continuations are (properly) supported
begin
begin; require 'continuation'; rescue LoadError; end
Expand Down
2 changes: 1 addition & 1 deletion lib/yard/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module Helpers # Namespace for template helpers
autoload :BaseHelper, __p('templates/helpers/base_helper')
autoload :FilterHelper, __p('templates/helpers/filter_helper')
autoload :HtmlHelper, __p('templates/helpers/html_helper')
autoload :HtmlSyntaxHighlightHelper, __p('templates/helpers/html_syntax_highlight_helper' + (LEGACY_PARSER ? '18' : ''))
autoload :HtmlSyntaxHighlightHelper, __p('templates/helpers/html_syntax_highlight_helper')
autoload :MarkupHelper, __p('templates/helpers/markup_helper')
autoload :MethodHelper, __p('templates/helpers/method_helper')
autoload :ModuleHelper, __p('templates/helpers/module_helper')
Expand Down
11 changes: 5 additions & 6 deletions lib/yard/parser/source_parser.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'stringio'

begin
require 'continuation'
rescue LoadError; end
begin require 'continuation'; rescue LoadError; end

module YARD
module Parser
Expand Down Expand Up @@ -133,13 +131,14 @@ def parser_type_for_extension(extension)
end

# Returns the validated parser type. Basically, enforces that :ruby
# type is never set from Ruby 1.8
# type is never set if the Ripper library is not available
#
# @param [Symbol] type the parser type to set
# @return [Symbol] the validated parser type
# @private
def validated_parser_type(type)
!HAVE_RIPPER && type == :ruby ? :ruby18 : type
begin require 'ripper'; rescue LoadError; end
!defined?(::Ripper) && type == :ruby ? :ruby18 : type
end

private
Expand Down Expand Up @@ -171,7 +170,7 @@ def parse_in_order(*files)

self.parser_type = :ruby

register_parser_type :ruby, Ruby::RubyParser if HAVE_RIPPER
register_parser_type :ruby, Ruby::RubyParser
register_parser_type :ruby18, Ruby::Legacy::RubyParser
register_parser_type :c, CParser, ['c', 'cc', 'cxx', 'cpp']

Expand Down
28 changes: 28 additions & 0 deletions lib/yard/templates/helpers/html_syntax_highlight_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ module HtmlSyntaxHighlightHelper
# @param [String] source the Ruby source code
# @return [String] the highlighted Ruby source
def html_syntax_highlight_ruby(source)
if Parser::SourceParser.parser_type == :ruby
html_syntax_highlight_ruby_ripper(source)
else
html_syntax_highlight_ruby_legacy(source)
end
end

private

def html_syntax_highlight_ruby_ripper(source)
tokenlist = Parser::Ruby::RubyParser.parse(source, "(syntax_highlight)").tokens
output = ""
tokenlist.each do |s|
Expand All @@ -25,6 +35,24 @@ def html_syntax_highlight_ruby(source)
rescue Parser::ParserSyntaxError
h(source)
end

def html_syntax_highlight_ruby_legacy(source)
tokenlist = Parser::Ruby::Legacy::TokenList.new(source)
tokenlist.map do |s|
prettyclass = s.class.class_name.sub(/^Tk/, '').downcase
prettysuper = s.class.superclass.class_name.sub(/^Tk/, '').downcase

case s
when Parser::Ruby::Legacy::RubyToken::TkWhitespace, Parser::Ruby::Legacy::RubyToken::TkUnknownChar
h s.text
when Parser::Ruby::Legacy::RubyToken::TkId
prettyval = h(s.text)
"<span class='#{prettyval} #{prettyclass} #{prettysuper}'>#{prettyval}</span>"
else
"<span class='#{prettyclass} #{prettysuper}'>#{h s.text}</span>"
end
end.join
end
end
end
end
Expand Down
25 changes: 0 additions & 25 deletions lib/yard/templates/helpers/html_syntax_highlight_helper18.rb

This file was deleted.

40 changes: 20 additions & 20 deletions spec/handlers/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'ostruct'

include Parser

Expand Down Expand Up @@ -110,15 +111,15 @@ def process
end

describe '.in_file' do
def parse(filename, src = "class A; end")
parser = Parser::SourceParser.new
def parse(filename, parser_type, src = "class A; end")
parser = Parser::SourceParser.new(parser_type)
parser.instance_variable_set("@file", filename)
parser.parse(StringIO.new(src))
end

def create_handler(stmts)
def create_handler(stmts, parser_type)
@@counter ||= 0
sklass = Parser::SourceParser.parser_type == :ruby ? "Base" : "Legacy::Base"
sklass = parser_type == :ruby ? "Base" : "Legacy::Base"
instance_eval(<<-eof)
class ::InFileHandler#{@@counter += 1} < Handlers::Ruby::#{sklass}
handles /^class/
Expand All @@ -128,46 +129,45 @@ def process; MethodObject.new(:root, :FOO) end
eof
end

def test_handler(file, stmts, creates = true)
def test_handler(file, stmts, creates = true, parser_type = :ruby)
Registry.clear
Registry.at('#FOO').should be_nil
create_handler(stmts)
parse(file)
create_handler(stmts, parser_type)
parse(file, parser_type)
Registry.at('#FOO').send(creates ? :should_not : :should, be_nil)
Handlers::Base.subclasses.delete_if {|k,v| k.to_s =~ /^InFileHandler/ }
end

[:ruby, :ruby18].each do |parser_type|
next if parser_type == :ruby && LEGACY_PARSER
describe "Parser type = #{parser_type.inspect}" do
Parser::SourceParser.parser_type = parser_type
it "should allow handler to be specific to a file" do
test_handler 'file_a.rb', 'in_file "file_a.rb"', true
test_handler 'file_a.rb', 'in_file "file_a.rb"', true, parser_type
end

it "should ignore handler if filename does not match" do
test_handler 'file_b.rb', 'in_file "file_a.rb"', false
test_handler 'file_b.rb', 'in_file "file_a.rb"', false, parser_type
end

it "should only test filename part when given a String" do
test_handler '/path/to/file_a.rb', 'in_file "/to/file_a.rb"', false
test_handler '/path/to/file_a.rb', 'in_file "/to/file_a.rb"', false, parser_type
end

it "should test exact match for entire String" do
test_handler 'file_a.rb', 'in_file "file"', false
test_handler 'file_a.rb', 'in_file "file"', false, parser_type
end

it "should allow a Regexp as argument and test against full path" do
test_handler 'file_a.rbx', 'in_file /\.rbx$/', true
test_handler '/path/to/file_a.rbx', 'in_file /\/to\/file_/', true
test_handler '/path/to/file_a.rbx', 'in_file /^\/path/', true
test_handler 'file_a.rbx', 'in_file /\.rbx$/', true, parser_type
test_handler '/path/to/file_a.rbx', 'in_file /\/to\/file_/', true, parser_type
test_handler '/path/to/file_a.rbx', 'in_file /^\/path/', true, parser_type
end

it "should allow multiple in_file declarations" do
stmts = 'in_file "x"; in_file /y/; in_file "foo.rb"'
test_handler 'foo.rb', stmts, true
test_handler 'xyzzy.rb', stmts, true
test_handler 'x', stmts, true
test_handler 'foo.rb', stmts, true, parser_type
test_handler 'xyzzy.rb', stmts, true, parser_type
test_handler 'x', stmts, true, parser_type
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/handlers/ruby/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StringHandler < Handlers::Ruby::Base
handles "x"
end
Handlers::Base.stub!(:subclasses).and_return [StringHandler]
ast = RubyParser.parse("if x == 2 then true end").ast
ast = Parser::Ruby::RubyParser.parse("if x == 2 then true end").ast
valid StringHandler, ast[0][0][0]
invalid StringHandler, ast[0][1]
end
Expand All @@ -51,7 +51,7 @@ class RegexHandler < Handlers::Ruby::Base
handles %r{^if x ==}
end
Handlers::Base.stub!(:subclasses).and_return [RegexHandler]
ast = RubyParser.parse("if x == 2 then true end").ast
ast = Parser::Ruby::RubyParser.parse("if x == 2 then true end").ast
valid RegexHandler, ast
invalid RegexHandler, ast[0][1]
end
Expand All @@ -70,7 +70,7 @@ class MethCallHandler < Handlers::Ruby::Base
handles method_call(:meth)
end
Handlers::Base.stub!(:subclasses).and_return [MethCallHandler]
ast = RubyParser.parse(<<-"eof").ast
ast = Parser::Ruby::RubyParser.parse(<<-"eof").ast
meth # 0
meth() # 1
meth(1,2,3) # 2
Expand Down
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'yard'))

unless defined?(HAVE_RIPPER)
begin require 'ripper'; rescue LoadError; end
HAVE_RIPPER = defined?(::Ripper) && !ENV['LEGACY'] ? true : false
LEGACY_PARSER = !HAVE_RIPPER

class YARD::Parser::SourceParser
def self.parser_type; :ruby18 end
end if ENV['LEGACY']
end

def parse_file(file, thisfile = __FILE__, log_level = log.level, ext = '.rb.txt')
Registry.clear
path = File.join(File.dirname(thisfile), 'examples', file.to_s + ext)
Expand Down
6 changes: 5 additions & 1 deletion spec/templates/helpers/html_syntax_highlight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
end

it "should highlight source (legacy)" do
type = Parser::SourceParser.parser_type
Parser::SourceParser.parser_type = :ruby18
should_receive(:options).and_return(:no_highlight => false)
expect = "<span class='def def kw'>def</span><span class='x identifier id'>x</span>
<span class='string val'>'x'</span><span class='plus op'>+</span>
<span class='regexp val'>/x/i</span><span class='end end kw'>end</span>"
result = html_syntax_highlight("def x\n 'x' + /x/i\nend")
html_equals_string(result, expect)
end if LEGACY_PARSER
Parser::SourceParser.parser_type = type
end

it "should highlight source (ripper)" do
should_receive(:options).and_return(:no_highlight => false)
Parser::SourceParser.parser_type = :ruby
expect = "<span class='kw'>def</span> <span class='id x'>x</span>
<span class='tstring'><span class='tstring_beg'>'</span>
<span class='tstring_content'>x</span><span class='tstring_end'>'</span>
Expand Down

0 comments on commit bdd8a8f

Please sign in to comment.