summaryrefslogtreecommitdiff
path: root/test/did_you_mean/test_tree_spell_checker.rb
diff options
context:
space:
mode:
authorYuki Nishijima <yk.nishijima@gmail.com>2020-05-22 17:17:10 -0400
committerYuki Nishijima <yk.nishijima@gmail.com>2020-06-06 13:02:08 -0400
commite5f54465284b4505042fca10ace998e1d29c2313 (patch)
tree3770bc320b3cb7fdd7e5406bb1fbcd5c4c705c77 /test/did_you_mean/test_tree_spell_checker.rb
parent0c00a4176ba353d59d8c991428574ef2c2676674 (diff)
Sync did_you_mean
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3135
Diffstat (limited to 'test/did_you_mean/test_tree_spell_checker.rb')
-rw-r--r--test/did_you_mean/test_tree_spell_checker.rb203
1 files changed, 100 insertions, 103 deletions
diff --git a/test/did_you_mean/test_tree_spell_checker.rb b/test/did_you_mean/test_tree_spell_checker.rb
index b61a491e20..9ab1c1bc9e 100644
--- a/test/did_you_mean/test_tree_spell_checker.rb
+++ b/test/did_you_mean/test_tree_spell_checker.rb
@@ -1,11 +1,12 @@
-require 'set'
-require 'yaml'
+# frozen_string_literal: true
-require_relative './helper'
+require "yaml"
+
+require_relative "./helper"
class TreeSpellCheckerTest < Test::Unit::TestCase
- MINI_DIRECTORIES = YAML.load_file(File.expand_path('fixtures/mini_dir.yml', __dir__))
- RSPEC_DIRECTORIES = YAML.load_file(File.expand_path('fixtures/rspec_dir.yml', __dir__))
+ MINI_DIRECTORIES = YAML.load_file(File.expand_path("fixtures/mini_dir.yml", __dir__))
+ RSPEC_DIRECTORIES = YAML.load_file(File.expand_path("fixtures/rspec_dir.yml", __dir__))
def setup
@dictionary =
@@ -20,154 +21,150 @@ class TreeSpellCheckerTest < Test::Unit::TestCase
spec/models/gfsga_spec.rb
spec/controllers/vixen_controller_spec.rb
)
- @test_str = 'spek/modeks/confirns/viken_spec.rb'
- @tsp = DidYouMean::TreeSpellChecker.new(dictionary: @dictionary)
+ @test_str = "spek/modeks/confirns/viken_spec.rb"
+ @tree_spell_checker = DidYouMean::TreeSpellChecker.new(dictionary: @dictionary)
end
def test_corrupt_root
- word = 'test/verbose_formatter_test.rb'
- word_error = 'btets/cverbose_formatter_etst.rb suggestions'
- tsp = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES)
- s = tsp.correct(word_error).first
- assert_match s, word
+ assert_tree_spell "test/verbose_formatter_test.rb",
+ input: "btets/cverbose_formatter_etst.rb suggestions",
+ dictionary: MINI_DIRECTORIES
end
def test_leafless_state
- tsp = DidYouMean::TreeSpellChecker.new(dictionary: @dictionary.push('spec/features'))
- word = 'spec/modals/confirms/efgh_spec.rb'
- word_error = 'spec/modals/confirXX/efgh_spec.rb'
- s = tsp.correct(word_error).first
- assert_equal s, word
- s = tsp.correct('spec/featuresXX')
- assert_equal 'spec/features', s.first
+ assert_tree_spell "spec/modals/confirms/efgh_spec.rb",
+ input: "spec/modals/confirXX/efgh_spec.rb",
+ dictionary: [*@dictionary, "spec/features"]
+
+ assert_tree_spell "spec/features",
+ input: "spec/featuresXX",
+ dictionary: [*@dictionary, "spec/features"]
end
def test_rake_dictionary
- dict = %w(parallel:prepare parallel:create parallel:rake parallel:migrate)
- word_error = 'parallel:preprare'
- tsp = DidYouMean::TreeSpellChecker.new(dictionary: dict, separator: ':')
- s = tsp.correct(word_error).first
- assert_match s, 'parallel:prepare'
+ assert_tree_spell "parallel:prepare",
+ input: "parallel:preprare",
+ dictionary: %w[parallel:prepare parallel:create parallel:rake parallel:migrate],
+ separator: ":"
end
def test_special_words_mini
- tsp = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES)
- special_words_mini.each do |word, word_error|
- s = tsp.correct(word_error).first
- assert_match s, word
+ [
+ %w(test/fixtures/book.rb test/fixture/book.rb),
+ %w(test/edit_distance/jaro_winkler_test.rb test/edit_distace/jaro_winkler_test.rb),
+ %w(test/edit_distance/jaro_winkler_test.rb teste/dit_distane/jaro_winkler_test.rb),
+ %w(test/fixtures/book.rb test/fixturWes/book.rb),
+ %w(test/test_helper.rb tes!t/test_helper.rb),
+ %w(test/fixtures/book.rb test/hfixtures/book.rb),
+ %w(test/edit_distance/jaro_winkler_test.rb test/eidt_distance/jaro_winkler_test.@rb),
+ %w(test/spell_checker_test.rb test/spell_checke@r_test.rb),
+ %w(test/tree_spell_human_typo_test.rb testt/ree_spell_human_typo_test.rb),
+ %w(test/edit_distance/jaro_winkler_test.rb test/edit_distance/jaro_winkler_tuest.rb),
+ ].each do |expected, user_input|
+ assert_tree_spell expected, input: user_input, dictionary: MINI_DIRECTORIES
end
- end
- def test_special_words_rspec
- tsp = DidYouMean::TreeSpellChecker.new(dictionary: RSPEC_DIRECTORIES)
- special_words_rspec.each do |word, word_error|
- s = tsp.correct(word_error)
- assert_match s.first, word
- end
- end
-
- def special_words_rspec
[
- ['spec/rspec/core/formatters/exception_presenter_spec.rb','spec/rspec/core/formatters/eception_presenter_spec.rb'],
- ['spec/rspec/core/ordering_spec.rb', 'spec/spec/core/odrering_spec.rb'],
- ['spec/rspec/core/metadata_spec.rb', 'spec/rspec/core/metadata_spe.crb'],
- ['spec/support/mathn_integration_support.rb', 'spec/support/mathn_itegrtion_support.rb']
- ]
+ %w(test/spell_checking/variable_name_check_test.rb test/spell_checking/vriabl_ename_check_test.rb),
+ %w(test/spell_checking/key_name_check_test.rb tesit/spell_checking/key_name_choeck_test.rb),
+ ].each do |expected, user_input|
+ assert_equal expected, DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES).correct(user_input)[0]
+ end
end
- def special_words_mini
+ def test_special_words_rspec
[
- ['test/fixtures/book.rb', 'test/fixture/book.rb'],
- ['test/fixtures/book.rb', 'test/fixture/book.rb'],
- ['test/edit_distance/jaro_winkler_test.rb', 'test/edit_distace/jaro_winkler_test.rb'],
- ['test/edit_distance/jaro_winkler_test.rb', 'teste/dit_distane/jaro_winkler_test.rb'],
- ['test/fixtures/book.rb', 'test/fixturWes/book.rb'],
- ['test/test_helper.rb', 'tes!t/test_helper.rb'],
- ['test/fixtures/book.rb', 'test/hfixtures/book.rb'],
- ['test/edit_distance/jaro_winkler_test.rb', 'test/eidt_distance/jaro_winkler_test.@rb'],
- ['test/spell_checker_test.rb', 'test/spell_checke@r_test.rb'],
- ['test/tree_spell_human_typo_test.rb', 'testt/ree_spell_human_typo_test.rb'],
- ['test/spell_checking/variable_name_check_test.rb', 'test/spell_checking/vriabl_ename_check_test.rb'],
- ['test/spell_checking/key_name_check_test.rb', 'tesit/spell_checking/key_name_choeck_test.rb'],
- ['test/edit_distance/jaro_winkler_test.rb', 'test/edit_distance/jaro_winkler_tuest.rb']
- ]
+ %w(spec/rspec/core/formatters/exception_presenter_spec.rb spec/rspec/core/formatters/eception_presenter_spec.rb),
+ %w(spec/rspec/core/metadata_spec.rb spec/rspec/core/metadata_spe.crb),
+ %w(spec/rspec/core/ordering_spec.rb spec/spec/core/odrering_spec.rb),
+ %w(spec/support/mathn_integration_support.rb spec/support/mathn_itegrtion_support.rb),
+ ].each do |expected, user_input|
+ assert_tree_spell expected, input: user_input, dictionary: RSPEC_DIRECTORIES
+ end
end
def test_file_in_root
- word = 'test/spell_checker_test.rb'
- word_error = 'test/spell_checker_test.r'
- suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES).correct word_error
- assert_equal word, suggestions.first
+ assert_tree_spell "test/spell_checker_test.rb", input: "test/spell_checker_test.r", dictionary: MINI_DIRECTORIES
end
def test_no_plausible_states
- word_error = 'testspell_checker_test.rb'
- suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES).correct word_error
- assert_equal [], suggestions
+ assert_tree_spell [], input: "testspell_checker_test.rb", dictionary: MINI_DIRECTORIES
end
def test_no_plausible_states_with_augmentation
- word_error = 'testspell_checker_test.rb'
- suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES).correct word_error
- assert_equal [], suggestions
- suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES, augment: true).correct word_error
- assert_equal 'test/spell_checker_test.rb', suggestions.first
+ assert_tree_spell [], input: "testspell_checker_test.rb", dictionary: MINI_DIRECTORIES
+
+ suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES, augment: true).correct("testspell_checker_test.rb")
+
+ assert_equal suggestions.first, "test/spell_checker_test.rb"
end
def test_no_idea_with_augmentation
- word_error = 'test/spell_checking/key_name.rb'
- suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES).correct word_error
- assert_equal [], suggestions
- suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES, augment: true).correct word_error
- assert_equal 'test/spell_checking/key_name_check_test.rb', suggestions.first
+ assert_tree_spell [], input: "test/spell_checking/key_name.rb", dictionary: MINI_DIRECTORIES
+
+ suggestions = DidYouMean::TreeSpellChecker.new(dictionary: MINI_DIRECTORIES, augment: true).correct("test/spell_checking/key_name.rb")
+
+ assert_equal suggestions.first, "test/spell_checking/key_name_check_test.rb"
end
def test_works_out_suggestions
- exp = ['spec/models/concerns/vixen_spec.rb',
- 'spec/models/concerns/vixenus_spec.rb']
- suggestions = @tsp.correct(@test_str)
- assert_equal suggestions.to_set, exp.to_set
+ assert_tree_spell %w(spec/models/concerns/vixen_spec.rb spec/models/concerns/vixenus_spec.rb),
+ input: "spek/modeks/confirns/viken_spec.rb",
+ dictionary: %w(spec/models/concerns/vixen_spec.rb spec/models/concerns/vixenus_spec.rb)
end
def test_works_when_input_is_correct
- correct_input = 'spec/models/concerns/vixenus_spec.rb'
- suggestions = @tsp.correct correct_input
- assert_equal suggestions.first, correct_input
+ assert_tree_spell "spec/models/concerns/vixenus_spec.rb",
+ input: "spec/models/concerns/vixenus_spec.rb",
+ dictionary: @dictionary
end
def test_find_out_leaves_in_a_path
- path = 'spec/modals/confirms'
- names = @tsp.send(:find_leaves, path)
- assert_equal names.to_set, %w(abcd_spec.rb efgh_spec.rb).to_set
+ names = @tree_spell_checker.find_leaves("spec/modals/confirms")
+
+ assert_equal %w[abcd_spec.rb efgh_spec.rb], names
end
def test_works_out_nodes
- exp_paths = ['spec/models/concerns',
- 'spec/models/confirms',
- 'spec/modals/concerns',
- 'spec/modals/confirms',
- 'spec/controllers/concerns',
- 'spec/controllers/confirms'].to_set
- states = @tsp.send(:parse_dimensions)
- nodes = states[0].product(*states[1..-1])
- paths = @tsp.send(:possible_paths, nodes)
- assert_equal paths.to_set, exp_paths.to_set
+ exp_paths = ["spec/models/concerns",
+ "spec/models/confirms",
+ "spec/modals/concerns",
+ "spec/modals/confirms",
+ "spec/controllers/concerns",
+ "spec/controllers/confirms"]
+
+ states = @tree_spell_checker.dimensions
+ nodes = states[0].product(*states[1..-1])
+ paths = @tree_spell_checker.possible_paths(nodes)
+
+ assert_equal paths, exp_paths
end
def test_works_out_state_space
- suggestions = @tsp.send(:plausible_dimensions, @test_str)
- assert_equal suggestions, [["spec"], ["models", "modals"], ["confirms", "concerns"]]
+ suggestions = @tree_spell_checker.plausible_dimensions(@test_str)
+
+ assert_equal [["spec"], %w[models modals], %w[confirms concerns]], suggestions
end
def test_parses_dictionary
- states = @tsp.send(:parse_dimensions)
- assert_equal states, [["spec"], ["models", "modals", "controllers"], ["concerns", "confirms"]]
+ states = @tree_spell_checker.dimensions
+
+ assert_equal [["spec"], %w[models modals controllers], %w[concerns confirms]], states
end
def test_parses_elementary_dictionary
- dictionary = ['spec/models/user_spec.rb', 'spec/services/account_spec.rb']
- tsp = DidYouMean::TreeSpellChecker.new(dictionary: dictionary)
- states = tsp.send(:parse_dimensions)
- assert_equal states, [['spec'], ['models', 'services']]
+ dimensions = DidYouMean::TreeSpellChecker
+ .new(dictionary: %w(spec/models/user_spec.rb spec/services/account_spec.rb))
+ .dimensions
+
+ assert_equal [["spec"], %w[models services]], dimensions
+ end
+
+ private
+
+ def assert_tree_spell(expected, input:, dictionary:, separator: "/")
+ suggestions = DidYouMean::TreeSpellChecker.new(dictionary: dictionary, separator: separator).correct(input)
+
+ assert_equal Array(expected), suggestions, "Expected to suggest #{expected}, but got #{suggestions.inspect}"
end
end