Skip to content

Commit bc8be1f

Browse files
committed
fall back to memberwise cloning for dictionaries with no empty ctor
1 parent 90e1494 commit bc8be1f

File tree

2 files changed

+258
-204
lines changed

2 files changed

+258
-204
lines changed

FastCloner.Tests/SpecialCaseTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,42 @@ public void CloneJsonNode()
20322032
Assert.That(((JsonObject)original.Config!)["a"]!.GetValue<int>(), Is.EqualTo(1), "Original config should remain unchanged");
20332033
});
20342034
}
2035+
2036+
public class DictionaryWithNonOptionalCtor : Dictionary<string, string>
2037+
{
2038+
public int RequiredValue { get; }
2039+
2040+
public DictionaryWithNonOptionalCtor(int requiredValue)
2041+
{
2042+
RequiredValue = requiredValue;
2043+
}
2044+
}
2045+
2046+
[Test]
2047+
public void DictionaryWithNonOptionalConstructor_ShouldFallbackToMemberwiseClone()
2048+
{
2049+
// Arrange
2050+
DictionaryWithNonOptionalCtor original = new DictionaryWithNonOptionalCtor(42)
2051+
{
2052+
["key1"] = "value1",
2053+
["key2"] = "value2"
2054+
};
2055+
2056+
// Act
2057+
DictionaryWithNonOptionalCtor clone = original.DeepClone();
2058+
clone["key1"] = "value3";
2059+
2060+
// Assert
2061+
Assert.Multiple(() =>
2062+
{
2063+
Assert.That(clone, Is.Not.SameAs(original), "Should create new instance");
2064+
Assert.That(clone, Is.AssignableTo<DictionaryWithNonOptionalCtor>(), "Should preserve type");
2065+
Assert.That(clone.Count, Is.EqualTo(original.Count), "Should preserve count");
2066+
Assert.That(clone["key1"], Is.EqualTo("value3"), "Should preserve values");
2067+
Assert.That(clone["key2"], Is.EqualTo("value2"), "Should preserve values");
2068+
Assert.That(original["key1"], Is.EqualTo("value1"), "Should not affect original value");
2069+
});
2070+
}
20352071

20362072
[Test]
20372073
public void CloneAllJsonNodeTypes()

0 commit comments

Comments
 (0)