diff --git a/src/Cake.Issues.Tests/ByteArrayExtensionsTests.cs b/src/Cake.Issues.Tests/ByteArrayExtensionsTests.cs index c9b64fba3..a072f3a78 100644 --- a/src/Cake.Issues.Tests/ByteArrayExtensionsTests.cs +++ b/src/Cake.Issues.Tests/ByteArrayExtensionsTests.cs @@ -56,17 +56,17 @@ public void Should_Throw_If_Value_Is_Null() } [Fact] - public void Should_Throw_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed() + public void Should_Ignore_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed() { // Given var stringValue = "foo🐱bar"; var byteArrayValue = stringValue.ToByteArray(); // When - var result = Record.Exception(() => byteArrayValue.ToStringUsingEncoding(true)); + var result = byteArrayValue.ToStringUsingEncoding(true); // Then - result.IsArgumentException("value"); + result.ShouldBe(stringValue); } [Theory] @@ -131,7 +131,7 @@ public void Should_Throw_If_Value_Is_Null() } [Fact] - public void Should_Throw_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed() + public void Should_Ignore_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed() { // Given var encoding = Encoding.UTF32; @@ -139,10 +139,10 @@ public void Should_Throw_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed() var byteArrayValue = stringValue.ToByteArray(encoding); // When - var result = Record.Exception(() => byteArrayValue.ToStringUsingEncoding(encoding, true)); + var result = byteArrayValue.ToStringUsingEncoding(encoding, true); // Then - result.IsArgumentException("value"); + result.ShouldBe(stringValue); } [Theory] diff --git a/src/Cake.Issues/ByteArrayExtensions.cs b/src/Cake.Issues/ByteArrayExtensions.cs index aeb2d86ef..946c31e67 100644 --- a/src/Cake.Issues/ByteArrayExtensions.cs +++ b/src/Cake.Issues/ByteArrayExtensions.cs @@ -49,12 +49,10 @@ public static string ToStringUsingEncoding(this byte[] value, Encoding encoding, { var preamble = encoding.GetPreamble(); - if (preamble.Where((p, i) => p != value[i]).Any()) + if (value.Zip(preamble, (x, y) => x == y).All(x => x)) { - throw new ArgumentException("Value contains no preamble.", nameof(value)); + value = value.Skip(preamble.Length).ToArray(); } - - value = value.Skip(preamble.Length).ToArray(); } return encoding.GetString(value);