Repository: dotnet/roslyn
File: src/Compilers/Core/CodeAnalysisTest/Text/SourceTextTests.cs (C#)

Code before:

            var writer = new StringWriter();
            sourceText.Write(writer, span);

            Assert.Equal(Text.Substring(span.Start, span.Length), writer.ToString());
        }

        [Fact]
        public void WriteWithSpanStartingAfterEndThrowsOutOfRange()
        {
            var ex = Assert.ThrowsAny<ArgumentOutOfRangeException>(() =>
                SourceText.From("ABC").Write(TextWriter.Null, TextSpan.FromBounds(4, 4)));

            Assert.Equal("span", ex.ParamName);
        }

        [Fact]
        public void WriteWithSpanEndingAfterEndThrowsOutOfRange()
        {
            var ex = Assert.ThrowsAny<ArgumentOutOfRangeException>(() =>
                SourceText.From("ABC").Write(TextWriter.Null, TextSpan.FromBounds(2, 4)));

            Assert.Equal("span", ex.ParamName);
        }

        [Fact]
        public void Comparer_EqualityContract_SameContent_DifferentEncodings()
        {
            // Test case from the issue: two SourceText instances with same content but different encodings
            // should be equal and have the same hash code when using content-based comparison
            const string content = "Hello, World!";
            var utf8 = Encoding.UTF8;
            var unicode = Encoding.Unicode;

            var text1 = SourceText.From(content, utf8);
            var text2 = SourceText.From(content, unicode);

            var comparer = SourceTextComparer.Instance;

            // Both should be considered equal by the comparer (content-based)
            Assert.True(comparer.Equals(text1, text2));

            // They must have the same hash code to satisfy IEqualityComparer contract
            Assert.Equal(comparer.GetHashCode(text1), comparer.GetHashCode(text2));
        }

        [Fact]
        public void Comparer_EqualityContract_SameContent_WithAndWithoutBOM()
        {
            // Test the case described in the issue comments:
            // Create source texts from byte arrays where one has a BOM and the other doesn't

Review comment (suggestion):
@copilot update this to 	`[Fact, WorkItem("")]` with the write url in the workitem for what is being fixed.

Code after:

            var writer = new StringWriter();
            sourceText.Write(writer, span);

            Assert.Equal(Text.Substring(span.Start, span.Length), writer.ToString());
        }

        [Fact]
        public void WriteWithSpanStartingAfterEndThrowsOutOfRange()
        {
            var ex = Assert.ThrowsAny<ArgumentOutOfRangeException>(() =>
                SourceText.From("ABC").Write(TextWriter.Null, TextSpan.FromBounds(4, 4)));

            Assert.Equal("span", ex.ParamName);
        }

        [Fact]
        public void WriteWithSpanEndingAfterEndThrowsOutOfRange()
        {
            var ex = Assert.ThrowsAny<ArgumentOutOfRangeException>(() =>
                SourceText.From("ABC").Write(TextWriter.Null, TextSpan.FromBounds(2, 4)));

            Assert.Equal("span", ex.ParamName);
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71006")]
        public void Comparer_EqualityContract_SameContent_DifferentEncodings()
        {
            // Test case from the issue: two SourceText instances with same content but different encodings
            // should be equal and have the same hash code when using content-based comparison
            const string content = "Hello, World!";
            var utf8 = Encoding.UTF8;
            var unicode = Encoding.Unicode;

            var text1 = SourceText.From(content, utf8);
            var text2 = SourceText.From(content, unicode);

            var comparer = SourceTextComparer.Instance;

            // Both should be considered equal by the comparer (content-based)
            Assert.True(comparer.Equals(text1, text2));

            // They must have the same hash code to satisfy IEqualityComparer contract
            Assert.Equal(comparer.GetHashCode(text1), comparer.GetHashCode(text2));
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71006")]
        public void Comparer_EqualityContract_SameContent_WithAndWithoutBOM()
        {
            // Test the case described in the issue comments:
            // Create source texts from byte arrays where one has a BOM and the other doesn't