Skip to content

Commit

Permalink
Unit tests for Path.
Browse files Browse the repository at this point in the history
  • Loading branch information
zorbathut committed Nov 5, 2024
1 parent 2141c15 commit bbd8073
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ internal Context(string filename = null, System.Xml.Linq.XElement element = null
this.path = path;
}

public string PathString()
{
return path?.Serialize() ?? "[unknown]";
}

public override string ToString()
{
if (this.element != null)
Expand All @@ -33,7 +38,7 @@ public override string ToString()
}
else if (path != null)
{
return path.ToString();
return path.Serialize();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static void PrepCwd()
// Find our data directory
while (!Directory.Exists("data"))
{
Environment.CurrentDirectory = Path.GetDirectoryName(Environment.CurrentDirectory);
Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(Environment.CurrentDirectory);
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/unit/Golden.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public class Golden : Base
[Test, TestCaseSource(nameof(GenerateValidationParser))]
public void Validation(string id, [Values] ParserMode mode)
{
string directory = Path.Combine("data", "golden", "parser", id);
string directory = System.IO.Path.Combine("data", "golden", "parser", id);

Assembly assembly;
if (!AssemblyLookup.TryGetValue(id, out assembly))
{
// gotta load
assembly = DecUtilLib.Compilation.Compile(DecUtilLib.Compress.ReadFromFile(Path.Combine(directory, "Harness.cs")), new Assembly[] { this.GetType().Assembly });
assembly = DecUtilLib.Compilation.Compile(DecUtilLib.Compress.ReadFromFile(System.IO.Path.Combine(directory, "Harness.cs")), new Assembly[] { this.GetType().Assembly });
AssemblyLookup[id] = assembly;
}

var type = assembly.GetType("DecTest.Harness");
type.GetMethod("Setup").Invoke(null, null);

var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, DecUtilLib.Compress.ReadFromFile(Path.Combine(directory, "data.xml")));
parser.AddString(Dec.Parser.FileType.Xml, DecUtilLib.Compress.ReadFromFile(System.IO.Path.Combine(directory, "data.xml")));
parser.Finish();

DoParserTests(mode, validation_assemblies: new Assembly[] { assembly });
Expand All @@ -39,7 +39,7 @@ public static IEnumerable<object[]> GenerateValidationParser()
{
PrepCwd();

var targetDir = Path.Combine("data", "golden", "parser");
var targetDir = System.IO.Path.Combine("data", "golden", "parser");

if (!Directory.Exists(targetDir))
{
Expand All @@ -48,7 +48,7 @@ public static IEnumerable<object[]> GenerateValidationParser()

foreach (var path in Directory.GetDirectories(targetDir))
{
var id = Path.GetFileName(path);
var id = System.IO.Path.GetFileName(path);
yield return new object[] { id, ParserMode.Bare };
yield return new object[] { id, ParserMode.RewrittenBare };
yield return new object[] { id, ParserMode.RewrittenPretty };
Expand Down
216 changes: 216 additions & 0 deletions test/unit/Path.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@

using NUnit.Framework;
using System;
using System.Collections.Generic;

namespace DecTest
{
[TestFixture]
public class Path : Base
{
private class PathTester : Dec.IRecordable
{
public static int validations = 0;
public string text;

public void Record(Dec.Recorder recorder)
{
recorder.RecordAsThis(ref text);

Assert.AreEqual(text, recorder.Context.PathString());
++validations;
}
}

private class PathDec : Dec.Dec
{
public PathTester member;
public PathTester[] array;
public PathTester[,] arrayMulti;
public List<PathTester> list;
public Dictionary<PathTester, string> dictKey;
public Dictionary<string, PathTester> dictValue;
public HashSet<PathTester> set;
}

[SetUp]
public void Setup()
{
UpdateTestParameters(new Dec.Config.UnitTestParameters { explicitTypes = new Type[] { typeof(PathDec) } });
PathTester.validations = 0;
}

[Test]
public void MemberPath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<member>PathDec.TestDec.member</member>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 1);
}

[Test]
public void ArrayPath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<array>
<li>PathDec.TestDec.array[0]</li>
<li>PathDec.TestDec.array[1]</li>
</array>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 2);
}

[Test]
public void MultiDimensionalArrayPath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<arrayMulti>
<li>
<li>PathDec.TestDec.arrayMulti[0,0]</li>
<li>PathDec.TestDec.arrayMulti[0,1]</li>
</li>
<li>
<li>PathDec.TestDec.arrayMulti[1,0]</li>
<li>PathDec.TestDec.arrayMulti[1,1]</li>
</li>
</arrayMulti>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 4);
}

[Test]
public void ListPath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<list>
<li>PathDec.TestDec.list[0]</li>
<li>PathDec.TestDec.list[1]</li>
</list>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 2);
}

[Test]
public void DictionaryKeyPath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<dictKey>
<li>
<key>PathDec.TestDec.dictKey[KEY]</key>
<value>sure</value>
</li>
</dictKey>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 1);
}

[Test]
public void DictionaryValuePath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<dictValue>
<li>
<key>first</key>
<value>PathDec.TestDec.dictValue[nyi]</value>
</li>
</dictValue>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 1);
}

[Test]
public void HashSetPath()
{
var parser = new Dec.Parser();
parser.AddString(Dec.Parser.FileType.Xml, @"
<Decs>
<PathDec decName=""TestDec"">
<set>
<li>PathDec.TestDec.set[KEY]</li>
</set>
</PathDec>
</Decs>");
parser.Finish();

Assert.IsTrue(PathTester.validations == 1);
}

// nyi due to needing a write step
/*
[Test]
public void Record()
{
var initialData = new PathTester { text = "RECORD" };
string serialized = Dec.Recorder.Write(initialData);
var deserialized = Dec.Recorder.Read<PathTester>(serialized);
Assert.IsNotNull(deserialized);
Assert.AreEqual(2, PathTester.validations); // Once for write, once for read
}
[Test]
public void RecordRef()
{
var initialData = new PathTester { text = "RECORD" };
string serialized = Dec.Recorder.Write((initialData, initialData));
var deserialized = Dec.Recorder.Read<(PathTester, PathTester)>(serialized);
Assert.IsNotNull(deserialized);
Assert.AreEqual(2, PathTester.validations); // Once for write, once for read
}
[Test]
public void Simple()
{
var initialData = new PathTester { text = "SimplePath" };
string serialized = Dec.Recorder.WriteSimple(initialData, "SimplePath");
var deserialized = Dec.Recorder.ReadSimple<PathTester>(serialized, "SimplePath");
Assert.IsNotNull(deserialized);
Assert.AreEqual(2, PathTester.validations); // Once for write, once for read
}*/
}
}

0 comments on commit bbd8073

Please sign in to comment.