test_unit_common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. var Class = require("rtl.js").Class;
  3. var Code = require("js/Code.js");
  4. var Context = require("context.js");
  5. var Errors = require("js/Errors.js");
  6. var oc = require("oc.js");
  7. var RTL = require("rtl_code.js").RTL;
  8. var Scope = require("js/Scope.js");
  9. var Stream = require("js/Stream.js");
  10. var Test = require("test.js");
  11. var TestError = Test.TestError;
  12. function context(grammar, source){
  13. return {grammar: grammar, source: source};
  14. }
  15. function pass(/*...*/){return Array.prototype.slice.call(arguments);}
  16. function fail(/*...*/){return Array.prototype.slice.call(arguments);}
  17. var TestModuleGenerator = Class.extend({
  18. init: function TestModuleGenerator(){},
  19. prolog: function(){return undefined;},
  20. epilog: function(){return undefined;}
  21. });
  22. var TestContext = Context.Context.extend({
  23. init: function TestContext(language){
  24. Context.Context.prototype.init.call(
  25. this,
  26. { codeGenerator: Code.nullGenerator(),
  27. moduleGenerator: function(){return new TestModuleGenerator();},
  28. rtl: new RTL(),
  29. types: language.types,
  30. stdSymbols: language.stdSymbols
  31. });
  32. this.pushScope(Scope.makeModule("test", language.stdSymbols));
  33. },
  34. qualifyScope: function(){return "";}
  35. });
  36. function makeContext(language){return new TestContext(language);}
  37. function testWithSetup(setup, pass, fail){
  38. return function(){
  39. var test = setup();
  40. var i;
  41. for(i = 0; i < pass.length; ++i)
  42. test.expectOK(pass[i]);
  43. if (fail)
  44. for(i = 0; i < fail.length; ++i){
  45. var f = fail[i];
  46. test.expectError(f[0], f[1]);
  47. }
  48. };
  49. }
  50. function parseInContext(grammar, s, context){
  51. var stream = Stream.make(s);
  52. if (!grammar(stream, context) || !Stream.eof(stream))
  53. throw new Errors.Error("not parsed");
  54. }
  55. function runAndHandleErrors(action, s, handlerError){
  56. try {
  57. action(s);
  58. }
  59. catch (x){
  60. if (!(x instanceof Errors.Error))
  61. throw new Error("'" + s + "': " + x + "\n"
  62. + (x.stack ? x.stack : "(no stack)"));
  63. if (handlerError)
  64. handlerError(x);
  65. //else
  66. // throw x;
  67. // console.log(s + ": " + x);
  68. return false;
  69. }
  70. return true;
  71. }
  72. function setup(run){
  73. return {
  74. expectOK: function(s){
  75. function handleError(e){throw new TestError(s + "\n\t" + e);}
  76. if (!runAndHandleErrors(run, s, handleError))
  77. throw new TestError(s + ": not parsed");
  78. },
  79. expectError: function(s, error){
  80. function handleError(actualError){
  81. var sErr = actualError.toString();
  82. if (sErr != error)
  83. throw new TestError(s + "\n\texpected error: " + error + "\n\tgot: " + sErr );
  84. }
  85. if (runAndHandleErrors(run, s, handleError))
  86. throw new TestError(s + ": should not be parsed, expect error: " + error);
  87. }
  88. };
  89. }
  90. function parseUsingGrammar(parser, language, s, cxFactory){
  91. var baseContext = makeContext(language);
  92. var context = cxFactory ? cxFactory(baseContext) : baseContext;
  93. parseInContext(parser, s, context);
  94. context.currentScope().close();
  95. }
  96. function setupParser(parser, language, contextFactory){
  97. function parseImpl(s){
  98. return parseUsingGrammar(parser, language, s, contextFactory);
  99. }
  100. return setup(parseImpl);
  101. }
  102. function setupWithContext(grammar, contextGrammar, language, source){
  103. function innerMakeContext(){
  104. var context = makeContext(language);
  105. try {
  106. parseInContext(contextGrammar, source, context);
  107. }
  108. catch (x) {
  109. if (x instanceof Errors.Error)
  110. throw new TestError("setup error: " + x + "\n" + source);
  111. throw x;
  112. }
  113. return context;
  114. }
  115. return setupParser(grammar, language, innerMakeContext);
  116. }
  117. function testWithContext(context, contextGrammar, language, pass, fail){
  118. return testWithSetup(
  119. function(){return setupWithContext(context.grammar, contextGrammar, language, context.source);},
  120. pass,
  121. fail);
  122. }
  123. function testWithGrammar(parser, language, pass, fail){
  124. return testWithSetup(
  125. function(){return setupParser(parser, language);},
  126. pass,
  127. fail);
  128. }
  129. var TestContextWithModule = TestContext.extend({
  130. init: function(module, language){
  131. TestContext.prototype.init.call(this, language);
  132. this.__module = module;
  133. },
  134. findModule: function(){return this.__module;}
  135. });
  136. function testWithModule(src, language, pass, fail){
  137. var grammar = language.grammar;
  138. return testWithSetup(
  139. function(){
  140. var imported = oc.compileModule(grammar, Stream.make(src), makeContext(language));
  141. var module = imported.symbol().info();
  142. return setup(function(s){
  143. oc.compileModule(grammar,
  144. Stream.make(s),
  145. new TestContextWithModule(module, language));
  146. });},
  147. pass,
  148. fail);
  149. }
  150. exports.context = context;
  151. exports.pass = pass;
  152. exports.fail = fail;
  153. exports.setupParser = setupParser;
  154. exports.testWithContext = testWithContext;
  155. exports.testWithGrammar = testWithGrammar;
  156. exports.testWithModule = testWithModule;
  157. exports.testWithSetup = testWithSetup;