-
Notifications
You must be signed in to change notification settings - Fork 29
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
Feature/runtime global function mocking #250
Merged
chrisdp
merged 27 commits into
rokucommunity:feature/runtime-global-function-mocking
from
chrisdp:feature/runtime-global-function-mocking
Jan 22, 2024
Merged
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0e07019
Added function to bast test suite to store global mocked functions
chrisdp 77d06a4
Moved some logic to the utils
chrisdp 9f75924
Added logic to inject code that adds global mock checks at the start …
chrisdp 90edc04
Fixed a reversed check
chrisdp 2c4feef
Removed new api in favor of expanding stubcall and updated injection …
chrisdp 9f6111e
Updated some of the global function detection logic
chrisdp fadbf2c
Unit tests, fixes, sample test project update
chrisdp fc6ce1d
removed console logs
chrisdp 8b3fee5
Transpile the file if we had to touch modify it
chrisdp 3307955
Fixed global mocks and stubs not clearing
chrisdp a155a29
updated some of the checks around transforming stubcall functions
chrisdp a342165
Made some code reusable and fixed some imports
chrisdp 02c6f4e
Added back the disablemocking logic and removed unused code
chrisdp 02c64df
Fixed bad ast related to noEarlyExit
chrisdp 86656dd
Updated bsc in tests app
chrisdp db71048
added tests for global stubcall on device
chrisdp 4c3d753
Fixed some device tests
chrisdp b4024ee
Fixed more on device tests
chrisdp 67ec05a
More test fixes as the result of moving to ast editor
chrisdp bcfd24d
Fixed some indenting
chrisdp 9f4eabd
Fixed node test xml files being added after modifying assertions lead…
chrisdp 3acbf16
Updated the modify stub detection logic for globals
chrisdp 1be78f6
more tests for global stub call modifications
chrisdp 3164492
Fixed some race conditons and more global function detection refinments
chrisdp 4132da2
Fixed some issues picking the wrong scope and make sure stubcall work…
chrisdp f435bba
Moved global stub clearing to clearStubs()
chrisdp a9e1450
Merge branch 'master' into feature/runtime-global-function-mocking
chrisdp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { AstEditor, CallExpression, DottedGetExpression } 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'; | ||
|
@@ -8,7 +8,6 @@ import type { TestCase } from './TestCase'; | |
import type { TestSuite } from './TestSuite'; | ||
import { TestBlock } from './TestSuite'; | ||
import { getAllDottedGetParts, getRootObjectFromDottedGet, getStringPathFromDottedGet, sanitizeBsJsonString } from './Utils'; | ||
import type { NamespaceContainer } from './RooibosSession'; | ||
|
||
export class TestGroup extends TestBlock { | ||
|
||
|
@@ -40,14 +39,13 @@ export class TestGroup extends TestBlock { | |
} else { | ||
this.hasAsyncTests = testCase.isAsync; | ||
} | ||
|
||
} | ||
|
||
public getTestCases(): TestCase[] { | ||
return [...this.testCases.values()]; | ||
} | ||
|
||
public modifyAssertions(testCase: TestCase, noEarlyExit: boolean, editor: AstEditor, namespaceLookup: Map<string, NamespaceContainer>) { | ||
public modifyAssertions(testCase: TestCase, noEarlyExit: boolean, editor: AstEditor, namespaceLookup: Map<string, NamespaceContainer>, scope: Scope) { | ||
//for each method | ||
//if assertion | ||
//wrap with if is not fail | ||
|
@@ -64,21 +62,22 @@ 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); | ||
} | ||
const trailingLine = Parser.parse(`${noEarlyExit ? '' : `if m.currentResult?.isFail = true then m.done() : return ${isSub ? '' : 'invalid'}`}`).ast.statements[0]; | ||
|
||
editor.arraySplice(owner, key + 1, 0, trailingLine); | ||
|
||
if (!noEarlyExit) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed some undefined ast being injected resulting in this annotation causing down stream crashes at transpile. Required to be able to run the test app on device, |
||
const trailingLine = Parser.parse(`if m.currentResult?.isFail = true then m.done() : return ${isSub ? '' : 'invalid'}`).ast.statements[0]; | ||
editor.arraySplice(owner, key + 1, 0, trailingLine); | ||
} | ||
const leadingLine = Parser.parse(`m.currentAssertLineNumber = ${callExpression.range.start.line}`).ast.statements[0]; | ||
editor.arraySplice(owner, key, 0, leadingLine); | ||
} | ||
|
@@ -89,23 +88,30 @@ export class TestGroup extends TestBlock { | |
walkMode: brighterscript.WalkMode.visitStatementsRecursive | ||
}); | ||
} catch (e) { | ||
// console.log(e); | ||
console.error(e); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
diagnosticErrorProcessingFile(this.testSuite.file, e.message); | ||
} | ||
} | ||
|
||
private modifyModernRooibosExpectCallExpression(callExpression: CallExpression, editor: AstEditor, namespaceLookup: Map<string, NamespaceContainer>) { | ||
private modifyModernRooibosExpectCallExpression(callExpression: CallExpression, editor: AstEditor, namespaceLookup: Map<string, NamespaceContainer>, scope: Scope) { | ||
let isNotCalled = false; | ||
let isStubCall = false; | ||
|
||
//modify args | ||
let arg0 = callExpression.args[0]; | ||
let arg1 = callExpression.args[1]; | ||
if (isDottedGetExpression(callExpression.callee)) { | ||
const nameText = callExpression.callee.name.text; | ||
editor.setProperty(callExpression.callee.name, 'text', `_${nameText}`); | ||
isNotCalled = nameText === 'expectNotCalled'; | ||
isStubCall = nameText === 'stubCall'; | ||
|
||
if (isStubCall && this.shouldNotModifyStubCall(arg0, namespaceLookup, scope)) { | ||
return; | ||
} | ||
editor.setProperty(callExpression.callee.name, 'text', `_${nameText}`); | ||
} | ||
//modify args | ||
let arg0 = callExpression.args[0]; | ||
|
||
if (brighterscript.isCallExpression(arg0) && isDottedGetExpression(arg0.callee)) { | ||
|
||
//is it a namespace? | ||
|
@@ -191,6 +197,20 @@ export class TestGroup extends TestBlock { | |
} | ||
} | ||
|
||
private shouldNotModifyStubCall(arg0: Expression, namespaceLookup: Map<string, NamespaceContainer>, scope: Scope) { | ||
if (brighterscript.isDottedGetExpression(arg0)) { | ||
let nameParts = getAllDottedGetParts(arg0); | ||
let functionName = nameParts.join('.'); | ||
return scope.getCallableByName(functionName); | ||
} else if (brighterscript.isVariableExpression(arg0)) { | ||
return ( | ||
scope.symbolTable.hasSymbol(arg0.getName(ParseMode.BrightScript)) || | ||
scope.getCallableByName(arg0.getName(ParseMode.BrighterScript)) | ||
); | ||
} | ||
return false; | ||
} | ||
|
||
public asText(): string { | ||
let testCaseText = [...this.testCases.values()].filter((tc) => tc.isIncluded).map((tc) => tc.asText()); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add the component file to the program before we start looking for stub and expect calls to be modified now that we use the file scopes to reliably know if a global is in scope for this suite.