Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs (C#)

Code before:
            // For partial methods, Locations and DeclaringSyntaxReferences return only one location,
            // not both the definition and implementation parts. To get all locations, you must use
            // PartialDefinitionPart and PartialImplementationPart properties.

            var source = @"
namespace N1
{
    partial class C1
    {
        partial void PartialM();
    }
}";

            var source2 = @"
namespace N1
{
    partial class C1
    {
        partial void PartialM() { }
    }
}";

            var comp = CreateCompilation(new[] { source, source2 });
            comp.VerifyDiagnostics();

            var methodSymbols = comp.GetSymbolsWithName("PartialM");
            Assert.Equal(1, methodSymbols.Count());

            var method = methodSymbols.First() as IMethodSymbol;
            Assert.NotNull(method);

            // For partial methods, Locations and DeclaringSyntaxReferences contain only one location
            Assert.Equal(1, method.Locations.Length);
            Assert.Equal(1, method.DeclaringSyntaxReferences.Length);

            // The single location is the definition part
            Assert.True(method.IsPartialDefinition);
            Assert.Null(method.PartialDefinitionPart);
            Assert.NotNull(method.PartialImplementationPart);

            // To get all locations, you need to use PartialImplementationPart
            var implementationPart = method.PartialImplementationPart;
            Assert.Equal(1, implementationPart.Locations.Length);
            Assert.Equal(1, implementationPart.DeclaringSyntaxReferences.Length);

            // Verify the locations are different
            Assert.NotEqual(method.Locations[0], implementationPart.Locations[0]);
        }
    }
}


Review comment (suggestion):
This is unnecessary lengthy and complicated. Since we know there's only one and it's a method symbol: we can do 1 line, `var method = (IMethodSymbol)comp.GetSymbolsWithName("PartialM").Single();`, instead of 5 lines #Closed

Code after:
                namespace N1
                {
                    partial class C1
                    {
                        partial void PartialM();
                    }
                }
                """;

            var source2 = """
                namespace N1
                {
                    partial class C1
                    {
                        partial void PartialM() { }
                    }
                }
                """;

            var comp = CreateCompilation([(source1, "source1"), (source2, "source2")]);
            comp.VerifyDiagnostics();

            var method = (IMethodSymbol)comp.GetSymbolsWithName("PartialM").Single();

            // For partial methods, Locations and DeclaringSyntaxReferences contain only one location
            Assert.Equal(1, method.Locations.Length);
            Assert.Equal(1, method.DeclaringSyntaxReferences.Length);

            // The single location is the definition part
            Assert.True(method.IsPartialDefinition);
            Assert.Null(method.PartialDefinitionPart);
            Assert.NotNull(method.PartialImplementationPart);

            // To get all locations, you need to use PartialImplementationPart
            var implementationPart = method.PartialImplementationPart;
            Assert.Equal(1, implementationPart.Locations.Length);
            Assert.Equal(1, implementationPart.DeclaringSyntaxReferences.Length);

            // Verify the locations are different
            Assert.NotEqual(method.Locations[0], implementationPart.Locations[0]);

            Assert.Equal("source1", method.Locations[0].SourceTree.FilePath);
            Assert.Equal("source2", implementationPart.Locations[0].SourceTree.FilePath);
        }
    }
}