Skip to content

Commit

Permalink
#5 XUnit tests
Browse files Browse the repository at this point in the history
- Replace generic exceptions with custom ones
- Pull computation out of assertion calls
  • Loading branch information
araszka committed May 29, 2023
1 parent 176e439 commit 6a87717
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 29 deletions.
14 changes: 6 additions & 8 deletions src/ManiaTemplates/Components/MtComponent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Diagnostics;
using System.Xml;
using ManiaTemplates.Exceptions;
using ManiaTemplates.Lib;

namespace ManiaTemplates.Components;
Expand Down Expand Up @@ -101,7 +99,7 @@ private static MtComponentImport ParseImportNode(ManiaTemplateEngine engine, Xml

if (resource == null)
{
throw new Exception($"Missing required attribute 'component' for element '{node.OuterXml}'.");
throw new MissingAttributeException($"Missing required attribute 'component' for element '{node.OuterXml}'.");
}

tag ??= resource.Split('.')[^2];
Expand All @@ -122,7 +120,7 @@ private static string ParseComponentUsingStatement(XmlNode node)

if (nameSpace == null)
{
throw new Exception($"Missing attribute 'namespace' for element '{node.OuterXml}'.");
throw new MissingAttributeException($"Missing attribute 'namespace' for element '{node.OuterXml}'.");
}

return nameSpace;
Expand Down Expand Up @@ -162,12 +160,12 @@ private static MtComponentProperty ParseComponentProperty(XmlNode node)

if (name == null)
{
throw new Exception($"Missing attribute 'name' for element '{node.OuterXml}'.");
throw new MissingAttributeException($"Missing attribute 'name' for element '{node.OuterXml}'.");
}

if (type == null)
{
throw new Exception($"Missing attribute 'type' for element '{node.OuterXml}'.");
throw new MissingAttributeException($"Missing attribute 'type' for element '{node.OuterXml}'.");
}

var property = new MtComponentProperty
Expand Down
7 changes: 4 additions & 3 deletions src/ManiaTemplates/Components/MtComponentScript.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml;
using ManiaTemplates.Exceptions;

namespace ManiaTemplates.Components;

Expand Down Expand Up @@ -44,8 +45,8 @@ public static MtComponentScript FromNode(ManiaTemplateEngine engine, XmlNode nod

if (content == null)
{
throw new Exception(
"Failed to get ManiaScript contents. Script tags need to either specify a body or resource-attribute.");
throw new ManiaScriptSourceMissingException(
"Script tags need to either specify a body or resource-attribute.");
}

return new MtComponentScript
Expand All @@ -60,4 +61,4 @@ public int ContentHash()
{
return Content.GetHashCode();
}
}
}
18 changes: 18 additions & 0 deletions src/ManiaTemplates/Exceptions/ManiaScriptSourceMissingException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ManiaTemplates.Exceptions;

public class ManiaScriptSourceMissingException : Exception
{
public ManiaScriptSourceMissingException()
{
}

public ManiaScriptSourceMissingException(string message)
: base(message)
{
}

public ManiaScriptSourceMissingException(string message, Exception inner)
: base(message, inner)
{
}
}
18 changes: 18 additions & 0 deletions src/ManiaTemplates/Exceptions/MissingAttributeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ManiaTemplates.Exceptions;

public class MissingAttributeException : Exception
{
public MissingAttributeException()
{
}

public MissingAttributeException(string message)
: base(message)
{
}

public MissingAttributeException(string message, Exception inner)
: base(message, inner)
{
}
}
3 changes: 2 additions & 1 deletion tests/ManiaTemplates.Tests/Components/MtComponentMapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public class MtComponentMapTest
{
private readonly MtComponentMap _mtComponentMap = new();

public MtComponentMapTest()
[Fact]
public void Should_Contain_Single_Component_Import()
{
_mtComponentMap["test"] = new MtComponentImport { Tag = "preTest", TemplateKey = "preTest" };
Assert.Single(_mtComponentMap);
Expand Down
15 changes: 9 additions & 6 deletions tests/ManiaTemplates.Tests/Components/MtComponentScriptTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Xml;
using ManiaTemplates.Components;
using ManiaTemplates.Exceptions;

namespace ManiaTemplates.Tests.Components;

Expand Down Expand Up @@ -40,8 +41,8 @@ public void Should_Fail_To_Load_Empty_ManiaScript(string input)
var document = new XmlDocument();
document.LoadXml(input);

var exception = Assert.Throws<Exception>(() => MtComponentScript.FromNode(_templateEngine, document.DocumentElement!));
Assert.Equal("Failed to get ManiaScript contents. Script tags need to either specify a body or resource-attribute.", exception.Message);
Assert.Throws<ManiaScriptSourceMissingException>(() =>
MtComponentScript.FromNode(_templateEngine, document.DocumentElement!));
}

[Fact]
Expand All @@ -51,13 +52,15 @@ public void Should_Load_ManiaScript_With_Different_Hash_Codes()
var document1 = new XmlDocument();
document1.LoadXml(firstInput);
var script1 = MtComponentScript.FromNode(_templateEngine, document1.DocumentElement!);

var script1Hash = script1.ContentHash();

const string secondInput = "<script resource='res'/>";
var document2 = new XmlDocument();
document2.LoadXml(secondInput);
var script2 = MtComponentScript.FromNode(_templateEngine, document2.DocumentElement!);

var script2Hash = script2.ContentHash();

Assert.NotEqual(script1, script2);
Assert.NotEqual(script1.ContentHash(), script2.ContentHash());
Assert.NotEqual(script1Hash, script2Hash);
}
}
}
15 changes: 7 additions & 8 deletions tests/ManiaTemplates.Tests/Components/MtComponentTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using FluentAssertions;
using ManiaTemplates.Components;
using ManiaTemplates.Exceptions;

namespace ManiaTemplates.Tests.Components;

Expand Down Expand Up @@ -87,16 +88,14 @@ public void Should_Populate_Template()
}

[Theory]
[InlineData(@"<property name=""text""/>", "property", "type")]
[InlineData(@"<property type=""int""/>", "property", "name")]
[InlineData(@"<import/>", "import", "component")]
[InlineData(@"<using/>", "using", "namespace")]
public void Should_Not_Read_Template_Missing_Attributes(string content, string nodeName, string attributeName)
[InlineData(@"<property name=""text""/>")]
[InlineData(@"<property type=""int""/>")]
[InlineData(@"<import/>")]
[InlineData(@"<using/>")]
public void Should_Not_Read_Invalid_Nodes(string content)
{
var component = $"<component>{content}</component>";

var exception = Assert.Throws<Exception>(() => MtComponent.FromTemplate(_engine, component));
Assert.Contains(nodeName, exception.Message);
Assert.Contains($"'{attributeName}'", exception.Message);
Assert.Throws<MissingAttributeException>(() => MtComponent.FromTemplate(_engine, component));
}
}
6 changes: 3 additions & 3 deletions tests/ManiaTemplates.Tests/Lib/MtTransformerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public void Should_Build_Manialink()
_maniaTemplateEngine.GetType().GetField("_components", BindingFlags.NonPublic | BindingFlags.Instance)
?.SetValue(_maniaTemplateEngine, components);

var expected = StripLineBreaks(File.ReadAllText("Lib/expected.tt"));

var sanitizedExpected = StripLineBreaks(File.ReadAllText("Lib/expected.tt"));
var result = _transformer.BuildManialink(_testComponent, "expected");
var sanitizedResult = StripLineBreaks(TransformCodeToOrderNumber(result));

Assert.Equal(expected, StripLineBreaks(TransformCodeToOrderNumber(result)));
Assert.Equal(sanitizedExpected, sanitizedResult);
}

private static string StripLineBreaks(string input)
Expand Down

0 comments on commit 6a87717

Please sign in to comment.