Permalink
Browse files

Update specs to allow toggling on and off of LEGACY environment varia…

…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...
1 parent 1ee355e commit bdd8a8ffd08316319e269125303940d4ab2c4564 @lsegal committed Feb 26, 2011
View
@@ -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'
View
@@ -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
View
@@ -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')
@@ -1,8 +1,6 @@
require 'stringio'
-begin
- require 'continuation'
-rescue LoadError; end
+begin require 'continuation'; rescue LoadError; end
module YARD
module Parser
@@ -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
@@ -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']
@@ -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|
@@ -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
@@ -1,25 +0,0 @@
-module YARD
- module Templates
- module Helpers
- module HtmlSyntaxHighlightHelper
- def html_syntax_highlight_ruby(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
-end
View
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/spec_helper'
+require 'ostruct'
include Parser
@@ -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/
@@ -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
@@ -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
@@ -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
@@ -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
View
@@ -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)
@@ -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>

0 comments on commit bdd8a8f

Please sign in to comment.