diff --git a/Tests/NFUnitTestArithmetic/UnitTestFormat.cs b/Tests/NFUnitTestArithmetic/UnitTestFormat.cs index 486add7..dbc1677 100644 --- a/Tests/NFUnitTestArithmetic/UnitTestFormat.cs +++ b/Tests/NFUnitTestArithmetic/UnitTestFormat.cs @@ -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")] diff --git a/Tests/NFUnitTestSystemLib/UnitTestStringTests.cs b/Tests/NFUnitTestSystemLib/UnitTestStringTests.cs index 46b94cf..ac2cc8a 100644 --- a/Tests/NFUnitTestSystemLib/UnitTestStringTests.cs +++ b/Tests/NFUnitTestSystemLib/UnitTestStringTests.cs @@ -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() { diff --git a/nanoFramework.CoreLibrary/System/String.cs b/nanoFramework.CoreLibrary/System/String.cs index 66faa69..c9417e0 100644 --- a/nanoFramework.CoreLibrary/System/String.cs +++ b/nanoFramework.CoreLibrary/System/String.cs @@ -48,7 +48,7 @@ public IEnumerator GetEnumerator() /// 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. public override bool Equals(object obj) { - var s = obj as string; + string s = obj as string; return s != null && Equals(this, s); } @@ -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 @@ -215,17 +215,17 @@ public extern int Length #nullable enable /// - /// 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. /// /// An array of Unicode characters to remove, or . - /// The string that remains after all occurrences of characters in the parameter are removed from the start of the current string. If is 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. + /// The string that remains after all occurrences of characters in the parameter are removed from the start and end of the current string. If is 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. [MethodImpl(MethodImplOptions.InternalCall)] public extern string Trim(params char[]? trimChars); /// - /// 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. /// - /// 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. + /// 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. [MethodImpl(MethodImplOptions.InternalCall)] public extern string TrimStart(); @@ -238,7 +238,7 @@ public extern int Length public extern string TrimStart(params char[]? trimChars); /// - /// 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. /// /// 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. [MethodImpl(MethodImplOptions.InternalCall)]