summaryrefslogtreecommitdiff
path: root/test/did_you_mean
diff options
context:
space:
mode:
authorYuki Nishijima <yk.nishijima@gmail.com>2021-12-21 19:03:10 +0900
committerYuki Nishijima <yk.nishijima@gmail.com>2021-12-21 19:03:25 +0900
commit505dfae05d56d844ea150676edb87850a406d071 (patch)
tree76ecd819384b101f76c7d4179922ea41bf4a6e98 /test/did_you_mean
parent12fa4f2aceff06aeff028a3c72ec8791f78bfdb0 (diff)
downloadruby-505dfae05d56d844ea150676edb87850a406d071.tar.gz
* gems/default_gems: Sync did_you_mean
Diffstat (limited to 'test/did_you_mean')
-rw-r--r--test/did_you_mean/core_ext/test_name_error_extension.rb7
-rw-r--r--test/did_you_mean/helper.rb4
-rw-r--r--test/did_you_mean/test_ractor_compatibility.rb102
3 files changed, 110 insertions, 3 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..91871cda9a 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,7 @@
require_relative '../helper'
class NameErrorExtensionTest < Test::Unit::TestCase
- SPELL_CHECKERS = DidYouMean::SPELL_CHECKERS
+ SPELL_CHECKERS = DidYouMean.spell_checkers
class TestSpellChecker
def initialize(*); end
@@ -9,13 +9,14 @@ 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
diff --git a/test/did_you_mean/helper.rb b/test/did_you_mean/helper.rb
index d8aa41c3d1..7cb7b10282 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__))
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..2970793e3f
--- /dev/null
+++ b/test/did_you_mean/test_ractor_compatibility.rb
@@ -0,0 +1,102 @@
+return if not DidYouMean::TestHelper.ractor_compatible?
+
+require_relative './helper'
+
+class RactorCompatibilityTest < Test::Unit::TestCase
+ include DidYouMean::TestHelper
+
+ class ::Book; end
+ class FirstNameError < NameError; end
+
+ def test_class_name_suggestion_works_in_ractor
+ 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
+ end
+
+ def test_key_name_suggestion_works_in_ractor
+ 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", error.to_s
+ end
+
+ def test_method_name_suggestion_works_in_ractor
+ 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", error.to_s
+ end
+
+ if defined?(::NoMatchingPatternKeyError)
+ def test_pattern_key_name_suggestion_works_in_ractor
+ 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", error.to_s
+ end
+ end
+
+ def test_can_raise_other_name_error_in_ractor
+ 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)
+ end
+
+ def test_variable_name_suggestion_works_in_ractor
+ 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", error.to_s
+ end
+end