Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override DictionaryEntry.ToString to match KeyValuePair.ToString #64959

Merged
merged 3 commits into from
Feb 9, 2022
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
Original file line number Diff line number Diff line change
@@ -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<object?[]> 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)}]" };
}
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

[Theory]
[MemberData(nameof(ToString_Inputs))]
public void ToString_FormatsKeyValue(object key, object? value, string expected)
{
var entry = new DictionaryEntry(key, value);
var result = entry.ToString();
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal(expected, result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
Link="Common\System\Collections\TestBase.Generic.Tests.cs" />
<Compile Include="$(CommonTestPath)System\Collections\DebugView.Tests.cs"
Link="Common\System\Collections\DebugView.Tests.cs" />
<Compile Include="$(CommonTestPath)System\Collections\DictionaryEntry.Tests.cs"
Link="Common\System\Collections\DictionaryEntry.Tests.cs" />
<Compile Include="$(CommonTestPath)System\Collections\TestingTypes.cs"
Link="Common\System\Collections\TestingTypes.cs" />
<Compile Include="$(CommonTestPath)System\EnumTypes.cs"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.ComponentModel;

namespace System.Collections
Expand Down Expand Up @@ -40,5 +41,8 @@ public void Deconstruct(out object key, out object? value)
key = Key;
value = Value;
}

public override string ToString() =>
KeyValuePair.PairToString(_key, _value);
}
}