-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64a30b4
commit 4203f20
Showing
2 changed files
with
138 additions
and
71 deletions.
There are no files selected for viewing
142 changes: 103 additions & 39 deletions
142
src/Reveal.Sdk.Dom.Tests/Core/Utilities/CloneUtilityFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> { "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<CloneTestClass> 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<CloneTestClass> | ||
{ | ||
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<string> { "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]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>(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<T>(JsonConvert.SerializeObject(item, serializeSettings), deserializeSettings); | ||
} | ||
|
||
internal static List<T> Clone<T>(this List<T> 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>(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<T>(JsonConvert.SerializeObject(item, serializeSettings), deserializeSettings); | ||
} | ||
|
||
internal static List<T> Clone<T>(this List<T> list) | ||
{ | ||
if (list is null) | ||
return new List<T>(); | ||
|
||
return list.Select(item => Clone(item)).ToList(); | ||
} | ||
} | ||
} |