public static IEnumerable<string> AllValuesForColumn(int columnIndex, Model model) { yield return model.Header.Skip(columnIndex).First(); foreach (var row in model.Rows) { yield return row.Skip(columnIndex).First(); } }
static void RenderRows(Model model, int[] widths, StringBuilder result) { foreach (var row in model.Rows) { for (var i = 0; i < model.Header.Count(); i++) { result.Append(row.Skip(i).First().PadRight(widths.Skip(i).First())).Append("|"); } result.AppendLine(); } }
public void AllValues_Should_YieldAllValues() { var model = new Model(); model.SetHeader(new[] { "Foo", "Bar" }); model.AddRow(new[] { "1", "2" }); model.AddRow(new[] { "1111", "2222" }); var column1 = Model.AllValuesForColumn(0, model); var column2 = Model.AllValuesForColumn(1, model); CollectionAssert.AreEqual(column1, new [] { "Foo","1", "1111" }); CollectionAssert.AreEqual(column2, new [] { "Bar","2", "2222" }); }
public string Format(Model model) { var widths = model.Header .Select((c, index) => Model.AllValuesForColumn(index, model)) .Select(colValues => colValues.Max(x => x.Length)) .ToArray(); var result = new StringBuilder(); RenderHeader(model, widths, result); RenderRows(model, widths, result); return result.ToString(); }
static void RenderHeader(Model model, int[] widths, StringBuilder result) { for (var i = 0; i < model.Header.Count(); i++) { result.Append(model.Header.Skip(i).First().PadRight(widths.Skip(i).First())).Append("|"); } result.AppendLine(); for (var i = 0; i < model.Header.Count(); i++) { result.Append(new string('-', widths.Skip(i).First())).Append("+"); } result.AppendLine(); }
public void CanRenderSimpleTable() { var model = new Model(); model.SetHeader(new[] { "Foo", "Bar" }); model.AddRow(new[] { "1", "2" }); model.AddRow(new[] { "1111", "2222" }); var output = new TableFormatter().Format(model); Assert.AreEqual(@"Foo |Bar | ----+----+ 1 |2 | 1111|2222| ", output); }
public void ShouldYieldNumberOfPages() { var model = new Model(); model.SetHeader(new[] { "Foo", "Bar" }); model.AddRow(new[] { "1", "2" }); model.AddRow(new[] { "1111", "2222" }); model.AddRow(new[] { "1111", "2222" }); model.AddRow(new[] { "1111", "2222" }); model.AddRow(new[] { "1111", "2222" }); model.AddRow(new[] { "1111", "2222" }); model.AddRow(new[] { "1111", "2222" }); var paged = new PagedModel(model, 0, 2); Assert.AreEqual(3, paged.MaxPageIndex); }
public void CanRenderPagedTable_Page1() { var model = new Model(); model.SetHeader(new[] { "Foo", "Bar" }); model.AddRow(new[] { "1", "2" }); model.AddRow(new[] { "1111", "2222" }); var page1 = new PagedModel(model, 1, 1); var output = new TableFormatter().Format(page1); Assert.AreEqual(@"Foo |Bar | ----+----+ 1111|2222| ", output); }
public void RowsOfNumbredModel_Should_ContainNumbers() { var model = new Model() .SetHeader(new[] { "header" }) .AddRow(new[] { "one" }) .AddRow(new[] { "two" }); var numbered = new NumberedModel(model); Assert.AreEqual(2, numbered.Header.Count()); Assert.AreEqual(2, numbered.Rows.First().Count()); Assert.AreEqual("No.", numbered.Header.First()); Assert.AreEqual("1.", numbered.Rows.First().First()); Assert.AreEqual("2.", numbered.Rows.Skip(1).First().First()); }
public Model Read(Stream input) { var result = new Model(); using (var reader = new StreamReader(input)) { Action<IEnumerable<string>> addRow = items => result.AddRow(items); Action<IEnumerable<string>> add = items => result.SetHeader(items); string line; while ((line = reader.ReadLine()) != null) { var items = line.Split(';'); add(items); add = addRow; } } return result; }
public NumberedModel(Model model) { _model = model; }