Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extensions): Add _brs_.testData associative array #646

Merged
merged 6 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]));
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -230,6 +231,7 @@ export async function createExecuteWithScope(
interpreter.errors = [];

return (filenames: string[], args: BrsTypes.BrsType[]) => {
resetTestData();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this solution better than exporting it!

let ast = lexParseSync(filenames, interpreter.options);
let execErrors: BrsError.BrsError[] = [];
let returnValue = interpreter.inSubEnv((subInterpreter) => {
Expand Down
30 changes: 30 additions & 0 deletions test/extensions/testData.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});