1 module it.cpp.class_; 2 3 import it; 4 5 @("POD struct") 6 @safe unittest { 7 shouldCompile( 8 Cpp( 9 q{ 10 struct Foo { int i; double d; }; 11 } 12 ), 13 D( 14 q{ 15 auto f = Foo(42, 33.3); 16 static assert(is(Foo == struct), "Foo should be a struct"); 17 f.i = 7; 18 f.d = 3.14; 19 } 20 ), 21 ); 22 } 23 24 @("POD struct private then public") 25 @safe unittest { 26 shouldCompile( 27 Cpp( 28 q{ 29 struct Foo { 30 private: 31 int i; 32 public: 33 double d; 34 }; 35 } 36 ), 37 D( 38 q{ 39 auto f = Foo(42, 33.3); 40 static assert(is(Foo == struct), "Foo should be a struct"); 41 static assert(__traits(getProtection, f.i) == "private"); 42 static assert(__traits(getProtection, f.d) == "public"); 43 f.d = 22.2; 44 } 45 ), 46 ); 47 } 48 49 50 @("POD class") 51 @safe unittest { 52 shouldCompile( 53 Cpp( 54 q{ 55 class Foo { int i; double d; }; 56 } 57 ), 58 D( 59 q{ 60 auto f = Foo(42, 33.3); 61 static assert(is(Foo == struct), "Foo should be a struct"); 62 static assert(__traits(getProtection, f.i) == "private"); 63 static assert(__traits(getProtection, f.d) == "private"); 64 } 65 ), 66 ); 67 } 68 69 @("POD class public then private") 70 @safe unittest { 71 shouldCompile( 72 Cpp( 73 q{ 74 class Foo { 75 public: 76 int i; 77 private: 78 double d; 79 }; 80 } 81 ), 82 D( 83 q{ 84 auto f = Foo(42, 33.3); 85 static assert(is(Foo == struct), "Foo should be a struct"); 86 static assert(__traits(getProtection, f.i) == "public"); 87 static assert(__traits(getProtection, f.d) == "private"); 88 f.i = 7; // public, ok 89 } 90 ), 91 ); 92 } 93 94 95 @("struct method") 96 @safe unittest { 97 shouldCompile( 98 Cpp( 99 q{ 100 struct Adder { 101 int add(int i, int j); 102 }; 103 } 104 ), 105 D( 106 q{ 107 static assert(is(Adder == struct), "Adder should be a struct"); 108 auto adder = Adder(); 109 int i = adder.add(2, 3); 110 } 111 ), 112 ); 113 } 114 115 @("ctor") 116 @safe unittest { 117 shouldCompile( 118 Cpp( 119 q{ 120 struct Adder { 121 int i; 122 Adder(int i, int j); 123 int add(int j); 124 }; 125 } 126 ), 127 D( 128 q{ 129 static assert(is(Adder == struct), "Adder should be a struct"); 130 auto adder = Adder(1, 2); 131 int i = adder.add(4); 132 } 133 ), 134 ); 135 } 136 137 @("const method") 138 @safe unittest { 139 shouldCompile( 140 Cpp( 141 q{ 142 struct Adder { 143 int i; 144 Adder(int i, int j); 145 int add(int j) const; 146 }; 147 } 148 ), 149 D( 150 q{ 151 static assert(is(Adder == struct), "Adder should be a struct"); 152 auto adder = const Adder(1, 2); 153 int i = adder.add(4); 154 } 155 ), 156 ); 157 }