summaryrefslogtreecommitdiff
path: root/test/did_you_mean/test_ractor_compatibility.rb
blob: 7385f106122888bb4a3c88fdf3bea50c68832e9a (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
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