describeAggregate

Describes classes, structs, unions and interfaces

describeAggregate
(
T
bool withUnitTests = false
)
()
if (
isAggregateType!T
)

Examples

It should describe a class with interfaces and base classes

1 interface I1 { }
2 interface I2 { }
3 
4 class C1 { }
5 class C2 : C1, I1 { }
6 class C3 : C2, I2 { }
7 
8 auto result = describeAggregate!C3;
9 
10 result.name.should.equal("C3");
11 
12 result.baseClasses.length.should.equal(3);
13 result.baseClasses[0].name.should.equal("C2");
14 result.baseClasses[1].name.should.equal("C1");
15 result.baseClasses[2].name.should.equal("Object");
16 
17 result.interfaces.length.should.equal(2);
18 result.interfaces[0].name.should.equal("I1");
19 result.interfaces[1].name.should.equal("I2");
20 
21 result.location.file.should.equal("source/introspection/aggregate.d");
22 result.location.line.should.be.greaterThan(0);
23 result.location.column.should.equal(3);
24 
25 result.protection = Protection.public_;

It should describe struct attributes

1 int attr(int) { return 0; }
2 
3 @("attribute1") @attr(1)
4 struct Test { }
5 
6 auto result = describeAggregate!Test;
7 
8 result.name.should.equal("Test");
9 result.attributes.length.should.equal(2);
10 
11 result.attributes[0].name.should.equal(`"attribute1"`);
12 result.attributes[0].type.name.should.equal(`string`);
13 
14 result.attributes[1].name.should.equal("0");
15 result.attributes[1].type.name.should.equal(`int`);

It should find properties from a struct

1 struct Test { int a; string b; }
2 
3 auto result = describeAggregate!Test;
4 
5 result.properties.length.should.equal(2);
6 
7 result.properties[0].name.should.equal("a");
8 result.properties[0].isStatic.should.equal(false);
9 result.properties[0].protection.should.equal(Protection.public_);
10 result.properties[0].type.name.should.equal("int");
11 
12 result.properties[1].name.should.equal("b");
13 result.properties[0].isStatic.should.equal(false);
14 result.properties[1].protection.should.equal(Protection.public_);
15 result.properties[1].type.name.should.equal("string");

It should find a static property

1 struct Test {
2   static int a;
3   string b;
4 }
5 
6 auto result = describeAggregate!Test;
7 
8 result.properties[0].name.should.equal("a");
9 result.properties[0].isStatic.should.equal(true);
10 result.properties[1].isStatic.should.equal(false);

It should find property protection levels

1 class Test {
2   public int a;
3   protected int b;
4   private int c;
5   export int d;
6 }
7 
8 auto result = describeAggregate!Test;
9 
10 result.properties.length.should.equal(4);
11 
12 result.properties[0].name.should.equal("a");
13 result.properties[0].protection.should.equal(Protection.public_);
14 
15 result.properties[1].name.should.equal("b");
16 result.properties[1].protection.should.equal(Protection.protected_);
17 
18 result.properties[2].name.should.equal("c");
19 result.properties[2].protection.should.equal(Protection.private_);
20 
21 result.properties[3].name.should.equal("d");
22 result.properties[3].protection.should.equal(Protection.export_);

It should describe struct property attributes

1 int attr(int) { return 0; }
2 
3 struct Test {
4   @("attribute1") @attr(1)
5   string name;
6 }
7 
8 auto result = describeAggregate!Test;
9 
10 result.name.should.equal("Test");
11 
12 result.properties[0].attributes.length.should.equal(2);
13 result.properties[0].attributes[0].name.should.equal(`"attribute1"`);
14 result.properties[0].attributes[0].type.name.should.equal(`string`);

It should find public methods from a struct

1 struct Test {
2   void a() {}
3   void b() {}
4 }
5 
6 auto result = describeAggregate!Test;
7 
8 result.methods.length.should.equal(2);
9 
10 result.methods[0].name.should.equal("a");
11 result.methods[0].isStatic.should.equal(false);
12 result.methods[0].protection.should.equal(Protection.public_);
13 result.methods[0].type.name.should.equal("void()");
14 
15 result.methods[1].name.should.equal("b");
16 result.methods[1].isStatic.should.equal(false);
17 result.methods[1].protection.should.equal(Protection.public_);
18 result.methods[1].type.name.should.equal("void()");

It should find all methods from a class

1 class Test {
2   public void a() {}
3   protected void b() {}
4   private void c() {}
5 }
6 
7 auto result = describeAggregate!Test;
8 
9 result.methods.length.should.equal(8);
10 
11 result.methods.map!(a => a.name).array.should.equal(["a", "b", "c", "toString", "toHash", "opCmp", "opEquals", "factory"]);
12 
13 result.methods[0].name.should.equal("a");
14 result.methods[0].isStatic.should.equal(false);
15 result.methods[0].protection.should.equal(Protection.public_);
16 result.methods[0].type.name.should.equal("void()");
17 
18 result.methods[1].name.should.equal("b");
19 result.methods[1].isStatic.should.equal(false);
20 result.methods[1].protection.should.equal(Protection.protected_);
21 result.methods[1].type.name.should.equal("void()");
22 
23 result.methods[2].name.should.equal("c");
24 result.methods[2].isStatic.should.equal(false);
25 result.methods[2].protection.should.equal(Protection.private_);
26 result.methods[2].type.name.should.equal("void()");

It should find overloaded methods

1 class Test {
2   void a(int) {}
3   void a(string) {}
4 }
5 
6 auto result = describeAggregate!Test;
7 
8 result.methods.length.should.equal(7);
9 
10 result.methods.map!(a => a.name).array.should.equal(["a", "a", "toString", "toHash", "opCmp", "opEquals", "factory"]);
11 
12 result.methods[0].name.should.equal("a");
13 result.methods[0].isStatic.should.equal(false);
14 result.methods[0].protection.should.equal(Protection.public_);
15 result.methods[0].type.name.should.equal("void(int)");
16 result.methods[0].parameters[0].name.should.equal("_param_0");
17 result.methods[0].parameters[0].type.name.should.equal("int");
18 
19 result.methods[1].name.should.equal("a");
20 result.methods[1].isStatic.should.equal(false);
21 result.methods[1].protection.should.equal(Protection.public_);
22 result.methods[1].type.name.should.equal("void(string)");
23 result.methods[1].parameters[0].name.should.equal("_param_0");
24 result.methods[1].parameters[0].type.name.should.equal("string");

It should describe struct method attributes

1 int attr(int) { return 0; }
2 
3 struct Test {
4   @("attribute1") @attr(1)
5   string name();
6 }
7 
8 auto result = describeAggregate!Test;
9 
10 result.name.should.equal("Test");
11 result.methods[0].attributes.length.should.equal(2);
12 result.methods[0].attributes[0].name.should.equal(`"attribute1"`);
13 result.methods[0].attributes[0].type.name.should.equal(`string`);

It should describe static struct method

1 struct Test {
2   static string name();
3   string name(int);
4 }
5 
6 auto result = describeAggregate!Test;
7 
8 result.name.should.equal("Test");
9 result.methods[0].name.should.equal("name");
10 result.methods[0].isStatic.should.equal(true);
11 result.methods[0].parameters.length.should.equal(0);
12 
13 result.methods[1].name.should.equal("name");
14 result.methods[1].isStatic.should.equal(false);
15 result.methods[1].parameters.length.should.equal(1);

It should describe enums defined in classes

1 class Test {
2   enum Other : int {
3     a, b, c, d
4   }
5 }
6 
7 auto result = describeAggregate!Test;
8 
9 result.enums.length.should.equal(1);
10 result.enums[0].name.should.equal("Other");

It should describe a manifest constant

1 class Test {
2   enum constant = 4;
3 }
4 
5 auto result = describeAggregate!Test;
6 
7 result.manifestConstants.length.should.equal(1);
8 result.manifestConstants[0].name.should.equal("constant");

It should describe a struct type defined inside a class

1 class Test {
2   struct Other {}
3 }
4 
5 auto result = describeAggregate!Test;
6 
7 result.nested.length.should.equal(1);
8 result.nested[0].name.should.equal("Other");

It should describe a template defined inside a class

1 class Test {
2   struct Other(T) {}
3 }
4 
5 auto result = describeAggregate!Test;
6 
7 result.nested.length.should.equal(0);
8 
9 result.templates.length.should.equal(1);
10 result.templates[0].name.should.equal("Other");

It should describe an instantiated template

1 class Test(T) {
2   T bar(T val) { return val; }
3 }
4 
5 alias TestInstantiated = Test!string;
6 
7 auto result = describeAggregate!TestInstantiated;
8 
9 result.name.should.equal("Test!string");
10 result.nested.length.should.equal(0);
11 result.templates.length.should.equal(0);
12 
13 result.methods.length.should.equal(6);
14 result.methods[0].name.should.equal("bar");
15 result.methods[0].returns.name.should.equal("string");

It should describe unittests defined in a struct

struct Test { unittest { } }

auto result = describeAggregate!(Test, true);
result.unitTests.length.should.equal(1);

It should describe attributes that return structs

1 struct Mapper { }
2 Mapper mapper() {
3   return Mapper();
4 }
5 
6 class MockClass {
7   @mapper @Mapper @("mapper")
8   void mapper() @trusted nothrow { }
9 }
10 
11 enum result = describeAggregate!(MockClass, true);
12 result.methods[0].attributes.length.should.equal(3);
13 result.methods[0].attributes[0].name.should.equal("mapper");
14 result.methods[0].attributes[0].type.name.should.equal("nothrow @trusted void()");
15 result.methods[0].attributes[1].name.should.equal("Mapper");
16 result.methods[0].attributes[2].name.should.equal(`"mapper"`);

Meta