Skip to content

Commit 9b8c698

Browse files
committed
Unit tests for SharpForge.Core
There's very little logic here. This is good.
1 parent fb06209 commit 9b8c698

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using NUnit.Framework;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using SharpForge.Core.Nodes;
3+
4+
namespace SharpForge.Core.UnitTests.Nodes;
5+
6+
[TestFixture]
7+
public class NodeTests
8+
{
9+
[Test]
10+
[TestCase(0, false)]
11+
[TestCase(1, true)]
12+
[TestCase(17, true)]
13+
public void ShouldSerializeContents_ReturnsTrue_IfThereAreAnyContents(int numContents, bool expected)
14+
{
15+
// Arrange
16+
var node = new Node();
17+
for (var i = 0; i < numContents; i++)
18+
{
19+
node.Contents.Add(new Node());
20+
}
21+
22+
// Act/Assert
23+
Assert.That(node.ShouldSerializeContents(), Is.EqualTo(expected));
24+
}
25+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using SharpForge.Core.Nodes;
2+
using SharpForge.Core.Persistence;
3+
4+
namespace SharpForge.Core.UnitTests.Persistence;
5+
6+
[TestFixture]
7+
public class NodeSerializerTests
8+
{
9+
[Test]
10+
public void Serialize_ThrowsIfRootIsNull()
11+
{
12+
// Act/Assert
13+
Assert.Throws<ArgumentNullException>(() => new NodeSerializer().Serialize(null));
14+
}
15+
16+
[Test]
17+
public void Serialize_ReturnsPrettyJson()
18+
{
19+
// Arrange
20+
var node = new Node();
21+
node.Contents.Add(new Sprite() {
22+
IsCentered = true,
23+
ImageFile = "logo.png",
24+
Position = new System.Numerics.Vector2(13, 31),
25+
});
26+
27+
// Act
28+
var actual = new NodeSerializer().Serialize(node);
29+
30+
// Assert
31+
Assert.That(actual.Contains(" {"));
32+
}
33+
34+
[Test]
35+
public void Deserialize_Throws_IfTextIsNull()
36+
{
37+
// Act/Assert
38+
Assert.Throws<ArgumentNullException>(() => new NodeSerializer().Deserialize<Node>(null));
39+
}
40+
41+
[Test]
42+
[TestCase("")]
43+
[TestCase(" ")]
44+
public void Deserialize_Throws_IfTextIsWhitespace(string input)
45+
{
46+
// Act/Assert
47+
Assert.Throws<ArgumentException>(() => new NodeSerializer().Deserialize<Node>(input));
48+
}
49+
50+
[Test]
51+
public void Deserialize_ReturnsNonNullValue()
52+
{
53+
// Arrange
54+
var expected = new Sprite()
55+
{
56+
ImageFile = "hi.png",
57+
IsCentered = false,
58+
Position = new System.Numerics.Vector2(123, 321),
59+
};
60+
61+
var json = new NodeSerializer().Serialize(expected);
62+
63+
// Act
64+
var actual = new NodeSerializer().Deserialize<Sprite>(json);
65+
66+
// Assert
67+
Assert.That(actual, Is.InstanceOf<Sprite>());
68+
Assert.That(actual.ImageFile, Is.EqualTo(expected.ImageFile));
69+
Assert.That(actual.IsCentered, Is.EqualTo(expected.IsCentered));
70+
Assert.That(actual.Position, Is.EqualTo(expected.Position));
71+
}
72+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14+
<PackageReference Include="NUnit" Version="3.13.3" />
15+
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
16+
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
17+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\SharpForge.Core\SharpForge.Core.csproj" />
22+
</ItemGroup>
23+
24+
</Project>

source/SharpForge.Core/Nodes/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SharpForge.Core.Nodes;
99
public class Node
1010
{
1111
public Vector2 Position { get; set; } = Vector2.Zero;
12-
public readonly IList<Node> Contents = new List<Node>();
12+
public IList<Node> Contents { get; } = new List<Node>();
1313

1414
// Some JSON.NET magic; don't serialize "Contents" unless it has any entries.
1515
// This, coupled with the serializer's "IgnoreAndPopulate," means empty lists don't get serialized; but on

source/SharpForge.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpForge.BackEnd.MonoGame
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpForge.Framework.UnitTests", "SharpForge.Framework.UnitTests\SharpForge.Framework.UnitTests.csproj", "{A0330725-EFAD-4ADC-A782-27F7D0A0BD4F}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpForge.Core.UnitTests", "SharpForge.Core.UnitTests\SharpForge.Core.UnitTests.csproj", "{753F594F-8554-4FB7-AB38-31AA70217108}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -48,5 +50,9 @@ Global
4850
{A0330725-EFAD-4ADC-A782-27F7D0A0BD4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
4951
{A0330725-EFAD-4ADC-A782-27F7D0A0BD4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
5052
{A0330725-EFAD-4ADC-A782-27F7D0A0BD4F}.Release|Any CPU.Build.0 = Release|Any CPU
53+
{753F594F-8554-4FB7-AB38-31AA70217108}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54+
{753F594F-8554-4FB7-AB38-31AA70217108}.Debug|Any CPU.Build.0 = Debug|Any CPU
55+
{753F594F-8554-4FB7-AB38-31AA70217108}.Release|Any CPU.ActiveCfg = Release|Any CPU
56+
{753F594F-8554-4FB7-AB38-31AA70217108}.Release|Any CPU.Build.0 = Release|Any CPU
5157
EndGlobalSection
5258
EndGlobal

0 commit comments

Comments
 (0)