diff --git a/src/extensions/index.ts b/src/extensions/index.ts index 97367b9a9..14b5c9da0 100644 --- a/src/extensions/index.ts +++ b/src/extensions/index.ts @@ -26,6 +26,12 @@ export const _brs_ = new RoAssociativeArray([ { name: new BrsString("runInScope"), value: RunInScope }, { name: new BrsString("process"), value: Process }, { name: new BrsString("global"), value: mGlobal }, + { name: new BrsString("testData"), value: new RoAssociativeArray([]) }, { name: new BrsString("triggerKeyEvent"), value: triggerKeyEvent }, { name: new BrsString("getStackTrace"), value: GetStackTrace }, ]); + +/** resets _brs_.testData values to `{}` in brightscript representation */ +export function resetTestData() { + _brs_.set(new BrsString("testData"), new RoAssociativeArray([])); +} diff --git a/src/index.ts b/src/index.ts index 8836b1627..1b25baf05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import { } from "./componentprocessor"; import { Parser } from "./parser"; import { Interpreter, ExecutionOptions, defaultExecutionOptions } from "./interpreter"; +import { resetTestData } from "./extensions"; import * as BrsError from "./Error"; import * as LexerParser from "./LexerParser"; import { CoverageCollector } from "./coverage"; @@ -230,6 +231,7 @@ export async function createExecuteWithScope( interpreter.errors = []; return (filenames: string[], args: BrsTypes.BrsType[]) => { + resetTestData(); let ast = lexParseSync(filenames, interpreter.options); let execErrors: BrsError.BrsError[] = []; let returnValue = interpreter.inSubEnv((subInterpreter) => { diff --git a/test/extensions/testData.test.js b/test/extensions/testData.test.js new file mode 100644 index 000000000..97289d6e0 --- /dev/null +++ b/test/extensions/testData.test.js @@ -0,0 +1,30 @@ +const brs = require("brs"); +const { BrsString, BrsInvalid, ValueKind } = brs.types; +const { Interpreter } = require("../../lib/interpreter"); +const { identifier } = require("../parser/ParserTests"); +const { resetTestData } = require("../../lib/extensions"); + +describe("_brs_.testData", () => { + it("check testData object", () => { + let _brs_ = new Interpreter().environment.get(identifier("_brs_")); + let testData = _brs_.get(new BrsString("testData")); + + expect(testData.kind).toBe(ValueKind.Object); + expect(testData.componentName).toBe("roAssociativeArray"); + expect(testData.elements.size).toBe(0); + testData.set(new BrsString("foo"), BrsInvalid.Instance); + expect(testData.elements.size).toBe(1); + + testData = new Interpreter().environment + .get(identifier("_brs_")) + .get(new BrsString("testData")); + expect(testData.elements.size).toBe(1); + + resetTestData(); // will clear testData for next new Interpreter instances + + testData = new Interpreter().environment + .get(identifier("_brs_")) + .get(new BrsString("testData")); + expect(testData.elements.size).toBe(0); + }); +});