Raise an error if there are more cells than headers in a row

It's to prevent silly mistakes when writing and copy-pasting tables in
specs.
This commit is contained in:
Christophe Bliard
2024-05-23 10:51:08 +02:00
parent 76f2989a3b
commit d728ea37db
2 changed files with 23 additions and 0 deletions
@@ -33,6 +33,10 @@ module TableHelpers
def parse(representation)
headers, *rows = representation.split("\n").filter_map { |line| split_line_into_cells(line) }
work_packages_data = rows.map.with_index do |cells, index|
if cells.size > headers.size
raise ArgumentError, "Too many cells in row #{index + 1}, have you forgotten some headers?"
end
{
attributes: {},
index:,
@@ -73,6 +73,25 @@ RSpec.describe TableHelpers::TableParser do
.to raise_error(ArgumentError, 'Please use "remaining work" instead of "remaining hours"')
end
it "raises an error if there are more cells than headers in a row" do
table = <<~TABLE
subject | work
wp | 4h | 6h
TABLE
expect { described_class.new.parse(table) }
.to raise_error(ArgumentError, "Too many cells in row 1, have you forgotten some headers?")
end
it "is ok to have more headers than cells (value of missing cells will be nil)" do
table = <<~TABLE
subject | work | remaining work
wp | 4h
TABLE
parsed_data = described_class.new.parse(table)
expect(parsed_data.dig(0, :attributes, :estimated_hours)).to eq(4.0)
expect(parsed_data.dig(0, :attributes, :remaining_hours)).to be_nil
end
describe "subject column" do
let(:table) do
<<~TABLE