1 /* REQUIRED_ARGS: -unittest 2 */ 3 module e7804; 4 5 struct Bar {static struct B{}} 6 alias BarB = __traits(getMember, Bar, "B"); 7 static assert(is(BarB == Bar.B)); 8 static assert(is(const(__traits(getMember, Bar, "B")) == const(Bar.B))); 9 10 alias BarBParent = __traits(parent, BarB); 11 static assert(is(BarBParent == Bar)); 12 13 struct Foo {alias MyInt = int;} 14 alias FooInt = __traits(getMember, Foo, "MyInt"); 15 static immutable FooInt fi = 42; 16 static assert(fi == 42); 17 void declVsStatementSupport() 18 { 19 __traits(getMember, Foo, "MyInt") i1 = 1; 20 const(__traits(getMember, Foo, "MyInt")) i2 = 1; 21 assert(i1 == i2); 22 __traits(getMember, Foo, "MyInt") i3 = __traits(getMember, Foo, "MyInt").max; 23 assert(i3 == int.max); 24 } 25 26 27 enum __traits(getMember, Foo, "MyInt") a0 = 12; 28 static assert(is(typeof(a0) == int)); 29 static assert(a0 == 12); 30 31 32 const __traits(getMember, Foo, "MyInt") a1 = 46; 33 34 35 __traits(getMember, Foo, "MyInt") a2 = 78; 36 37 38 const(__traits(getMember, Foo, "MyInt")) a3 = 63; 39 40 41 struct WithSym {static int foo; static int bar(){return 42;}} 42 alias m1 = __traits(getMember, WithSym, "foo"); 43 alias m2 = WithSym.foo; 44 static assert(__traits(isSame, m1, m2)); 45 alias f1 = __traits(getMember, WithSym, "bar"); 46 alias f2 = WithSym.bar; 47 static assert(__traits(isSame, f1, f2)); 48 49 50 auto ovld(const(char)[] s){return s;} 51 auto ovld(int i){return i;} 52 alias ovlds = __traits(getOverloads, e7804, "ovld"); 53 54 55 struct TmpPrm(T) 56 if (is(T == int)){T t;} 57 TmpPrm!(__traits(getMember, Foo, "MyInt")) tpt = TmpPrm!(__traits(getMember, Foo, "MyInt"))(42); 58 59 60 @Foo @(1) class Class 61 { 62 final void virtual(){} 63 int virtual(int p){return p;} 64 void test(this T)() 65 { 66 alias vf = __traits(getVirtualFunctions, Class, "virtual"); 67 assert(vf.length == 2); 68 alias vm = __traits(getVirtualMethods, Class, "virtual"); 69 assert(vm.length == 1); 70 assert(vm[0](42) == 42); 71 alias attribs = __traits(getAttributes, Class); 72 assert(attribs.length == 2); 73 assert(is(typeof(attribs[0]()) == Foo)); 74 assert(attribs[1] == 1); 75 76 alias objectAll = __traits(allMembers, Object); 77 alias classDerived = __traits(derivedMembers, Class); 78 alias classAll = __traits(allMembers, Class); 79 enum Seq(T...) = T; 80 static assert (classAll == Seq!(classDerived, objectAll)); 81 } 82 } 83 84 85 struct UnitTests 86 { 87 static int count; 88 unittest { count++; } 89 unittest {++++count;} 90 static void test() 91 { 92 alias tests = __traits(getUnitTests, UnitTests); 93 static assert(tests.length == 2); 94 foreach(t; tests) t(); 95 assert(count == 6); // not 3 because executed automatically (DRT) then manually 96 } 97 } 98 99 100 class One 101 { 102 void foo(){} 103 void foo(int){} 104 } 105 106 class Two : One 107 { 108 void test() 109 { 110 alias Seq(T...) = T; 111 alias p1 = Seq!(__traits(getMember, super, "foo"))[0]; 112 alias p2 = __traits(getMember, super, "foo"); 113 static assert(__traits(isSame, p1, p2)); 114 } 115 } 116 117 118 class SingleSymTuple 119 { 120 int foo(){return 42;} 121 void test() 122 { 123 alias f = __traits(getMember, this, "foo"); 124 assert(f() == 42); 125 } 126 } 127 128 129 struct WithAliasThis 130 { 131 auto getter(){return 42;} 132 alias getter this; 133 void test() 134 { 135 alias getterCall = __traits(getAliasThis, typeof(this)); 136 assert(mixin(getterCall[0]) == 42); 137 } 138 } 139 140 void main() 141 { 142 declVsStatementSupport(); 143 assert(a1 == 46); 144 assert(a2 == 78); 145 assert(a3 == 63); 146 assert(f1() == f2()); 147 Foo.MyInt fmi = cast(__traits(getMember, Foo, "MyInt")) 0; 148 auto c = __traits(getMember, Foo, "MyInt").max; 149 assert(c == int.max); 150 assert(ovlds[0]("farfelu") == "farfelu"); 151 assert(ovlds[1](42) == 42); 152 (new Class).test(); 153 UnitTests.test(); 154 (new WithAliasThis).test(); 155 (new Two).test(); 156 (new SingleSymTuple).test(); 157 } 158 159 /* https://issues.dlang.org/show_bug.cgi?id=19708 */ 160 struct Foo19708 {} 161 struct Bar19708 {} 162 template Baz19708(T) { struct Baz19708{T t;} } 163 int symbol19708; 164 165 @Foo19708 @Bar19708 @Baz19708 @symbol19708 int bar19708; 166 167 alias TR19708 = __traits(getAttributes, bar19708); 168 alias TRT = __traits(getAttributes, bar19708)[2]; 169 170 TR19708[0] a119708; 171 TR19708[1] a219708; 172 alias A3 = TRT!int; 173 174 alias C19708 = TR19708[0]; 175 alias D19708 = TR19708[1]; 176 C19708 c1; 177 D19708 d1; 178 179 static assert(__traits(isSame, TR19708[3], symbol19708)); 180