DisplayTestCase.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import haxe.display.Position.Range;
  2. import utest.Assert;
  3. import Types;
  4. using Lambda;
  5. @:autoBuild(Macro.buildTestCase())
  6. class DisplayTestCase implements utest.ITest {
  7. var ctx:DisplayTestContext;
  8. public function new() {}
  9. // api
  10. inline function pos(name)
  11. return ctx.pos(name);
  12. inline function fields(pos)
  13. return ctx.fields(pos);
  14. inline function toplevel(pos)
  15. return ctx.toplevel(pos);
  16. inline function type(pos)
  17. return ctx.type(pos);
  18. inline function position(pos)
  19. return ctx.position(pos);
  20. inline function usage(pos)
  21. return ctx.usage(pos);
  22. inline function range(pos1, pos2)
  23. return ctx.range(pos1, pos2);
  24. inline function signature(pos1)
  25. return ctx.signature(pos1);
  26. inline function metadataDoc(pos1)
  27. return ctx.metadataDoc(pos1);
  28. inline function diagnostics()
  29. return ctx.diagnostics();
  30. inline function noCompletionPoint(f)
  31. return ctx.hasErrorMessage(f, "No completion point");
  32. inline function typeNotFound(f, typeName)
  33. return ctx.hasErrorMessage(f, "Type not found : " + typeName);
  34. function assert(v:Bool)
  35. Assert.isTrue(v);
  36. function eq<T>(expected:T, actual:T, ?pos:haxe.PosInfos) {
  37. Assert.equals(expected, actual, pos);
  38. }
  39. function arrayEq<T>(expected:Array<T>, actual:Array<T>, ?pos:haxe.PosInfos) {
  40. Assert.same(expected, actual, pos);
  41. }
  42. function arrayCheck<T>(expected:Array<T>, actual:Array<T>, f:T->String, ?pos:haxe.PosInfos) {
  43. var expected = [for (expected in expected) f(expected) => expected];
  44. for (actual in actual) {
  45. var key = f(actual);
  46. Assert.isTrue(expected.exists(key), "Result not part of expected Array: " + Std.string(actual), pos);
  47. expected.remove(key);
  48. }
  49. for (expected in expected) {
  50. Assert.fail("Expected result was not part of actual Array: " + Std.string(expected), pos);
  51. return;
  52. }
  53. }
  54. function hasField(a:Array<FieldElement>, name:String, type:String, ?kind:String):Bool {
  55. return a.exists(function(t) return t.type == type && t.name == name && (kind == null || t.kind == kind));
  56. }
  57. function hasToplevel(a:Array<ToplevelElement>, kind:String, name:String, ?type:String = null):Bool {
  58. return a.exists(function(t) return t.kind == kind && t.name == name && (type == null || t.type == type));
  59. }
  60. function hasPath(a:Array<FieldElement>, name:String):Bool {
  61. return a.exists(function(t) return t.name == name);
  62. }
  63. function diagnosticsRange(start:Position, end:Position):Range {
  64. var range = ctx.source.findRange(start, end);
  65. // this is probably correct...?
  66. range.start.character--;
  67. range.end.character--;
  68. return range;
  69. }
  70. function sigEq(arg:Int, params:Array<Array<String>>, sig:SignatureHelp, ?pos:haxe.PosInfos) {
  71. eq(arg, sig.activeParameter, pos);
  72. eq(params.length, sig.signatures.length, pos);
  73. for (i in 0...params.length) {
  74. var sigInf = sig.signatures[i];
  75. var args = params[i];
  76. eq(sigInf.parameters.length, args.length, pos);
  77. for (i in 0...args.length) {
  78. eq(sigInf.parameters[i].label, args[i], pos);
  79. }
  80. }
  81. }
  82. function report(message, pos:haxe.PosInfos) {
  83. Assert.fail(message, pos);
  84. }
  85. }