1 module cogito.tests.functions; 2 3 import cogito; 4 import std.algorithm; 5 import std.sumtype; 6 7 @("2 functions") 8 unittest 9 { 10 auto meter = runOnCode(q{ 11 void f() 12 { 13 } 14 void g() 15 { 16 } 17 }); 18 19 assert(meter.tryMatch!((Source source) => count(source.inner[])) == 2); 20 } 21 22 @("class function") 23 unittest 24 { 25 auto meter = runOnCode(q{ 26 class C 27 { 28 void f() 29 { 30 if (true) 31 { 32 } 33 } 34 } 35 }); 36 37 assert(meter.tryMatch!((Source source) => source.score) == 1); 38 } 39 40 @("struct function") 41 unittest 42 { 43 auto meter = runOnCode(q{ 44 struct C 45 { 46 void f() 47 { 48 if (true) 49 { 50 } 51 } 52 } 53 }); 54 55 assert(meter.tryMatch!((Source source) => source.score) == 1); 56 } 57 58 @("class function") 59 unittest 60 { 61 auto meter = runOnCode(q{ 62 class C 63 { 64 void f() 65 { 66 if (true) 67 { 68 } 69 } 70 } 71 }); 72 73 assert(meter.tryMatch!((Source source) => source.score) == 1); 74 } 75 76 @("Interface function") 77 unittest 78 { 79 auto meter = runOnCode(q{ 80 interface C 81 { 82 void f(); 83 } 84 }); 85 86 assert(meter.tryMatch!((Source source) => source.inner[].front.name) == "C"); 87 } 88 89 @("Union") 90 unittest 91 { 92 auto meter = runOnCode(q{ 93 union U 94 { 95 } 96 }); 97 98 assert(meter.tryMatch!((Source source) => source.inner[].front.name) == "U"); 99 } 100 101 @("Constructor") 102 unittest 103 { 104 auto meter = runOnCode(q{ 105 class C 106 { 107 this() 108 { 109 if (true) 110 { 111 } 112 } 113 } 114 }); 115 116 assert(meter.tryMatch!((Source source) => source.score) == 1); 117 } 118 119 @("Class destructor") 120 unittest 121 { 122 auto meter = runOnCode(q{ 123 class C 124 { 125 ~this() 126 { 127 if (true) 128 { 129 } 130 } 131 } 132 }); 133 134 assert(meter.tryMatch!((Source source) => source.score) == 1); 135 } 136 137 @("Struct destructor") 138 unittest 139 { 140 auto meter = runOnCode(q{ 141 struct C 142 { 143 ~this() 144 { 145 if (true) 146 { 147 } 148 } 149 } 150 }); 151 152 assert(meter.tryMatch!((Source source) => source.score) == 1); 153 } 154 155 @("Postblit constructor") 156 unittest 157 { 158 auto meter = runOnCode(q{ 159 struct C 160 { 161 this(this) 162 { 163 if (true) 164 { 165 } 166 } 167 } 168 }); 169 170 assert(meter.tryMatch!((Source source) => source.score) == 1); 171 } 172 173 @("Module static constructor") 174 unittest 175 { 176 auto meter = runOnCode(q{ 177 shared static this() 178 { 179 if (true) 180 { 181 } 182 } 183 }); 184 185 assert(meter.tryMatch!((Source source) => source.score) == 1); 186 } 187 188 @("Module static destructor") 189 unittest 190 { 191 auto meter = runOnCode(q{ 192 shared static ~this() 193 { 194 if (true) 195 { 196 } 197 } 198 }); 199 200 assert(meter.tryMatch!((Source source) => source.score) == 1); 201 } 202 203 @("Function literal") 204 unittest 205 { 206 auto meter = runOnCode(q{ 207 int[] f() { 208 return [1, 2].map(i => i % 2 ? i : i); 209 } 210 }); 211 212 assert(meter.tryMatch!((Source source) => source.score) == 2); 213 } 214 215 @("Function literal template parameter") 216 unittest 217 { 218 auto meter = runOnCode(q{ 219 int[] f() { 220 return [1, 2].map!(i => i % 2 ? i : i); 221 } 222 }); 223 224 assert(meter.tryMatch!((Source source) => source.score) == 2); 225 } 226 227 @("Nested function") 228 unittest 229 { 230 auto meter = runOnCode(q{ 231 void f() { 232 int g(int i) { 233 return i % 2 ? i : i; 234 } 235 } 236 }); 237 238 assert(meter.tryMatch!((Source source) => source.score) == 2); 239 } 240 241 @("else-if in aggregate function") 242 unittest 243 { 244 auto meter = runOnCode(q{ 245 struct S 246 { 247 @property const(char)[] name() 248 { 249 if (stringName.empty) 250 { 251 return "(λ)"; 252 } 253 else if (stringName == "__ctor") 254 { 255 return "this"; 256 } 257 else if (stringName == "__dtor") 258 { 259 return "~this"; 260 } 261 else 262 { 263 return stringName; 264 } 265 } 266 } 267 }); 268 269 assert(meter.tryMatch!((Source source) => source.score) == 4); 270 }