test_unit_eberon.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. var grammar = require("eberon/eberon_grammar.js").grammar;
  3. var TestUnitCommon = require("test_unit_common.js");
  4. var pass = TestUnitCommon.pass;
  5. var fail = TestUnitCommon.fail;
  6. var context = TestUnitCommon.context;
  7. function testWithContext(context, pass, fail){
  8. return TestUnitCommon.testWithContext(context, grammar.declarationSequence, pass, fail);
  9. }
  10. exports.suite = {
  11. "new method declaration": testWithContext(
  12. context(grammar.declarationSequence,
  13. "TYPE T = RECORD intField: INTEGER END; A = ARRAY 1 OF INTEGER;"),
  14. pass("PROCEDURE T.p(), NEW; END T.p;",
  15. "PROCEDURE T.p*(), NEW; END T.p;"
  16. ),
  17. fail(["PROCEDURE TUnk.p(), NEW; END TUnk.p;", "undeclared identifier: 'TUnk'"],
  18. ["PROCEDURE A.p(), NEW; END A.p;",
  19. "RECORD type expected in method declaration, got 'ARRAY 1 OF INTEGER'"],
  20. ["PROCEDURE T.p(), NEW; END;", "not parsed"],
  21. ["PROCEDURE T.p(), NEW; END p;",
  22. "mismatched procedure names: 'T.p' at the begining and 'p.' at the end"],
  23. ["PROCEDURE T.p(), NEW; END T2.p;",
  24. "mismatched procedure names: 'T.p' at the begining and 'T2.p' at the end"],
  25. ["PROCEDURE T.p(), NEW; END T.p2;",
  26. "mismatched procedure names: 'T.p' at the begining and 'T.p2' at the end"],
  27. ["PROCEDURE T.intField(), NEW; END T.intField;",
  28. "cannot declare method, record already has field 'intField'"],
  29. ["PROCEDURE T.p(), NEW; END T.p; PROCEDURE T.p(), NEW; END T.p;",
  30. "'T.p' already declared"]
  31. )
  32. ),
  33. "overridden method declaration": testWithContext(
  34. context(grammar.declarationSequence,
  35. "TYPE Base = RECORD END; T = RECORD (Base) END;"
  36. + "PROCEDURE Base.p(), NEW; END Base.p;"),
  37. pass("PROCEDURE T.p(); END T.p;"),
  38. fail(["PROCEDURE T.pUnk(); END T.pUnk;",
  39. "there is no method 'pUnk' to override in base type(s) of 'T' (NEW attribute is missed?)"],
  40. ["PROCEDURE T.p(), NEW; END T.p;",
  41. "base record already has method 'p' (unwanted NEW attribute?)"],
  42. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  43. "'T.p' already declared"],
  44. ["PROCEDURE T.p(a: INTEGER); END T.p;",
  45. "overridden method 'p' signature mismatch: should be 'PROCEDURE', got 'PROCEDURE(INTEGER)'"]
  46. )
  47. ),
  48. "SELF": testWithContext(
  49. context(grammar.declarationSequence,
  50. "TYPE T = RECORD i: INTEGER END;"),
  51. pass("PROCEDURE T.p(), NEW; BEGIN SELF.i := 0; END T.p;"),
  52. fail()
  53. ),
  54. "method call": testWithContext(
  55. context(grammar.expression,
  56. "TYPE T = RECORD END;"
  57. + "VAR o: T;"
  58. + "PROCEDURE T.p(), NEW; END T.p;"
  59. + "PROCEDURE T.f(): INTEGER, NEW; RETURN 0 END T.f;"
  60. ),
  61. pass("o.f()"),
  62. fail(["o.p()", "procedure returning no result cannot be used in an expression"])
  63. ),
  64. "method super call": testWithContext(
  65. context(grammar.declarationSequence,
  66. "TYPE T = RECORD END; D = RECORD(T) END;"
  67. + "PROCEDURE T.p(), NEW; END T.p;"
  68. ),
  69. pass("PROCEDURE D.p(); BEGIN SUPER() END D.p;"),
  70. fail(["PROCEDURE D.pNoSuper(), NEW; BEGIN SUPER() END D.pNoSuper;",
  71. "there is no super method in base type(s)"])
  72. )
  73. };