summaryrefslogtreecommitdiff
path: root/test/did_you_mean/core_ext/test_name_error_extension.rb
blob: 1fdbd4510fb1e9abbe283aaa80b4754690c747f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require_relative '../helper'

class NameErrorExtensionTest < Test::Unit::TestCase
  include DidYouMean::TestHelper

  SPELL_CHECKERS = DidYouMean.spell_checkers

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

  def setup
    @original_spell_checker = DidYouMean.spell_checkers['NameError']
    DidYouMean.correct_error(NameError, TestSpellChecker)

    @error = assert_raise(NameError){ doesnt_exist }
  end

  def teardown
    DidYouMean.correct_error(NameError, @original_spell_checker)
  end

  def test_message
    if Exception.method_defined?(:detailed_message)
      assert_match(/Did you mean\?  does_exist/, @error.detailed_message)
    else
      assert_match(/Did you mean\?  does_exist/, @error.to_s)
      assert_match(/Did you mean\?  does_exist/, @error.message)
    end
  end

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

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

  def test_correctable_error_objects_are_dumpable
    error =
      begin
        Dir.chdir(__dir__) { File.open('test_name_error_extension.rb') { |f| f.sizee } }
      rescue NoMethodError => e
        e
      end

    get_message(error)

    assert_equal "undefined method `sizee' for #<File:test_name_error_extension.rb (closed)>",
                 Marshal.load(Marshal.dump(error)).original_message
  end
end