Class: TestMarkdownTableFormatter

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

Instance Method Summary collapse

Instance Method Details

#setupObject



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

def setup
  @lines = [
    '| Header 1 | Header 2 | Header 3 |',
    '|----------|:--------:|---------:|',
    '| Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 |',
    '| Row 2 Col 1 | Row 2 Col 2 | Row 2 Col 3 |'
  ]
  @column_count = 3
end

#test_alignment_detectionObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/format_table.rb', line 251

def test_alignment_detection
  lines_with_alignment = [
    '| Header 1 | Header 2 | Header 3 |',
    '|:-------- |:--------:| --------:|'
  ]
  result = MarkdownTableFormatter.format_table(
    lines: lines_with_alignment,
    column_count: @column_count
  )
  expected = [
    '| Header 1  |  Header 2  |  Header 3 |',
    '| --------- | ---------- | --------- |'
  ]
  assert_equal expected, result[0..1] # only checking the header and separator
end

#test_format_tableObject



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/format_table.rb', line 200

def test_format_table
  result = MarkdownTableFormatter.format_table(
    column_count: @column_count, lines: @lines
  )
  expected = [
    '| Header 1    |  Header 2   |    Header 3 |',
    '| ----------- | ----------- | ----------- |',
    '| Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 |',
    '| Row 2 Col 1 | Row 2 Col 2 | Row 2 Col 3 |'
  ]
  assert_equal expected, result
end

#test_format_table_with_decorationObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/format_table.rb', line 213

def test_format_table_with_decoration
  decorate = { header_row: :upcase, row: %i[downcase upcase] }
  result = MarkdownTableFormatter.format_table(
    column_count: @column_count,
    decorate: decorate,
    lines: @lines
  )
  expected = [
    '| HEADER 1    |  HEADER 2   |    HEADER 3 |',
    '| ----------- | ----------- | ----------- |',
    '| ROW 1 COL 1 | ROW 1 COL 2 | ROW 1 COL 3 |',
    '| row 2 col 1 | row 2 col 2 | row 2 col 3 |'
  ]
  assert_equal expected, result
end

#test_format_table_with_empty_linesObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/format_table.rb', line 229

def test_format_table_with_empty_lines
  lines_with_empty = [
    '| Header 1 | Header 2 | Header 3 |',
    '|----------|:--------:|---------:|',
    '| Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 |',
    '',
    '| Row 2 Col 1 | Row 2 Col 2 | Row 2 Col 3 |'
  ]
  result = MarkdownTableFormatter.format_table(
    lines: lines_with_empty,
    column_count: @column_count
  )
  expected = [
    '| Header 1    |  Header 2   |    Header 3 |',
    '| ----------- | ----------- | ----------- |',
    '| Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 |',
    '',
    '| Row 2 Col 1 | Row 2 Col 2 | Row 2 Col 3 |'
  ]
  assert_equal expected, result
end