1 2 module witchcraft.unittests.noMixin; 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 @Column 24 string username; 25 26 @Column("pass_word") 27 string password; 28 29 string email; 30 31 ulong createdAt; 32 ulong updatedAt; 33 34 this() 35 { 36 } 37 38 this(string email) 39 { 40 this.email = email; 41 } 42 43 void updateEmail(string email) 44 { 45 this.email = email; 46 updatedAt += 1; 47 } 48 } 49 50 void testSuite(C)(C metaObject) 51 { 52 assert(metaObject.isAggregate == true); 53 assert(metaObject.isClass == true); 54 assert(metaObject.isStruct == false); 55 56 /+ - Classes - +/ 57 58 assert(metaObject.getName == "User"); 59 assert(metaObject.getFullName == "witchcraft.unittests.noMixin.User"); 60 assert(metaObject.isAbstract == false); 61 assert(metaObject.isFinal == false); 62 63 /+ - Class Attributes - +/ 64 65 assert(metaObject.getAttributes.empty == false); 66 assert(metaObject.getAttributes!Entity.empty == false); 67 assert(metaObject.getAttributes!Column.empty == true); 68 69 assert(metaObject.getAttributes!Entity[0].isType == true); 70 assert(metaObject.getAttributes!Entity[0].isExpression == false); 71 72 /+ - Constructors - +/ 73 74 assert(metaObject.getConstructors.length == 2); 75 76 assert(metaObject.getConstructor !is null); 77 assert(metaObject.getConstructor!string !is null); 78 79 User user = metaObject.getConstructor!string.create!User("test@email.com"); 80 81 assert(user !is null); 82 assert(user.email == "test@email.com"); 83 assert(user.updatedAt == 0); 84 85 /+ - Fields - +/ 86 87 assert(metaObject.getFieldNames.isPermutation([ 88 "username", "password", "email", "createdAt", "updatedAt" 89 ])); 90 91 assert(metaObject.getField("username") !is null); 92 auto username = metaObject.getField("username"); 93 94 /+ - Field Attributes - +/ 95 96 assert(username.getAttributes.empty == false); 97 assert(username.getAttributes!Entity.empty == true); 98 assert(username.getAttributes!Column.empty == false); 99 100 assert(username.getAttributes!Column[0].isType == true); 101 assert(username.getAttributes!Column[0].isExpression == false); 102 103 assert(metaObject.getField("password") !is null); 104 auto password = metaObject.getField("password"); 105 106 /+ - Field Attributes (Expression) - +/ 107 108 assert(password.getAttributes.empty == false); 109 assert(password.getAttributes!Entity.empty == true); 110 assert(password.getAttributes!Column.empty == false); 111 112 assert(password.getAttributes!Column[0].isType == false); 113 assert(password.getAttributes!Column[0].isExpression == true); 114 115 assert(password.getAttributes!Column[0].get!Column.name == "pass_word"); 116 117 /+ - Methods - +/ 118 119 assert(metaObject.getLocalMethodNames == [ "updateEmail" ]); 120 121 assert(metaObject.getMethods("updateEmail").empty == false); 122 assert(metaObject.getMethod!(string)("updateEmail") !is null); 123 auto updateEmail = metaObject.getMethod!(string)("updateEmail"); 124 125 updateEmail.invoke(user, "user@email.com"); 126 assert(user.email == "user@email.com"); 127 assert(user.updatedAt == 1); 128 } 129 } 130 131 unittest 132 { 133 auto metaObject = getMeta!User(); 134 assert(metaObject !is null); 135 136 testSuite(metaObject); 137 }