Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/48 fallthrough properties #49

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ManiaTemplates/Components/MtComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,6 @@ private static HashSet<string> GetSlotNamesInTemplate(XmlNode node)

public string Id()
{
return "MtContext" + GetHashCode().ToString().Replace("-", "N");
return GetHashCode().ToString().Replace("-", "N");
}
}
16 changes: 16 additions & 0 deletions src/ManiaTemplates/Exceptions/CurlyBraceCountMismatchException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ManiaTemplates.Exceptions;

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

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

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

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

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

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

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

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

public InterpolationRecursionException(string message, Exception inner) : base(message, inner)
{
}
}
167 changes: 123 additions & 44 deletions src/ManiaTemplates/Lib/MtTransformer.cs

Large diffs are not rendered by default.

53 changes: 45 additions & 8 deletions tests/ManiaTemplates.Tests/IntegrationTests/ManialinkEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,20 @@ await Assert.ThrowsAsync<DuplicateSlotException>(() =>
[Fact]
public async void Should_Render_Component_Without_Content_For_Slot()
{
var slotRecursionOuterTwoTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer-two.mt");
var slotRecursionOuterTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer.mt");
var slotRecursionInnerTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-inner.mt");
var slotRecursionOuterTwoTemplate =
await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer-two.mt");
var slotRecursionOuterTemplate =
await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer.mt");
var slotRecursionInnerTemplate =
await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-inner.mt");
var expected = await File.ReadAllTextAsync("IntegrationTests/expected/single-slot-unfilled.xml");
var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly };

_maniaTemplateEngine.AddTemplateFromString("SlotRecursionOuterTwo", slotRecursionOuterTwoTemplate);
_maniaTemplateEngine.AddTemplateFromString("SlotRecursionOuter", slotRecursionOuterTemplate);
_maniaTemplateEngine.AddTemplateFromString("SlotRecursionInner", slotRecursionInnerTemplate);
var template = _maniaTemplateEngine.RenderAsync("SlotRecursionInner", new{}, assemblies).Result;

var template = _maniaTemplateEngine.RenderAsync("SlotRecursionInner", new { }, assemblies).Result;
Assert.Equal(expected, template, ignoreLineEndingDifferences: true);
}

Expand All @@ -110,15 +113,49 @@ public async void Should_Pass_Properties_To_Components_And_Slots()
var testComponentTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/component.mt");
var expected = await File.ReadAllTextAsync("IntegrationTests/expected/property-test.xml");
var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly };

_maniaTemplateEngine.AddTemplateFromString("PropertyTest", propertyTestTemplate);
_maniaTemplateEngine.AddTemplateFromString("Wrapper", testWrapperTemplate);
_maniaTemplateEngine.AddTemplateFromString("TestComponent", testComponentTemplate);

var template = _maniaTemplateEngine.RenderAsync("PropertyTest", new
{
testVariable = "integration"
}, assemblies).Result;
Assert.Equal(expected, template, ignoreLineEndingDifferences: true);
}

[Fact]
public async void Should_Append_Parent_Attributes_To_Single_Component_Child()
{
var fallthroughComponent = await File.ReadAllTextAsync("IntegrationTests/templates/fallthrough-component.mt");
var fallthroughWrapper = await File.ReadAllTextAsync("IntegrationTests/templates/fallthrough-wrapper.mt");
var expected = await File.ReadAllTextAsync("IntegrationTests/expected/fallthrough.xml");
var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly };

_maniaTemplateEngine.AddTemplateFromString("FallthroughComponent", fallthroughComponent);
_maniaTemplateEngine.AddTemplateFromString("FallthroughWrapper", fallthroughWrapper);

var template = _maniaTemplateEngine.RenderAsync("FallthroughWrapper", new
{
testString = "unit",
index = -1
}, assemblies).Result;
Assert.Equal(expected, template, ignoreLineEndingDifferences: true);
}

[Fact]
public async void Should_Not_Append_Parent_Attributes_To_Multiple_Component_Children()
{
var fallthroughComponent = await File.ReadAllTextAsync("IntegrationTests/templates/fallthrough-component-multi-child.mt");
var fallthroughWrapper = await File.ReadAllTextAsync("IntegrationTests/templates/fallthrough-wrapper.mt");
var expected = await File.ReadAllTextAsync("IntegrationTests/expected/fallthrough-multi-child.xml");
var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly };

_maniaTemplateEngine.AddTemplateFromString("FallthroughComponent", fallthroughComponent);
_maniaTemplateEngine.AddTemplateFromString("FallthroughWrapper", fallthroughWrapper);

var template = _maniaTemplateEngine.RenderAsync("FallthroughWrapper", new { }, assemblies).Result;
Assert.Equal(expected, template, ignoreLineEndingDifferences: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<manialink version="3" id="MtFallthroughWrapper" name="EvoSC#-MtFallthroughWrapper">
<label text="negative" />
<label text="negative" />
<label text="negative" />
<label text="negative" />
<label text="negative" />
<label text="negative" />
</manialink>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manialink version="3" id="MtFallthroughWrapper" name="EvoSC#-MtFallthroughWrapper">
<label text="test" i="1" data-index="1" data-test="unit" />
<label text="test" i="2" data-index="2" data-test="unit" />
<label text="test" i="3" data-index="3" data-test="unit" />
</manialink>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component>
<template>
<label text="negative" />
<label text="negative" />
</template>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<component>
<property type="int" name="index" default="-2" />
<property type="int" name="i" default="-2" />

<template>
<label text="test" i="{{ i }}" />
</template>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<component>
<using namespace="System.Linq" />
<import component="FallthroughComponent" as="FallthroughComponent"/>
<property type="string" name="testString" />
<property type="int" name="index" />

<template>
<FallthroughComponent foreach="int i in Enumerable.Range(1, 3)" data-index="{{ i }}" index="{{ i * 10 }}" data-test="unit"/>
</template>
</component>
87 changes: 86 additions & 1 deletion tests/ManiaTemplates.Tests/Lib/MtTransformerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using ManiaTemplates.Components;
using ManiaTemplates.Exceptions;
using ManiaTemplates.Languages;
using ManiaTemplates.Lib;
using Xunit.Abstractions;
Expand All @@ -13,6 +15,7 @@ public class MtTransformerTest
private readonly MtTransformer _transformer;
private readonly ManiaTemplateEngine _maniaTemplateEngine = new();
private readonly Regex _hashCodePattern = new("[0-9]{6,10}");

private readonly MtComponent _testComponent = new()
{
Namespaces = new() { "namespace" },
Expand Down Expand Up @@ -131,7 +134,25 @@ public void Should_Import_Components_Recursively()
}

[Fact]
public void Should_Replace_Curly_Braces_Correctly()
public void Should_Throw_Interpolation_Recursion_Exception()
{
Assert.Throws<InterpolationRecursionException>(() =>
MtTransformer.CheckInterpolationRecursion("{{ {{ a }} {{ b }} }}"));
Assert.Throws<InterpolationRecursionException>(() =>
MtTransformer.CheckInterpolationRecursion("{{ {{ b }} }}"));
}

[Fact]
public void Should_Throw_Curly_Brace_Count_Mismatch_Exception()
{
Assert.Throws<CurlyBraceCountMismatchException>(() => MtTransformer.CheckForCurlyBraceCountMismatch("{{ { }}"));
Assert.Throws<CurlyBraceCountMismatchException>(() => MtTransformer.CheckForCurlyBraceCountMismatch("{{ } }}"));
Assert.Throws<CurlyBraceCountMismatchException>(() => MtTransformer.CheckForCurlyBraceCountMismatch("{"));
Assert.Throws<CurlyBraceCountMismatchException>(() => MtTransformer.CheckForCurlyBraceCountMismatch("}}"));
}

[Fact]
public void Should_Replace_Curly_Braces()
{
Assert.Equal("abcd", MtTransformer.ReplaceCurlyBraces("{{a}}{{ b }}{{c }}{{ d}}", s => s));
Assert.Equal("x y z", MtTransformer.ReplaceCurlyBraces("{{x}} {{ y }} {{z }}", s => s));
Expand All @@ -144,5 +165,69 @@ public void Should_Replace_Curly_Braces_Correctly()
public void Should_Wrap_Strings_In_Quotes()
{
Assert.Equal(@"$""unit test""", MtTransformer.WrapStringInQuotes("unit test"));
Assert.Equal(@"$""""", MtTransformer.WrapStringInQuotes(""));
}

[Fact]
public void Should_Join_Feature_Blocks()
{
Assert.Equal("<#+\n unittest \n#>",
MtTransformer.JoinFeatureBlocks("<#+#><#+\n #> <#+ unittest \n#><#+ \n\n\n#>"));
}

[Fact]
public void Should_Convert_String_To_Xml_Node()
{
var node = MtTransformer.XmlStringToNode("<unit>test</unit>");
Assert.IsAssignableFrom<XmlNode>(node);
Assert.Equal("test", node.InnerText);
Assert.Equal("<unit>test</unit>", node.InnerXml);
Assert.Equal("<doc><unit>test</unit></doc>", node.OuterXml);
}

[Fact]
public void Should_Create_Xml_Opening_Tag()
{
var attributeList = new MtComponentAttributes();
Assert.Equal("<test />", _transformer.CreateXmlOpeningTag("test", attributeList, false));
Assert.Equal("<test>", _transformer.CreateXmlOpeningTag("test", attributeList, true));

attributeList["prop"] = "value";
Assert.Equal("""<test prop="value" />""", _transformer.CreateXmlOpeningTag("test", attributeList, false));
Assert.Equal("""<test prop="value">""", _transformer.CreateXmlOpeningTag("test", attributeList, true));
}

[Fact]
public void Should_Create_ManiaLink_Opening_Tag()
{
Assert.Equal("""<manialink version="99" id="Test" name="EvoSC#-Test">""",
MtTransformer.ManiaLinkStart("Test", 99));
Assert.Equal("""<manialink version="99" id="Test" name="EvoSC#-Test" layer="SomeLayer">""",
MtTransformer.ManiaLinkStart("Test", 99, "SomeLayer"));
}

[Fact]
public void Should_Convert_Xml_Node_Arguments_To_MtComponentAttributes_Instance()
{
var node = MtTransformer.XmlStringToNode("""<testNode arg1="test1" arg2="test2">testContent</testNode>""");
if (node.FirstChild == null) return;

var attributes = MtTransformer.GetXmlNodeAttributes(node.FirstChild);
Assert.Equal(2, attributes.Count);
Assert.Equal("test1", attributes["arg1"]);
Assert.Equal("test2", attributes["arg2"]);
}

[Fact]
public void Should_Detect_Correct_Type_String_For_CSharp_Scripting()
{
Assert.Equal("int", MtTransformer.GetFormattedName(0.GetType()));
Assert.Equal("double", MtTransformer.GetFormattedName(0.0.GetType()));
Assert.Equal("string", MtTransformer.GetFormattedName("test".GetType()));
Assert.Equal("System.Collections.Generic.List<string>",
MtTransformer.GetFormattedName(new List<string>().GetType()));
Assert.Equal("System.Collections.Generic.HashSet<string>",
MtTransformer.GetFormattedName(new HashSet<string>().GetType()));
Assert.Equal("dynamic", MtTransformer.GetFormattedName(new TestDynamicObject().GetType()));
}
}
7 changes: 7 additions & 0 deletions tests/ManiaTemplates.Tests/Lib/TestDynamicObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Dynamic;

namespace ManiaTemplates.Tests.Lib;

public class TestDynamicObject : DynamicObject
{
}
Loading
Loading