Skip to content

Commit

Permalink
Fix issue with template processing (#26196)
Browse files Browse the repository at this point in the history
* Fix issue with template processing

The template is processing the actual code

* Added a test

* Fix the tests

* Add temporary test for CollectionViewHandler2

* Move temporary test comment to correct location

* Remove redundant `#endif` assertions in tests
  • Loading branch information
mattleibow authored Jan 14, 2025
1 parent 289ed27 commit cffbb96
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Templates/src/templates/maui-mobile/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public static MauiApp CreateMauiApp()
.ConfigureSyncfusionToolkit()
.ConfigureMauiHandlers(handlers =>
{
//-:cnd:noEmit
#if IOS || MACCATALYST
handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
#endif
//+:cnd:noEmit
})
#endif
.ConfigureFonts(fonts =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ protected void EnableTizen(string projectFile)
{ "</TargetFrameworks> -->", "</TargetFrameworks>" },
});
}

protected void AssertContains(string expected, string actual)
{
Assert.IsTrue(
actual.Contains(expected, StringComparison.Ordinal),
$"Expected string '{actual}' to contain '{expected}'.");
}

protected void AssertDoesNotContain(string expected, string actual)
{
Assert.IsFalse(
actual.Contains(expected, StringComparison.Ordinal),
$"Expected string '{actual}' to not contain '{expected}'.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Xml.Linq;
using System.Xml.Linq;

namespace Microsoft.Maui.IntegrationTests;

Expand Down Expand Up @@ -329,4 +329,32 @@ public void BuildWithDifferentVersionNumber(string id, string config, string dis
Assert.IsTrue(DotnetInternal.Build(projectFile, config, properties: buildProps, msbuildWarningsAsErrors: true),
$"Project {Path.GetFileName(projectFile)} failed to build. Check test output/attachments for errors.");
}

// This test is super temporary and is just for the interim
// while we productize the CollectionViewHandler2. Once we
// ship it as the default, this test will fail and can be deleted.
[Test]
[TestCase("maui", DotNetCurrent, "", false)]
[TestCase("maui", DotNetCurrent, "--sample-content", true)]
public void SampleShouldHaveHandler2Registered(string id, string framework, string additionalDotNetNewParams, bool shouldHaveHandler2)
{
var projectDir = TestDirectory;
var programFile = Path.Combine(projectDir, "MauiProgram.cs");

Assert.IsTrue(DotnetInternal.New(id, projectDir, framework, additionalDotNetNewParams),
$"Unable to create template {id}. Check test output for errors.");

var programContents = File.ReadAllText(programFile);

if (shouldHaveHandler2)
{
AssertContains("#if IOS || MACCATALYST", programContents);
AssertContains("handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();", programContents);
}
else
{
AssertDoesNotContain("#if IOS || MACCATALYST", programContents);
AssertDoesNotContain("handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();", programContents);
}
}
}

0 comments on commit cffbb96

Please sign in to comment.