Skip to content

Commit

Permalink
Fix pass parent component properly
Browse files Browse the repository at this point in the history
  • Loading branch information
araszka committed Feb 4, 2024
1 parent c9c1a92 commit b9a6027
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 34 deletions.
27 changes: 10 additions & 17 deletions src/ManiaTemplates/Lib/MtTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public string BuildManialink(MtComponent rootComponent, string className, int ve
XmlStringToNode(rootComponent.TemplateContent),
_engine.BaseMtComponents.Overload(rootComponent.ImportedComponents),
rootContext,
isRootNode:true
parentComponent: rootComponent,
isRootNode: true
);

var template = new Snippet
Expand Down Expand Up @@ -235,12 +236,12 @@ private string CreateSlotRenderMethod(MtComponent component, int scope, MtDataCo

if (context.ParentContext != null)
{
output.AppendLine(CreateLocalVariablesFromContext(context.ParentContext));
output.AppendLine(CreateLocalVariablesFromContext(context.ParentContext, parentComponent?.Properties.Keys));
variablesInherited.AddRange(context.ParentContext.Keys);
}

output
.AppendLine(CreateLocalVariablesFromContext(context, variablesInherited))
.AppendLine(CreateLocalVariablesFromContext(context, parentComponent?.Properties.Keys))
.AppendLine(_maniaTemplateLanguage.FeatureBlockEnd())
.AppendLine(slotContent)
.AppendLine(_maniaTemplateLanguage.FeatureBlockStart())
Expand Down Expand Up @@ -347,7 +348,8 @@ private string ProcessNode(XmlNode node, MtComponentMap availableMtComponents, M
if (hasChildren)
{
subSnippet.AppendLine(1,
ProcessNode(childNode, availableMtComponents, currentContext));
ProcessNode(childNode, availableMtComponents, currentContext,
parentComponent: parentComponent));
subSnippet.AppendLine(CreateXmlClosingTag(tag));
}

Expand Down Expand Up @@ -530,7 +532,7 @@ private string ProcessComponentNode(
renderComponentCall.Append(
$", __slotRenderer_{parentSlotName}: __slotRenderer_{parentSlotName}");
}

foreach (var propertyName in parentComponent.Properties.Keys)
{
renderComponentCall.Append($",{propertyName}: {propertyName}");
Expand All @@ -540,19 +542,10 @@ private string ProcessComponentNode(
{
foreach (var parentSlotName in component.Slots)
{
if (isRootNode)
{
renderComponentCall.Append(
$", __slotRenderer_{parentSlotName}: () => DoNothing()");
}
else
{
renderComponentCall.Append(
$", __slotRenderer_{parentSlotName}: __slotRenderer_{parentSlotName}");
}
renderComponentCall.Append($", __slotRenderer_{parentSlotName}: () => DoNothing()");
}
}

renderComponentCall.Append(')');

i++;
Expand All @@ -565,7 +558,7 @@ private string ProcessComponentNode(

return renderComponentCall.ToString();
}

/// <summary>
/// Creates the method which renders the contents of a component.
/// </summary>
Expand Down
36 changes: 28 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,32 @@ 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_Fill_All_Slots()
{
var baseTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slots/base.mt");
var containerTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slots/container.mt");
var windowTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slots/window.mt");
var expected = await File.ReadAllTextAsync("IntegrationTests/expected/slots/manialink.xml");
var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly };

_maniaTemplateEngine.AddTemplateFromString("Base", baseTemplate);
_maniaTemplateEngine.AddTemplateFromString("Container", containerTemplate);
_maniaTemplateEngine.AddTemplateFromString("Window", windowTemplate);

var template = _maniaTemplateEngine.RenderAsync("Base", new { }, assemblies).Result;
Assert.Equal(expected, template, ignoreLineEndingDifferences: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<manialink version="3" id="MtBase" name="EvoSC#-MtBase">
<Theme />
<frame>
<label text="labelBefore" />
<frame id="evosc_container" pos="5 -5" size="200 130" scale="0" hidden="0">
<label text="Next element should be TextInput" />
<TextInput name="Search" />
</frame>
</frame>
</manialink>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<component>
<import component="Window" as="Window"/>

<template>
<Theme />

<Window width="200" height="130">
<TextInput name="Search"/>
</Window>
</template>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<component>
<property type="double" name="x" default="0.0" />
<property type="double" name="y" default="0.0" />
<property type="double" name="width" default="0.0" />
<property type="double" name="height" default="0.0" />
<property type="double" name="scale" default="0.0" />
<property type="bool" name="scrollable" default="false" />
<property type="bool" name="hidden" default="false" />
<property type="int" name="zIndex" default="0" />
<property type="int" name="rotate" default="0" />
<property type="string" name="className" default="" />

<template>
<frame
id="evosc_container"
pos="{{ x }} {{ y }}"
size="{{ width }} {{ height }}"
scriptevents='{{ scrollable ? "1" : "0" }}'
z-index="{{ zIndex }}"
rot="{{ rotate }}"
scale="{{ scale }}"
class="{{ className }}"
hidden='{{ hidden ? "1" : "0" }}'
>
<quad size="9999 9999" pos="0 0" if="scrollable" />
<slot />
</frame>
</template>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<component>
<import component="Container" as="Container"/>

<property type="double" name="width" default="600.0" />
<property type="double" name="height" default="400.0" />
<property type="double" name="padding" default="5.0" />

<template>
<frame>
<label text="labelBefore" />

<Container x="{{ padding }}"
y="-{{ padding }}"
width="{{ width }}"
height="{{ height }}"

pos="{{ padding }} -{{ padding }}"
size="{{ width - padding * 2 }} {{ height - padding * 2 }}"
>
<label text="Next element should be TextInput" />
<slot />
</Container>
</frame>
</template>
</component>
14 changes: 5 additions & 9 deletions tests/ManiaTemplates.Tests/Lib/expected.tt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ var __outerIndex1 = 0;
foreach (int i in numbers) {
var __index = __outerIndex1;
if (enabled) {
Render_Component_MtContext2(__data: __data, x: (20 * __index), __slotRenderer_default: () => Render_Slot_3_default(__data: new CRoot_ForEachLoop1(__data){__index = __index, i = i}, __slotRenderer_default: () => DoNothing()));
Render_Component_MtContext2(__data: __data, x: (20 * __index), __slotRenderer_default: () => Render_Slot_3_default(__data: new CRoot_ForEachLoop1(__data){__index = __index, i = i},numbers: numbers,enabled: enabled));
}
__outerIndex1++;
}
Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_4_default(__data: __data, __slotRenderer_default: () => DoNothing()));
Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_4_default(__data: __data,numbers: numbers,enabled: enabled));
foreach(var maniaScriptRenderMethod in __maniaScriptRenderMethods){ maniaScriptRenderMethod(); }
#>
<script>scriptText1
Expand Down Expand Up @@ -90,9 +90,7 @@ __insertedOneTimeManiaScripts.Add("FWKSFxnFcgdsXVn9dn5IW3isD6T8z+2eK6liK8rPSpU="
</script>
<#+
}
void Render_Slot_3_default(CRoot_ForEachLoop1 __data,Action __slotRenderer_default) {
var numbers = __data.numbers;
var enabled = __data.enabled;
void Render_Slot_3_default(CRoot_ForEachLoop1 __data,List<int> numbers,boolean enabled = true) {
var __index = __data.__index;
var i = __data.i;
var __outerIndex7 = 0;
Expand All @@ -115,9 +113,7 @@ Render_Component_MtContext6(__data: __data, arg3: (new test()));
</test>
<#+
}
void Render_Slot_4_default(CRoot __data,Action __slotRenderer_default) {
var numbers = __data.numbers;
var enabled = __data.enabled;
Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_8_default(__data: __data, __slotRenderer_default: __slotRenderer_default));
void Render_Slot_4_default(CRoot __data,List<int> numbers,boolean enabled = true) {
Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_8_default(__data: __data, __slotRenderer_default: () => DoNothing()));
}
#>

0 comments on commit b9a6027

Please sign in to comment.