Sha256: 39c90507c5e9ce81d6976d1a6c338e74a643414dfde51ec35109b607d0558180

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

require 'test_helper'

class NameErrorExtensionTest < Minitest::Test
  SPELL_CHECKERS = DidYouMean::SPELL_CHECKERS

  class TestSpellChecker
    def initialize(*); end
    def corrections; ["does_exist"]; end
  end

  def setup
    @org, SPELL_CHECKERS['NameError'] = SPELL_CHECKERS['NameError'], TestSpellChecker

    @error = assert_raises(NameError){ doesnt_exist }
  end

  def teardown
    SPELL_CHECKERS['NameError'] = @org
  end

  def test_message
    message = <<~MESSAGE.chomp
      undefined local variable or method `doesnt_exist' for #{method(:to_s).super_method.call}
      Did you mean?  does_exist
    MESSAGE

    assert_equal message, @error.to_s
    assert_equal message, @error.message
  end

  def test_to_s_does_not_make_disruptive_changes_to_error_message
    error = assert_raises(NameError) do
      raise NameError, "uninitialized constant Object"
    end

    error.to_s
    assert_equal 1, error.to_s.scan("Did you mean?").count
  end
end

class IgnoreCallersTest < Minitest::Test
  SPELL_CHECKERS = DidYouMean::SPELL_CHECKERS

  class Boomer
    def initialize(*)
      raise Exception, "spell checker was created when it shouldn't!"
    end
  end

  def setup
    @org, SPELL_CHECKERS['NameError'] = SPELL_CHECKERS['NameError'], Boomer
    DidYouMean::IGNORED_CALLERS << /( |`)do_not_correct_typo'/

    @error = assert_raises(NameError){ doesnt_exist }
  end

  def teardown
    SPELL_CHECKERS['NameError'] = @org
    DidYouMean::IGNORED_CALLERS.clear
  end

  def test_ignore
    assert_nothing_raised { do_not_correct_typo }
  end

  private

  def do_not_correct_typo; @error.message end

  def assert_nothing_raised
    yield
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
did_you_mean-1.0.3 test/core_ext/name_error_extension_test.rb
did_you_mean-1.1.2 test/core_ext/name_error_extension_test.rb
ruby-compiler-0.1.1 vendor/ruby/gems/did_you_mean-1.1.0/test/core_ext/name_error_extension_test.rb
did_you_mean-1.1.0 test/core_ext/name_error_extension_test.rb
did_you_mean-1.0.2 test/core_ext/name_error_extension_test.rb
did_you_mean-1.0.1 test/core_ext/name_error_extension_test.rb