diff --git a/src/libraries/Common/tests/System/Collections/DictionaryEntry.Tests.cs b/src/libraries/Common/tests/System/Collections/DictionaryEntry.Tests.cs new file mode 100644 index 00000000000000..d096f890dc3405 --- /dev/null +++ b/src/libraries/Common/tests/System/Collections/DictionaryEntry.Tests.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using Xunit; + +namespace System.Collections.Tests +{ + public class DictionaryEntry_Tests + { + public static IEnumerable ToString_Inputs() + { + // Mainline scenarios + yield return new object?[] { "key", "value", "[key, value]" }; + yield return new object?[] { 1, 2, "[1, 2]" }; + + // Types, nulls, and empty strings get standard ToString behavior + yield return new object?[] { typeof(object), (object?)null, "[System.Object, ]" }; + yield return new object?[] { (object?)null, (object?)null, "[, ]" }; + yield return new object?[] { "", "", "[, ]" }; + + // There's no escaping; keys and values are emitted as-is + yield return new object?[] { "[key, value", "]]", "[[key, value, ]]]" }; + yield return new object?[] { new DictionaryEntry("key", "key"), new DictionaryEntry("value", "value"), "[[key, key], [value, value]]" }; + + // There's no truncation; keys and values are emitted as-is + yield return new object?[] { new String('K', 512), new String('V', 1024), $"[{new String('K', 512)}, {new String('V', 1024)}]" }; + } + + [Theory] + [MemberData(nameof(ToString_Inputs))] + public void ToString_FormatsKeyValue(object key, object? value, string expected) + { + var entry = new DictionaryEntry(key, value); + string result = entry.ToString(); + + Assert.Equal(expected, result); + } + } +} diff --git a/src/libraries/System.Collections/tests/System.Collections.Tests.csproj b/src/libraries/System.Collections/tests/System.Collections.Tests.csproj index d681aafe746bd2..6dee6b92c77644 100644 --- a/src/libraries/System.Collections/tests/System.Collections.Tests.csproj +++ b/src/libraries/System.Collections/tests/System.Collections.Tests.csproj @@ -37,6 +37,8 @@ Link="Common\System\Collections\TestBase.Generic.Tests.cs" /> + + KeyValuePair.PairToString(_key, _value); } }