Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions Tests/NFUnitTestArithmetic/UnitTestFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public void StringFormat_01()
Assert.ThrowsException(typeof(ArgumentNullException), () => { string.Format(nullFormat, 12345.67); });
}

[TestMethod]
public void StringFormat_NullArgs()
{
// passing a null params array should throw ArgumentNullException
Assert.ThrowsException(typeof(ArgumentNullException), () => { string.Format("{0}", (object[])null); });
}

[TestMethod]
[DataRow("Left align in 10 chars: {0,-10:N2}: and then more", 1234.5641, "Left align in 10 chars: 1,234.56 : and then more")]
[DataRow("Right align in 10 chars: {0,10:N2}: and then more", 1234.5641, "Right align in 10 chars: 1,234.56: and then more")]
Expand Down
26 changes: 26 additions & 0 deletions Tests/NFUnitTestSystemLib/UnitTestStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@ public void TrimEnd_Test21()
Assert.AreEqual(str2.TrimEnd(car1), "@abc @ de");
}

[TestMethod]
public void TrimNullOrEmpty_Test()
{
// null or empty trimChars should behave like the no-arg (whitespace-only) overloads
string withSpaces = " hello ";
string noSpaces = "hello";
char[] empty = new char[] { };

// Trim(null) / Trim(empty) → same as Trim()
Assert.AreEqual(withSpaces.Trim(null), "hello", "Trim(null) should remove whitespace");
Assert.AreEqual(withSpaces.Trim(empty), "hello", "Trim(empty) should remove whitespace");

// TrimStart(null) / TrimStart(empty) → same as TrimStart()
Assert.AreEqual(withSpaces.TrimStart(null), "hello ", "TrimStart(null) should remove leading whitespace");
Assert.AreEqual(withSpaces.TrimStart(empty), "hello ", "TrimStart(empty) should remove leading whitespace");

// TrimEnd(null) / TrimEnd(empty) → same as TrimEnd()
Assert.AreEqual(withSpaces.TrimEnd(null), " hello", "TrimEnd(null) should remove trailing whitespace");
Assert.AreEqual(withSpaces.TrimEnd(empty), " hello", "TrimEnd(empty) should remove trailing whitespace");

// Nothing to trim → original value returned
Assert.AreEqual(noSpaces.Trim(), "hello", "Trim() on string with no whitespace should return same value");
Assert.AreEqual(noSpaces.TrimStart(), "hello", "TrimStart() on string with no whitespace should return same value");
Assert.AreEqual(noSpaces.TrimEnd(), "hello", "TrimEnd() on string with no whitespace should return same value");
}

[TestMethod]
public void IndexOf_Test28()
{
Expand Down
14 changes: 7 additions & 7 deletions nanoFramework.CoreLibrary/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IEnumerator GetEnumerator()
/// <returns>true if obj is a String and its value is the same as this instance; otherwise, false. If obj is null, the method returns false.</returns>
public override bool Equals(object obj)
{
var s = obj as string;
string s = obj as string;
return s != null && Equals(this, s);
}

Expand All @@ -62,7 +62,7 @@ public override bool Equals(object obj)

#nullable enable
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool Equals(string a, string b);
public static extern bool Equals(string? a, string? b);
#nullable restore

#else
Expand Down Expand Up @@ -215,17 +215,17 @@ public extern int Length
#nullable enable

/// <summary>
/// Removes all the leading occurrences of a set of characters specified in an array from the current string.
/// Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.
/// </summary>
/// <param name="trimChars">An array of Unicode characters to remove, or <see langword="null"/>.</param>
/// <returns>The string that remains after all occurrences of characters in the <paramref name="trimChars"/> parameter are removed from the start of the current string. If <paramref name="trimChars"/> is <see langword="null"/> or an empty array, white-space characters are removed instead. If no characters can be trimmed from the current instance, the method returns the current instance unchanged.</returns>
/// <returns>The string that remains after all occurrences of characters in the <paramref name="trimChars"/> parameter are removed from the start and end of the current string. If <paramref name="trimChars"/> is <see langword="null"/> or an empty array, white-space characters are removed instead. If no characters can be trimmed from the current instance, the method returns the current instance unchanged.</returns>
[MethodImpl(MethodImplOptions.InternalCall)]
public extern string Trim(params char[]? trimChars);

/// <summary>
/// Removes all the leading occurrences of a set of characters specified in an array from the current string.
/// Removes all leading white-space characters from the current string.
/// </summary>
/// <returns>The string that remains after all occurrences of characters in the trimChars parameter are removed from the start of the current string. If trimChars is null or an empty array, white-space characters are removed instead.</returns>
/// <returns>The string that remains after all white-space characters are removed from the start of the current string. If no characters can be trimmed from the current instance, the method returns the current instance unchanged.</returns>
[MethodImpl(MethodImplOptions.InternalCall)]
public extern string TrimStart();

Expand All @@ -238,7 +238,7 @@ public extern int Length
public extern string TrimStart(params char[]? trimChars);

/// <summary>
/// Removes all the trailing occurrences of a set of characters specified in an array from the current string.
/// Removes all trailing white-space characters from the current string.
/// </summary>
/// <returns>The string that remains after all white-space characters are removed from the end of the current string. If no characters can be trimmed from the current instance, the method returns the current instance unchanged.</returns>
[MethodImpl(MethodImplOptions.InternalCall)]
Expand Down