Sha256: efdee239ec2ea2c373a6e593088686f42639f424226c2f47179721d3e86c64d3

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

require_relative '../helper'

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

  def test_corrects_hash_key_name_with_fetch
    hash = { "foo" => 1, bar: 2 }

    error = assert_raise(KeyError) { hash.fetch(:bax) }
    assert_correction ":bar", error.corrections
    assert_match "Did you mean?  :bar", error.to_s

    error = assert_raise(KeyError) { hash.fetch("fooo") }
    assert_correction %("foo"), error.corrections
    assert_match %(Did you mean?  "foo"), error.to_s
  end

  def test_corrects_hash_key_name_with_fetch_values
    hash = { "foo" => 1, bar: 2 }

    error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, :bax) }
    assert_correction ":bar", error.corrections
    assert_match "Did you mean?  :bar", error.to_s

    error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, "fooo") }
    assert_correction %("foo"), error.corrections
    assert_match %(Did you mean?  "foo"), error.to_s
  end

  def test_correct_symbolized_hash_keys_with_string_value
    hash = { foo_1: 1, bar_2: 2 }

    error = assert_raise(KeyError) { hash.fetch('foo_1') }
    assert_correction %(:foo_1), error.corrections
    assert_match %(Did you mean?  :foo_1), error.to_s
  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
  end

  def test_corrects_env_key_name
    ENV["FOO"] = "1"
    ENV["BAR"] = "2"
    error = assert_raise(KeyError) { ENV.fetch("BAX") }
    assert_correction %("BAR"), error.corrections
    assert_match %(Did you mean?  "BAR"), error.to_s
  ensure
    ENV.delete("FOO")
    ENV.delete("BAR")
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
did_you_mean-1.6.1 test/spell_checking/test_key_name_check.rb
did_you_mean-1.5.0 test/spell_checking/test_key_name_check.rb
did_you_mean-1.4.0 test/spell_checking/test_key_name_check.rb