Skip to content

Commit ae71dfb

Browse files
committed
test: add StepCatalog data model unit tests
1 parent c8d71b3 commit ae71dfb

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System.Text.Json;
2+
using SharpFM.Scripting.Catalog;
3+
using Xunit;
4+
5+
namespace SharpFM.Tests.Scripting;
6+
7+
public class StepCatalogTests
8+
{
9+
// --- BlockPairRole serialization (converter is on StepBlockPair.Role) ---
10+
11+
[Theory]
12+
[InlineData("{\"role\":\"open\",\"partners\":[]}", BlockPairRole.Open)]
13+
[InlineData("{\"role\":\"middle\",\"partners\":[]}", BlockPairRole.Middle)]
14+
[InlineData("{\"role\":\"close\",\"partners\":[]}", BlockPairRole.Close)]
15+
public void BlockPairRole_Deserializes(string json, BlockPairRole expected)
16+
{
17+
var pair = JsonSerializer.Deserialize<StepBlockPair>(json);
18+
Assert.Equal(expected, pair!.Role);
19+
}
20+
21+
[Theory]
22+
[InlineData(BlockPairRole.Open, "open")]
23+
[InlineData(BlockPairRole.Middle, "middle")]
24+
[InlineData(BlockPairRole.Close, "close")]
25+
public void BlockPairRole_Serializes(BlockPairRole value, string expected)
26+
{
27+
var pair = new StepBlockPair { Role = value, Partners = [] };
28+
var json = JsonSerializer.Serialize(pair);
29+
Assert.Contains($"\"role\":\"{expected}\"", json);
30+
}
31+
32+
[Fact]
33+
public void BlockPairRole_Deserialize_Unknown_Throws()
34+
{
35+
Assert.Throws<JsonException>(() =>
36+
JsonSerializer.Deserialize<StepBlockPair>("{\"role\":\"unknown\",\"partners\":[]}"));
37+
}
38+
39+
// --- StepDefinition record ---
40+
41+
[Fact]
42+
public void StepDefinition_Deserializes_FromJson()
43+
{
44+
var json = """
45+
{
46+
"name": "Set Variable",
47+
"id": 141,
48+
"category": "Control",
49+
"selfClosing": true,
50+
"params": [
51+
{ "xmlElement": "Name", "type": "text" }
52+
]
53+
}
54+
""";
55+
56+
var def = JsonSerializer.Deserialize<StepDefinition>(json);
57+
Assert.NotNull(def);
58+
Assert.Equal("Set Variable", def!.Name);
59+
Assert.Equal(141, def.Id);
60+
Assert.Equal("Control", def.Category);
61+
Assert.True(def.SelfClosing);
62+
Assert.Single(def.Params);
63+
Assert.Equal("Name", def.Params[0].XmlElement);
64+
}
65+
66+
[Fact]
67+
public void StepDefinition_Defaults_AreCorrect()
68+
{
69+
var def = new StepDefinition();
70+
Assert.Equal("", def.Name);
71+
Assert.Null(def.Id);
72+
Assert.Equal("", def.Category);
73+
Assert.False(def.SelfClosing);
74+
Assert.Empty(def.Params);
75+
Assert.Null(def.BlockPair);
76+
}
77+
78+
// --- StepParam record ---
79+
80+
[Fact]
81+
public void StepParam_Deserializes_WithOptionalFields()
82+
{
83+
var json = """
84+
{
85+
"xmlElement": "Calculation",
86+
"type": "namedCalc",
87+
"hrLabel": "Value",
88+
"wrapperElement": "Value",
89+
"required": true,
90+
"invertedHr": true
91+
}
92+
""";
93+
94+
var param = JsonSerializer.Deserialize<StepParam>(json);
95+
Assert.NotNull(param);
96+
Assert.Equal("Calculation", param!.XmlElement);
97+
Assert.Equal("namedCalc", param.Type);
98+
Assert.Equal("Value", param.HrLabel);
99+
Assert.Equal("Value", param.WrapperElement);
100+
Assert.True(param.Required);
101+
Assert.True(param.InvertedHr);
102+
}
103+
104+
[Fact]
105+
public void StepParam_Defaults_AreCorrect()
106+
{
107+
var param = new StepParam();
108+
Assert.Equal("", param.XmlElement);
109+
Assert.Equal("", param.Type);
110+
Assert.Null(param.HrLabel);
111+
Assert.Null(param.XmlAttr);
112+
Assert.Null(param.WrapperElement);
113+
Assert.Null(param.ParentElement);
114+
Assert.False(param.Required);
115+
Assert.False(param.InvertedHr);
116+
}
117+
118+
// --- StepBlockPair record ---
119+
120+
[Fact]
121+
public void StepBlockPair_Deserializes()
122+
{
123+
var json = """{"role": "open", "partners": ["Else", "Else If", "End If"]}""";
124+
125+
var pair = JsonSerializer.Deserialize<StepBlockPair>(json);
126+
Assert.NotNull(pair);
127+
Assert.Equal(BlockPairRole.Open, pair!.Role);
128+
Assert.Equal(3, pair.Partners.Length);
129+
Assert.Contains("Else", pair.Partners);
130+
Assert.Contains("End If", pair.Partners);
131+
}
132+
133+
// --- StepParam with hrEnumValues ---
134+
135+
[Fact]
136+
public void StepParam_HrEnumValues_Deserializes()
137+
{
138+
var json = """
139+
{
140+
"xmlElement": "RowPageLocation",
141+
"type": "enum",
142+
"hrEnumValues": { "1": "First", "2": "Last", "3": "Previous", "4": "Next" }
143+
}
144+
""";
145+
146+
var param = JsonSerializer.Deserialize<StepParam>(json);
147+
Assert.NotNull(param!.HrEnumValues);
148+
Assert.Equal("First", param.HrEnumValues!["1"]);
149+
Assert.Equal(4, param.HrEnumValues.Count);
150+
}
151+
}

0 commit comments

Comments
 (0)