import IGrammarProvider from "./IGrammarProvider"; import GSuiteContext from "./GSuiteContext"; import { ITypeManifest, IIterator, ArrayIterator } from "@cafetextual/util"; import GrammarTest from "../testsuite/GrammarTest"; import GrammarTestSrc from "../testsuite/GrammarTestSrc"; import ParseResult from "../testsuite/ParseResult"; import GrammarTestSuite from "../testsuite/GrammarTestSuite"; import SeepGrammar from "../parser/SeepGrammar"; /** * context for the execution of grammar tests * */ export default class GTestContext implements IGrammarProvider { constructor(ts:GrammarTestSuite, suiteContext:GSuiteContext) { this.testSuite = ts; this._suiteContext = suiteContext; } // constructor testSuite:GrammarTestSuite // ; private _suiteContext:GSuiteContext; private _localGrammarsByURI:{[uri:string]:SeepGrammar} = {} // {[uri:string]:SeepGrammar} = {} private _grammarsById:{[id:string]:SeepGrammar} = {} // {[grammarID:string]:SeepGrammar} = {} private _grammarsByTestUid:{[uid:string]:SeepGrammar} ={} // <-- indexed by _testSuite.tests registerGrammarByUri(g:SeepGrammar, suri:string):void { this._localGrammarsByURI[suri] = g; } registerGrammarById(g:SeepGrammar, id:string):void { this._localGrammarsByURI[id] = g; } grammarById(id:string):SeepGrammar { return this._grammarsById[id]; } // grammarById getGrammar(suri:string):SeepGrammar { var g:SeepGrammar = this._localGrammarsByURI[suri] if (!g) { g = this._suiteContext.getGrammar(suri) } return g } // getGrammar getManifest():ITypeManifest { return this._suiteContext.getManifest(); } private _log:Array = [];// = [] log(v:string):void { console.log(v) this._log.push(v); } registerGrammarForTest(g:SeepGrammar, gtest:GrammarTest):void { this._grammarsByTestUid[gtest.uid] = g } grammarByTest(gtest:GrammarTest):SeepGrammar { return this._grammarsByTestUid[gtest.uid]; } // grammarByTestCase it():IIterator { return new ArrayIterator(this.testSuite.tests); } logStart(testSrc:GrammarTestSrc):void { this.log("---- test #" + testSrc.inputID + "----- :" + testSrc.ruleName + "\n"); this.log(testSrc.inputText.join("\n")); this.log("-----------"); } // logStart /** * Log results of parsing (excluding data) * */ logResult(result:ParseResult):void { if (result.ok) { var json:string = JSON.stringify(result.data, null, " "); this.log(" -- json data ---\n" + json + "\n ---"); } else { this.log("failed"); } } // logResult _results:{[testid:string]:any} = {}; // hash on gtest uid, then src uid /** * logs expected data */ logTestDataResult(gtest:GrammarTest, src:GrammarTestSrc, ok:boolean):void { var gtestResults:any = this._results[gtest.uid] if (!gtestResults) { gtestResults = {} this._results[gtest.uid] = gtestResults } gtestResults[src.uid] = {ok:ok} // TODO - more data, ie on location of failures this.log(" test " + ok); } // logTestDataResult uri():string { return this.testSuite.provUri; } // ---- update source before running updateSource(gtest:GrammarTest, gsrc:GrammarTestSrc, newSrc:Array):void { // 1st instance: just set new text, don't bother /w ops gsrc.inputText = newSrc } } // class