1 module painlessjson.unittesttypes;
2 
3 import painlessjson.annotations;
4 import std.json;
5 import std.algorithm;
6 import std.stdio;
7 import painlessjson.painlessjson;
8 
9 ///
10 struct Point
11 {
12     double x = 0; ///
13     double y = 1; ///
14     this(double x_, double y_)
15     {
16         x = x_;
17         y = y_;
18     }
19 
20     string foo()
21     {
22         writeln("Functions should not be called");
23         return "Noooooo!";
24     }
25 
26     static string bar()
27     {
28         writeln("Static functions should not be called");
29         return "Noooooo!";
30     }
31 
32 }
33 
34 ///
35 class PointC
36 {
37     ///
38     double x = 0;
39     double y = 1;
40     this()
41     {
42     }
43 
44     this(double x_, double y_)
45     {
46         x = x_;
47         y = y_;
48     }
49 
50     string foo()
51     {
52         writeln("Class functions should not be called");
53         return "Noooooo!";
54     }
55 
56 }
57 
58 class PointPrivate
59 {
60     private double _x;
61     private double _y;
62     this(double x_, double y_)
63     {
64         _x = x_;
65         _y = y_;
66     }
67 
68     string foo()
69     {
70         writeln("Class functions should not be called");
71         return "Noooooo!";
72     }
73 
74     const double x()
75     {
76         return _x;
77     }
78 
79     const double y()
80     {
81         return _y;
82     }
83 
84     static PointPrivate _fromJSON(JSONValue value)
85     {
86         return new PointPrivate(fromJSON!double(value["x"]), fromJSON!double(value["y"]));
87     }
88 
89     const JSONValue _toJSON()
90     {
91         JSONValue[string] json;
92         json["x"] = JSONValue(x);
93         json["y"] = JSONValue(y);
94         return JSONValue(json);
95     }
96 
97 }
98 
99 class PointDefaultFromJSON
100 {
101     double _x;
102     private double _y;
103 
104     this()
105     {
106     }
107 
108     this(double x_, double y_)
109     {
110         _x = x_;
111         _y = y_;
112     }
113 
114     const double x()
115     {
116         return _x;
117     }
118 
119     const double y()
120     {
121         return _y;
122     }
123 
124     static PointDefaultFromJSON _fromJSON(JSONValue value)
125     {
126         auto pnt = defaultFromJSON!PointDefaultFromJSON(value);
127         pnt._y = fromJSON!double(value["y"]);
128         return pnt;
129     }
130 
131     const JSONValue _toJSON()
132     {
133         JSONValue[string] json;
134         json = this.defaultToJSON.object;
135         json["y"] = JSONValue(_y);
136         return JSONValue(json);
137     }
138 }
139 
140 struct PointPrivateProperty
141 {
142     private double _x;
143     private double _y;
144     this(double x_, double y_)
145     {
146         _x = x_;
147         _y = y_;
148     }
149 
150     const string foo()
151     {
152         writeln("Class functions should not be called");
153         return "Noooooo!";
154     }
155 
156     const @property double x()
157     {
158         return _x;
159     }
160 
161     @property void x(double x_)
162     {
163         _x = x_;
164     }
165 
166     const @property double y()
167     {
168         return _y;
169     }
170 
171     @property void y(double y_)
172     {
173         _y = y_;
174     }
175 
176     const @property double z()
177     {
178         return 1.0;
179     }
180 
181     const @property void bar(double a, double b)
182     {
183         writeln(
184             "Functions annotated with @property and more than one variable should not be called");
185         assert(0);
186     }
187 
188 }
189 
190 struct PointSerializationName
191 {
192     @SerializedName("xOut", "yOut") double x = 0;
193     @SerializedToName("yOut") @SerializedFromName("xOut") double y = 1;
194     this(double x_, double y_)
195     {
196         x = x_;
197         y = y_;
198     }
199 
200     string foo()
201     {
202         writeln("Functions should not be called");
203         return "Noooooo!";
204     }
205 
206     static string bar()
207     {
208         writeln("Static functions should not be called");
209         return "Noooooo!";
210     }
211 
212 }
213 
214 struct SimpleStruct {
215     string str;
216 }
217 
218 struct StructWithStructAndAA {
219     struct Inner {
220         string str;
221     }
222     string[string] stringToString;
223     Inner[string] stringToInner;
224 }
225 
226 ///
227 struct PointSerializationIgnore
228 {
229     @SerializeIgnore double x = 0; ///
230     @SerializedToName("z") @SerializeFromIgnore double y = 1; ///
231     @SerializeToIgnore double z = 2; ///
232 
233     ///
234     this(double x_, double y_, double z_)
235     {
236         x = x_;
237         y = y_;
238         z = z_;
239     }
240 
241     ///
242     @SerializeIgnore @property double foo()
243     {
244         return 0.1;
245     }
246 
247     ///
248     @SerializeIgnore @property void foo(double a)
249     {
250     }
251 
252 }
253 
254 ///
255 struct PointUseConstructor
256 {
257     @disable this();
258     double x = 0; ///
259     private double _y = 1; ///
260     this(double x, double y)
261     {
262         this.x = x;
263         this._y = y;
264     }
265 
266     string foo()
267     {
268         writeln("Functions should not be called");
269         return "Noooooo!";
270     }
271 
272     static string bar()
273     {
274         writeln("Static functions should not be called");
275         return "Noooooo!";
276     }
277 
278     @property double y()
279     {
280         return _y;
281     }
282 }
283 
284 ///
285 class IdAndName
286 {
287     immutable string name; ///
288     immutable int id; ///
289 
290     this(string name)
291     {
292         this.id = -1;
293         this.name = name;
294     }
295 
296     this(int id, string name)
297     {
298         this.id = id;
299         this.name = name;
300     }
301 
302     this(int id)
303     {
304         this.id = id;
305         this.name = "Undefined";
306     }
307 }
308 
309 struct CamelCaseConversion
310 {
311     int wasCamelCase;
312     int was_underscore;
313 }