From 036544e8b0469d202091f47fa6485232fe7d43ea Mon Sep 17 00:00:00 2001 From: sriv Date: Fri, 12 Aug 2016 12:33:33 +0530 Subject: [PATCH] consider header also when computing table column width --- Lib.UnitTests/TableTests.cs | 10 +++++----- Lib/Table.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib.UnitTests/TableTests.cs b/Lib.UnitTests/TableTests.cs index a843575..e33aa96 100644 --- a/Lib.UnitTests/TableTests.cs +++ b/Lib.UnitTests/TableTests.cs @@ -55,15 +55,15 @@ public void ShouldBeAbleToAccessRowValuesUsingColumnNames() [Test] public void ShouldGetTableAsMarkdownString() { - var headers = new List { "foo", "bar" }; + var headers = new List { "foo", "bar_with_big_header" }; var table = new Table(headers); table.AddRow(new List { "foo_val", "bar_val" }); table.AddRow(new List { "foo_val1", "bar_val1" }); - const string expected = "|foo |bar |\n" + - "|--------|--------|\n" + - "|foo_val |bar_val |\n" + - "|foo_val1|bar_val1|"; + const string expected = "|foo |bar_with_big_header|\n" + + "|--------|-------------------|\n" + + "|foo_val |bar_val |\n" + + "|foo_val1|bar_val1 |"; Assert.AreEqual(expected, table.ToString()); } diff --git a/Lib/Table.cs b/Lib/Table.cs index f89a452..5116afc 100644 --- a/Lib/Table.cs +++ b/Lib/Table.cs @@ -110,8 +110,8 @@ public override string ToString() IEnumerable columnStrings = new string[_rows.Count + 2]; foreach (var header in GetColumnNames()) { - var columnValues = GetColumnValues(header).ToArray(); - var columnWidth = columnValues.Max(s => s.Length); + var columnValues = GetColumnValues(header).ToList(); + var columnWidth = columnValues.Concat(new [] {header}).Max(s => s.Length); Func formatCellValue = s => string.Format("|{0}", s.PadRight(columnWidth, ' ')); var paddedColumn = new[] {header, new string('-', columnWidth)}.Concat(columnValues).Select(formatCellValue); columnStrings = columnStrings.Zip(paddedColumn, string.Concat);