Skip to content

Commit 1684b17

Browse files
author
Amirhossein Beheshti
committed
test: add unit tests for SetSharpJsonReader
Add comprehensive test cases for SetSharpJsonReader to verify correct behavior when reading values from JSON dictionaries. Tests cover various scenarios including top-level and nested paths, non-existent keys, null inputs, and invalid paths.
1 parent 1161bc1 commit 1684b17

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using SetSharp.Helpers;
2+
3+
namespace SetSharp.Tests.Helpers
4+
{
5+
public class SetSharpJsonReaderTests
6+
{
7+
private readonly Dictionary<string, object> _testJson = new()
8+
{
9+
{ "TopLevelString", "Hello World" },
10+
{ "TopLevelInt", 123 },
11+
{ "SetSharp", new Dictionary<string, object>
12+
{
13+
{ "Enabled", true },
14+
{ "Generation", new Dictionary<string, object>
15+
{
16+
{ "Poco", true },
17+
{ "OptionsPattern", false }
18+
}
19+
}
20+
}
21+
}
22+
};
23+
24+
[Fact]
25+
public void Read_WithValidTopLevelPath_ReturnsCorrectValue()
26+
{
27+
// Arrange
28+
var keyPath = "TopLevelString";
29+
30+
// Act
31+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
32+
33+
// Assert
34+
Assert.Equal("Hello World", result);
35+
}
36+
37+
[Fact]
38+
public void Read_WithValidNestedPath_ReturnsCorrectValue()
39+
{
40+
// Arrange
41+
var keyPath = "SetSharp:Enabled";
42+
43+
// Act
44+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
45+
46+
// Assert
47+
Assert.IsType<bool>(result);
48+
Assert.Equal(true, result);
49+
}
50+
51+
[Fact]
52+
public void Read_WithDeeplyNestedPath_ReturnsCorrectValue()
53+
{
54+
// Arrange
55+
var keyPath = "SetSharp:Generation:Poco";
56+
57+
// Act
58+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
59+
60+
// Assert
61+
Assert.IsType<bool>(result);
62+
Assert.Equal(true, result);
63+
}
64+
65+
[Fact]
66+
public void Read_PathToADictionary_ReturnsDictionaryObject()
67+
{
68+
// Arrange
69+
var keyPath = "SetSharp:Generation";
70+
71+
// Act
72+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
73+
74+
// Assert
75+
var dictResult = Assert.IsType<Dictionary<string, object>>(result);
76+
Assert.True((bool)dictResult["Poco"]);
77+
Assert.False((bool)dictResult["OptionsPattern"]);
78+
}
79+
80+
[Fact]
81+
public void Read_WithNonExistentTopLevelKey_ReturnsNull()
82+
{
83+
// Arrange
84+
var keyPath = "NonExistent";
85+
86+
// Act
87+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
88+
89+
// Assert
90+
Assert.Null(result);
91+
}
92+
93+
[Fact]
94+
public void Read_WithNonExistentNestedKey_ReturnsNull()
95+
{
96+
// Arrange
97+
var keyPath = "SetSharp:Generation:NonExistent";
98+
99+
// Act
100+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
101+
102+
// Assert
103+
Assert.Null(result);
104+
}
105+
106+
[Fact]
107+
public void Read_PathGoesThroughLeafValue_ReturnsNull()
108+
{
109+
// Arrange
110+
// Trying to navigate deeper into "TopLevelString", which is not a dictionary
111+
var keyPath = "TopLevelString:Deeper";
112+
113+
// Act
114+
var result = SetSharpJsonReader.Read(_testJson, keyPath);
115+
116+
// Assert
117+
Assert.Null(result);
118+
}
119+
120+
[Fact]
121+
public void Read_WithNullJson_ReturnsNull()
122+
{
123+
// Arrange
124+
Dictionary<string, object> nullJson = null;
125+
var keyPath = "Any:Path";
126+
127+
// Act
128+
var result = SetSharpJsonReader.Read(nullJson, keyPath);
129+
130+
// Assert
131+
Assert.Null(result);
132+
}
133+
134+
[Theory]
135+
[InlineData(null)]
136+
[InlineData("")]
137+
[InlineData(" ")]
138+
public void Read_WithInvalidKeyPath_ReturnsNull(string invalidKeyPath)
139+
{
140+
// Act
141+
var result = SetSharpJsonReader.Read(_testJson, invalidKeyPath);
142+
143+
// Assert
144+
Assert.Null(result);
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)