Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions PolyShim.Tests/NetCore10/StringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.NetCore10;

public class StringTests
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
[Fact]
public void Join_StringSeparator_ObjectArray_Test()
{
// Act & assert
string.Join(", ", new object?[] { 1, "two", 3.0 }).Should().Be("1, two, 3");

Check failure on line 13 in PolyShim.Tests/NetCore10/StringTests.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

PolyShim.Tests.NetCore10.StringTests.Join_StringSeparator_ObjectArray_Test

Expected string.Join(", ", new object?[] { null, "a", null }) to be ", a, " with a length of 5, but "" has a length of 0, differs near "" (index 0).
string.Join(", ", new object?[] { }).Should().Be("");
string.Join(", ", new object?[] { null, "a", null }).Should().Be(", a, ");
}

[Fact]
public void Join_StringSeparator_EnumerableT_Test()
{
// Act & assert
string.Join(", ", new List<int> { 1, 2, 3 }).Should().Be("1, 2, 3");
string.Join("-", new List<string?> { "a", null, "b" }).Should().Be("a--b");
string.Join(", ", new List<int> { }).Should().Be("");
}
}
42 changes: 42 additions & 0 deletions PolyShim.Tests/NetCore20/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,46 @@ public void Split_String_Count_Test()
str.Split(" ", 2).Should().Equal("a", "b c");
str.Split(" ", 1).Should().Equal("a b c");
}

[Fact]
public void Join_CharSeparator_ObjectArray_Test()
{
// Act & assert
string.Join(',', new object?[] { 1, "two", 3.0 }).Should().Be("1,two,3");
string.Join(',', new object?[] { }).Should().Be("");
string.Join(',', new object?[] { null, "a", null }).Should().Be(",a,");
}
Comment thread
Tyrrrz marked this conversation as resolved.

[Fact]
public void Join_CharSeparator_StringArray_Test()
{
// Act & assert
string.Join(',', "a", "b", "c").Should().Be("a,b,c");
string.Join(',', new string?[] { }).Should().Be("");
string.Join(',', new string?[] { null, "a", null }).Should().Be(",a,");
}

[Fact]
public void Join_CharSeparator_EnumerableT_Test()
{
// Act & assert
string.Join(',', new System.Collections.Generic.List<int> { 1, 2, 3 })
.Should()
.Be("1,2,3");
string.Join(',', new System.Collections.Generic.List<string?> { "a", null, "b" })
.Should()
.Be("a,,b");
}

[Fact]
public void Join_CharSeparator_WithIndexAndCount_Test()
{
// Arrange
var values = new string?[] { "a", "b", "c", "d" };

// Act & assert
string.Join(',', values, 1, 2).Should().Be("b,c");
string.Join(',', values, 0, 4).Should().Be("a,b,c,d");
string.Join(',', values, 2, 0).Should().Be("");
}
}
9 changes: 9 additions & 0 deletions PolyShim/NetCore10/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics.CodeAnalysis;

Expand All @@ -19,6 +20,14 @@ internal static class MemberPolyfills_NetCore10_String
// https://learn.microsoft.com/dotnet/api/system.string.isnullorwhitespace
public static bool IsNullOrWhiteSpace(string? value) =>
value is null || value.All(char.IsWhiteSpace);

// https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-string-system-object())
public static string Join(string? separator, params object?[] values) =>
string.Join(separator, values.Select(v => v?.ToString()).ToArray());

// https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-string-system-collections-generic-ienumerable((-0)))
public static string Join<T>(string? separator, IEnumerable<T> values) =>
string.Join(separator, values.Select(v => ((object?)v)?.ToString()).ToArray());
Comment thread
Tyrrrz marked this conversation as resolved.
}
}
#endif
24 changes: 24 additions & 0 deletions PolyShim/NetCore20/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -141,5 +143,27 @@ public string[] Split(
StringSplitOptions options = StringSplitOptions.None
) => str.Split([separator ?? ""], options);
}

extension(string)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
// https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-object())
public static string Join(char separator, params object?[] values) =>
string.Join(separator.ToString(), values.Select(v => v?.ToString()).ToArray());

Comment thread
Tyrrrz marked this conversation as resolved.
// https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string())
public static string Join(char separator, params string?[] values) =>
string.Join(separator.ToString(), values);

// https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-char-system-collections-generic-ienumerable((-0)))
public static string Join<T>(char separator, IEnumerable<T> values) =>
string.Join(
separator.ToString(),
values.Select(v => ((object?)v)?.ToString()).ToArray()
);
Comment thread
Tyrrrz marked this conversation as resolved.

// https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string()-system-int32-system-int32)
public static string Join(char separator, string?[] value, int startIndex, int count) =>
Comment thread
Tyrrrz marked this conversation as resolved.
string.Join(separator.ToString(), value, startIndex, count);
}
}
#endif
10 changes: 8 additions & 2 deletions PolyShim/Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 373
- **Total:** 379
- **Types:** 79
- **Members:** 294
- **Members:** 300

___

Expand Down Expand Up @@ -371,6 +371,12 @@ ___
- [`bool IsNullOrWhiteSpace(string?)`](https://learn.microsoft.com/dotnet/api/system.string.isnullorwhitespace) <sup><sub>.NET Core 1.0</sub></sup>
- [`bool StartsWith(char)`](https://learn.microsoft.com/dotnet/api/system.string.startswith#system-string-startswith(system-char)) <sup><sub>.NET Core 2.0</sub></sup>
- [`int GetHashCode(StringComparison)`](https://learn.microsoft.com/dotnet/api/system.string.gethashcode#system-string-gethashcode(system-stringcomparison)) <sup><sub>.NET Core 2.0</sub></sup>
- [`string Join(char, params object?[])`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-object())) <sup><sub>.NET Core 2.0</sub></sup>
- [`string Join(char, params string?[])`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string())) <sup><sub>.NET Core 2.0</sub></sup>
- [`string Join(char, string?[], int, int)`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string()-system-int32-system-int32)) <sup><sub>.NET Core 2.0</sub></sup>
- [`string Join(string?, params object?[])`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-string-system-object())) <sup><sub>.NET Core 1.0</sub></sup>
- [`string Join<T>(char, IEnumerable<T>)`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-char-system-collections-generic-ienumerable((-0)))) <sup><sub>.NET Core 2.0</sub></sup>
- [`string Join<T>(string?, IEnumerable<T>)`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-string-system-collections-generic-ienumerable((-0)))) <sup><sub>.NET Core 1.0</sub></sup>
- [`string Replace(string, string?, bool, CultureInfo?)`](https://learn.microsoft.com/dotnet/api/system.string.replace#system-string-replace(system-string-system-string-system-boolean-system-globalization-cultureinfo)) <sup><sub>.NET Core 2.0</sub></sup>
- [`string Replace(string, string?, StringComparison)`](https://learn.microsoft.com/dotnet/api/system.string.replace#system-string-replace(system-string-system-string-system-stringcomparison)) <sup><sub>.NET Core 2.0</sub></sup>
- [`string ReplaceLineEndings()`](https://learn.microsoft.com/dotnet/api/system.string.replacelineendings#system-string-replacelineendings) <sup><sub>.NET 6.0</sub></sup>
Expand Down
Loading