test_unit_oberon.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. var language = require("oberon/oberon_grammar.js").language;
  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, language, pass, fail);
  9. }
  10. function testWithGrammar(parser, pass, fail){
  11. return TestUnitCommon.testWithGrammar(parser, language, pass, fail);
  12. }
  13. var grammar = language.grammar;
  14. exports.suite = {
  15. "arithmetic operators": testWithContext(
  16. context(grammar.statement, "VAR b1: BOOLEAN;"),
  17. pass(),
  18. fail(["b1 := b1 + b1", "operator '+' type mismatch: numeric type or SET expected, got 'BOOLEAN'"])
  19. ),
  20. "eberon key words can be identifiers": testWithGrammar(
  21. grammar.variableDeclaration,
  22. pass("SELF: INTEGER",
  23. "SUPER: INTEGER"
  24. )
  25. ),
  26. "eberon types are missing": testWithGrammar(
  27. grammar.variableDeclaration,
  28. pass(),
  29. fail(["s: STRING", "undeclared identifier: 'STRING'"])
  30. ),
  31. "array does not have indexOf() method": testWithContext(
  32. context(grammar.expression,
  33. "VAR a: ARRAY 3 OF INTEGER;"),
  34. pass(),
  35. fail(["a.indexOf(123)", "selector '.indexOf' cannot be applied to 'ARRAY 3 OF INTEGER'"])
  36. ),
  37. "cannot designate call result in expression": testWithContext(
  38. context(grammar.expression,
  39. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  40. + "ProcType = PROCEDURE(): INTEGER;"
  41. + "VAR p: PT;"
  42. + "PROCEDURE proc(): PT; RETURN p END proc;"
  43. + "PROCEDURE p1(): INTEGER; RETURN 1 END p1;"
  44. + "PROCEDURE p2(): ProcType; RETURN p1 END p2;"),
  45. pass(),
  46. fail(["proc().field", "not parsed"],
  47. ["p2()()", "not parsed"])
  48. ),
  49. "cannot designate call result in statement": testWithContext(
  50. context(grammar.statement,
  51. "PROCEDURE p; END p;"),
  52. pass(),
  53. fail(["p()()", "not parsed"])
  54. ),
  55. "procedure arguments can be modified": testWithContext(
  56. context(grammar.procedureDeclaration, ""),
  57. pass("PROCEDURE p(a: INTEGER); BEGIN a := a + 1 END p")
  58. ),
  59. "Non-VAR ARRAY parameter cannot be passed as VAR": testWithContext(
  60. context(grammar.procedureDeclaration,
  61. "PROCEDURE pArrayRef(VAR a: ARRAY OF INTEGER); END pArrayRef;"
  62. ),
  63. pass(),
  64. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pArrayRef(a) END p",
  65. "non-VAR formal parameter cannot be passed as VAR actual parameter"]
  66. )
  67. ),
  68. "Non-VAR RECORD parameter cannot be passed as VAR": testWithContext(
  69. context(grammar.procedureDeclaration,
  70. "TYPE T = RECORD i: INTEGER END;"
  71. + "PROCEDURE recordVar(VAR r: T); END recordVar;"
  72. ),
  73. pass(),
  74. fail(["PROCEDURE p(r: T); BEGIN recordVar(r); END p",
  75. "non-VAR formal parameter cannot be passed as VAR actual parameter"]
  76. )
  77. ),
  78. "Non-VAR open array assignment fails": testWithGrammar(
  79. grammar.procedureDeclaration,
  80. pass(),
  81. fail(["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  82. "cannot assign to non-VAR formal parameter"])
  83. ),
  84. "string assignment to non-VAR open array fails": testWithGrammar(
  85. grammar.procedureDeclaration,
  86. pass(),
  87. fail(["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "cannot assign to non-VAR formal parameter"])
  88. ),
  89. "procedure": testWithGrammar(
  90. grammar.procedureDeclaration,
  91. pass(),
  92. fail(["PROCEDURE p; END", "not parsed"])
  93. ),
  94. "syntax strictness": testWithGrammar(
  95. grammar.procedureDeclaration,
  96. pass(),
  97. fail(["TYPE T = RECORD field: INTEGER; END;", "not parsed"],
  98. ["PROCEDURE p(): INTEGER; RETURN 0; END;", "END expected (PROCEDURE)"])
  99. )
  100. };