Repository: dotnet/maui
File: src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SimplifyOnPlatform.cs (C#)

Code before:
}
""";

		var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0-android");
		Assert.False(result.Diagnostics.Any());

		// OnPlatform should NOT be simplified because the target value is a simple ValueNode
		// and we cannot transfer x:Key to a ValueNode
		Assert.Contains("OnPlatform", generated, StringComparison.Ordinal);
		Assert.Contains("TestColor", generated, StringComparison.Ordinal);
	}

	[Fact]
	public void SimplifyOnPlatformWithXKeyAndElementNode()
	{
		// OnPlatform with x:Key should be simplified if the target value is an ElementNode
		// and the x:Key should be transferred to the replacement element
		var xaml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
	xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	x:Class="Test.TestPage">
	<ContentPage.Resources>
		<OnPlatform x:Key="TestStyle" x:TypeArguments="Style">
			<On Platform="Android">
				<Style TargetType="Label">
					<Setter Property="TextColor" Value="Red" />
				</Style>
			</On>
			<On Platform="iOS">
				<Style TargetType="Label">
					<Setter Property="TextColor" Value="Blue" />
				</Style>
			</On>
		</OnPlatform>
	</ContentPage.Resources>
	<Label Text="Test" Style="{StaticResource TestStyle}" />
</ContentPage>
""";

		var code =
"""
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Test;

[XamlProcessing(XamlInflator.SourceGen)]

Review comment (suggestion):
I'm having hard time figuring out what this is... is this supposed to become `<Color x:Key="TestColor">Red</Color>` on Android and `<Color x:Key="TestColor">Blue</Color>` on iOS? If yes, then I don't understand "OnPlatform should NOT be simplified because the target value is a simple ValueNode and we cannot transfer x:Key to a ValueNode" -- there's the `x:TypeARguments` so we know how to transform the markup, right? I suggest we do that in a follow-up PR.

Code after:
""";

		var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0-android");
		Assert.False(result.Diagnostics.Any());

		// OnPlatform should NOT be simplified because there's no x:TypeArguments and the target is a ValueNode
		Assert.Contains("OnPlatform", generated, StringComparison.Ordinal);
	}

	[Fact]
	public void SimplifyOnPlatformWithXKeyAndXTypeArguments()
	{
		// OnPlatform with x:Key and x:TypeArguments should be simplified by creating a typed element
		// For example, OnPlatform with x:TypeArguments="Color" should become <Color x:Key="TestColor">Red</Color>
		var xaml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
	xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	x:Class="Test.TestPage">
	<ContentPage.Resources>
		<OnPlatform x:Key="TestColor" x:TypeArguments="Color">
			<On Platform="Android" Value="Red" />
			<On Platform="iOS" Value="Blue" />
		</OnPlatform>
	</ContentPage.Resources>
	<Label Text="Test" TextColor="{StaticResource TestColor}" />
</ContentPage>
""";

		var code =
"""
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Test;

[XamlProcessing(XamlInflator.SourceGen)]
public partial class TestPage : ContentPage
{
	public TestPage()
	{
		InitializeComponent();
	}
}
""";

		var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0-android");
		Assert.False(result.Diagnostics.Any());