test_unit_oberon.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "scalar variables cannot be exported": testWithGrammar(
  21. grammar.declarationSequence,
  22. pass(),
  23. fail(["VAR r*: RECORD END;",
  24. "variable 'r' cannot be exported: only scalar variables can be exported"],
  25. ["VAR a*: ARRAY 5 OF INTEGER;",
  26. "variable 'a' cannot be exported: only scalar variables can be exported"]
  27. )
  28. ),
  29. "eberon key words can be identifiers": testWithGrammar(
  30. grammar.variableDeclaration,
  31. pass("SELF: INTEGER",
  32. "SUPER: INTEGER"
  33. )
  34. ),
  35. "eberon types are missing": testWithGrammar(
  36. grammar.variableDeclaration,
  37. pass(),
  38. fail(["s: STRING", "undeclared identifier: 'STRING'"])
  39. ),
  40. "cannot designate call result in expression": testWithContext(
  41. context(grammar.expression,
  42. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  43. + "ProcType = PROCEDURE(): INTEGER;"
  44. + "VAR p: PT;"
  45. + "PROCEDURE proc(): PT; RETURN p END proc;"
  46. + "PROCEDURE p1(): INTEGER; RETURN 1 END p1;"
  47. + "PROCEDURE p2(): ProcType; RETURN p1 END p2;"),
  48. pass(),
  49. fail(["proc().field", "not parsed"],
  50. ["p2()()", "not parsed"])
  51. ),
  52. "cannot designate call result in statement": testWithContext(
  53. context(grammar.statement,
  54. "PROCEDURE p; END p;"),
  55. pass(),
  56. fail(["p()()", "not parsed"])
  57. ),
  58. "procedure arguments can be modified": testWithContext(
  59. context(grammar.procedureDeclaration, ""),
  60. pass("PROCEDURE p(a: INTEGER); BEGIN a := a + 1 END p")
  61. ),
  62. "Non-VAR ARRAY parameter cannot be passed as VAR": testWithContext(
  63. context(grammar.procedureDeclaration,
  64. "PROCEDURE pArrayRef(VAR a: ARRAY OF INTEGER); END pArrayRef;"
  65. ),
  66. pass(),
  67. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pArrayRef(a) END p",
  68. "read-only variable cannot be used as VAR parameter"]
  69. )
  70. ),
  71. "Non-VAR RECORD parameter cannot be passed as VAR": testWithContext(
  72. context(grammar.procedureDeclaration,
  73. "TYPE T = RECORD i: INTEGER END;"
  74. + "PROCEDURE recordVar(VAR r: T); END recordVar;"
  75. ),
  76. pass(),
  77. fail(["PROCEDURE p(r: T); BEGIN recordVar(r); END p",
  78. "read-only variable cannot be used as VAR parameter"]
  79. )
  80. ),
  81. "Non-VAR open array assignment fails": testWithGrammar(
  82. grammar.procedureDeclaration,
  83. pass(),
  84. fail(["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  85. "cannot assign to read-only variable"])
  86. ),
  87. "string assignment to non-VAR open array fails": testWithGrammar(
  88. grammar.procedureDeclaration,
  89. pass(),
  90. fail(["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "cannot assign to read-only variable"])
  91. ),
  92. };