From bdd8a8ffd08316319e269125303940d4ab2c4564 Mon Sep 17 00:00:00 2001 From: Loren Segal Date: Sat, 26 Feb 2011 00:28:25 -0500 Subject: [PATCH] Update specs to allow toggling on and off of LEGACY environment variable for legacy tests in 1.8/1.9 Remove HAVE_RIPPER and LEGACY_PARSER from main YARD code, only use in specs. --- Rakefile | 6 +-- lib/yard.rb | 8 ---- lib/yard/autoload.rb | 2 +- lib/yard/parser/source_parser.rb | 11 +++-- .../helpers/html_syntax_highlight_helper.rb | 28 +++++++++++++ .../helpers/html_syntax_highlight_helper18.rb | 25 ------------ spec/handlers/base_spec.rb | 40 +++++++++---------- spec/handlers/ruby/base_spec.rb | 6 +-- spec/spec_helper.rb | 10 +++++ .../html_syntax_highlight_helper_spec.rb | 6 ++- 10 files changed, 75 insertions(+), 67 deletions(-) delete mode 100644 lib/yard/templates/helpers/html_syntax_highlight_helper18.rb diff --git a/Rakefile b/Rakefile index 928e46929..d5b515823 100644 --- a/Rakefile +++ b/Rakefile @@ -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' diff --git a/lib/yard.rb b/lib/yard.rb index 0e27ae043..39545545a 100644 --- a/lib/yard.rb +++ b/lib/yard.rb @@ -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 diff --git a/lib/yard/autoload.rb b/lib/yard/autoload.rb index 8b9763b70..fdd1fc726 100644 --- a/lib/yard/autoload.rb +++ b/lib/yard/autoload.rb @@ -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') diff --git a/lib/yard/parser/source_parser.rb b/lib/yard/parser/source_parser.rb index 269eddd33..f4fc0c240 100644 --- a/lib/yard/parser/source_parser.rb +++ b/lib/yard/parser/source_parser.rb @@ -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'] diff --git a/lib/yard/templates/helpers/html_syntax_highlight_helper.rb b/lib/yard/templates/helpers/html_syntax_highlight_helper.rb index 56a77ae23..77ca874e3 100644 --- a/lib/yard/templates/helpers/html_syntax_highlight_helper.rb +++ b/lib/yard/templates/helpers/html_syntax_highlight_helper.rb @@ -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) + "#{prettyval}" + else + "#{h s.text}" + end + end.join + end end end end diff --git a/lib/yard/templates/helpers/html_syntax_highlight_helper18.rb b/lib/yard/templates/helpers/html_syntax_highlight_helper18.rb deleted file mode 100644 index 34c95a716..000000000 --- a/lib/yard/templates/helpers/html_syntax_highlight_helper18.rb +++ /dev/null @@ -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) - "#{prettyval}" - else - "#{h s.text}" - end - end.join - end - end - end - end -end diff --git a/spec/handlers/base_spec.rb b/spec/handlers/base_spec.rb index 47f5ed914..db14dd26b 100644 --- a/spec/handlers/base_spec.rb +++ b/spec/handlers/base_spec.rb @@ -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,11 +129,11 @@ 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 @@ -140,34 +141,33 @@ def test_handler(file, stmts, creates = true) [: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 diff --git a/spec/handlers/ruby/base_spec.rb b/spec/handlers/ruby/base_spec.rb index 5b6f4a778..ef5b2f6cc 100644 --- a/spec/handlers/ruby/base_spec.rb +++ b/spec/handlers/ruby/base_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8b53d1a8d..0cc716dc9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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) diff --git a/spec/templates/helpers/html_syntax_highlight_helper_spec.rb b/spec/templates/helpers/html_syntax_highlight_helper_spec.rb index 5e63a8fbb..cd59f3d1f 100644 --- a/spec/templates/helpers/html_syntax_highlight_helper_spec.rb +++ b/spec/templates/helpers/html_syntax_highlight_helper_spec.rb @@ -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 = "defx 'x'+ /x/iend" 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 = "def x ' x'