summaryrefslogtreecommitdiff
path: root/test/did_you_mean/spell_checking/test_key_name_check.rb
blob: 2f246f04d73702fdfbbd40ef232e55576cd49476 (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
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", get_message(error)

    error = assert_raise(KeyError) { hash.fetch("fooo") }
    assert_correction %("foo"), error.corrections
    assert_match %(Did you mean?  "foo"), get_message(error)
  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", get_message(error)

    error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, "fooo") }
    assert_correction %("foo"), error.corrections
    assert_match %(Did you mean?  "foo"), get_message(error)
  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), 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", get_message(error)
  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"), get_message(error)
  ensure
    ENV.delete("FOO")
    ENV.delete("BAR")
  end
end