Skip to content

Commit

Permalink
added unit tests for clone utility
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed May 13, 2024
1 parent 64a30b4 commit 4203f20
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 71 deletions.
142 changes: 103 additions & 39 deletions src/Reveal.Sdk.Dom.Tests/Core/Utilities/CloneUtilityFixture.cs
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]);
}
}
}
}
67 changes: 35 additions & 32 deletions src/Reveal.Sdk.Dom/Core/Utilities/CloneUtility.cs
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();
}
}
}

0 comments on commit 4203f20

Please sign in to comment.