summaryrefslogtreecommitdiff
path: root/test/did_you_mean
diff options
context:
space:
mode:
Diffstat (limited to 'test/did_you_mean')
-rw-r--r--test/did_you_mean/core_ext/test_name_error_extension.rb27
-rw-r--r--test/did_you_mean/helper.rb14
-rw-r--r--test/did_you_mean/spell_checking/test_key_name_check.rb14
-rw-r--r--test/did_you_mean/spell_checking/test_method_name_check.rb38
-rw-r--r--test/did_you_mean/spell_checking/test_pattern_key_name_check.rb20
-rw-r--r--test/did_you_mean/spell_checking/test_require_path_check.rb6
-rw-r--r--test/did_you_mean/spell_checking/test_variable_name_check.rb36
-rw-r--r--test/did_you_mean/test_ractor_compatibility.rb117
-rw-r--r--test/did_you_mean/test_spell_checker.rb1
-rw-r--r--test/did_you_mean/test_verbose_formatter.rb38
10 files changed, 231 insertions, 80 deletions
diff --git a/test/did_you_mean/core_ext/test_name_error_extension.rb b/test/did_you_mean/core_ext/test_name_error_extension.rb
index 9dc08dbde3..116c7cd7b9 100644
--- a/test/did_you_mean/core_ext/test_name_error_extension.rb
+++ b/test/did_you_mean/core_ext/test_name_error_extension.rb
@@ -1,7 +1,9 @@
require_relative '../helper'
class NameErrorExtensionTest < Test::Unit::TestCase
- SPELL_CHECKERS = DidYouMean::SPELL_CHECKERS
+ include DidYouMean::TestHelper
+
+ SPELL_CHECKERS = DidYouMean.spell_checkers
class TestSpellChecker
def initialize(*); end
@@ -9,18 +11,23 @@ class NameErrorExtensionTest < Test::Unit::TestCase
end
def setup
- @org, SPELL_CHECKERS['NameError'] = SPELL_CHECKERS['NameError'], TestSpellChecker
+ @original_spell_checker = DidYouMean.spell_checkers['NameError']
+ DidYouMean.correct_error(NameError, TestSpellChecker)
@error = assert_raise(NameError){ doesnt_exist }
end
def teardown
- SPELL_CHECKERS['NameError'] = @org
+ DidYouMean.correct_error(NameError, @original_spell_checker)
end
def test_message
- assert_match(/Did you mean\? does_exist/, @error.to_s)
- assert_match(/Did you mean\? does_exist/, @error.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
@@ -28,8 +35,8 @@ class NameErrorExtensionTest < Test::Unit::TestCase
raise NameError, "uninitialized constant Object"
end
- error.to_s
- assert_equal 1, error.to_s.scan("Did you mean?").count
+ get_message(error)
+ assert_equal 1, get_message(error).scan("Did you mean?").count
end
def test_correctable_error_objects_are_dumpable
@@ -40,9 +47,9 @@ class NameErrorExtensionTest < Test::Unit::TestCase
e
end
- error.to_s
+ get_message(error)
- assert_equal "undefined method `sizee' for #<File:test_name_error_extension.rb (closed)>",
- Marshal.load(Marshal.dump(error)).original_message
+ assert_match(/^undefined method [`']sizee' for /,
+ Marshal.load(Marshal.dump(error)).original_message)
end
end
diff --git a/test/did_you_mean/helper.rb b/test/did_you_mean/helper.rb
index d8aa41c3d1..d40d58d95d 100644
--- a/test/did_you_mean/helper.rb
+++ b/test/did_you_mean/helper.rb
@@ -4,6 +4,10 @@ module DidYouMean
module TestHelper
class << self
attr_reader :root
+
+ def ractor_compatible?
+ defined?(Ractor) && RUBY_VERSION >= "3.1.0"
+ end
end
if File.file?(File.expand_path('../lib/did_you_mean.rb', __dir__))
@@ -25,5 +29,15 @@ module DidYouMean
def assert_correction(expected, array)
assert_equal Array(expected), array, "Expected #{array.inspect} to only include #{expected.inspect}"
end
+
+ def get_message(err)
+ if err.respond_to?(:detailed_message)
+ err.detailed_message(highlight: false)
+ else
+ err.to_s
+ end
+ end
+
+ module_function :get_message
end
end
diff --git a/test/did_you_mean/spell_checking/test_key_name_check.rb b/test/did_you_mean/spell_checking/test_key_name_check.rb
index ea05ff69e4..2f246f04d7 100644
--- a/test/did_you_mean/spell_checking/test_key_name_check.rb
+++ b/test/did_you_mean/spell_checking/test_key_name_check.rb
@@ -8,11 +8,11 @@ class KeyNameCheckTest < Test::Unit::TestCase
error = assert_raise(KeyError) { hash.fetch(:bax) }
assert_correction ":bar", error.corrections
- assert_match "Did you mean? :bar", error.to_s
+ assert_match "Did you mean? :bar", get_message(error)
error = assert_raise(KeyError) { hash.fetch("fooo") }
assert_correction %("foo"), error.corrections
- assert_match %(Did you mean? "foo"), error.to_s
+ assert_match %(Did you mean? "foo"), get_message(error)
end
def test_corrects_hash_key_name_with_fetch_values
@@ -20,11 +20,11 @@ class KeyNameCheckTest < Test::Unit::TestCase
error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, :bax) }
assert_correction ":bar", error.corrections
- assert_match "Did you mean? :bar", error.to_s
+ assert_match "Did you mean? :bar", get_message(error)
error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, "fooo") }
assert_correction %("foo"), error.corrections
- assert_match %(Did you mean? "foo"), error.to_s
+ assert_match %(Did you mean? "foo"), get_message(error)
end
def test_correct_symbolized_hash_keys_with_string_value
@@ -32,13 +32,13 @@ class KeyNameCheckTest < Test::Unit::TestCase
error = assert_raise(KeyError) { hash.fetch('foo_1') }
assert_correction %(:foo_1), error.corrections
- assert_match %(Did you mean? :foo_1), error.to_s
+ assert_match %(Did you mean? :foo_1), get_message(error)
end
def test_corrects_sprintf_key_name
error = assert_raise(KeyError) { sprintf("%<foo>d", {fooo: 1}) }
assert_correction ":fooo", error.corrections
- assert_match "Did you mean? :fooo", error.to_s
+ assert_match "Did you mean? :fooo", get_message(error)
end
def test_corrects_env_key_name
@@ -46,7 +46,7 @@ class KeyNameCheckTest < Test::Unit::TestCase
ENV["BAR"] = "2"
error = assert_raise(KeyError) { ENV.fetch("BAX") }
assert_correction %("BAR"), error.corrections
- assert_match %(Did you mean? "BAR"), error.to_s
+ assert_match %(Did you mean? "BAR"), get_message(error)
ensure
ENV.delete("FOO")
ENV.delete("BAR")
diff --git a/test/did_you_mean/spell_checking/test_method_name_check.rb b/test/did_you_mean/spell_checking/test_method_name_check.rb
index 6e14e6acc4..4daaf7cec7 100644
--- a/test/did_you_mean/spell_checking/test_method_name_check.rb
+++ b/test/did_you_mean/spell_checking/test_method_name_check.rb
@@ -4,6 +4,8 @@ class MethodNameCheckTest < Test::Unit::TestCase
include DidYouMean::TestHelper
class User
+ attr_writer :writer
+ attr_reader :reader
def friends; end
def first_name; end
def descendants; end
@@ -41,28 +43,28 @@ class MethodNameCheckTest < Test::Unit::TestCase
error = assert_raise(NoMethodError){ @user.flrst_name }
assert_correction :first_name, error.corrections
- assert_match "Did you mean? first_name", error.to_s
+ 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", error.to_s
+ 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", error.to_s
+ 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", error.to_s
+ assert_match "Did you mean? load", get_message(error)
end
def test_private_methods_should_not_be_suggested
@@ -77,7 +79,7 @@ class MethodNameCheckTest < Test::Unit::TestCase
error = assert_raise(NoMethodError){ @user.call_incorrect_private_method }
assert_correction :raise, error.corrections
- assert_match "Did you mean? raise", error.to_s
+ assert_match "Did you mean? raise", get_message(error)
end
def test_exclude_methods_on_nil
@@ -104,7 +106,7 @@ class MethodNameCheckTest < Test::Unit::TestCase
end
end
- assert_equal 1, error.to_s.scan(/Did you mean/).count
+ assert_equal 1, get_message(error).scan(/Did you mean/).count
end
def test_does_not_append_suggestions_three_times
@@ -116,7 +118,7 @@ class MethodNameCheckTest < Test::Unit::TestCase
end
end
- assert_equal 1, error.to_s.scan(/Did you mean/).count
+ assert_equal 1, get_message(error).scan(/Did you mean/).count
end
def test_suggests_corrections_on_nested_error
@@ -128,20 +130,36 @@ class MethodNameCheckTest < Test::Unit::TestCase
end
end
- assert_equal 1, error.to_s.scan(/Did you mean/).count
+ 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", error.to_s
+ 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/, error.to_s)
+ assert_not_match(/Did you mean\? +yield/, get_message(error))
end if RUBY_ENGINE != "jruby"
+
+ # Do not suggest `name=` for `name`
+ def test_does_not_suggest_writer
+ error = assert_raise(NoMethodError) { @user.writer }
+
+ assert_correction [], error.corrections
+ assert_not_match(/Did you mean\? writer=/, get_message(error))
+ end
+
+ # Do not suggest `name` for `name=`
+ def test_does_not_suggest_reader
+ error = assert_raise(NoMethodError) { @user.reader = 1 }
+
+ assert_correction [], error.corrections
+ assert_not_match(/Did you mean\? reader/, get_message(error))
+ end
end
diff --git a/test/did_you_mean/spell_checking/test_pattern_key_name_check.rb b/test/did_you_mean/spell_checking/test_pattern_key_name_check.rb
new file mode 100644
index 0000000000..10f973802b
--- /dev/null
+++ b/test/did_you_mean/spell_checking/test_pattern_key_name_check.rb
@@ -0,0 +1,20 @@
+require_relative '../helper'
+
+return if !defined?(::NoMatchingPatternKeyError)
+
+class PatternKeyNameCheckTest < Test::Unit::TestCase
+ include DidYouMean::TestHelper
+
+ def test_corrects_hash_key_name_with_single_pattern_match
+ error = assert_raise(NoMatchingPatternKeyError) do
+ eval(<<~RUBY, binding, __FILE__, __LINE__)
+ hash = {foo: 1, bar: 2, baz: 3}
+ hash => {fooo:}
+ fooo = 1 # suppress "unused variable: fooo" warning
+ RUBY
+ end
+
+ assert_correction ":foo", error.corrections
+ assert_match "Did you mean? :foo", get_message(error)
+ end
+end
diff --git a/test/did_you_mean/spell_checking/test_require_path_check.rb b/test/did_you_mean/spell_checking/test_require_path_check.rb
index f67fab0568..d6c06e9999 100644
--- a/test/did_you_mean/spell_checking/test_require_path_check.rb
+++ b/test/did_you_mean/spell_checking/test_require_path_check.rb
@@ -11,7 +11,7 @@ class RequirePathCheckTest < Test::Unit::TestCase
end
assert_correction 'ostruct', error.corrections
- assert_match "Did you mean? ostruct", error.to_s
+ assert_match "Did you mean? ostruct", get_message(error)
end
def test_load_error_from_require_for_nested_files_has_suggestions
@@ -20,13 +20,13 @@ class RequirePathCheckTest < Test::Unit::TestCase
end
assert_correction 'net/http', error.corrections
- assert_match "Did you mean? net/http", error.to_s
+ assert_match "Did you mean? net/http", get_message(error)
error = assert_raise LoadError do
require 'net-http'
end
assert_correction ['net/http', 'net/https'], error.corrections
- assert_match "Did you mean? net/http", error.to_s
+ assert_match "Did you mean? net/http", get_message(error)
end
end
diff --git a/test/did_you_mean/spell_checking/test_variable_name_check.rb b/test/did_you_mean/spell_checking/test_variable_name_check.rb
index 193e2b7520..0350b62660 100644
--- a/test/did_you_mean/spell_checking/test_variable_name_check.rb
+++ b/test/did_you_mean/spell_checking/test_variable_name_check.rb
@@ -39,7 +39,7 @@ class VariableNameCheckTest < Test::Unit::TestCase
end
assert_correction :first_name, error.corrections
- assert_match "Did you mean? first_name", error.to_s
+ assert_match "Did you mean? first_name", get_message(error)
end
def test_corrections_include_method_from_module
@@ -48,7 +48,7 @@ class VariableNameCheckTest < Test::Unit::TestCase
end
assert_correction :from_module, error.corrections
- assert_match "Did you mean? from_module", error.to_s
+ assert_match "Did you mean? from_module", get_message(error)
end
def test_corrections_include_local_variable_name
@@ -57,7 +57,7 @@ class VariableNameCheckTest < Test::Unit::TestCase
error = (eprson rescue $!) # Do not use @assert_raise here as it changes a scope.
assert_correction :person, error.corrections
- assert_match "Did you mean? person", error.to_s
+ assert_match "Did you mean? person", get_message(error)
end
end
@@ -81,30 +81,30 @@ class VariableNameCheckTest < Test::Unit::TestCase
end
assert_correction :false, false_error.corrections
- assert_match "Did you mean? false", false_error.to_s
+ assert_match "Did you mean? false", get_message(false_error)
assert_correction :true, true_error.corrections
- assert_match "Did you mean? true", true_error.to_s
+ assert_match "Did you mean? true", get_message(true_error)
assert_correction :nil, nil_error.corrections
- assert_match "Did you mean? nil", nil_error.to_s
+ assert_match "Did you mean? nil", get_message(nil_error)
assert_correction :__FILE__, file_error.corrections
- assert_match "Did you mean? __FILE__", file_error.to_s
+ assert_match "Did you mean? __FILE__", get_message(file_error)
end
def test_suggests_yield
error = assert_raise(NameError) { yeild }
assert_correction :yield, error.corrections
- assert_match "Did you mean? yield", error.to_s
+ assert_match "Did you mean? yield", get_message(error)
end
def test_corrections_include_instance_variable_name
error = assert_raise(NameError){ @user.to_s }
assert_correction :@email_address, error.corrections
- assert_match "Did you mean? @email_address", error.to_s
+ assert_match "Did you mean? @email_address", get_message(error)
end
def test_corrections_include_private_method
@@ -113,7 +113,7 @@ class VariableNameCheckTest < Test::Unit::TestCase
end
assert_correction :cia_codename, error.corrections
- assert_match "Did you mean? cia_codename", error.to_s
+ assert_match "Did you mean? cia_codename", get_message(error)
end
@@does_exist = true
@@ -122,7 +122,7 @@ class VariableNameCheckTest < Test::Unit::TestCase
error = assert_raise(NameError){ @@doesnt_exist }
assert_correction :@@does_exist, error.corrections
- assert_match "Did you mean? @@does_exist", error.to_s
+ assert_match "Did you mean? @@does_exist", get_message(error)
end
def test_struct_name_error
@@ -130,11 +130,23 @@ class VariableNameCheckTest < Test::Unit::TestCase
error = assert_raise(NameError){ value[:doesnt_exist] }
assert_correction [:does_exist, :does_exist=], error.corrections
- assert_match "Did you mean? does_exist", error.to_s
+ assert_match "Did you mean? does_exist", get_message(error)
end
def test_exclude_typical_incorrect_suggestions
error = assert_raise(NameError){ foo }
assert_empty error.corrections
end
+
+ def test_exclude_duplicates_with_same_name
+ error = assert_raise(NameError) do
+ eval(<<~RUBY, binding, __FILE__, __LINE__)
+ bar = 1
+ def bar;end
+ zar
+ RUBY
+ end
+
+ assert_correction [:bar], error.corrections
+ end
end
diff --git a/test/did_you_mean/test_ractor_compatibility.rb b/test/did_you_mean/test_ractor_compatibility.rb
new file mode 100644
index 0000000000..7385f10612
--- /dev/null
+++ b/test/did_you_mean/test_ractor_compatibility.rb
@@ -0,0 +1,117 @@
+require_relative './helper'
+
+return if not DidYouMean::TestHelper.ractor_compatible?
+
+class RactorCompatibilityTest < Test::Unit::TestCase
+ def test_class_name_suggestion_works_in_ractor
+ assert_ractor(<<~CODE, require_relative: "helper")
+ class ::Book; end
+ include DidYouMean::TestHelper
+ error = Ractor.new {
+ begin
+ Boook
+ rescue NameError => e
+ e.corrections # It is important to call the #corrections method within Ractor.
+ e
+ end
+ }.take
+
+ assert_correction "Book", error.corrections
+ CODE
+ end
+
+ def test_key_name_suggestion_works_in_ractor
+ assert_ractor(<<~CODE, require_relative: "helper")
+ include DidYouMean::TestHelper
+ error = Ractor.new {
+ begin
+ hash = { "foo" => 1, bar: 2 }
+
+ hash.fetch(:bax)
+ rescue KeyError => e
+ e.corrections # It is important to call the #corrections method within Ractor.
+ e
+ end
+ }.take
+
+ assert_correction ":bar", error.corrections
+ assert_match "Did you mean? :bar", get_message(error)
+ CODE
+ end
+
+ def test_method_name_suggestion_works_in_ractor
+ assert_ractor(<<~CODE, require_relative: "helper")
+ include DidYouMean::TestHelper
+ error = Ractor.new {
+ begin
+ self.to__s
+ rescue NoMethodError => e
+ e.corrections # It is important to call the #corrections method within Ractor.
+ e
+ end
+ }.take
+
+ assert_correction :to_s, error.corrections
+ assert_match "Did you mean? to_s", get_message(error)
+ CODE
+ end
+
+ if defined?(::NoMatchingPatternKeyError)
+ def test_pattern_key_name_suggestion_works_in_ractor
+ assert_ractor(<<~CODE, require_relative: "helper")
+ include DidYouMean::TestHelper
+ error = Ractor.new {
+ begin
+ eval(<<~RUBY, binding, __FILE__, __LINE__)
+ hash = {foo: 1, bar: 2, baz: 3}
+ hash => {fooo:}
+ fooo = 1 # suppress "unused variable: fooo" warning
+ RUBY
+ rescue NoMatchingPatternKeyError => e
+ e.corrections # It is important to call the #corrections method within Ractor.
+ e
+ end
+ }.take
+
+ assert_correction ":foo", error.corrections
+ assert_match "Did you mean? :foo", get_message(error)
+ CODE
+ end
+ end
+
+ def test_can_raise_other_name_error_in_ractor
+ assert_ractor(<<~CODE, require_relative: "helper")
+ class FirstNameError < NameError; end
+ include DidYouMean::TestHelper
+ error = Ractor.new {
+ begin
+ raise FirstNameError, "Other name error"
+ rescue FirstNameError => e
+ e.corrections # It is important to call the #corrections method within Ractor.
+ e
+ end
+ }.take
+
+ assert_not_match(/Did you mean\?/, error.message)
+ CODE
+ end
+
+ def test_variable_name_suggestion_works_in_ractor
+ assert_ractor(<<~CODE, require_relative: "helper")
+ include DidYouMean::TestHelper
+ error = Ractor.new {
+ in_ractor = in_ractor = 1
+
+ begin
+ in_reactor
+ rescue NameError => e
+ e.corrections # It is important to call the #corrections method within Ractor.
+ e
+ end
+ }.take
+
+ assert_correction :in_ractor, error.corrections
+ assert_match "Did you mean? in_ractor", get_message(error)
+ CODE
+ end
+end
diff --git a/test/did_you_mean/test_spell_checker.rb b/test/did_you_mean/test_spell_checker.rb
index 98460b4d94..8445380de3 100644
--- a/test/did_you_mean/test_spell_checker.rb
+++ b/test/did_you_mean/test_spell_checker.rb
@@ -10,6 +10,7 @@ class SpellCheckerTest < Test::Unit::TestCase
assert_spell 'eval', input: 'veal', dictionary: ['email', 'fail', 'eval']
assert_spell 'sub!', input: 'suv!', dictionary: ['sub', 'gsub', 'sub!']
assert_spell 'sub', input: 'suv', dictionary: ['sub', 'gsub', 'sub!']
+ assert_spell 'Foo', input: 'FOo', dictionary: ['Foo', 'FOo']
assert_spell %w(gsub! gsub), input: 'gsuv!', dictionary: %w(sub gsub gsub!)
assert_spell %w(sub! sub gsub!), input: 'ssub!', dictionary: %w(sub sub! gsub gsub!)
diff --git a/test/did_you_mean/test_verbose_formatter.rb b/test/did_you_mean/test_verbose_formatter.rb
deleted file mode 100644
index 411f175180..0000000000
--- a/test/did_you_mean/test_verbose_formatter.rb
+++ /dev/null
@@ -1,38 +0,0 @@
-require_relative './helper'
-
-class VerboseFormatterTest < Test::Unit::TestCase
- class ErrorHighlightDummyFormatter
- def message_for(spot)
- ""
- end
- end
-
- def setup
- require_relative File.join(DidYouMean::TestHelper.root, 'verbose')
-
- DidYouMean.formatter = DidYouMean::VerboseFormatter.new
-
- if defined?(ErrorHighlight)
- @error_highlight_old_formatter = ErrorHighlight.formatter
- ErrorHighlight.formatter = ErrorHighlightDummyFormatter.new
- end
- end
-
- def teardown
- DidYouMean.formatter = DidYouMean::PlainFormatter.new
-
- if defined?(ErrorHighlight)
- ErrorHighlight.formatter = @error_highlight_old_formatter
- end
- end
-
- def test_message
- @error = assert_raise(NoMethodError){ 1.zeor? }
-
- assert_match <<~MESSAGE.strip, @error.message
- undefined method `zeor?' for 1:Integer
-
- Did you mean? zero?
- MESSAGE
- end
-end