TestMeta.hx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package unit;
  2. @enumMeta private enum E {
  3. @a A;
  4. @b(0) B;
  5. }
  6. @classMeta("x") class TestMeta extends Test {
  7. @empty() @_int(-45) @complex([{ x : 0, y : "hello", z : -1.48, b : true, k : null }]) static var foo : Int;
  8. @new public function new() {
  9. super();
  10. }
  11. function fields( o : Dynamic ) {
  12. if( o == null ) return null;
  13. var fl = Reflect.fields(o);
  14. fl.sort(Reflect.compare);
  15. return fl.join("#");
  16. }
  17. public function testMeta() {
  18. var m = haxe.rtti.Meta.getType(E);
  19. eq( fields(m), "enumMeta" );
  20. eq( m.enumMeta, null );
  21. var m = haxe.rtti.Meta.getType(TestMeta);
  22. eq( fields(m), "classMeta" );
  23. eq( Std.string(m.classMeta), "[x]" );
  24. var m = haxe.rtti.Meta.getFields(E);
  25. eq( fields(m), "A#B" );
  26. eq( fields(m.A), "a" );
  27. eq( m.A.a, null );
  28. eq( fields(m.B), "b" );
  29. eq( Std.string(m.B.b), "[0]" );
  30. var m = haxe.rtti.Meta.getFields(TestMeta);
  31. eq( fields(m), "_" );
  32. eq( fields(m._), #if as3 "_"+#end "new" );
  33. var m = haxe.rtti.Meta.getStatics(TestMeta);
  34. eq( fields(m), "foo" );
  35. eq( fields(m.foo), "_int#complex#empty" );
  36. eq( m.foo.empty, null );
  37. eq( Std.string(m.foo._int), "[-45]" );
  38. var c : Dynamic = m.foo.complex[0][0];
  39. eq( fields(c), "b#k#x#y#z" );
  40. eq( c.x, 0 );
  41. eq( c.y, "hello" );
  42. eq( c.z, -1.48 );
  43. eq( c.b, true );
  44. eq( c.k, null );
  45. }
  46. public function testExprMeta() {
  47. eq(getMeta(@foo a).name, "foo");
  48. eq(getMeta(@foo("a") b).name, "foo");
  49. eq(getMeta(@foo ("a")).name, "foo");
  50. var m = getMeta(@bar("1", "foo") null);
  51. eq(m.name, "bar");
  52. eq(m.args[0], "1");
  53. eq(m.args[1], "foo");
  54. eq(getMeta(@foo ("1")).args.length, 0);
  55. eq(getMeta(@foo("1") "2").args.length, 1);
  56. }
  57. static macro function getMeta(e) {
  58. switch(e.expr) {
  59. case EMeta(m, _):
  60. return macro { name: $v{m.name}, args: $a{m.params} };
  61. default:
  62. return macro report("Metadata expected");
  63. }
  64. }
  65. }