summaryrefslogtreecommitdiff
path: root/test/did_you_mean/spell_checking/test_method_name_check.rb
blob: d2e46d58f3e4008af86fcf9102c83fd36421076a (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require_relative '../helper'

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

  class User
    def friends; end
    def first_name; end
    def descendants; end
    def call_incorrect_private_method
      raiae NoMethodError
    end

    def raise_no_method_error
      self.firstname
    rescue NoMethodError => e
      raise e, e.message, e.backtrace
    end

    protected
    def the_protected_method; end

    private
    def friend; end
    def the_private_method; end

    class << self
      def load; end
    end
  end

  module UserModule
    def from_module; end
  end

  def setup
    @user = User.new.extend(UserModule)
  end

  def test_corrections_include_instance_method
    error = assert_raise(NoMethodError){ @user.flrst_name }

    assert_correction :first_name, error.corrections
    assert_match "Did you mean?  first_name",  get_message(error)
  end

  def test_corrections_include_private_method
    error = assert_raise(NoMethodError){ @user.friend }

    assert_correction :friends, error.corrections
    assert_match "Did you mean?  friends", get_message(error)
  end

  def test_corrections_include_method_from_module
    error = assert_raise(NoMethodError){ @user.fr0m_module }

    assert_correction :from_module, error.corrections
    assert_match "Did you mean?  from_module", get_message(error)
  end

  def test_corrections_include_class_method
    error = assert_raise(NoMethodError){ User.l0ad }

    assert_correction :load, error.corrections
    assert_match "Did you mean?  load", get_message(error)
  end

  def test_private_methods_should_not_be_suggested
    error = assert_raise(NoMethodError){ User.new.the_protected_method }
    refute_includes error.corrections, :the_protected_method

    error = assert_raise(NoMethodError){ User.new.the_private_method }
    refute_includes error.corrections, :the_private_method
  end

  def test_corrections_when_private_method_is_called_with_args
    error = assert_raise(NoMethodError){ @user.call_incorrect_private_method }

    assert_correction :raise, error.corrections
    assert_match "Did you mean?  raise", get_message(error)
  end

  def test_exclude_methods_on_nil
    error = assert_raise(NoMethodError){ nil.map }
    assert_empty error.corrections
  end

  def test_does_not_exclude_custom_methods_on_nil
    def nil.empty?
    end

    error = assert_raise(NoMethodError){ nil.empty }
    assert_correction :empty?, error.corrections
  ensure
    NilClass.class_eval { undef empty? }
  end

  def test_does_not_append_suggestions_twice
    error = assert_raise NoMethodError do
      begin
        @user.firstname
      rescue NoMethodError => e
        raise e, e.message, e.backtrace
      end
    end

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

  def test_does_not_append_suggestions_three_times
    error = assert_raise NoMethodError do
      begin
        @user.raise_no_method_error
      rescue NoMethodError => e
        raise e, e.message, e.backtrace
      end
    end

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

  def test_suggests_corrections_on_nested_error
    error = assert_raise NoMethodError do
      begin
        @user.firstname
      rescue NoMethodError
        @user.firstname
      end
    end

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

  def test_suggests_yield
    error = assert_raise(NoMethodError) { yeild(1) }

    assert_correction :yield, error.corrections
    assert_match "Did you mean?  yield", get_message(error)
  end

  def test_does_not_suggest_yield
    error = assert_raise(NoMethodError) { 1.yeild }

    assert_correction [], error.corrections
    assert_not_match(/Did you mean\? +yield/, get_message(error))
  end if RUBY_ENGINE != "jruby"
end