1 module boilerplate;
2 
3 public import boilerplate.accessors;
4 
5 public import boilerplate.autostring;
6 
7 public import boilerplate.conditions;
8 
9 public import boilerplate.constructor;
10 
11 enum GenerateAll = GenerateThis ~ GenerateToString ~ GenerateFieldAccessors ~ GenerateInvariants;
12 
13 @("can use all four generators at once")
14 unittest
15 {
16     import core.exception : AssertError;
17     import std.conv : to;
18     import unit_threaded.should : shouldEqual, shouldThrow;
19 
20     class Class
21     {
22         @ConstRead @Write @NonInit
23         int i_;
24 
25         mixin(GenerateAll);
26     }
27 
28     auto obj = new Class(5);
29 
30     obj.i.shouldEqual(5);
31     obj.to!string.shouldEqual("Class(i=5)");
32     obj.i(0).shouldThrow!AssertError;
33 }
34 
35 // regression test for workaround for https://issues.dlang.org/show_bug.cgi?id=19731
36 @("accessor on field in struct with invariant and constructor")
37 unittest
38 {
39     import core.exception : AssertError;
40     import unit_threaded.should : shouldThrow;
41 
42     struct Struct
43     {
44         @NonNull
45         @ConstRead
46         Object constObject_;
47 
48         @NonNull
49         @Read
50         Object object_;
51 
52         mixin(GenerateAll);
53     }
54 
55     Struct().constObject.shouldThrow!AssertError;
56     Struct().object.shouldThrow!AssertError;
57 }
58 
59 @("field with reserved name")
60 unittest
61 {
62     struct Struct
63     {
64         int version_;
65 
66         mixin(GenerateAll);
67     }
68 
69     with (Struct.Builder())
70     {
71         version_ = 5;
72 
73         assert(value.version_ == 5);
74         assert(value.BuilderFrom().value.version_ == 5);
75     }
76 }
77 
78 @("class with no members")
79 unittest
80 {
81     static class Class
82     {
83         mixin(GenerateAll);
84     }
85 
86     auto instance = Class.Builder().value;
87 }
88 
89 @("underscore property is aliased to this")
90 unittest
91 {
92     struct Foo
93     {
94         @ConstRead
95         int i_;
96 
97         alias i_ this;
98 
99         mixin(GenerateAll);
100     }
101 
102     auto builder = Foo.Builder();
103 
104     cast(void) builder;
105 }