Class: TestMarkdownTableFormatter

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

Instance Method Summary collapse

Instance Method Details

#setupObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/format_table.rb', line 257

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
  @table = { delimiter: '|' }
end

#test_alignment_detectionObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/format_table.rb', line 321

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,
    table: @table
  )
  expected = [
    '| Header 1  |  Header 2  |  Header 3 |',
    '| --------- | ---------- | --------- |'
  ]
  assert_equal expected, result[0..1] # only checking the header and separator
end

#test_format_tableObject



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/format_table.rb', line 268

def test_format_table
  result = MarkdownTableFormatter.format_table(
    column_count: @column_count, lines: @lines, table: @table
  )
  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



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/format_table.rb', line 281

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,
    table: @table
  )
  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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/format_table.rb', line 298

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,
    table: @table
  )
  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