Test.hx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import haxe.Json;
  2. import validation.ValidationReport;
  3. import haxe.io.Path;
  4. import sys.io.Process;
  5. import validation.Target;
  6. using sys.FileSystem;
  7. using StringTools;
  8. using Test;
  9. using Lambda;
  10. class Test {
  11. static public function main() {
  12. installDependencies();
  13. var success = true;
  14. for(target in [Js, Php]) {
  15. Sys.println('[Testing ${target.getName()}]');
  16. collectCases().iter(module -> success = runCase(module, target) && success);
  17. }
  18. Sys.exit(success ? 0 : 1);
  19. }
  20. static function runCase(module:String, target:Target):Bool {
  21. var proc = new Process(
  22. 'haxe',
  23. targetCompilerFlags(target).concat([
  24. '-cp', 'src',
  25. '-lib', 'sourcemap',
  26. '-D', 'source-map',
  27. '-D', 'analyzer-optimize',
  28. '-dce', 'full',
  29. '-main', module
  30. ])
  31. );
  32. var stdErr = proc.stderr.readAll().toString();
  33. var stdOut = proc.stdout.readAll().toString();
  34. if(stdErr.trim().length > 0) {
  35. Sys.println(stdErr.trim());
  36. }
  37. var report:ValidationReport = try {
  38. Json.parse(stdOut);
  39. } catch(e:Dynamic) {
  40. Sys.println('$module: Failed to parse compilation result:\n$stdOut');
  41. return false;
  42. }
  43. Sys.println('$module: ${report.summary.assertions} tests, ${report.summary.failures} failures');
  44. if(!report.success) {
  45. for(error in report.errors) {
  46. Sys.println('${error.pos.file}:${error.pos.line}:${error.pos.column}: Failed to find expected code: ${error.expected}');
  47. }
  48. }
  49. return report.success;
  50. }
  51. static function targetCompilerFlags(target:Target):Array<String> {
  52. return switch(target) {
  53. case Js: ['-js', 'bin/test.js'];
  54. case Php: ['-php', 'bin/php'];
  55. }
  56. }
  57. static function collectCases():Array<String> {
  58. var result = [];
  59. for(fileName in 'src/cases'.readDirectory()) {
  60. var path = new Path(fileName);
  61. if(path.isTestCase()) {
  62. result.push('cases.${path.file}');
  63. }
  64. }
  65. return result;
  66. }
  67. static function isTestCase(path:Path):Bool {
  68. var firstChar = path.file.charAt(0);
  69. return path.ext == 'hx' && firstChar == firstChar.toUpperCase();
  70. }
  71. static function installDependencies() {
  72. Sys.command('haxelib', ['install', 'sourcemap']);
  73. }
  74. }