require_relative '../helper'

class NameErrorExtensionTest < Test::Unit::TestCase
  SPELL_CHECKERS = DidYouMean::SPELL_CHECKERS

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

  def setup
    @org, SPELL_CHECKERS['NameError'] = SPELL_CHECKERS['NameError'], TestSpellChecker

    @error = assert_raise(NameError){ doesnt_exist }
  end

  def teardown
    SPELL_CHECKERS['NameError'] = @org
  end

  def test_message
    assert_match(/Did you mean\?  does_exist/, @error.to_s)
    assert_match(/Did you mean\?  does_exist/, @error.message)
  end

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

    error.to_s
    assert_equal 1, error.to_s.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').sizee }
      rescue NoMethodError => e
        e
      end

    error.to_s

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

require_relative '../helper'

class NameErrorExtensionTest < Test::Unit::TestCase
  SPELL_CHECKERS = DidYouMean::SPELL_CHECKERS

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

  def setup
    @org, SPELL_CHECKERS['NameError'] = SPELL_CHECKERS['NameError'], TestSpellChecker

    @error = assert_raise(NameError){ doesnt_exist }
  end

  def teardown
    SPELL_CHECKERS['NameError'] = @org
  end

  def test_message
    assert_match(/Did you mean\?  does_exist/, @error.to_s)
    assert_match(/Did you mean\?  does_exist/, @error.message)
  end

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

    error.to_s
    assert_equal 1, error.to_s.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

    error.to_s

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

Fix Leaked file descriptor in test/did_you_mean
Fix Leaked file descriptor in test/did_you_mean

https://github.com/ruby/ruby/commit/de74d2c3b0005048a2c4433bde68b9be10c86f01/checks?check_suite_id=336910877#step:19:131
```
Leaked file descriptor: NameErrorExtensionTest#test_correctable_error_objects_are_dumpable: 7 : #<File:test_name_error_extension.rb>
```

Original commit: https://github.com/ruby/ruby/commit/1a88adcd75d92cb36ef5b1d5f4aeb40e6fe4dcb7
