OLD | NEW |
(Empty) | |
| 1 import 'dart:mirrors' as m; |
| 2 import 'package:checked_mirrors/checked_mirrors.dart'; |
| 3 import 'package:checked_mirrors/control.dart'; |
| 4 |
| 5 import 'package:logging/logging.dart'; |
| 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/compact_vm_config.dart'; |
| 8 |
| 9 @MirrorsUsed(symbols: 'y', targets: const [B], metaTargets: const[Reflected]) |
| 10 const checked_mirrors_workaround_for_issue_10360 = 0; |
| 11 |
| 12 class Reflected { const Reflected(); } |
| 13 const reflected = const Reflected(); |
| 14 |
| 15 class A { |
| 16 int x = 1; |
| 17 int y = 2; // declared symbol 'y', but target is not declared either |
| 18 } |
| 19 |
| 20 class B { |
| 21 int x = 3; // covered by class B |
| 22 } |
| 23 |
| 24 @Reflected() // all symbols in this class are declared |
| 25 class C { |
| 26 int x = 4; |
| 27 int z = 5; |
| 28 } |
| 29 |
| 30 class D { |
| 31 @reflected int x = 6; |
| 32 int z = 7; // not declared |
| 33 } |
| 34 |
| 35 var a = new A(); |
| 36 var b = new B(); |
| 37 var c = new C(); |
| 38 var d = new D(); |
| 39 |
| 40 main() { |
| 41 hierarchicalLoggingEnabled = true; |
| 42 useCompactVMConfiguration(); |
| 43 |
| 44 var logger = new Logger('checked_mirrors'); |
| 45 logger.level = Level.ALL; |
| 46 final records = []; |
| 47 var firstError = null; |
| 48 var reportedErrors = 0; |
| 49 logger.onRecord.listen((r) { |
| 50 reportedErrors++; |
| 51 if (firstError == null) { |
| 52 firstError = r; |
| 53 return; |
| 54 } |
| 55 records.add(r); |
| 56 }); |
| 57 initialize(throwOnWarning: true, log: true, hints: 'my-hint-goes-here'); |
| 58 |
| 59 tearDown(() { |
| 60 try { |
| 61 // Ensure that each test accounts for all reported warnings. |
| 62 expect(records, hasLength(0)); |
| 63 if (reportedErrors > 0) { |
| 64 expect(firstError.message, '${FIRST_ERROR_MESSAGE}my-hint-goes-here'); |
| 65 } |
| 66 } finally { |
| 67 records.clear(); |
| 68 } |
| 69 }); |
| 70 |
| 71 group('get name,', () { |
| 72 test('default mirrors', () { |
| 73 expect(m.MirrorSystem.getName(#x), 'x'); |
| 74 expect(m.MirrorSystem.getSymbol('x'), #x); |
| 75 expect(m.MirrorSystem.getName(#y), 'y'); |
| 76 expect(m.MirrorSystem.getSymbol('y'), #y); |
| 77 }); |
| 78 |
| 79 test('symbol is declared', () { |
| 80 expect(MirrorSystem.getName(#y), 'y'); |
| 81 expect(MirrorSystem.getSymbol('y'), #y); |
| 82 }); |
| 83 |
| 84 test('symbol is not declared', () { |
| 85 expect(() => MirrorSystem.getName(#x), throws); |
| 86 expect(records.length, 1); // woo hoo! this was detected |
| 87 var r = records.removeLast(); |
| 88 expect(r.message, |
| 89 'Symbol "x" was used, but it is missing a @MirrorsUsed annotation.'); |
| 90 expect(() => MirrorSystem.getSymbol('x'), throws); |
| 91 expect(records.length, 1); |
| 92 r = records.removeLast(); |
| 93 expect(r.message, |
| 94 'Symbol "x" was used, but it is missing a @MirrorsUsed annotation.'); |
| 95 }); |
| 96 |
| 97 }); |
| 98 group('get field,', () { |
| 99 test('default mirrors', () { |
| 100 expect(m.reflect(a).getField(#y).reflectee, 2); |
| 101 expect(m.reflect(b).getField(#x).reflectee, 3); |
| 102 |
| 103 expect(m.reflect(c).getField(#x).reflectee, 4); |
| 104 expect(m.reflect(c).getField(#z).reflectee, 5); |
| 105 |
| 106 expect(m.reflect(d).getField(#x).reflectee, 6); |
| 107 expect(m.reflect(d).getField(#z).reflectee, 7); |
| 108 }); |
| 109 |
| 110 test('symbol is not declared', () { |
| 111 expect(() => reflect(a).getField(#x), throws); |
| 112 expect(records.length, 1); |
| 113 var r = records.removeLast(); |
| 114 expect(r.message, 'Tried to access "A.x" via mirrors, ' |
| 115 'but it is missing a @MirrorsUsed annotation.'); |
| 116 }); |
| 117 |
| 118 test('even if symbol is declared', () { |
| 119 expect(() => reflect(a).getField(#y), throws); |
| 120 expect(records.length, 1); |
| 121 var r = records.removeLast(); |
| 122 expect(r.message, 'Tried to access "A.y" via mirrors, ' |
| 123 'but it is missing a @MirrorsUsed annotation.'); |
| 124 }); |
| 125 |
| 126 test('target is declared', () { |
| 127 expect(reflect(b).getField(#x).reflectee, 3); |
| 128 }); |
| 129 |
| 130 test('meta target is declared on class', () { |
| 131 expect(reflect(c).getField(#x).reflectee, 4); |
| 132 expect(reflect(c).getField(#z).reflectee, 5); |
| 133 }); |
| 134 |
| 135 test('meta target is declared on field', () { |
| 136 expect(reflect(d).getField(#x).reflectee, 6); |
| 137 expect(() => reflect(d).getField(#z), throws); |
| 138 expect(records, hasLength(1)); |
| 139 var r = records.removeLast(); |
| 140 expect(r.message, 'Tried to access "D.z" via mirrors, ' |
| 141 'but it is missing a @MirrorsUsed annotation.'); |
| 142 }); |
| 143 }); |
| 144 |
| 145 group('set field,', () { |
| 146 test('default mirrors', () { |
| 147 m.reflect(a).setField(#x, 101); |
| 148 m.reflect(a).setField(#y, 102); |
| 149 m.reflect(b).setField(#x, 103); |
| 150 m.reflect(c).setField(#x, 104); |
| 151 m.reflect(c).setField(#z, 105); |
| 152 m.reflect(d).setField(#x, 106); |
| 153 m.reflect(d).setField(#z, 107); |
| 154 |
| 155 expect(a.x, 101); |
| 156 expect(a.y, 102); |
| 157 expect(b.x, 103); |
| 158 expect(c.x, 104); |
| 159 expect(c.z, 105); |
| 160 expect(d.x, 106); |
| 161 expect(d.z, 107); |
| 162 }); |
| 163 |
| 164 test('symbol not declared', () { |
| 165 expect(() => reflect(a).setField(#x, 201), throws); |
| 166 expect(records.length, 1); |
| 167 var r = records.removeLast(); |
| 168 expect(r.message, 'Tried to access "A.x" via mirrors, ' |
| 169 'but it is missing a @MirrorsUsed annotation.'); |
| 170 }); |
| 171 |
| 172 test('even if symbol is declared', () { |
| 173 expect(() => reflect(a).setField(#y, 202), throws); |
| 174 expect(records.length, 1); |
| 175 var r = records.removeLast(); |
| 176 expect(r.message, 'Tried to access "A.y" via mirrors, ' |
| 177 'but it is missing a @MirrorsUsed annotation.'); |
| 178 }); |
| 179 |
| 180 test('target is declared', () { |
| 181 reflect(b).setField(#x, 203); |
| 182 expect(b.x, 203); |
| 183 }); |
| 184 |
| 185 test('meta target is declared on class', () { |
| 186 reflect(c).setField(#x, 204); |
| 187 reflect(c).setField(#z, 205); |
| 188 expect(c.x, 204); |
| 189 expect(c.z, 205); |
| 190 }); |
| 191 |
| 192 test('meta target is declared on field', () { |
| 193 reflect(d).setField(#x, 206); |
| 194 expect(() => reflect(d).setField(#z, 207), throws); |
| 195 expect(d.x, 206); |
| 196 expect(records, hasLength(1)); |
| 197 var r = records.removeLast(); |
| 198 expect(r.message, 'Tried to access "D.z" via mirrors, ' |
| 199 'but it is missing a @MirrorsUsed annotation.'); |
| 200 }); |
| 201 }); |
| 202 } |
OLD | NEW |