Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
georgejecook committed Oct 24, 2023
1 parent 63e75d8 commit cfdaf2f
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 72 deletions.
58 changes: 49 additions & 9 deletions bsc-plugin/src/lib/rooibos/MockUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function trimLeading(text: string) {
return text.split('\n').map((line) => line.trimStart()).join('\n');
}

describe('MockUtil', () => {
describe.only('MockUtil', () => {

Check failure on line 16 in bsc-plugin/src/lib/rooibos/MockUtil.spec.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

describe.only not permitted

Check failure on line 16 in bsc-plugin/src/lib/rooibos/MockUtil.spec.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

describe.only not permitted
let program: Program;
let builder: ProgramBuilder;
let plugin: RooibosPlugin;
Expand All @@ -34,7 +34,8 @@ describe('MockUtil', () => {
isGlobalMethodMockingEnabled: true,
globalMethodMockingExcludedFiles: [
'**/*.coverageExcluded.bs'
]
],
isGlobalMethodMockingEfficientMode: false
},
allowBrighterScriptInBrightScript: true
};
Expand Down Expand Up @@ -78,7 +79,8 @@ describe('MockUtil', () => {
let a = getContents('source/code.brs');
let b = trimLeading(`function sayHello(a1, a2)
if RBS_CC_1_getMocksByFunctionName()["sayHello"] <> invalid
return RBS_CC_1_getMocksByFunctionName()["sayHello"](a1,a2)
result = RBS_CC_1_getMocksByFunctionName()["sayHello"].callback(a1,a2)
return result
end if
print "hello"
end function
Expand Down Expand Up @@ -108,7 +110,42 @@ describe('MockUtil', () => {
let a = getContents('source/code.brs');
let b = trimLeading(`function sayHello(a1, a2)
if RBS_CC_1_getMocksByFunctionName()["sayHello"] <> invalid
return RBS_CC_1_getMocksByFunctionName()["sayHello"](a1,a2)
result = RBS_CC_1_getMocksByFunctionName()["sayHello"].callback(a1,a2)
return result
end if
print "hello"
end function
function RBS_CC_1_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
return m._rMocksByFunctionName
end function
`);
expect(a).to.equal(b);

});
it.only('weird raletracker task issue I saw', async () => {

Check failure on line 129 in bsc-plugin/src/lib/rooibos/MockUtil.spec.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

it.only not permitted

Check failure on line 129 in bsc-plugin/src/lib/rooibos/MockUtil.spec.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

it.only not permitted
program.setFile('source/code.bs', `
Sub RedLines_SetRulerLines(rulerLines)
For Each line In rulerLines.Items()
RedLines_AddLine(line.key, line.value.position, line.value.coords, m.node, m.childMap)
End For
end Sub
Sub RedLines_AddLine(id, position, coords, node, childMap) as Object
line = CreateObject("roSGNode", "Rectangle")
line.setField("id", id)
end sub
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
let a = getContents('source/code.brs');
let b = trimLeading(`function sayHello(a1, a2)
if RBS_CC_1_getMocksByFunctionName()["sayHello"] <> invalid
result = RBS_CC_1_getMocksByFunctionName()["sayHello"].callback(a1,a2)
return result
end if
print "hello"
end function
Expand Down Expand Up @@ -136,7 +173,7 @@ describe('MockUtil', () => {
let a = getContents('source/code.brs');
let b = trimLeading(`sub sayHello(a1, a2)
if RBS_CC_1_getMocksByFunctionName()["sayHello"] <> invalid
RBS_CC_1_getMocksByFunctionName()["sayHello"](a1,a2)
result = RBS_CC_1_getMocksByFunctionName()["sayHello"].callback(a1,a2)
return
end if
print "hello"
Expand Down Expand Up @@ -167,7 +204,8 @@ describe('MockUtil', () => {
let a = getContents('source/code.brs');
let b = trimLeading(`function person_utils_sayHello(a1, a2)
if RBS_CC_1_getMocksByFunctionName()["person_utils_sayHello"] <> invalid
return RBS_CC_1_getMocksByFunctionName()["person_utils_sayHello"](a1,a2)
result = RBS_CC_1_getMocksByFunctionName()["person_utils_sayHello"].callback(a1,a2)
return result
end if
print "hello"
end function
Expand Down Expand Up @@ -197,7 +235,7 @@ describe('MockUtil', () => {
let a = getContents('source/code.brs');
let b = trimLeading(`sub person_utils_sayHello(a1, a2)
if RBS_CC_1_getMocksByFunctionName()["person_utils_sayHello"] <> invalid
RBS_CC_1_getMocksByFunctionName()["person_utils_sayHello"](a1,a2)
result = RBS_CC_1_getMocksByFunctionName()["person_utils_sayHello"].callback(a1,a2)
return
end if
print "hello"
Expand Down Expand Up @@ -280,14 +318,16 @@ describe('MockUtil', () => {
function beings_sayHello()
if RBS_CC_1_getMocksByFunctionName()["beings_sayHello"] <> invalid
return RBS_CC_1_getMocksByFunctionName()["beings_sayHello"]()
result = RBS_CC_1_getMocksByFunctionName()["beings_sayHello"].callback()
return result
end if
print "hello2"
end function
function sayHello()
if RBS_CC_1_getMocksByFunctionName()["sayHello"] <> invalid
return RBS_CC_1_getMocksByFunctionName()["sayHello"]()
result = RBS_CC_1_getMocksByFunctionName()["sayHello"].callback()
return result
end if
print "hello3"
end function
Expand Down
21 changes: 14 additions & 7 deletions bsc-plugin/src/lib/rooibos/MockUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import { RawCodeStatement } from './RawCodeStatement';
import { Range } from 'vscode-languageserver-types';
import type { FileFactory } from './FileFactory';
import undent from 'undent';
import type { RooibosSession } from './RooibosSession';

export class MockUtil {

constructor(builder: ProgramBuilder, fileFactory: FileFactory) {
constructor(builder: ProgramBuilder, fileFactory: FileFactory, session: RooibosSession) {
this.config = (builder.options as any).rooibos as RooibosConfig || {};
this.filePathMap = {};
this.fileId = 0;
this.session = session;
this.fileFactory = fileFactory;
try {
} catch (e) {
console.log('Error:', e.stack);
}
}
session: RooibosSession;

private brsFileAdditions = `
function RBS_CC_#ID#_getMocksByFunctionName()
Expand Down Expand Up @@ -61,19 +60,27 @@ export class MockUtil {

private enableMockOnFunction(functionExpression: brighterscript.FunctionExpression) {
if (isClassStatement(functionExpression.parent?.parent)) {
console.log('skipping class', functionExpression.parent?.parent.name.text)

Check failure on line 63 in bsc-plugin/src/lib/rooibos/MockUtil.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Missing semicolon

Check failure on line 63 in bsc-plugin/src/lib/rooibos/MockUtil.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Missing semicolon
return;
}
if (this.processedStatements.has(functionExpression)) {
return;
}

const methodName = functionExpression.functionStatement.getName(brighterscript.ParseMode.BrightScript);
const methodName = functionExpression?.functionStatement?.getName(brighterscript.ParseMode.BrightScript) || '';
// console.log('MN', methodName);
if (this.config.isGlobalMethodMockingEfficientMode && !this.session.globalStubbedMethods.has(methodName)) {
return;
}

//TODO check if the user has actually mocked or stubbed this function, otherwise leave it alone!

for (let param of functionExpression.parameters) {
param.asToken = null;
}
const paramNames = functionExpression.parameters.map((param) => param.name.text).join(',');

const returnStatement = (functionExpression.functionType.kind === brighterscript.TokenKind.Sub || functionExpression.returnTypeToken?.kind === brighterscript.TokenKind.Void) ? 'return' : 'return result';
const returnStatement = ((functionExpression.functionType?.kind === brighterscript.TokenKind.Sub && (functionExpression.returnTypeToken === undefined || functionExpression.returnTypeToken?.kind === brighterscript.TokenKind.Void)) || functionExpression.returnTypeToken?.kind === brighterscript.TokenKind.Void) ? 'return' : 'return result';
this.astEditor.addToArray(functionExpression.body.statements, 0, new RawCodeStatement(undent`
if RBS_CC_${this.fileId}_getMocksByFunctionName()["${methodName}"] <> invalid
result = RBS_CC_${this.fileId}_getMocksByFunctionName()["${methodName}"].callback(${paramNames})
Expand Down
1 change: 1 addition & 0 deletions bsc-plugin/src/lib/rooibos/RooibosConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum RooibosLogLevel {
}

export interface RooibosConfig {
isGlobalMethodMockingEfficientMode?: boolean;
coverageExcludedFiles?: string[];
isRecordingCodeCoverage?: boolean;
isGlobalMethodMockingEnabled?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion bsc-plugin/src/lib/rooibos/RooibosSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RooibosSession {
private _suiteBuilder: TestSuiteBuilder;

public sessionInfo: SessionInfo;

public globalStubbedMethods = new Set<string>();
public reset() {
this.sessionInfo = new SessionInfo(this.config);
}
Expand Down
26 changes: 14 additions & 12 deletions bsc-plugin/src/lib/rooibos/TestGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,20 @@ export class TestGroup extends TestBlock {
editor.addToArray(callExpression.args, 0, createInvalidLiteral());
editor.addToArray(callExpression.args, 0, createStringLiteral(functionName));
editor.addToArray(callExpression.args, 0, brighterscript.createVariableExpression(functionName));
this.testSuite.session.globalStubbedMethods.add(functionName);
} else {
let functionName = arg0.callee.name.text;
let fullPath = this.getStringPathFromDottedGet(arg0.callee.obj as DottedGetExpression);
editor.removeFromArray(callExpression.args, 0);
if (!isNotCalled && !isStubCall) {
const expectedArgs = new ArrayLiteralExpression(arg0.args, createToken(TokenKind.LeftSquareBracket), createToken(TokenKind.RightSquareBracket));
editor.addToArray(callExpression.args, 0, expectedArgs);
}
editor.addToArray(callExpression.args, 0, fullPath ?? createInvalidLiteral());
editor.addToArray(callExpression.args, 0, this.getRootObjectFromDottedGet(arg0.callee));
editor.addToArray(callExpression.args, 0, createStringLiteral(functionName));
editor.addToArray(callExpression.args, 0, arg0.callee.obj);
}
} else {
let functionName = arg0.callee.name.text;
let fullPath = this.getStringPathFromDottedGet(arg0.callee.obj as DottedGetExpression);
editor.removeFromArray(callExpression.args, 0);
if (!isNotCalled && !isStubCall) {
const expectedArgs = new ArrayLiteralExpression(arg0.args, createToken(TokenKind.LeftSquareBracket), createToken(TokenKind.RightSquareBracket));
editor.addToArray(callExpression.args, 0, expectedArgs);
}
editor.addToArray(callExpression.args, 0, fullPath ?? createInvalidLiteral());
editor.addToArray(callExpression.args, 0, this.getRootObjectFromDottedGet(arg0.callee));
editor.addToArray(callExpression.args, 0, createStringLiteral(functionName));
editor.addToArray(callExpression.args, 0, arg0.callee.obj);
}
} else if (brighterscript.isDottedGetExpression(arg0)) {
let functionName = arg0.name.text;
Expand Down Expand Up @@ -186,6 +187,7 @@ export class TestGroup extends TestBlock {
editor.addToArray(callExpression.args, 0, createInvalidLiteral());
editor.addToArray(callExpression.args, 0, createStringLiteral(functionName));
editor.addToArray(callExpression.args, 0, brighterscript.createVariableExpression(functionName));
this.testSuite.session.globalStubbedMethods.add(functionName);
}
}

Expand Down
2 changes: 2 additions & 0 deletions bsc-plugin/src/lib/rooibos/TestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { RooibosAnnotation } from './Annotation';

import type { TestGroup } from './TestGroup';
import { addOverriddenMethod, sanitizeBsJsonString } from './Utils';
import { RooibosSession } from './RooibosSession';

Check failure on line 9 in bsc-plugin/src/lib/rooibos/TestSuite.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in bsc-plugin/src/lib/rooibos/TestSuite.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

All imports in the declaration are only used as types. Use `import type`

/**
* base of test suites and blocks..
Expand Down Expand Up @@ -80,6 +81,7 @@ export class TestSuite extends TestBlock {
public generatedNodeName: string;
public hasSoloGroups = false;
public isNodeTest = false;
public session: RooibosSession;

public addGroup(group: TestGroup) {
this.testGroups.set(group.name, group);
Expand Down
7 changes: 5 additions & 2 deletions bsc-plugin/src/lib/rooibos/TestSuiteBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import type { BrsFile,
import type {
BrsFile,
ClassMethodStatement,
ClassStatement } from 'brighterscript';
ClassStatement
} from 'brighterscript';
import {
isClassMethodStatement
} from 'brighterscript';
Expand Down Expand Up @@ -97,6 +99,7 @@ export class TestSuiteBuilder {

public processClass(annotation: RooibosAnnotation, classStatement: ClassStatement): TestSuite {
this.testSuite = new TestSuite(annotation, classStatement);
this.testSuite.session = this.session;
this.currentGroup = null;
this.annotation = null;
for (let s of classStatement.body) {
Expand Down
49 changes: 31 additions & 18 deletions bsc-plugin/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let _rootDir = s`${tmpPath}/rootDir`;
let _stagingFolderPath = s`${tmpPath}/staging`;
const version = fsExtra.readJsonSync(__dirname + '/../package.json').version;

describe.only('RooibosPlugin', () => {
describe('RooibosPlugin', () => {
let program: Program;
let builder: ProgramBuilder;
let plugin: RooibosPlugin;
Expand Down Expand Up @@ -932,37 +932,49 @@ describe.only('RooibosPlugin', () => {
}
m.currentAssertLineNumber = 7
m._expectCalled(invalid, "sayHello", invalid, invalid, [
m._expectCalled(sayHello, "sayHello", invalid, invalid, [
"arg1"
"arg2"
], "return")
if m.currentResult?.isFail = true then m.done() : return invalid
m.currentAssertLineNumber = 8
m._expectCalled(invalid, "sayHello", invalid, invalid, [])
m._expectCalled(sayHello, "sayHello", invalid, invalid, [])
if m.currentResult?.isFail = true then m.done() : return invalid
m.currentAssertLineNumber = 9
m._expectCalled(invalid, "sayHello", invalid, invalid, [], "return")
m._expectCalled(sayHello, "sayHello", invalid, invalid, [], "return")
if m.currentResult?.isFail = true then m.done() : return invalid
m.currentAssertLineNumber = 10
m._expectCalled(invalid, "sayHello", invalid, invalid, [
m._expectCalled(sayHello, "sayHello", invalid, invalid, [
"arg1"
"arg2"
])
if m.currentResult?.isFail = true then m.done() : return invalid
`);

let codeText = getContents('code.brs');
expect(codeText).to.equal(`
TODO
`);
expect(codeText).to.equal(undent`
function sayHello(firstName = "", lastName = "")
if RBS_CC_1_getMocksByFunctionName()["sayHello"] <> invalid
result = RBS_CC_1_getMocksByFunctionName()["sayHello"].callback(firstName,lastName)
return result
end if
print firstName + " " + lastName
end function
function RBS_CC_1_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
return m._rMocksByFunctionName
end function`);
});
it.only('correctly transpiles namespaced function calls', async () => {
it('correctly transpiles namespaced function calls', async () => {
plugin.config.isGlobalMethodMockingEnabled = true;
program.setFile('source/test.spec.bs', `
@suite
Expand Down Expand Up @@ -1001,7 +1013,7 @@ describe.only('RooibosPlugin', () => {
m._expectCalled(utils_sayhello, "utils_sayhello", invalid, invalid, [
"arg1"
"arg2"
])
], "return")
if m.currentResult?.isFail = true then m.done() : return invalid
Expand All @@ -1011,7 +1023,7 @@ describe.only('RooibosPlugin', () => {
m.currentAssertLineNumber = 9
m._expectCalled(utils_sayhello, "utils_sayhello", invalid, invalid, [])
m._expectCalled(utils_sayhello, "utils_sayhello", invalid, invalid, [], "return")
if m.currentResult?.isFail = true then m.done() : return invalid
Expand All @@ -1026,7 +1038,8 @@ describe.only('RooibosPlugin', () => {
let codeText = trimLeading(getContents('code.brs'));
expect(codeText).to.equal(trimLeading(`function utils_sayHello(firstName = "", lastName = "")
if RBS_CC_1_getMocksByFunctionName()["utils_sayHello"] <> invalid
return RBS_CC_1_getMocksByFunctionName()["utils_sayHello"](firstName,lastName)
result = RBS_CC_1_getMocksByFunctionName()["utils_sayHello"].callback(firstName,lastName)
return result
end if
print firstName + " " + lastName
end function
Expand All @@ -1036,7 +1049,7 @@ describe.only('RooibosPlugin', () => {
m._rMocksByFunctionName = {}
end if
return m._rMocksByFunctionName
end functiond`));
end function`));
});
});

Expand Down Expand Up @@ -1581,15 +1594,15 @@ describe.only('RooibosPlugin', () => {
end function
instance.getRuntimeConfig = function()
return {
"failFast": false
"sendHomeOnFinish": false
"failFast": true
"sendHomeOnFinish": true
"logLevel": 0
"showOnlyFailures": false
"printTestTimes": false
"showOnlyFailures": true
"printTestTimes": true
"lineWidth": 60
"printLcov": false
"port": "invalid"
"catchCrashes": false
"catchCrashes": true
"keepAppOpen": true
}
end function
Expand Down
Loading

0 comments on commit cfdaf2f

Please sign in to comment.