UnitBuilder.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package unit;
  23. import haxe.macro.Context;
  24. import haxe.macro.Expr;
  25. import haxe.macro.Type;
  26. using StringTools;
  27. class UnitBuilder {
  28. static public macro function build(basePath:String):Array<Field> {
  29. var ret = Context.getBuildFields();
  30. var numFiles = 0;
  31. function readDir(path) {
  32. var dir = sys.FileSystem.readDirectory(path);
  33. path = path.endsWith("\\") || path.endsWith("/") ? path : path + "/";
  34. for (file in dir) {
  35. var filePath = path + file;
  36. if (file.endsWith(".unit.hx")) {
  37. numFiles++;
  38. var func = {
  39. args: [],
  40. ret: null,
  41. params: [],
  42. expr: read(filePath)
  43. }
  44. ret.push( {
  45. name: "test" + ~/\./g.map(file, function(_) return "_"),
  46. kind: FFun(func),
  47. pos: Context.makePosition( { min:0, max:0, file:filePath + file } ),
  48. access: [APublic],
  49. doc: null,
  50. meta: []
  51. });
  52. } else if (sys.FileSystem.isDirectory(filePath)) {
  53. readDir(filePath);
  54. }
  55. }
  56. }
  57. readDir(basePath);
  58. //trace("Added " +numFiles + " .unit.hx files");
  59. return ret;
  60. }
  61. #if macro
  62. static function collapseToOrExpr(el:Array<Expr>) {
  63. return switch(el) {
  64. case []: throw "";
  65. case [e]: e;
  66. case _:
  67. var e = el.pop();
  68. { expr: EBinop(OpBoolOr, e, collapseToOrExpr(el)), pos: e.pos }
  69. }
  70. }
  71. static function mkEq(e1, e2, p) {
  72. function isFloat(e) {
  73. try return switch(Context.typeof(e)) {
  74. case TAbstract(tr, _):
  75. tr.get().name == "Float";
  76. case _:
  77. false;
  78. } catch (e:Dynamic) {
  79. return false;
  80. }
  81. }
  82. var e = switch [isFloat(e1) || isFloat(e2), e2.expr] {
  83. // hell yeah
  84. case [true, EField( { expr:EConst(CIdent("Math")) }, "POSITIVE_INFINITY" | "NEGATIVE_INFINITY")] if (Context.defined("cpp") || Context.defined("php")):
  85. macro t($e1 == $e2);
  86. case [true, _]:
  87. macro feq($e1, $e2);
  88. case _:
  89. macro eq($e1, $e2);
  90. }
  91. return {
  92. expr: e.expr,
  93. pos: p
  94. }
  95. }
  96. static public function read(path:String) {
  97. var p = Context.makePosition( { min:0, max:0, file:path } );
  98. var file = sys.io.File.getContent(path);
  99. var code = Context.parseInlineString("{" + file + "}", p);
  100. var block = switch(code.expr) {
  101. case EBlock(b): b;
  102. case _: throw "false";
  103. }
  104. var ret = [];
  105. for (e in block) {
  106. var e = switch(e.expr) {
  107. case EBinop(OpEq, e1, { expr: EConst(CIdent("false")) } )
  108. | EBinop(OpEq, { expr: EConst(CIdent("false")) }, e1):
  109. {
  110. expr: (macro f($e1)).expr,
  111. pos: e.pos
  112. }
  113. case EBinop(OpEq, e1, { expr: EConst(CIdent("true")) } )
  114. | EBinop(OpEq, { expr: EConst(CIdent("true")) }, e1):
  115. {
  116. expr: (macro t($e1)).expr,
  117. pos: e.pos
  118. }
  119. case EBinop(OpEq, e1, { expr: EArrayDecl(el) } )
  120. | EBinop(OpEq, { expr: EArrayDecl(el) }, e1 ):
  121. var el2 = [];
  122. for (i in 0...el.length) {
  123. var e2 = el[i];
  124. el2.push(mkEq((macro $e1[$v{i}]), e2, e.pos));
  125. }
  126. if (el2.length == 0)
  127. mkEq((macro $e1.length), (macro 0), e.pos);
  128. else
  129. macro { $a{el2}; };
  130. case EBinop(OpEq, e1, e2):
  131. mkEq(e1, e2, e.pos);
  132. case EBinop(OpGt | OpGte | OpLt | OpLte, _, _):
  133. {
  134. expr: (macro t($e)).expr,
  135. pos: e.pos
  136. }
  137. case EThrow(e):
  138. macro exc(function() $e);
  139. case EIn(e1, {expr:EArrayDecl(el) }):
  140. var el2 = [];
  141. for (e in el)
  142. el2.push(macro $e1 == $e);
  143. macro @:pos(e.pos) t(${ collapseToOrExpr(el2) } );
  144. case EVars(vl):
  145. for (v in vl)
  146. if (v.name == "t" || v.name == "f" || v.name == "eq" || v.name == "neq")
  147. Context.error('${v.name} is reserved for unit testing', e.pos);
  148. e;
  149. case _:
  150. e;
  151. }
  152. ret.push(e);
  153. }
  154. return macro { $a{ret}; };
  155. }
  156. #end
  157. }