File: test_key_name_check.rb

package info (click to toggle)
ruby-did-you-mean 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 412 kB
  • sloc: ruby: 1,755; makefile: 7
file content (54 lines) | stat: -rw-r--r-- 1,796 bytes parent folder | download | duplicates (5)
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