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
5 changes: 5 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
exclude_paths:
# Vendored third-party comparison code (the FastDeepCloner library), kept verbatim so the tests
# can compare behaviour against it. It is not ours to refactor.
- "DeepCloner.Tests/Imported/**"
5 changes: 3 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ licensing and community files are governed by the open source PanoramicData.Nuge

## Scope and boundaries

- Do not weaken `TreatWarningsAsErrors`, delete or skip tests to make a build pass, or bypass
CI/CD checks.
- Do not weaken `TreatWarningsAsErrors`.
- Do not delete or skip tests to make a build pass.
- Do not bypass CI/CD checks.
- Do not commit secrets, credentials, or API tokens.
- Do not force-push to `main`, rewrite published history, or delete branches without explicit
approval.
Expand Down
3 changes: 2 additions & 1 deletion DeepCloner.Tests/ArraysSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable
#pragma warning disable S1104 // Public fields are deliberate: these fixtures exist to test field cloning
#nullable disable

using Xunit;
using PanoramicData.DeepCloner;
Expand Down
3 changes: 2 additions & 1 deletion DeepCloner.Tests/ConstructorsSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable
#pragma warning disable S3877 // Equals throws deliberately: the test checks that cloning never calls it
#nullable disable

using Xunit;
using PanoramicData.DeepCloner;
Expand Down
29 changes: 29 additions & 0 deletions DeepCloner.Tests/CopyToObjectSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,35 @@ public void MultiDim_Array_Should_Be_Cloned(bool isDeep)
Assert.Equal(1, arrTo.GetValue(0, 0));
}

public readonly record struct StructWithReference(int[] Items);

[Theory]
[InlineData(false)]
[InlineData(true)]
public void TwoDim_Array_Of_Structs_With_References_Should_Be_Cloned(bool isDeep)
{
var arrFrom = new[,]
{
{ new StructWithReference([1]), new StructWithReference([2]) },
{ new StructWithReference([3]), new StructWithReference([4]) }
};
var arrTo = new StructWithReference[2, 2];

if (isDeep)
{
arrFrom.DeepCloneTo(arrTo);
}
else
{
arrFrom.ShallowCloneTo(arrTo);
}

Assert.Equal([4], arrTo[1, 1].Items);
Assert.Equal([1], arrTo[0, 0].Items);
Assert.Equal([3], arrTo[1, 0].Items);
Assert.Equal(isDeep, !ReferenceEquals(arrFrom[1, 1].Items, arrTo[1, 1].Items));
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down
3 changes: 2 additions & 1 deletion DeepCloner.Tests/InheritanceSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable
#pragma warning disable S1104, S2357 // Public fields are deliberate: these fixtures exist to test field cloning
#nullable disable

using Xunit;
using PanoramicData.DeepCloner;
Expand Down
3 changes: 2 additions & 1 deletion DeepCloner.Tests/Objects/DoableStruct1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace PanoramicData.DeepCloner.Test.Objects;
#pragma warning disable S1104 // Public fields are deliberate: these fixtures exist to test field cloning
namespace PanoramicData.DeepCloner.Test.Objects;

public struct DoableStruct1 : IDoable, IEquatable<DoableStruct1>
{
Expand Down
119 changes: 119 additions & 0 deletions DeepCloner.Tests/ReadonlyStructSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#nullable disable

using Xunit;

namespace PanoramicData.DeepCloner.Test;

/// <summary>
/// A struct whose fields are all readonly (a <c>readonly struct</c> or <c>readonly record struct</c>) must be
/// deep cloned like any other struct: its reference-type fields must not be shared with the source.
/// </summary>
public class ReadonlyStructSpec() : BaseTest(true)
{
public readonly record struct Holder(int[] Items);

public sealed class Box
{
public int Value { get; set; }
}

public readonly record struct BoxHolder(Box Box);

public record struct MutableHolder
{
public int[] Items { get; set; }
}

public sealed class HoldsReadonlyStruct
{
public Holder Holder { get; set; }
}

[Fact]
public void Readonly_Struct_With_Array_Should_Be_Deep_Cloned()
{
var source = new Holder([1, 2, 3]);

var clone = source.DeepClone();

Assert.Equal([1, 2, 3], clone.Items);
Assert.NotSame(source.Items, clone.Items);
}

[Fact]
public void Readonly_Struct_With_Class_Should_Be_Deep_Cloned()
{
var source = new BoxHolder(new Box { Value = 7 });

var clone = source.DeepClone();

Assert.Equal(7, clone.Box.Value);
Assert.NotSame(source.Box, clone.Box);
}

[Fact]
public void Readonly_Struct_Inside_Class_Should_Be_Deep_Cloned()
{
var source = new HoldsReadonlyStruct { Holder = new Holder([4, 5]) };

var clone = source.DeepClone();

Assert.Equal([4, 5], clone.Holder.Items);
Assert.NotSame(source.Holder.Items, clone.Holder.Items);
}

[Fact]
public void One_Dim_Array_Of_Readonly_Structs_Should_Be_Deep_Cloned()
{
var source = new[] { new Holder([1]), new Holder([2]) };

var clone = source.DeepClone();

Assert.Equal([2], clone[1].Items);
Assert.NotSame(source[1].Items, clone[1].Items);
}

[Fact]
public void Two_Dim_Array_Of_Readonly_Structs_Should_Be_Deep_Cloned()
{
var source = new[,] { { new Holder([1]), new Holder([2]) }, { new Holder([3]), new Holder([4]) } };

var clone = source.DeepClone();

Assert.Equal([4], clone[1, 1].Items);
Assert.NotSame(source[1, 1].Items, clone[1, 1].Items);
}

[Fact]
public void Deep_Clone_To_Existing_Array_Of_Readonly_Structs_Should_Be_Deep()
{
var source = new[,] { { new Holder([1]), new Holder([2]) }, { new Holder([3]), new Holder([4]) } };
var target = new Holder[2, 2];

source.DeepCloneTo(target);

Assert.Equal([4], target[1, 1].Items);
Assert.NotSame(source[1, 1].Items, target[1, 1].Items);
}

[Fact]
public void Shallow_Clone_Of_Readonly_Struct_Should_Share_References()
{
var source = new Holder([1, 2, 3]);

var clone = source.ShallowClone();

Assert.Same(source.Items, clone.Items);
}

[Fact]
public void Mutable_Struct_With_Array_Should_Still_Be_Deep_Cloned()
{
var source = new MutableHolder { Items = [1, 2, 3] };

var clone = source.DeepClone();

Assert.Equal([1, 2, 3], clone.Items);
Assert.NotSame(source.Items, clone.Items);
}
}
3 changes: 2 additions & 1 deletion DeepCloner.Tests/SimpleObjectSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable
#pragma warning disable S1104, S2357 // Public fields are deliberate: these fixtures exist to test field cloning
#nullable disable

using Xunit;
using PanoramicData.DeepCloner.Test.Objects;
Expand Down
Loading
Loading