1 
2 module witchcraft.unittests.base;
3 
4 import witchcraft;
5 
6 version(unittest)
7 {
8     import std.algorithm;
9     import std.array;
10 
11     struct Entity
12     {
13     }
14 
15     struct Column
16     {
17         string name;
18     }
19 
20     @Entity
21     class User
22     {
23         mixin Witchcraft;
24 
25         @Column
26         string username;
27 
28         @Column("pass_word")
29         string password;
30 
31         string email;
32 
33         ulong createdAt;
34         ulong updatedAt;
35 
36         this()
37         {
38         }
39 
40         this(string email)
41         {
42             this.email = email;
43         }
44 
45         void updateEmail(string email)
46         {
47             this.email = email;
48             updatedAt += 1;
49         }
50     }
51     
52     void testSuite(C)(C metaObject)
53     {
54         assert(metaObject.isAggregate == true);
55         assert(metaObject.isClass     == true);
56         assert(metaObject.isStruct    == false);
57 
58         /+ - Classes - +/
59 
60         assert(metaObject.getName     == "User");
61         assert(metaObject.getFullName == "witchcraft.unittests.base.User");
62         assert(metaObject.isAbstract  == false);
63         assert(metaObject.isFinal     == false);
64 
65         /+ - Class Attributes - +/
66 
67         assert(metaObject.getAttributes.empty        == false);
68         assert(metaObject.getAttributes!Entity.empty == false);
69         assert(metaObject.getAttributes!Column.empty == true);
70 
71         assert(metaObject.getAttributes!Entity[0].isType       == true);
72         assert(metaObject.getAttributes!Entity[0].isExpression == false);
73 
74         /+ - Constructors - +/
75 
76         assert(metaObject.getConstructors.length == 2);
77 
78         assert(metaObject.getConstructor !is null);
79         assert(metaObject.getConstructor!string !is null);
80 
81         User user = metaObject.getConstructor!string.create!User("test@email.com");
82 
83         assert(user !is null);
84         assert(user.email == "test@email.com");
85         assert(user.updatedAt == 0);
86 
87         /+ - Fields - +/
88 
89         assert(metaObject.getFieldNames.isPermutation([
90             "username", "password", "email", "createdAt", "updatedAt"
91         ]));
92 
93         assert(metaObject.getField("username") !is null);
94         auto username = metaObject.getField("username");
95 
96         /+ - Field Attributes - +/
97 
98         assert(username.getAttributes.empty        == false);
99         assert(username.getAttributes!Entity.empty == true);
100         assert(username.getAttributes!Column.empty == false);
101 
102         assert(username.getAttributes!Column[0].isType       == true);
103         assert(username.getAttributes!Column[0].isExpression == false);
104 
105         assert(metaObject.getField("password") !is null);
106         auto password = metaObject.getField("password");
107 
108         /+ - Field Attributes (Expression) - +/
109 
110         assert(password.getAttributes.empty        == false);
111         assert(password.getAttributes!Entity.empty == true);
112         assert(password.getAttributes!Column.empty == false);
113 
114         assert(password.getAttributes!Column[0].isType       == false);
115         assert(password.getAttributes!Column[0].isExpression == true);
116 
117         assert(password.getAttributes!Column[0].get!Column.name == "pass_word");
118 
119         /+ - Methods - +/
120 
121         assert(metaObject.getLocalMethodNames.isPermutation([ "getMetaType", "updateEmail" ]));
122 
123         assert(metaObject.getMethods("updateEmail").empty == false);
124         assert(metaObject.getMethod!(string)("updateEmail") !is null);
125         auto updateEmail = metaObject.getMethod!(string)("updateEmail");
126 
127         updateEmail.invoke(user, "user@email.com");
128         assert(user.email == "user@email.com");
129         assert(user.updatedAt == 1);
130     }
131 }
132 
133 unittest
134 {
135     assert(User.metaof !is null);
136     assert(getMeta!User() !is null);
137     auto metaObjectByProperty = User.metaof;
138     auto metaObjectByFunction = getMeta!User();
139     
140     // dirty hack to compare meta object...
141     assert(metaObjectByProperty.getFullName() == metaObjectByFunction.getFullName());
142     
143     // ...less dirty way to ensure the same behaviour
144     testSuite(metaObjectByProperty);
145     testSuite(metaObjectByFunction);
146 }