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

Code before:

[XamlProcessing(XamlInflator.SourceGen)]
public partial class TestPage : ContentPage
{
	public Container? Container { get; set; }
}

public class Container
{
	public int Count { get; set; }
}
""";

		var (result, generated) = RunGenerator(xaml, code);
		
		Assert.False(result.Diagnostics.Any(d => d.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error));
		Assert.NotNull(generated);
		
		// Verify setter uses pattern matching for conditional access
		Assert.Contains("if (source.Container is {} p0)", generated, StringComparison.Ordinal);
		
		// Should NOT have HasValue check since Count is int, not int?
		Assert.DoesNotContain("HasValue", generated, StringComparison.Ordinal);
		
		// Getter should have ?? default for value type through nullable path
		Assert.Contains("?? default", generated, StringComparison.Ordinal);
	}

	[Fact]
	public void NonNullableReferenceTypeAtEndOfConditionalAccessPath()
	{
		// Test non-nullable reference type property at the end of a binding path with conditional access
		// The setter should have an early return for null since the target property doesn't accept null
		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"
	xmlns:test="clr-namespace:Test"
	x:Class="Test.TestPage"
	x:DataType="test:TestPage">
	<Entry Text="{Binding Container.Label}"/>
</ContentPage>
""";

		var code =
"""
#nullable enable
using System;
using Microsoft.Maui.Controls;

Review comment (suggestion):
I think it is safe to assert the full expected expression here. This `?? default` lacks any context and could match some other location in the generated code in the future.

Code after:

[XamlProcessing(XamlInflator.SourceGen)]
public partial class TestPage : ContentPage
{
	public Container? Container { get; set; }
}

public class Container
{
	public int Count { get; set; }
}
""";

		var (result, generated) = RunGenerator(xaml, code);
		
		Assert.False(result.Diagnostics.Any(d => d.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error));
		Assert.NotNull(generated);
		
		// Verify setter uses pattern matching for conditional access
		Assert.Contains("if (source.Container is {} p0)", generated, StringComparison.Ordinal);
		
		// Should NOT have HasValue check since Count is int, not int?
		Assert.DoesNotContain("HasValue", generated, StringComparison.Ordinal);
		
		// Getter should have ?? default for value type through nullable path
		Assert.Contains("getter: source => (source.Container?.Count ?? default, true)", generated, StringComparison.Ordinal);
	}

	[Fact]
	public void NonNullableReferenceTypeAtEndOfConditionalAccessPath()
	{
		// Test non-nullable reference type property at the end of a binding path with conditional access
		// The setter should have an early return for null since the target property doesn't accept null
		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"
	xmlns:test="clr-namespace:Test"
	x:Class="Test.TestPage"
	x:DataType="test:TestPage">
	<Entry Text="{Binding Container.Label}"/>
</ContentPage>
""";

		var code =
"""
#nullable enable
using System;
using Microsoft.Maui.Controls;