Repository: dotnet/maui
File: src/Controls/tests/BindingSourceGen.UnitTests/IntegrationTests.cs (C#)

Code before:
            var mySourceClass = new MySourceClass();
            mySourceClass.SetBinding();

            namespace MyNamespace
            {
                public class MySourceClass
                {
                    public string Text { get; protected set; } = "Hello";

                    public void SetBinding()
                    {
                        var entry = new Entry();
                        entry.SetBinding(Entry.TextProperty, static (MySourceClass sc) => sc.Text);
                    }
                }
            }
        """;

		var result = SourceGenHelpers.Run(source);
		var id = Math.Abs(result.Binding!.SimpleLocation!.GetHashCode());
		AssertExtensions.AssertNoDiagnostics(result);
		
		// Verify that UnsafeAccessor is generated for the setter
		// Find the interceptor file (not the common helper file)
		var generatedCode = result.GeneratedFiles.First(kvp => kvp.Key.Contains("Path-To-Program", StringComparison.Ordinal)).Value;
		Assert.Contains("SetUnsafeProperty_Text", generatedCode, StringComparison.Ordinal);
		Assert.Contains("[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = \"set_Text\")]", generatedCode, StringComparison.Ordinal);
	}

	[Fact]
	public void GenerateBindingWithPublicPropertyInternalSetter()
	{
		var source = """

            using Microsoft.Maui.Controls;
            using MyNamespace;

            var mySourceClass = new MySourceClass();
            mySourceClass.SetBinding();

            namespace MyNamespace
            {
                public class MySourceClass
                {
                    public string Text { get; internal set; } = "Hello";

                    public void SetBinding()
                    {
                        var entry = new Entry();
                        entry.SetBinding(Entry.TextProperty, static (MySourceClass sc) => sc.Text);
                    }

Review comment (suggestion):
This assert will be fulfilled just by the declaration of the method. We need to confirm, that the method is actually called with the expected arguments: `SetUnsafeProperty_Text(source, value);`

Code after:
			Assert.DoesNotContain("SetUnsafeProperty_Text", generatedCode, StringComparison.Ordinal);
			Assert.Contains("source.Text = value;", generatedCode, StringComparison.Ordinal);
		}
	}

	[Fact]
	public void GenerateBindingWithNullableObjectAndNullableReferenceTypeProperty_SetBinding()
	{
		var source = """
		using Microsoft.Maui.Controls;
		var label = new Label();
		label.SetBinding(Label.TextProperty, static (Foo f) => f.NullableObject?.StringProperty);

		class Foo
		{
			public Bar? NullableObject { get; set; }
		}

		class Bar
		{
			public string StringProperty { get; set; } = "";
		}
		""";

		var result = SourceGenHelpers.Run(source);
		Assert.NotNull(result.Binding);

		var id = Math.Abs(result.Binding.SimpleLocation!.GetHashCode());

		// The key assertion: no CS8603 errors should be present
		AssertExtensions.AssertNoDiagnostics(result);
		AssertExtensions.CodeIsEqual(
			$$"""
			//------------------------------------------------------------------------------
			// <auto-generated>
			//     This code was generated by a .NET MAUI source generator.
			//
			//     Changes to this file may cause incorrect behavior and will be lost if
			//     the code is regenerated.
			// </auto-generated>
			//------------------------------------------------------------------------------
			#nullable enable

			namespace System.Runtime.CompilerServices
			{
			    using System;
			    using System.Diagnostics;

			    {{BindingCodeWriter.GeneratedCodeAttribute}}
			    [Conditional("DEBUG")]
			    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]