Initial commit: PANDA 1.3
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Panda
|
||||
{
|
||||
internal static class Tests
|
||||
{
|
||||
private static int failures;
|
||||
|
||||
private static void Main()
|
||||
{
|
||||
AssertEqual("BCD YZA", LetterShifter.Shift("ABC XYZ", 1), "shift up and wrap");
|
||||
AssertEqual("Zab", LetterShifter.Shift("Abc", -1), "shift down and preserve case");
|
||||
AssertEqual("Öl 123!", LetterShifter.Shift("Öl 123!", 0), "zero shift");
|
||||
AssertEqual("Öm 123!", LetterShifter.Shift("Öl 123!", 1), "non A-Z characters unchanged");
|
||||
|
||||
string sample = "Name;Notiz\r\n\"Meyer, Anna\";\"Hallo; Welt\"\r\nBob;\"Zeile 1\r\nZeile 2\"\r\n";
|
||||
AssertEqual(';', CsvCodec.DetectDelimiter(sample), "delimiter detection");
|
||||
List<List<string>> parsed = CsvCodec.Parse(sample, ';');
|
||||
AssertEqual(3, parsed.Count, "record count");
|
||||
AssertEqual("Meyer, Anna", parsed[1][0], "quoted comma");
|
||||
AssertEqual("Hallo; Welt", parsed[1][1], "quoted delimiter");
|
||||
AssertEqual("Zeile 1\r\nZeile 2", parsed[2][1], "quoted newline");
|
||||
|
||||
var source = new CsvDocument { Delimiter = ';', FirstRowIsHeader = true };
|
||||
source.Headers.AddRange(new[] { "A", "B", "C" });
|
||||
source.Rows.Add(new List<string> { "a1", "b1", "c1" });
|
||||
source.Rows.Add(new List<string> { "a2", "b2", "c2" });
|
||||
var filtered = CsvCodec.SelectColumns(source, new List<int> { 2, 0 });
|
||||
AssertEqual(2, filtered.Headers.Count, "selected column count");
|
||||
AssertEqual("C", filtered.Headers[0], "selected column order");
|
||||
AssertEqual("a2", filtered.Rows[1][1], "selected column values");
|
||||
|
||||
string tempPath = Path.Combine(Path.GetTempPath(), "csv-buchstaben-test-" + Guid.NewGuid().ToString("N") + ".csv");
|
||||
try
|
||||
{
|
||||
var doc = new CsvDocument { Delimiter = ';', FirstRowIsHeader = true };
|
||||
doc.Headers.Add("Name");
|
||||
doc.Headers.Add("Notiz");
|
||||
var rows = new List<IList<string>>
|
||||
{
|
||||
new List<string> { "Anna", "Hallo; \"Welt\"" },
|
||||
new List<string> { "Bob", "Mehr\r\nZeilig" }
|
||||
};
|
||||
CsvCodec.Save(tempPath, doc, rows);
|
||||
var loaded = CsvCodec.Load(tempPath, true);
|
||||
AssertEqual("Hallo; \"Welt\"", loaded.Rows[0][1], "save/load quotes");
|
||||
AssertEqual("Mehr\r\nZeilig", loaded.Rows[1][1], "save/load newline");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempPath)) File.Delete(tempPath);
|
||||
}
|
||||
|
||||
if (failures > 0)
|
||||
{
|
||||
Console.Error.WriteLine(failures + " test(s) failed.");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
Console.WriteLine("All tests passed.");
|
||||
}
|
||||
|
||||
private static void AssertEqual<T>(T expected, T actual, string name)
|
||||
{
|
||||
if (!EqualityComparer<T>.Default.Equals(expected, actual))
|
||||
{
|
||||
failures++;
|
||||
Console.Error.WriteLine("FAIL " + name + ": expected [" + expected + "] actual [" + actual + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user