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