Skip to content

Commit

Permalink
Revert "Make global variables dynamic in t4-templates and remove meth…
Browse files Browse the repository at this point in the history
…od GetFo…"
  • Loading branch information
araszka authored Nov 23, 2023
1 parent b08a2ee commit 004961c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 65 deletions.
28 changes: 26 additions & 2 deletions src/ManiaTemplates/Lib/MtTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ private string CreateTemplatePropertiesBlock(MtComponent mtComponent)
{
var properties = new StringBuilder();

foreach (var propertyName in _engine.GlobalVariables.Keys)
foreach (var (propertyName, propertyValue) in _engine.GlobalVariables)
{
var type = propertyValue.GetType();

properties.AppendLine(_maniaTemplateLanguage
.FeatureBlock($"public dynamic ?{propertyName} {{ get; init; }}").ToString());
.FeatureBlock($"public {GetFormattedName(type)} ?{propertyName} {{ get; init; }}").ToString());
}

foreach (var property in mtComponent.Properties.Values)
Expand All @@ -184,6 +186,28 @@ private string CreateTemplatePropertiesBlock(MtComponent mtComponent)

return properties.ToString();
}

/// <summary>
/// Returns the type name. If this is a generic type, appends
/// the list of generic type arguments between angle brackets.
/// (Does not account for embedded / inner generic arguments.)
///
/// From: https://stackoverflow.com/a/66604069
/// </summary>
/// <param name="type">The type.</param>
/// <returns>System.String.</returns>
public static string GetFormattedName(Type type)
{
if(type.IsGenericType)
{
string genericArguments = type.GetGenericArguments()
.Select(x => x.Name)
.Aggregate((x1, x2) => $"{x1}, {x2}");
return $"{type.Name.Substring(0, type.Name.IndexOf("`"))}"
+ $"<{genericArguments}>";
}
return type.Name;
}

/// <summary>
/// Creates a block containing all render methods used in the template.
Expand Down
42 changes: 0 additions & 42 deletions tests/ManiaTemplates.Tests/IntegrationTests/DynamicDictionary.cs

This file was deleted.

28 changes: 9 additions & 19 deletions tests/ManiaTemplates.Tests/IntegrationTests/ManialinkEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
public class ManialinkEngineTest
{
private readonly ManiaTemplateEngine _maniaTemplateEngine = new();

[Theory]
[ClassData(typeof(TestDataProvider))]
public void Should_Convert_Templates_To_Result(string template, dynamic data, string expected)
{
_maniaTemplateEngine.AddTemplateFromString("test", template);
_maniaTemplateEngine.PreProcess("test", new[] { typeof(ManiaTemplateEngine).Assembly });

var pendingResult =
_maniaTemplateEngine.RenderAsync("test", data, new[] { typeof(ManiaTemplateEngine).Assembly });

var pendingResult = _maniaTemplateEngine.RenderAsync("test", data, new[] { typeof(ManiaTemplateEngine).Assembly });
var result = pendingResult.Result;

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

Expand All @@ -28,23 +27,14 @@ public void Should_Pass_Global_Variables()

_maniaTemplateEngine.AddTemplateFromString("ComponentGlobalVariable", componentWithGlobalVariable);
_maniaTemplateEngine.AddTemplateFromString("GlobalVariables", componentTemplate);

dynamic dynamicObject = new DynamicDictionary();
dynamicObject.DynamicProperty = "UnitTest";
AddGlobalVariable("dynamicObject", dynamicObject);


_maniaTemplateEngine.GlobalVariables["testVariable"] = "unittest";
_maniaTemplateEngine.GlobalVariables["complex"] = new ComplexDataType();
_maniaTemplateEngine.GlobalVariables["list"] = new List<int> { 3, 6, 9 };

var pendingResult = _maniaTemplateEngine.RenderAsync("GlobalVariables", new { }, assemblies);
var pendingResult = _maniaTemplateEngine.RenderAsync("GlobalVariables", new{}, assemblies);
var result = pendingResult.Result;

Assert.Equal(expectedOutput, result, ignoreLineEndingDifferences: true);
}

private void AddGlobalVariable(string name, object value)
{
_maniaTemplateEngine.GlobalVariables.AddOrUpdate(name, value, (s, o) => value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
<label text="6" />
<label text="9" />
<label class="inner" text="UnitTest" />
<label text="UnitTest" />
</manialink>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
<label foreach="int testList in complex.TestEnumerable" text="{{ testList }}"/>
<label foreach="int testListTwo in list" text="{{ testListTwo }}"/>
<ComponentGlobalVariable />
<label text="{{ dynamicObject.DynamicProperty }}"/>
</template>
</component>

0 comments on commit 004961c

Please sign in to comment.