test/parse_helper.rb in parser-2.4.0.2 vs test/parse_helper.rb in parser-2.5.0.0

- old
+ new

@@ -1,20 +1,16 @@ +# frozen_string_literal: true + module ParseHelper include AST::Sexp - if RUBY_VERSION == '1.8.7' - require 'parser/ruby18' + require 'parser/all' + require 'parser/macruby' + require 'parser/rubymotion' - ALL_VERSIONS = %w(1.8) - else - require 'parser/all' - require 'parser/macruby' - require 'parser/rubymotion' + ALL_VERSIONS = %w(1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 mac ios) - ALL_VERSIONS = %w(1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 mac ios) - end - def setup @diagnostics = [] super if defined?(super) end @@ -183,10 +179,46 @@ end end end end + # Use like this: + # ~~~ + # assert_diagnoses_many( + # [ + # [:warning, :ambiguous_literal], + # [:error, :unexpected_token, { :token => :tLCURLY }] + # ], + # %q{m /foo/ {}}, + # SINCE_2_4) + # ~~~ + def assert_diagnoses_many(diagnostics, code, versions=ALL_VERSIONS) + with_versions(versions) do |version, parser| + source_file = Parser::Source::Buffer.new('(assert_diagnoses_many)') + source_file.source = code + + begin + parser = parser.parse(source_file) + rescue Parser::SyntaxError + # do nothing; the diagnostic was reported + end + + assert_equal diagnostics.count, @diagnostics.count + + diagnostics.zip(@diagnostics) do |expected_diagnostic, actual_diagnostic| + level, reason, arguments = expected_diagnostic + arguments ||= {} + message = Parser::MESSAGES[reason] % arguments + + assert_equal level, actual_diagnostic.level + assert_equal reason, actual_diagnostic.reason + assert_equal arguments, actual_diagnostic.arguments + assert_equal message, actual_diagnostic.message + end + end + end + def refute_diagnoses(code, versions=ALL_VERSIONS) with_versions(versions) do |version, parser| source_file = Parser::Source::Buffer.new('(refute_diagnoses)') source_file.source = code @@ -197,9 +229,24 @@ end assert_empty @diagnostics, "(#{version}) emits no diagnostics, not\n" \ "#{@diagnostics.map(&:render).join("\n")}" + end + end + + def assert_context(context, code, versions=ALL_VERSIONS) + with_versions(versions) do |version, parser| + source_file = Parser::Source::Buffer.new('(assert_context)') + source_file.source = code + + begin + parser.parse(source_file) + rescue Parser::SyntaxError + # do nothing; the diagnostic was reported + end + + assert_equal parser.context.stack, context, "(#{version}) parsing context" end end SOURCE_MAP_DESCRIPTION_RE = /(?x)