Skip to content

Commit

Permalink
Add test cases for ReplaceCurlyBraces and WrapStringInQuotes
Browse files Browse the repository at this point in the history
  • Loading branch information
araszka committed Jan 11, 2024
1 parent 40555cb commit 24397d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/ManiaTemplates/Lib/MtTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ private static bool IsStringType(MtComponentProperty property)
/// <summary>
/// Wraps a string in quotes.
/// </summary>
private static string WrapStringInQuotes(string str)
public static string WrapStringInQuotes(string str)
{
return $@"$""{str}""";
}
Expand Down Expand Up @@ -1140,7 +1140,7 @@ private static XmlNode XmlStringToNode(string content)
/// <summary>
/// Takes the contents of double curly braces in a string and wraps them into something else. The second Argument takes a string-argument and returns the newly wrapped string.
/// </summary>
private static string ReplaceCurlyBraces(string value, Func<string, string> curlyContentWrapper)
public static string ReplaceCurlyBraces(string value, Func<string, string> curlyContentWrapper)
{
var matches = TemplateInterpolationRegex.Match(value);
var output = value;
Expand Down
38 changes: 27 additions & 11 deletions tests/ManiaTemplates.Tests/Lib/MtTransformerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace ManiaTemplates.Tests.Lib;
public class MtTransformerTest
{
private readonly ITestOutputHelper _testOutputHelper;
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 @@ -47,8 +47,6 @@ public class MtTransformerTest
</Frame>"
};

private readonly MtTransformer _transformer;

public MtTransformerTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
Expand Down Expand Up @@ -90,9 +88,9 @@ public void Should_Build_Manialink()
var expected = File.ReadAllText("Lib/expected.tt");
var result = _transformer.BuildManialink(_testComponent, "expected");
var generalizedResult = TransformCodeToOrderNumber(result);

_testOutputHelper.WriteLine(generalizedResult);

Assert.Equal(expected, generalizedResult, ignoreLineEndingDifferences: true);
}

Expand All @@ -118,15 +116,33 @@ public void Should_Import_Components_Recursively()
{
var assemblies = new List<Assembly>();
assemblies.Add(Assembly.GetExecutingAssembly());

_maniaTemplateEngine.AddTemplateFromString("Embeddable", "<component><template><el/></template></component>");
_maniaTemplateEngine.AddTemplateFromString("RecursionElement", "<component><import component='Embeddable' as='Comp' /><template><Comp/></template></component>");
_maniaTemplateEngine.AddTemplateFromString("RecursionRoot", "<component><import component='RecursionElement' as='REL' /><template><REL/></template></component>");

var output = _maniaTemplateEngine.RenderAsync("RecursionRoot", new {}, assemblies).Result;
Assert.Equal( @$"<manialink version=""3"" id=""MtRecursionRoot"" name=""EvoSC#-MtRecursionRoot"">
_maniaTemplateEngine.AddTemplateFromString("RecursionElement",
"<component><import component='Embeddable' as='Comp' /><template><Comp/></template></component>");
_maniaTemplateEngine.AddTemplateFromString("RecursionRoot",
"<component><import component='RecursionElement' as='REL' /><template><REL/></template></component>");

var output = _maniaTemplateEngine.RenderAsync("RecursionRoot", new { }, assemblies).Result;
Assert.Equal(@$"<manialink version=""3"" id=""MtRecursionRoot"" name=""EvoSC#-MtRecursionRoot"">
<el />
</manialink>
", output, ignoreLineEndingDifferences: true);
}

[Fact]
public void Should_Replace_Curly_Braces_Correctly()
{
Assert.Equal("abcd", MtTransformer.ReplaceCurlyBraces("{{a}}{{ b }}{{c }}{{ d}}", s => s));
Assert.Equal("x y z", MtTransformer.ReplaceCurlyBraces("{{x}} {{ y }} {{z }}", s => s));
Assert.Equal("unittest", MtTransformer.ReplaceCurlyBraces("{{ unit }}test", s => s));
Assert.Equal("#unit#test", MtTransformer.ReplaceCurlyBraces("{{ unit }}test", s => $"#{s}#"));
Assert.Equal("#{ unit#}test", MtTransformer.ReplaceCurlyBraces("{{{ unit }}}test", s => $"#{s}#"));
}

[Fact]
public void Should_Wrap_Strings_In_Quotes()
{
Assert.Equal(@"$""unit test""", MtTransformer.WrapStringInQuotes("unit test"));
}
}

0 comments on commit 24397d8

Please sign in to comment.