Permalink
Browse files

Added words from the documentation tree to the session dictionary to …

…avoid false positivies
  • Loading branch information...
1 parent f438c5e commit dc4e0e92862a5d0ed47b62d7d8a43bd6014949bf @drbrain drbrain committed Apr 6, 2012
Showing with 191 additions and 19 deletions.
  1. +58 −2 lib/rdoc/generator/spellcheck.rb
  2. +133 −17 test/test_rdoc_generator_spellcheck.rb
@@ -27,6 +27,8 @@ class RDoc::Generator::Spellcheck
SpellLanguage = Object.new
+ attr_reader :spell # :nodoc:
+
##
# Adds rdoc-spellcheck options to the rdoc command
@@ -67,16 +69,29 @@ def initialize options # :not-new:
end
##
+ # Adds +name+ to the dictionary, splitting the word on '_' (a character
+ # Aspell does not allow)
+
+ def add_name name
+ name.split('_').each do |part|
+ @spell.add_to_session part
+ end
+ end
+
+ ##
# Returns a report of misspelled words in +comment+. The report contains
# each misspelled word and its offset in the comment's text.
def find_misspelled comment
report = []
- comment.text.scan(/[a-z_]+/i) do |word|
+ comment.text.scan(/[a-z]+/i) do |word|
next if @spell.check word
- report << [word, $`.length + 1]
+ offset = $`.length
+ offset = offset.zero? ? 0 : offset + 1
+
+ report << [word, offset]
end
report
@@ -86,6 +101,8 @@ def find_misspelled comment
# Creates the spelling report
def generate files
+ setup_dictionary
+
report = []
RDoc::TopLevel.all_classes_and_modules.each do |mod|
@@ -166,6 +183,45 @@ def misspellings_for name, comment, location
end
##
+ # Adds file names, class names, module names, method names, etc. from the
+ # documentation tree to the session spelling dictionary.
+
+ def setup_dictionary
+ RDoc::TopLevel.all_classes_and_modules.each do |mod|
+ @spell.add_to_session mod.name
+
+ mod.each_include do |incl|
+ add_name incl.name
+ end
+
+ mod.each_constant do |const|
+ add_name const.name
+ end
+
+ mod.each_attribute do |attr|
+ add_name attr.name
+ end
+
+ mod.each_method do |meth|
+ add_name meth.name
+ end
+
+ aliases = mod.aliases + mod.external_aliases
+
+ aliases.each do |alas|
+ add_name alas.old_name
+ add_name alas.new_name
+ end
+ end
+
+ RDoc::TopLevel.all_files.each do |file|
+ file.absolute_name.split(%r%[/\\.]%).each do |part|
+ add_name part
+ end
+ end
+ end
+
+ ##
# Creates suggestion text for the misspelled +word+ at +offset+ in +text+
def suggestion_text text, word, offset
@@ -9,7 +9,8 @@ class TestRDocGeneratorSpellcheck < RDoc::TestCase
def setup
super
- @top_level = RDoc::TopLevel.new 'file.rb'
+ @top_level = RDoc::TopLevel.new 'funkify_thingus.rb'
+ @top_level.comment = comment 'funkify_thingus'
@SC = RDoc::Generator::Spellcheck
@options = RDoc::Options.new
@@ -20,6 +21,13 @@ def setup
@text = 'Hello, this class has real gud spelling!'
end
+ def test_add_name
+ @sc.add_name 'funkify_thingus'
+
+ assert @sc.spell.check('funkify'), 'funkify not added to wordlist'
+ assert @sc.spell.check('thingus'), 'thingus not added to wordlist'
+ end
+
def test_class_setup_options_default
orig_lang = ENV['LANG']
ENV['LANG'] = 'en_US.UTF-8'
@@ -66,6 +74,17 @@ def test_find_misspelled
assert_equal 28, offset
end
+ def test_find_misspelled_underscore
+ c = comment 'gud_method'
+
+ report = @sc.find_misspelled c
+
+ word, offset = report.shift
+
+ assert_equal 'gud', word
+ assert_equal 0, offset
+ end
+
def test_generate_alias
klass = @top_level.add_class RDoc::NormalClass, 'Object'
@@ -85,8 +104,8 @@ def test_generate_alias
assert_empty err
- assert_match %r%^Object alias old new in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^Object alias old new in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_generate_attribute
@@ -103,8 +122,8 @@ def test_generate_attribute
assert_empty err
- assert_match %r%^Object\.attr_accessor :attr in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^Object\.attr_accessor :attr in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_generate_class
@@ -119,8 +138,8 @@ def test_generate_class
assert_empty err
- assert_match %r%^class Object in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^class Object in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_generate_constant
@@ -137,8 +156,8 @@ def test_generate_constant
assert_empty err
- assert_match %r%^Object::CONSTANT in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^Object::CONSTANT in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_generate_correct
@@ -165,8 +184,8 @@ def test_generate_file
assert_empty err
- assert_match %r%^In file\.rb:%, out # actual file name would be different
- assert_match %r%^"gud"%, out
+ assert_match %r%^In funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_generate_include
@@ -183,8 +202,8 @@ def test_generate_include
assert_empty err
- assert_match %r%^Object\.include INCLUDE in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^Object\.include INCLUDE in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_generate_method
@@ -202,17 +221,17 @@ def test_generate_method
assert_empty err
- assert_match %r%^Object#method in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^Object#method in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_misspellings_for
out = @sc.misspellings_for 'class Object', comment(@text), @top_level
out = out.join "\n"
- assert_match %r%^class Object in file\.rb:%, out
- assert_match %r%^"gud"%, out
+ assert_match %r%^class Object in funkify_thingus\.rb:%, out
+ assert_match %r%^"gud"%, out
end
def test_misspellings_for_empty
@@ -221,6 +240,103 @@ def test_misspellings_for_empty
assert_empty out
end
+ def test_setup_dictionary_alias
+ klass = @top_level.add_class RDoc::NormalClass, 'Object'
+
+ meth = RDoc::AnyMethod.new nil, 'new'
+ meth.comment = comment ''
+ meth.record_location @top_level
+
+ klass.add_method meth
+ alas = RDoc::Alias.new nil, 'funkify_thingus', 'new', comment(@text)
+ alas.record_location @top_level
+
+ klass.add_alias alas
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('funkify'), 'funkify not added to wordlist'
+ assert @sc.spell.check('thingus'), 'thingus not added to wordlist'
+ end
+
+ def test_setup_dictionary_attribute
+ klass = @top_level.add_class RDoc::NormalClass, 'Object'
+
+ attribute = RDoc::Attr.new nil, 'funkify_thingus', 'RW', comment(@text)
+ attribute.record_location @top_level
+
+ klass.add_attribute attribute
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('FUNKIFY'), 'FUNKIFY not added to wordlist'
+ assert @sc.spell.check('THINGUS'), 'THINGUS not added to wordlist'
+ end
+
+ def test_setup_dictionary_class
+ klass = @top_level.add_class RDoc::NormalClass, 'FunkifyThingus'
+
+ c = comment @text
+ klass.add_comment c, @top_level
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('FunkifyThingus'),
+ 'FunkifyThingus not added to wordlist'
+ end
+
+ def test_setup_dictionary_constant
+ klass = @top_level.add_class RDoc::NormalClass, 'Object'
+
+ const = RDoc::Constant.new 'FUNKIFY_THINGUS', nil, comment(@text)
+ const.record_location @top_level
+
+ klass.add_constant const
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('FUNKIFY'), 'FUNKIFY not added to wordlist'
+ assert @sc.spell.check('THINGUS'), 'THINGUS not added to wordlist'
+ end
+
+ def test_setup_dictionary_file
+ RDoc::TopLevel.new 'funkify_thingus.rb'
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('funkify'), 'funkify not added to wordlist'
+ assert @sc.spell.check('thingus'), 'thingus not added to wordlist'
+ end
+
+ def test_setup_dictionary_include
+ klass = @top_level.add_class RDoc::NormalClass, 'Object'
+
+ incl = RDoc::Include.new 'FUNKIFY_THINGUS', comment(@text)
+ incl.record_location @top_level
+
+ klass.add_include incl
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('FUNKIFY'), 'FUNKIFY not added to wordlist'
+ assert @sc.spell.check('THINGUS'), 'THINGUS not added to wordlist'
+ end
+
+ def test_setup_dictionary_method
+ klass = @top_level.add_class RDoc::NormalClass, 'Object'
+
+ meth = RDoc::AnyMethod.new nil, 'funkify_thingus'
+ meth.record_location @top_level
+ meth.comment = comment @text, meth
+
+ klass.add_method meth
+
+ @sc.setup_dictionary
+
+ assert @sc.spell.check('funkify'), 'funkify not added to wordlist'
+ assert @sc.spell.check('thingus'), 'thingus not added to wordlist'
+ end
+
def test_suggestion_text
out = @sc.suggestion_text @text, 'gud', 28

0 comments on commit dc4e0e9

Please sign in to comment.