Repository: dotnet/runtime
File: src/libraries/System.Security.Cryptography/tests/Base64TransformsTests.cs (C#)

Code before:
            }

            string outputString = Text.Encoding.ASCII.GetString(outputBytes, 0, written);
            Assert.Equal(expected, outputString);
        }

        [Fact]
        public void TransformBlock_PartialBlockFollowedByPowerOfTwoInput_DoesNotThrow()
        {
            using FromBase64Transform transform = new();
            byte[] dest = new byte[2048];

            byte[] buf1 = "AAA"u8.ToArray();
            int written = transform.TransformBlock(buf1, 0, 3, dest, 0);

            // Nothing should be written because it is a partial block the transform will hold on to.
            Assert.Equal(0, written);

            // Transform a buffer that is exactly a power of two, meaning the ArrayPool (CryptoPool) will not be oversized.
            byte[] buf2 = new byte[1024];
            buf2.AsSpan().Fill((byte)'A');

            written = transform.TransformBlock(buf2, 0, buf2.Length, dest, written);

            // The transform should have processed data without throwing
            Assert.True(written > 0);
        }

        [Fact]
        public void TransformFinalBlock_PartialBlockFollowedByPowerOfTwoInput_DoesNotThrow()
        {
            using FromBase64Transform transform = new();
            byte[] dest = new byte[2048];

            byte[] buf1 = "AAA"u8.ToArray();
            int written = transform.TransformBlock(buf1, 0, 3, dest, 0);

            // Nothing should be written because it is a partial block the transform will hold on to.
            Assert.Equal(0, written);

            // Transform a buffer that is exactly a power of two via TransformFinalBlock
            byte[] buf2 = new byte[1024];
            buf2.AsSpan().Fill((byte)'A');

            // This should not throw
            byte[] result = transform.TransformFinalBlock(buf2, 0, buf2.Length);

            // The transform should have processed data without throwing
            Assert.True(result.Length > 0);
        }


Review comment (refactor):
@copilot let's clean up this test a little bit.

* Please change the `Assert.True(written > 0)` to `Assert.NotEqual(0, written)`.
* Let's change some of these variable names. Change `buf1` to `partialBlock` and `buf2` to `completeBlock`.
* For the invocation of the first call to `TransformBlock`, lets pass in `buf1.Length` as the third parameter instead of the literal value 3.
* Remove the comment "The transform should have processed data without throwing". The assert is reasonable enough to know we should have gotten that far without throwing.

Please apply the same changes to the `TransformFinalBlock` test as well.

Code after:
            }

            string outputString = Text.Encoding.ASCII.GetString(outputBytes, 0, written);
            Assert.Equal(expected, outputString);
        }

        [Fact]
        public void TransformBlock_PartialBlockFollowedByPowerOfTwoInput_DoesNotThrow()
        {
            using FromBase64Transform transform = new();
            byte[] dest = new byte[2048];

            byte[] partialBlock = "AAA"u8.ToArray();
            int written = transform.TransformBlock(partialBlock, 0, partialBlock.Length, dest, 0);

            // Nothing should be written because it is a partial block the transform will hold on to.
            Assert.Equal(0, written);

            // Transform a buffer that is exactly a power of two, meaning the ArrayPool (CryptoPool) will not be oversized.
            byte[] completeBlock = new byte[1024];
            completeBlock.AsSpan().Fill((byte)'A');

            written = transform.TransformBlock(completeBlock, 0, completeBlock.Length, dest, written);
            Assert.NotEqual(0, written);
        }

        [Fact]
        public void TransformFinalBlock_PartialBlockFollowedByPowerOfTwoInput_DoesNotThrow()
        {
            using FromBase64Transform transform = new();
            byte[] dest = new byte[2048];

            byte[] partialBlock = "AAA"u8.ToArray();
            int written = transform.TransformBlock(partialBlock, 0, partialBlock.Length, dest, 0);

            // Nothing should be written because it is a partial block the transform will hold on to.
            Assert.Equal(0, written);

            // Transform a buffer that is exactly a power of two via TransformFinalBlock
            byte[] completeBlock = new byte[1024];
            completeBlock.AsSpan().Fill((byte)'A');

            byte[] result = transform.TransformFinalBlock(completeBlock, 0, completeBlock.Length);
            Assert.NotEqual(0, result.Length);
        }

        [Fact]
        public void TransformBlock_OutputBufferTooSmall_ThrowsArgumentOutOfRangeException()
        {
            using FromBase64Transform transform = new();
            byte[] destination = new byte[0];