Class: TestTextAnalyzer

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/text_analyzer.rb

Instance Method Summary collapse

Instance Method Details

#test_analyze_hierarchy_with_arrayObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/text_analyzer.rb', line 133

def test_analyze_hierarchy_with_array
  hierarchy = [
    'This is a test string.',
    'Another test line.'
  ]
  pattern = /(test)/

  expected_output = [
    [
      { text: 'This is a ', color: $default_color },
      { text: 'test', color: $match_color },
      { text: ' string.', color: $default_color }
    ],
    [
      { text: 'Another ', color: $default_color },
      { text: 'test', color: $match_color },
      { text: ' line.', color: $default_color }
    ]
  ]

  assert_equal expected_output,
               TextAnalyzer.analyze_hierarchy(hierarchy, pattern,
                                              $default_color, $match_color)
end

#test_analyze_hierarchy_with_invalid_typeObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/text_analyzer.rb', line 190

def test_analyze_hierarchy_with_invalid_type
  hierarchy = 12_345
  # hierarchy = HierarchyString.new([{ text: '12345', color: $default_color }])
  pattern = /(test)/

  assert_raises(ArgumentError) do
    TextAnalyzer.analyze_hierarchy(hierarchy, pattern, $default_color,
                                   $match_color)
  end
end

#test_analyze_hierarchy_with_nested_arrayObject



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
# File 'lib/text_analyzer.rb', line 158

def test_analyze_hierarchy_with_nested_array
  hierarchy = [
    'This is a test string.',
    ['Another test line.', 'Yet another test.']
  ]
  pattern = /(test)/

  expected_output = [
    [
      { text: 'This is a ', color: $default_color },
      { text: 'test', color: $match_color },
      { text: ' string.', color: $default_color }
    ],
    [
      [
        { text: 'Another ', color: $default_color },
        { text: 'test', color: $match_color },
        { text: ' line.', color: $default_color }
      ],
      [
        { text: 'Yet another ', color: $default_color },
        { text: 'test', color: $match_color },
        { text: '.', color: $default_color }
      ]
    ]
  ]

  assert_equal expected_output,
               TextAnalyzer.analyze_hierarchy(hierarchy, pattern,
                                              $default_color, $match_color)
end

#test_analyze_hierarchy_with_stringObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/text_analyzer.rb', line 117

def test_analyze_hierarchy_with_string
  text = 'This is a test string.'
  pattern = /(test)/

  expected_output = [[[
    { text: 'This is a ', color: $default_color },
    { text: 'test', color: $match_color },
    { text: ' string.', color: $default_color }
  ]]]

  tree = HierarchyString.new([{ text: text, color: $default_color }])
  assert_equal expected_output,
               TextAnalyzer.analyze_hierarchy(tree.substrings, pattern, $default_color,
                                              $match_color)
end

#test_highlight_segmentsObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/text_analyzer.rb', line 201

def test_highlight_segments
  text = 'This is a test string.'
  pattern = /(test)/

  expected_output = [
    { text: 'This is a ', color: $default_color },
    { text: 'test', color: $match_color },
    { text: ' string.', color: $default_color }
  ]

  assert_equal expected_output,
               TextAnalyzer.highlight_segments(text, pattern, $default_color,
                                               $match_color)
end

#test_yield_matches_and_non_matchesObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/text_analyzer.rb', line 216

def test_yield_matches_and_non_matches
  text = 'This is a test string with multiple tests.'
  pattern = /(test)/
  segments = []

  TextAnalyzer.yield_matches_and_non_matches(text,
                                             pattern) do |segment, is_match|
    segments << { text: segment, is_match: is_match }
  end

  expected_output = [
    { text: 'This is a ', is_match: false },
    { text: 'test', is_match: true },
    { text: ' string with multiple ', is_match: false },
    { text: 'test', is_match: true },
    { text: 's.', is_match: false }
  ]

  assert_equal expected_output, segments
end