From b4024ee003626a9e38c2b3571db90e4720227437 Mon Sep 17 00:00:00 2001 From: Christopher Dwyer-Perkins Date: Tue, 16 Jan 2024 15:40:25 -0400 Subject: [PATCH] Fixed more on device tests --- bsc-plugin/src/lib/rooibos/TestGroup.ts | 48 ++++++++++++++++++++----- bsc-plugin/src/plugin.spec.ts | 6 ++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/bsc-plugin/src/lib/rooibos/TestGroup.ts b/bsc-plugin/src/lib/rooibos/TestGroup.ts index bff566f9..b9f081c1 100644 --- a/bsc-plugin/src/lib/rooibos/TestGroup.ts +++ b/bsc-plugin/src/lib/rooibos/TestGroup.ts @@ -1,5 +1,5 @@ -import type { AstEditor, CallExpression, DottedGetExpression, NamespaceContainer } from 'brighterscript'; -import { ArrayLiteralExpression, createInvalidLiteral, createStringLiteral, createToken, isDottedGetExpression, TokenKind, isFunctionExpression, Parser } from 'brighterscript'; +import type { AstEditor, CallExpression, DottedGetExpression, Expression, NamespaceContainer, Scope } from 'brighterscript'; +import { ArrayLiteralExpression, createInvalidLiteral, createStringLiteral, createToken, isDottedGetExpression, TokenKind, isFunctionExpression, Parser, ParseMode } from 'brighterscript'; import * as brighterscript from 'brighterscript'; import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState'; import { diagnosticErrorProcessingFile } from '../utils/Diagnostics'; @@ -39,7 +39,6 @@ export class TestGroup extends TestBlock { } else { this.hasAsyncTests = testCase.isAsync; } - } public getTestCases(): TestCase[] { @@ -51,6 +50,7 @@ export class TestGroup extends TestBlock { //if assertion //wrap with if is not fail //add line number as last param + let scope = this.file.program.getFirstScopeForFile(this.file); const transpileState = new BrsTranspileState(this.file); try { let func = this.testSuite.classStatement.methods.find((m) => m.name.text.toLowerCase() === testCase.funcName.toLowerCase()); @@ -63,16 +63,16 @@ export class TestGroup extends TestBlock { let assertRegex = /(?:fail|assert(?:[a-z0-9]*)|expect(?:[a-z0-9]*)|stubCall)/i; if (dge && assertRegex.test(dge.name.text)) { if (dge.name.text === 'stubCall') { - this.modifyModernRooibosExpectCallExpression(callExpression, editor, namespaceLookup); + this.modifyModernRooibosExpectCallExpression(callExpression, editor, namespaceLookup, scope); return expressionStatement; } else { if (dge.name.text === 'expectCalled' || dge.name.text === 'expectNotCalled') { - this.modifyModernRooibosExpectCallExpression(callExpression, editor, namespaceLookup); + this.modifyModernRooibosExpectCallExpression(callExpression, editor, namespaceLookup, scope); } if (dge.name.text === 'expectCalled' || dge.name.text === 'expectNotCalled') { - this.modifyModernRooibosExpectCallExpression(callExpression, editor, namespaceLookup); + this.modifyModernRooibosExpectCallExpression(callExpression, editor, namespaceLookup, scope); } if (!noEarlyExit) { @@ -95,7 +95,7 @@ export class TestGroup extends TestBlock { } } - private modifyModernRooibosExpectCallExpression(callExpression: CallExpression, editor: AstEditor, namespaceLookup: Map) { + private modifyModernRooibosExpectCallExpression(callExpression: CallExpression, editor: AstEditor, namespaceLookup: Map, scope: Scope) { let isNotCalled = false; let isStubCall = false; @@ -107,7 +107,7 @@ export class TestGroup extends TestBlock { isNotCalled = nameText === 'expectNotCalled'; isStubCall = nameText === 'stubCall'; - if (isStubCall && !brighterscript.isCallExpression(arg0)) { + if (isStubCall && this.shouldNotModifyStubCall(arg0, namespaceLookup, scope)) { return; } editor.setProperty(callExpression.callee.name, 'text', `_${nameText}`); @@ -198,6 +198,38 @@ export class TestGroup extends TestBlock { } } + private shouldNotModifyStubCall(arg0: Expression, namespaceLookup: Map, scope: Scope) { + if (brighterscript.isDottedGetExpression(arg0)) { + let nameParts = getAllDottedGetParts(arg0); + let name = nameParts.pop(); + let functionName: string; + + if (name) { + //is a namespace? + if (nameParts[0] && namespaceLookup.has(nameParts[0].toLowerCase())) { + //then this must be a namespace method + let fullPathName = nameParts.join('.').toLowerCase(); + let ns = namespaceLookup.get(fullPathName); + if (!ns) { + //TODO this is an error condition! + } + nameParts.push(name); + functionName = nameParts.join('.').toLowerCase(); + } + } + + if (functionName && scope.getCallableByName(functionName)) { + return true; + } + } else if (brighterscript.isVariableExpression(arg0)) { + const functionName = arg0.getName(ParseMode.BrightScript).toLowerCase(); + if (scope.getCallableByName(functionName)) { + return true; + } + } + return false; + } + public asText(): string { let testCaseText = [...this.testCases.values()].filter((tc) => tc.isIncluded).map((tc) => tc.asText()); diff --git a/bsc-plugin/src/plugin.spec.ts b/bsc-plugin/src/plugin.spec.ts index ca66f89e..f2d9a761 100644 --- a/bsc-plugin/src/plugin.spec.ts +++ b/bsc-plugin/src/plugin.spec.ts @@ -638,7 +638,7 @@ describe('RooibosPlugin', () => { expect(statements[0]).to.be.instanceof(PrintStatement); }); - describe.skip('expectCalled transpilation', () => { + describe('expectCalled transpilation', () => { it('correctly transpiles call funcs', async () => { program.setFile('source/test.spec.bs', ` @suite @@ -1125,7 +1125,7 @@ describe('RooibosPlugin', () => { }); }); - describe.skip('stubCall transpilation', () => { + describe('stubCall transpilation', () => { it('correctly transpiles call funcs', async () => { program.setFile('source/test.spec.bs', ` @suite @@ -1267,7 +1267,7 @@ describe('RooibosPlugin', () => { }); }); - describe.skip('expectNotCalled transpilation', () => { + describe('expectNotCalled transpilation', () => { it('correctly transpiles call funcs', async () => { program.setFile('source/test.spec.bs', ` @suite