diff --git a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/CloneUtilityFixture.cs b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/CloneUtilityFixture.cs index 14d368fc..7e992b3c 100644 --- a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/CloneUtilityFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/CloneUtilityFixture.cs @@ -1,39 +1,103 @@ -using Reveal.Sdk.Dom.Core.Utilities; -using System.Collections.Generic; -using Xunit; - -namespace Reveal.Sdk.Dom.Tests.Core.Utilities -{ - public class CloneUtilityFixture - { - [Fact] - public void Clone_Null_ReturnsDefault() - { - // Arrange - object item = null; - - // Act - var result = CloneUtility.Clone(item); - - // Assert - Assert.Equal(default, result); - } - - [Fact] - public void Clone_List_ReturnsNewInstanceWithIdenticalValues() - { - // Arrange - var original = new List { "one", "two", "three" }; - - // Act - var result = CloneUtility.Clone(original); - - // Assert - Assert.Equal(original.Count, result.Count); - for (int i = 0; i < original.Count; i++) - { - Assert.Equal(original[i], result[i]); - } - } - } -} +using Reveal.Sdk.Dom.Core.Utilities; +using System.Collections.Generic; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Core.Utilities +{ + public class CloneUtilityFixture + { + private class CloneTestClass + { + public int Id { get; set; } + public string Name { get; set; } + } + + [Fact] + public void Clone_ShouldReturnNull_WhenItemIsNull() + { + // Arrange + object item = null; + + // Act + var result = CloneUtility.Clone(item); + + // Assert + Assert.Null(result); + } + + [Fact] + public void Clone_ShouldReturnDeepCopyOfItem() + { + // Arrange + var item = new CloneTestClass + { + Id = 1, + Name = "Test" + }; + + // Act + var result = CloneUtility.Clone(item); + + // Assert + Assert.NotSame(item, result); + Assert.Equal(item.Id, result.Id); + Assert.Equal(item.Name, result.Name); + } + + [Fact] + public void CloneList_ShouldReturnEmptyList_WhenListIsNull() + { + // Arrange + List list = null; + + // Act + var result = CloneUtility.Clone(list); + + // Assert + Assert.NotNull(result); + Assert.Empty(result); + } + + [Fact] + public void CloneList_ShouldReturnDeepCopyOfList() + { + // Arrange + var list = new List + { + new CloneTestClass { Id = 1, Name = "Test1" }, + new CloneTestClass { Id = 2, Name = "Test2" }, + new CloneTestClass { Id = 3, Name = "Test3" } + }; + + // Act + var result = CloneUtility.Clone(list); + + // Assert + Assert.NotSame(list, result); + Assert.Equal(list.Count, result.Count); + for (int i = 0; i < list.Count; i++) + { + Assert.NotSame(list[i], result[i]); + Assert.Equal(list[i].Id, result[i].Id); + Assert.Equal(list[i].Name, result[i].Name); + } + } + + [Fact] + public void Clone_List_ReturnsNewInstanceWithIdenticalValues() + { + // Arrange + var original = new List { "one", "two", "three" }; + + // Act + var result = CloneUtility.Clone(original); + + // Assert + Assert.Equal(original.Count, result.Count); + for (int i = 0; i < original.Count; i++) + { + Assert.Equal(original[i], result[i]); + } + } + } +} diff --git a/src/Reveal.Sdk.Dom/Core/Utilities/CloneUtility.cs b/src/Reveal.Sdk.Dom/Core/Utilities/CloneUtility.cs index b5fef5b5..367da59e 100644 --- a/src/Reveal.Sdk.Dom/Core/Utilities/CloneUtility.cs +++ b/src/Reveal.Sdk.Dom/Core/Utilities/CloneUtility.cs @@ -1,32 +1,35 @@ -using Newtonsoft.Json; -using System.Collections.Generic; -using System.Linq; - -namespace Reveal.Sdk.Dom.Core.Utilities -{ - internal static class CloneUtility - { - internal static T Clone(T item) - { - if (item is null) - return default; - - var deserializeSettings = new JsonSerializerSettings - { - ObjectCreationHandling = ObjectCreationHandling.Replace - }; - var serializeSettings = new JsonSerializerSettings - { - ReferenceLoopHandling = ReferenceLoopHandling.Ignore, - NullValueHandling = NullValueHandling.Ignore - }; - - return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(item, serializeSettings), deserializeSettings); - } - - internal static List Clone(this List list) - { - return list.Select(item => Clone(item)).ToList(); - } - } -} +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Linq; + +namespace Reveal.Sdk.Dom.Core.Utilities +{ + internal static class CloneUtility + { + internal static T Clone(T item) + { + if (item is null) + return default; + + var deserializeSettings = new JsonSerializerSettings + { + ObjectCreationHandling = ObjectCreationHandling.Replace + }; + var serializeSettings = new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + NullValueHandling = NullValueHandling.Ignore + }; + + return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(item, serializeSettings), deserializeSettings); + } + + internal static List Clone(this List list) + { + if (list is null) + return new List(); + + return list.Select(item => Clone(item)).ToList(); + } + } +}