Skip to content

Commit

Permalink
modernize code base
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Dec 28, 2021
1 parent 1100f4e commit f3cf844
Show file tree
Hide file tree
Showing 26 changed files with 936 additions and 925 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
## 4.0.0
* Better exception when value extraction fails on object

## 4.1.0
* Update some packages, modernize code base


[gary-palmer]: https://github.com/gary-palmer
30 changes: 30 additions & 0 deletions Tababular.Tests/Bugs/TestRecordsAndStuff.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using NUnit.Framework;
// ReSharper disable NotAccessedPositionalProperty.Local
// ReSharper disable ArgumentsStyleStringLiteral
// ReSharper disable ArgumentsStyleLiteral

namespace Tababular.Tests.Bugs;

[TestFixture]
public class TestRecordsAndStuff
{
[Test]
public void CanDoIt()
{
var formatter = new TableFormatter();

var records = new[]
{
new SomeRecordThing(Number: 21, Text: "Big Number"),
new SomeRecordThing(Number: 5, Text: "Small Number"),
new SomeRecordThing(Number: 12, Text: "Medium Number")
};

var text = formatter.FormatObjects(records);

Console.WriteLine(text);
}

record SomeRecordThing(int Number, string Text);
}
23 changes: 11 additions & 12 deletions Tababular.Tests/Ex/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System;
using System.Linq;

namespace Tababular.Tests.Ex
namespace Tababular.Tests.Ex;

public static class StringExtensions
{
public static class StringExtensions
public static string Normalized(this string str)
{
public static string Normalized(this string str)
{
var lines = str.Split(new[] {Environment.NewLine, "\r", "\n"}, StringSplitOptions.None)
.SkipWhile(string.IsNullOrWhiteSpace)
.Reverse()
.SkipWhile(string.IsNullOrWhiteSpace)
.Reverse();
var lines = str.Split(new[] {Environment.NewLine, "\r", "\n"}, StringSplitOptions.None)
.SkipWhile(string.IsNullOrWhiteSpace)
.Reverse()
.SkipWhile(string.IsNullOrWhiteSpace)
.Reverse();

return string.Join(Environment.NewLine, lines.Select(l => l.TrimEnd()));
}
}
return string.Join(Environment.NewLine, lines.Select(l => l.TrimEnd()));
}
}
109 changes: 54 additions & 55 deletions Tababular.Tests/Examples/ReadmeCode.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
using System;
using NUnit.Framework;

namespace Tababular.Tests.Examples
namespace Tababular.Tests.Examples;

[TestFixture]
public class ReadmeCode
{
[TestFixture]
public class ReadmeCode
[Test]
public void Example1()
{
[Test]
public void Example1()
{
var tableFormatter = new TableFormatter();
var tableFormatter = new TableFormatter();

var objects = new[]
{
new {FirstColumn = "r1", SecondColumn = "hej", ThirdColumn = "hej igen"},
new {FirstColumn = "r2", SecondColumn = "hej", ThirdColumn = "hej igen"},
};
var objects = new[]
{
new {FirstColumn = "r1", SecondColumn = "hej", ThirdColumn = "hej igen"},
new {FirstColumn = "r2", SecondColumn = "hej", ThirdColumn = "hej igen"},
};

var text = tableFormatter.FormatObjects(objects);
var text = tableFormatter.FormatObjects(objects);

Console.WriteLine(text);
}
Console.WriteLine(text);
}

[Test]
public void Example2()
[Test]
public void Example2()
{
var objects = new[]
{
var objects = new[]
{
new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}},
new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {80, 5432}}
};
new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}},
new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {80, 5432}}
};

var text = new TableFormatter().FormatObjects(objects);
var text = new TableFormatter().FormatObjects(objects);

Console.WriteLine(text);
}
Console.WriteLine(text);
}

[Test]
public void Example3_WithoutHint()
[Test]
public void Example3_WithoutHint()
{
var objects = new[]
{
var objects = new[]
{
new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}, Comments = ""},
new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {5432},
Comments = @"This bad boy hosts our database and a couple of internal jobs."}
};
new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}, Comments = ""},
new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {5432},
Comments = @"This bad boy hosts our database and a couple of internal jobs."}
};

var formatter = new TableFormatter();
var formatter = new TableFormatter();

var text = formatter.FormatObjects(objects);
var text = formatter.FormatObjects(objects);

Console.WriteLine(text);
Console.WriteLine(text);

/*
/*
+----------------------------------------------------------------+-----------+-------------+-------+
| Comments | Ip | MachineName | Ports |
Expand All @@ -64,27 +64,27 @@ public void Example3_WithoutHint()
| This bad boy hosts our database and a couple of internal jobs. | 10.0.0.11 | ctxtest02 | 5432 |
+----------------------------------------------------------------+-----------+-------------+-------+
*/
}
*/
}

[Test]
public void Example3_WithHint()
[Test]
public void Example3_WithHint()
{
var objects = new[]
{
var objects = new[]
{
new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}, Comments = ""},
new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {5432},
Comments = @"This bad boy hosts our database and a couple of internal jobs."}
};
new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}, Comments = ""},
new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {5432},
Comments = @"This bad boy hosts our database and a couple of internal jobs."}
};

var hints = new Hints { MaxTableWidth = 80 };
var formatter = new TableFormatter(hints);
var hints = new Hints { MaxTableWidth = 80 };
var formatter = new TableFormatter(hints);

var text = formatter.FormatObjects(objects);
var text = formatter.FormatObjects(objects);

Console.WriteLine(text);
Console.WriteLine(text);

/*
/*
+------------------------------------------------+-----------+-------------+-------+
| Comments | Ip | MachineName | Ports |
Expand All @@ -96,8 +96,7 @@ public void Example3_WithHint()
| This bad boy hosts our database and a couple | 10.0.0.11 | ctxtest02 | 5432 |
| of internal jobs. | | | |
+------------------------------------------------+-----------+-------------+-------+
*/
}
*/
}
}
135 changes: 67 additions & 68 deletions Tababular.Tests/Extractors/TestDictionaryTableExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,96 @@
using NUnit.Framework;
using Tababular.Internals.Extractors;

namespace Tababular.Tests.Extractors
namespace Tababular.Tests.Extractors;

[TestFixture]
public class TestDictionaryTableExtractor
{
[TestFixture]
public class TestDictionaryTableExtractor
[Test]
public void DoesNotDieOnEmptyArray()
{
[Test]
public void DoesNotDieOnEmptyArray()
{
var table = new DictionaryTableExtractor(Enumerable.Empty<IDictionary<string, object>>()).GetTable();
var table = new DictionaryTableExtractor(Enumerable.Empty<IDictionary<string, object>>()).GetTable();

Assert.That(table.Rows.Count, Is.EqualTo(0));
Assert.That(table.Columns.Count, Is.EqualTo(0));
}
Assert.That(table.Rows.Count, Is.EqualTo(0));
Assert.That(table.Columns.Count, Is.EqualTo(0));
}

[Test]
public void DoesNotDieOnArrayWithEmptyObject()
[Test]
public void DoesNotDieOnArrayWithEmptyObject()
{
var table = new DictionaryTableExtractor(new[]
{
var table = new DictionaryTableExtractor(new[]
{
new Dictionary<string, object>(),
new Dictionary<string, object>()
}).GetTable();
new Dictionary<string, object>(),
new Dictionary<string, object>()
}).GetTable();

Assert.That(table.Rows.Count, Is.EqualTo(2));
Assert.That(table.Columns.Count, Is.EqualTo(0));
}
Assert.That(table.Rows.Count, Is.EqualTo(2));
Assert.That(table.Columns.Count, Is.EqualTo(0));
}

[Test]
public void GetsColumnsAsExpected()
[Test]
public void GetsColumnsAsExpected()
{
var dictionaries = new[]
{
var dictionaries = new[]
new Dictionary<string, object>
{
{ "First column", "Value 1" },
{ "Second column", "Value 1" },
},
new Dictionary<string, object>
{
new Dictionary<string, object>
{
{ "First column", "Value 1" },
{ "Second column", "Value 1" },
},
new Dictionary<string, object>
{
{ "First column", "Value 1" },
{ "Second column", "Value 1" },
}
};

var table = new DictionaryTableExtractor(dictionaries).GetTable();

Assert.That(table.Columns.Select(c => c.Label), Is.EqualTo(new[] { "First column", "Second column" }));
}

[Test]
public void CanExtractValues_Strings()
{ "First column", "Value 1" },
{ "Second column", "Value 1" },
}
};

var table = new DictionaryTableExtractor(dictionaries).GetTable();

Assert.That(table.Columns.Select(c => c.Label), Is.EqualTo(new[] { "First column", "Second column" }));
}

[Test]
public void CanExtractValues_Strings()
{
var dictionaries = new[]
{
var dictionaries = new[]
new Dictionary<string, object>
{
new Dictionary<string, object>
{
{ "col1", "v1" },
{ "col2", "v2" },
{ "col3", "v3" }
}
};
{ "col1", "v1" },
{ "col2", "v2" },
{ "col3", "v3" }
}
};

var table = new DictionaryTableExtractor(dictionaries).GetTable();
var table = new DictionaryTableExtractor(dictionaries).GetTable();

var row = table.Rows.Single();
var row = table.Rows.Single();

var cellTexts = row.GetAllCells().OrderBy(c => c.TextValue).Select(c => c.TextValue);
var cellTexts = row.GetAllCells().OrderBy(c => c.TextValue).Select(c => c.TextValue);

Assert.That(cellTexts, Is.EqualTo(new[] {"v1", "v2", "v3"}));
Assert.That(cellTexts, Is.EqualTo(new[] {"v1", "v2", "v3"}));

}
}

[Test]
public void CanExtractValues_Multiple()
[Test]
public void CanExtractValues_Multiple()
{
var dictionaries = new[]
{
var dictionaries = new[]
new Dictionary<string, object>
{
new Dictionary<string, object>
{
{ "col1", new[] {"line1", "line2", "line3"} }
}
};
{ "col1", new[] {"line1", "line2", "line3"} }
}
};

var table = new DictionaryTableExtractor(dictionaries).GetTable();
var table = new DictionaryTableExtractor(dictionaries).GetTable();

var row = table.Rows.Single();
var row = table.Rows.Single();

var cellLines = row.GetAllCells().Single().Lines;
var cellLines = row.GetAllCells().Single().Lines;

Assert.That(cellLines, Is.EqualTo(new[] {"line1", "line2", "line3"}));
Assert.That(cellLines, Is.EqualTo(new[] {"line1", "line2", "line3"}));

}
}
}
Loading

0 comments on commit f3cf844

Please sign in to comment.