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

Fix slot not being populated when called from root method #50

Closed
wants to merge 2 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
28 changes: 16 additions & 12 deletions src/ManiaTemplates/Lib/MtTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public string BuildManialink(MtComponent rootComponent, string className, int ve
var body = ProcessNode(
XmlStringToNode(rootComponent.TemplateContent),
_engine.BaseMtComponents.Overload(rootComponent.ImportedComponents),
rootContext
rootContext,
parentComponent: rootComponent,
isRootNode: true
);

var template = new Snippet
Expand Down Expand Up @@ -234,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 @@ -270,7 +272,7 @@ private static string CreateLocalVariablesFromContext(MtDataContext context,
/// Process a ManiaTemplate node.
/// </summary>
private string ProcessNode(XmlNode node, MtComponentMap availableMtComponents, MtDataContext context,
MtComponent? parentComponent = null)
MtComponent? parentComponent = null, bool isRootNode = false)
{
Snippet snippet = new();

Expand Down Expand Up @@ -312,7 +314,8 @@ private string ProcessNode(XmlNode node, MtComponentMap availableMtComponents, M
component
),
slotContents,
parentComponent
parentComponent,
isRootNode
);

subSnippet.AppendLine(_maniaTemplateLanguage.FeatureBlockStart())
Expand Down Expand Up @@ -345,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 @@ -420,7 +424,8 @@ private string ProcessComponentNode(
MtComponentAttributes attributeList,
string componentBody,
IReadOnlyDictionary<string, string> slotContents,
MtComponent? parentComponent = null
MtComponent? parentComponent = null,
bool isRootNode = false
)
{
foreach (var slotName in component.Slots)
Expand Down Expand Up @@ -527,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 @@ -537,11 +542,10 @@ private string ProcessComponentNode(
{
foreach (var parentSlotName in component.Slots)
{
renderComponentCall.Append(
$", __slotRenderer_{parentSlotName}: () => DoNothing()");
renderComponentCall.Append($", __slotRenderer_{parentSlotName}: () => DoNothing()");
}
}

renderComponentCall.Append(')');

i++;
Expand All @@ -554,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>
12 changes: 4 additions & 8 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;
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()));
}
#>
Loading