1 module yamlserialized.unittests;
2 
3 unittest {
4     import yamlserialized : YamlField, toYAMLNode, deserializeInto;
5     import dunit.toolkit : assertEqual;
6 
7     struct Test {
8         @YamlField("some_field")
9         string someField;
10     }
11 
12     Test ts = Test("hello world");
13 
14     auto node = ts.toYAMLNode();
15 
16     assertEqual(node["some_field"], "hello world");
17 
18     Test ts1;
19 
20     node.deserializeInto(ts1);
21 
22     assertEqual(ts1.someField, "hello world");
23 }
24 
25 unittest {
26     import std.conv : to;
27     import dunit.toolkit : assertEqual;
28     import dyaml : Loader;
29 
30     import yamlserialized : deserializeInto, deserializeTo, toYAMLNode;
31 
32     struct TestSubStruct {
33         int anotherInt;
34         string anotherString;
35     }
36 
37     struct TestStruct {
38         struct NestedStruct {
39             int nestedInt;
40             string nestedString;
41         }
42 
43         int singleInt;
44         float singleFloat;
45         int[] intArray;
46         int[][] arrayOfIntArrays;
47         int[string] intStringAssocArray;
48         int[int] intIntAssocArray;
49         char singleChar;
50         char[] charArray;
51         string singleString;
52         string[] stringArray;
53         string[][] arrayOfStringArrays;
54         string[string] stringAssocArray;
55         string[string][string] stringAssocArrayOfAssocArrays;
56         bool trueBool;
57         bool falseBool;
58 
59         TestSubStruct subStruct;
60         NestedStruct nestedStruct;
61         NestedStruct[] arrayOfNestedStructs;
62         NestedStruct[string] nestedStructAssocArray;
63     }
64 
65     // Create test struct and set it up with some test values
66     TestStruct ts;
67     with (ts) {
68         singleInt = 1234;
69         singleFloat = 1.234;
70         intArray = [1, 2, 3, 4];
71         arrayOfIntArrays = [[1, 2], [3, 4]];
72         intStringAssocArray = ["one": 1, "two": 2, "three": 3];
73         intIntAssocArray = [1: 3, 2: 1, 3: 2];
74         singleChar = 'A';
75         charArray = ['A', 'B', 'C', 'D'];
76         singleString = "just a string";
77         stringArray = ["a", "few", "strings"];
78         arrayOfStringArrays = [["a", "b"], ["c", "d"]];
79         stringAssocArray = ["a": "A", "b": "B", "c": "C"];
80         stringAssocArrayOfAssocArrays = ["a": ["a": "A", "b": "B"], "b": ["c": "C", "d": "D"]];
81         trueBool = true;
82         falseBool = false;
83         subStruct.anotherInt = 42;
84         subStruct.anotherString = "Another string";
85         nestedStruct.nestedInt = 53;
86         nestedStruct.nestedString = "Nested string";
87         arrayOfNestedStructs = [NestedStruct(1, "One"), NestedStruct(2, "Two")];
88         nestedStructAssocArray = ["one": NestedStruct(1, "One"), "two": NestedStruct(2, "Two")];
89     }
90 
91     // Serialize the struct to YAML
92     auto node = ts.toYAMLNode();
93 
94     // Create a new empty struct
95     TestStruct ts2;
96 
97     // Deserialize the JSONValue into it
98     node.deserializeInto(ts2);
99 
100     // Assert that both structs are identical
101     assertEqual(ts2.singleInt, ts.singleInt);
102     assertEqual(ts2.singleFloat, ts.singleFloat);
103     assertEqual(ts2.intArray, ts.intArray);
104     assertEqual(ts2.arrayOfIntArrays, ts.arrayOfIntArrays);
105     assertEqual(ts2.intStringAssocArray, ts.intStringAssocArray);
106     assertEqual(ts2.intIntAssocArray, ts.intIntAssocArray);
107     assertEqual(ts2.singleChar, ts.singleChar);
108     assertEqual(ts2.charArray, ts.charArray);
109     assertEqual(ts2.singleString, ts.singleString);
110     assertEqual(ts2.stringArray, ts.stringArray);
111     assertEqual(ts2.arrayOfStringArrays, ts.arrayOfStringArrays);
112     assertEqual(ts2.stringAssocArray, ts.stringAssocArray);
113     assertEqual(ts2.stringAssocArrayOfAssocArrays, ts.stringAssocArrayOfAssocArrays);
114     assertEqual(ts2.trueBool, ts.trueBool);
115     assertEqual(ts2.falseBool, ts.falseBool);
116     assertEqual(ts2.subStruct, ts.subStruct);
117     assertEqual(ts2.subStruct.anotherInt, ts.subStruct.anotherInt);
118     assertEqual(ts2.subStruct.anotherString, ts.subStruct.anotherString);
119     assertEqual(ts2.nestedStruct, ts.nestedStruct);
120     assertEqual(ts2.nestedStruct.nestedInt, ts.nestedStruct.nestedInt);
121     assertEqual(ts2.nestedStruct.nestedString, ts.nestedStruct.nestedString);
122     assertEqual(ts2.arrayOfNestedStructs, ts.arrayOfNestedStructs);
123     assertEqual(ts2.arrayOfNestedStructs[0].nestedInt, ts.arrayOfNestedStructs[0].nestedInt);
124     assertEqual(ts2.arrayOfNestedStructs[0].nestedString, ts.arrayOfNestedStructs[0].nestedString);
125     assertEqual(ts2.arrayOfNestedStructs[1].nestedInt, ts.arrayOfNestedStructs[1].nestedInt);
126     assertEqual(ts2.arrayOfNestedStructs[1].nestedString, ts.arrayOfNestedStructs[1].nestedString);
127     assertEqual(ts2.nestedStructAssocArray, ts.nestedStructAssocArray);
128     assertEqual(ts2.nestedStructAssocArray["one"].nestedInt, ts.nestedStructAssocArray["one"].nestedInt);
129     assertEqual(ts2.nestedStructAssocArray["two"].nestedString, ts.nestedStructAssocArray["two"].nestedString);
130 
131     // Attempt to deserialize partial YAML
132     TestStruct ts3;
133     Loader.fromString(`{ singleInt: 42, singleString: "Don't panic." }`.to!(char[])).load().deserializeInto(ts3);
134 
135     ts3.singleInt.assertEqual(42);
136     ts3.singleString.assertEqual("Don't panic.");
137 
138     // Attempt to deserialize YAML containing a property that does not exist in the struct
139     TestStruct ts4;
140     Loader.fromString(`{ nonexistentString: "Move along, nothing to see here." }`.to!(char[])).load().deserializeInto(ts4);
141 
142     auto ts5 = Loader.fromString(`{ singleInt: 42, singleString: "Don't panic." }`.to!(char[])).load().deserializeTo!TestStruct;
143     ts5.singleInt.assertEqual(42);
144     ts5.singleString.assertEqual("Don't panic.");
145 }
146 
147 
148 unittest {
149     import dunit.toolkit : assertEqual;
150     import dyaml : Loader;
151 
152     import yamlserialized : deserializeInto, toYAMLNode;
153 
154     class TestSubClass {
155         int anotherInt;
156         float anotherFloat;
157     }
158 
159     class TestClass {
160         static class NestedClass {
161             int nestedInt;
162             float nestedFloat;
163 
164             pure this() {
165             }
166 
167             this(in int initInt, in float initFloat) {
168                 nestedInt = initInt;
169                 nestedFloat = initFloat;
170             }
171         }
172 
173         int singleInt;
174         float singleFloat;
175         int[] intArray;
176         int[][] arrayOfIntArrays;
177         int[string] intStringAssocArray;
178         int[int] intIntAssocArray;
179         char singleChar;
180         char[] charArray;
181         string singleString;
182         string[] stringArray;
183         string[][] arrayOfStringArrays;
184         string[string] stringAssocArray;
185         string[string][string] stringAssocArrayOfAssocArrays;
186         NestedClass[] arrayOfNestedClasses;
187         NestedClass[string] nestedClassAssocArray;
188 
189         auto subClass = new TestSubClass();
190         auto nestedClass = new NestedClass(53, 5.3);
191     }
192 
193     // Create test struct and set it up with some test values
194     auto tc = new TestClass();
195     with (tc) {
196         singleInt = 1234;
197         singleFloat = 1.234;
198         intArray = [1, 2, 3, 4];
199         arrayOfIntArrays = [[1, 2], [3, 4]];
200         intStringAssocArray = ["one": 1, "two": 2, "three": 3];
201         intIntAssocArray = [1: 3, 2: 1, 3: 2];
202         singleChar = 'A';
203         charArray = ['A', 'B', 'C', 'D'];
204         singleString = "just a string";
205         stringArray = ["a", "few", "strings"];
206         arrayOfStringArrays = [["a", "b"], ["c", "d"]];
207         stringAssocArray = ["a": "A", "b": "B", "c": "C"];
208         stringAssocArrayOfAssocArrays = ["a": ["a": "A", "b": "B"], "b": ["c": "C", "d": "D"]];
209         arrayOfNestedClasses = [new NestedClass(1, 1.2), new NestedClass(2, 2.3)];
210         nestedClassAssocArray = ["one": new NestedClass(1, 1.2), "two": new NestedClass(2, 2.3)];
211         subClass.anotherInt = 42;
212         subClass.anotherFloat = 4.2;
213     }
214 
215     // Serialize the struct to a Node
216     auto node = tc.toYAMLNode();
217 
218     // Create a new empty struct
219     auto tc2 = new TestClass();
220 
221     // Deserialize the node into it
222     node.deserializeInto(tc2);
223 
224     // Assert that both structs are identical
225     assertEqual(tc2.singleInt, tc.singleInt);
226     assertEqual(tc2.singleFloat, tc.singleFloat);
227     assertEqual(tc2.intArray, tc.intArray);
228     assertEqual(tc2.arrayOfIntArrays, tc.arrayOfIntArrays);
229     assertEqual(tc2.intStringAssocArray, tc.intStringAssocArray);
230     assertEqual(tc2.intIntAssocArray, tc.intIntAssocArray);
231     assertEqual(tc2.singleChar, tc.singleChar);
232     assertEqual(tc2.charArray, tc.charArray);
233     assertEqual(tc2.singleString, tc.singleString);
234     assertEqual(tc2.stringArray, tc.stringArray);
235     assertEqual(tc2.arrayOfStringArrays, tc.arrayOfStringArrays);
236     assertEqual(tc2.stringAssocArray, tc.stringAssocArray);
237     assertEqual(tc2.stringAssocArrayOfAssocArrays, tc.stringAssocArrayOfAssocArrays);
238     assertEqual(tc2.subClass.anotherInt, tc.subClass.anotherInt);
239     assertEqual(tc2.subClass.anotherFloat, tc.subClass.anotherFloat);
240     assertEqual(tc2.nestedClass.nestedInt, tc.nestedClass.nestedInt);
241     assertEqual(tc2.nestedClass.nestedFloat, tc.nestedClass.nestedFloat);
242     assertEqual(tc2.arrayOfNestedClasses[0].nestedInt, tc.arrayOfNestedClasses[0].nestedInt);
243     assertEqual(tc2.arrayOfNestedClasses[0].nestedFloat, tc.arrayOfNestedClasses[0].nestedFloat);
244     assertEqual(tc2.arrayOfNestedClasses[1].nestedInt, tc.arrayOfNestedClasses[1].nestedInt);
245     assertEqual(tc2.arrayOfNestedClasses[1].nestedFloat, tc.arrayOfNestedClasses[1].nestedFloat);
246     assertEqual(tc2.nestedClassAssocArray["one"].nestedInt, tc.nestedClassAssocArray["one"].nestedInt);
247     assertEqual(tc2.nestedClassAssocArray["one"].nestedFloat, tc.nestedClassAssocArray["one"].nestedFloat);
248     assertEqual(tc2.nestedClassAssocArray["two"].nestedInt, tc.nestedClassAssocArray["two"].nestedInt);
249     assertEqual(tc2.nestedClassAssocArray["two"].nestedFloat, tc.nestedClassAssocArray["two"].nestedFloat);
250 }
251 
252 unittest {
253     import std.conv : to;
254     import dunit.toolkit;
255     import dyaml;
256 
257     import yamlserialized : deserializeInto;
258 
259     string[string] aa;
260     auto node = Loader.fromString(`{ aString: theString, anInt: 42 }`.to!(char[])).load();
261 
262     node.deserializeInto(aa);
263 
264     assertEqual(aa["aString"], "theString");
265     assertEqual(aa["anInt"], "42");
266 }