UnitBuilder.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, filter:String = ".unit.hx"):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(filter)) {
  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.follow(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. function mkBlock(e:Expr) {
  101. return switch(e.expr) {
  102. case EBlock(b): b;
  103. case _: [e];
  104. }
  105. }
  106. function bl(block:Array<Expr>):Array<Expr> {
  107. var ret = [];
  108. for (e in block) {
  109. var e = switch(e.expr) {
  110. case EBinop(OpEq, e1, { expr: EConst(CIdent("false")) } )
  111. | EBinop(OpEq, { expr: EConst(CIdent("false")) }, e1):
  112. {
  113. expr: (macro f($e1)).expr,
  114. pos: e.pos
  115. }
  116. case EBinop(OpEq, e1, { expr: EConst(CIdent("true")) } )
  117. | EBinop(OpEq, { expr: EConst(CIdent("true")) }, e1):
  118. {
  119. expr: (macro t($e1)).expr,
  120. pos: e.pos
  121. }
  122. case EBinop(OpEq, e1, { expr: EArrayDecl(el) } )
  123. | EBinop(OpEq, { expr: EArrayDecl(el) }, e1 ):
  124. var el2 = [];
  125. for (i in 0...el.length) {
  126. var e2 = el[i];
  127. el2.push(mkEq((macro $e1[$v{i}]), e2, e.pos));
  128. }
  129. if (el2.length == 0)
  130. mkEq((macro $e1.length), (macro 0), e.pos);
  131. else
  132. macro { $a{el2}; };
  133. case EBinop(OpEq, e1, e2):
  134. mkEq(e1, e2, e.pos);
  135. case EBinop(OpGt | OpGte | OpLt | OpLte, _, _):
  136. {
  137. expr: (macro t($e)).expr,
  138. pos: e.pos
  139. }
  140. case EThrow(e):
  141. macro exc(function() $e);
  142. case EIn(e1, {expr:EArrayDecl(el) }):
  143. var el2 = [];
  144. for (e in el)
  145. el2.push(macro $e1 == $e);
  146. macro @:pos(e.pos) t(${ collapseToOrExpr(el2) } );
  147. case EVars(vl):
  148. for (v in vl)
  149. if (v.name == "t" || v.name == "f" || v.name == "eq" || v.name == "neq")
  150. Context.error('${v.name} is reserved for unit testing', e.pos);
  151. e;
  152. case EFor(it, {expr: EBlock(el), pos: p}):
  153. { expr: EFor(it, {expr:EBlock(bl(el)), pos: p}), pos: e.pos };
  154. case _:
  155. e;
  156. }
  157. ret.push(e);
  158. }
  159. return ret;
  160. }
  161. var block = mkBlock(code);
  162. return macro $b{bl(block)};
  163. }
  164. #end
  165. }