/
test_rdoc_generator_spellcheck.rb
228 lines (151 loc) · 4.73 KB
/
test_rdoc_generator_spellcheck.rb
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
require 'rubygems'
require 'minitest/autorun'
require 'rdoc/generator/spellcheck'
require 'rdoc/test_case'
class TestRDocGeneratorSpellcheck < RDoc::TestCase
def setup
super
@top_level = RDoc::TopLevel.new 'file.rb'
@SC = RDoc::Generator::Spellcheck
@options = RDoc::Options.new
@options.spell_language = 'en_US'
@sc = @SC.new @options
@text = 'Hello, this class has real gud spelling!'
end
def test_class_setup_options_default
orig_lang = ENV['LANG']
ENV['LANG'] = 'en_US.UTF-8'
options = RDoc::Options.new
options.parse %w[--format spellcheck]
assert_equal 'en_US', options.spell_language
end
def test_class_setup_options_spell_language
options = RDoc::Options.new
options.parse %w[
--format spellcheck
--no-ignore-invalid
--spell-language en_GB
]
assert_equal 'en_GB', options.spell_language
end
def test_find_misspelled
c = comment @text
report = @sc.find_misspelled c
word, offset = report.shift
assert_equal 'gud', word
assert_equal 28, offset
end
def test_generate
klass = @top_level.add_class RDoc::NormalClass, 'Object'
c = comment @text
klass.add_comment c, @top_level
out, err = capture_io do
@sc.generate [@top_level]
end
assert_empty err
assert_match %r%^class Object in file\.rb:%, out
assert_match %r%^"gud"%, out
end
def test_generate_attribute
klass = @top_level.add_class RDoc::NormalClass, 'Object'
attribute = RDoc::Attr.new nil, 'attr', 'RW', comment(@text)
attribute.record_location @top_level
klass.add_attribute attribute
out, err = capture_io do
@sc.generate [@top_level]
end
assert_empty err
assert_match %r%^Object\.attr_accessor :attr in file\.rb:%, out
assert_match %r%^"gud"%, out
end
def test_generate_constant
klass = @top_level.add_class RDoc::NormalClass, 'Object'
const = RDoc::Constant.new 'CONSTANT', nil, comment(@text)
const.record_location @top_level
klass.add_constant const
out, err = capture_io do
@sc.generate [@top_level]
end
assert_empty err
assert_match %r%^Object::CONSTANT in file\.rb:%, out
assert_match %r%^"gud"%, out
end
def test_generate_correct
klass = @top_level.add_class RDoc::NormalClass, 'Object'
c = comment 'This class has perfect spelling!'
klass.add_comment c, @top_level
out, err = capture_io do
@sc.generate [@top_level]
end
assert_empty err
assert_equal "No misspellings found\n", out
end
def test_generate_include
klass = @top_level.add_class RDoc::NormalClass, 'Object'
incl = RDoc::Include.new 'INCLUDE', comment(@text)
incl.record_location @top_level
klass.add_include incl
out, err = capture_io do
@sc.generate [@top_level]
end
assert_empty err
assert_match %r%^Object\.include INCLUDE in file\.rb:%, out
assert_match %r%^"gud"%, out
end
def test_generate_method
klass = @top_level.add_class RDoc::NormalClass, 'Object'
meth = RDoc::AnyMethod.new nil, 'method'
meth.record_location @top_level
meth.comment = comment @text, meth
klass.add_method meth
out, err = capture_io do
@sc.generate [@top_level]
end
assert_empty err
assert_match %r%^Object#method in file\.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
end
def test_misspellings_for_empty
out = @sc.misspellings_for 'class Object', comment(''), @top_level
assert_empty out
end
def test_suggestion_text
out = @sc.suggestion_text @text, 'gud', 28
suggestions = suggest('gud').join ', '
expected = <<-EXPECTED
"...has real _\bg_\bu_\bd spelling!..."
"gud" suggestions:
\t#{suggestions}
EXPECTED
assert_equal expected, out
end
def test_suggestion_text_start
out = @sc.suggestion_text 'gud night world, see you tomorrow', 'gud', 0
suggestions = suggest('gud').join ', '
expected = <<-EXPECTED
"_\bg_\bu_\bd night wor..."
"gud" suggestions:
\t#{suggestions}
EXPECTED
assert_equal expected, out
end
def test_suggestion_text_end
out = @sc.suggestion_text 'you did real gud', 'gud', 14
suggestions = suggest('gud').join ', '
expected = <<-EXPECTED
"...did real _\bg_\bu_\bd"
"gud" suggestions:
\t#{suggestions}
EXPECTED
assert_equal expected, out
end
def suggest word
Aspell.new('en_US').suggest(word).first 5
end
end