Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
georgejecook committed Dec 7, 2023
1 parent 536782e commit cc3aec3
Showing 16 changed files with 449 additions and 403 deletions.
3 changes: 3 additions & 0 deletions bsc-plugin/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
"files.trimTrailingWhitespace": true,
"cSpell.words": [
"Lcov",
"noearlyexit",
"sgnode",
"undent"
]
}
8 changes: 4 additions & 4 deletions bsc-plugin/package.json
Original file line number Diff line number Diff line change
@@ -27,19 +27,19 @@
"@types/node": "^14.18.41",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"cz-conventional-changelog": "^3.3.0",
"brighterscript": "^0.66.0-alpha.9",
"brighterscript": "file:../../vsc/brighterscript",
"chai": "^4.2.0",
"chai-subset": "^1.6.0",
"coveralls": "^3.0.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.16.0",
"eslint-plugin-no-only-tests": "^2.4.0",
"fs-extra": "^10.1.0",
"minimatch": "^3.0.4",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"source-map-support": "^0.5.13",
"release-it": "^15.10.3",
"source-map-support": "^0.5.13",
"trim-whitespace": "^1.3.3",
"ts-node": "^9.0.0",
"typescript": "^4.9.5"
@@ -117,4 +117,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
3 changes: 1 addition & 2 deletions bsc-plugin/src/lib/rooibos/CodeCoverageProcessor.ts
Original file line number Diff line number Diff line change
@@ -4,10 +4,9 @@ import type { BrsFile, Editor, ExpressionStatement, Program, ProgramBuilder, Sta
import { Parser, isIfStatement, Position, WalkMode, createVisitor } from 'brighterscript';
import * as brighterscript from 'brighterscript';
import type { RooibosConfig } from './RooibosConfig';
import { RawCodeStatement } from './RawCodeStatement';
import { RawCodeStatement, RawCodeExpression } from './RawCodeStatement';
import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
import { Range } from 'vscode-languageserver-types';
import { RawCodeExpression } from './RawCodeExpression';
import type { FileFactory } from './FileFactory';

export enum CodeCoverageLineType {
92 changes: 46 additions & 46 deletions bsc-plugin/src/lib/rooibos/MockUtil.spec.ts
Original file line number Diff line number Diff line change
@@ -7,29 +7,29 @@ import { RooibosPlugin } from '../../plugin';

let tmpPath = s`${process.cwd()}/tmp`;
let _rootDir = s`${tmpPath}/rootDir`;
let _stagingFolderPath = s`${tmpPath}/staging`;
let _stagingDir = s`${tmpPath}/staging`;

function trimLeading(text: string) {
return text.split('\n').map((line) => line.trimStart()).join('\n');
}

describe('MockUtil', () => {
describe.only('MockUtil', () => {
let program: Program;
let builder: ProgramBuilder;
let plugin: RooibosPlugin;
let options;

function getContents(filename: string) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return trimLeading(fsExtra.readFileSync(s`${_stagingFolderPath}/${filename}`).toString());
return trimLeading(fsExtra.readFileSync(s`${_stagingDir}/${filename}`).toString());
}

describe('MockUtil', () => {
beforeEach(() => {
plugin = new RooibosPlugin();
options = {
rootDir: _rootDir,
stagingFolderPath: _stagingFolderPath,
stagingDir: _stagingDir,
rooibos: {
isGlobalMethodMockingEnabled: true,
globalMethodMockingExcludedFiles: [
@@ -39,7 +39,10 @@ describe('MockUtil', () => {
},
allowBrighterScriptInBrightScript: true
};
fsExtra.ensureDirSync(_stagingFolderPath);
fsExtra.emptyDirSync(_stagingDir);
fsExtra.emptyDirSync(_rootDir);
fsExtra.emptyDirSync(tmpPath);
fsExtra.ensureDirSync(_stagingDir);
fsExtra.ensureDirSync(_rootDir);
fsExtra.ensureDirSync(tmpPath);

@@ -48,15 +51,12 @@ describe('MockUtil', () => {
builder.options = util.normalizeAndResolveConfig(options);
builder.program = new Program(builder.options);
program = builder.program;
program.logger = builder.logger;
builder.plugins = new PluginInterface([plugin], { logger: builder.logger });
program.plugins = new PluginInterface([plugin], { logger: builder.logger });
program.plugins.add(plugin);
program.createSourceScope(); //ensure source scope is created
plugin.beforeProgramCreate(builder);

plugin.beforeProgramCreate({ builder: builder });
});
afterEach(() => {
plugin.afterProgramCreate(program);
plugin.afterProgramCreate({ program: program, builder: builder });
fsExtra.ensureDirSync(tmpPath);
fsExtra.emptyDirSync(tmpPath);
builder.dispose();
@@ -68,24 +68,24 @@ describe('MockUtil', () => {
// This test fails unless `allowBrighterScriptInBrightScript` is set to true when setting up the program
// in `beforeEach`. This is because the compiler normally skips processing .brs files and copies them as-is.
it('adds util code to a brs file', async () => {
program.setFile('source/code.brs', `
const file = program.setFile('source/code.brs', `
function sayHello(a1, a2)
print "hello"
end function
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`function sayHello(a1, a2)
if RBS_SM_1_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["sayhello"].callback(a1,a2)
if RBS_SM_2_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["sayhello"].callback(a1,a2)
return result
end if
print "hello"
end function
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -106,17 +106,17 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`function sayHello(a1, a2)
if RBS_SM_1_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["sayhello"].callback(a1,a2)
if RBS_SM_2_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["sayhello"].callback(a1,a2)
return result
end if
print "hello"
end function
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -140,11 +140,11 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`Sub RedLines_SetRulerLines(rulerLines)
if RBS_SM_1_getMocksByFunctionName()["redlines_setrulerlines"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["redlines_setrulerlines"].callback(rulerLines)
if RBS_SM_2_getMocksByFunctionName()["redlines_setrulerlines"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["redlines_setrulerlines"].callback(rulerLines)
return
end if
For Each line In rulerLines.Items()
@@ -153,15 +153,15 @@ describe('MockUtil', () => {
end Sub
Sub RedLines_AddLine(id, position, coords, node, childMap) as Object
if RBS_SM_1_getMocksByFunctionName()["redlines_addline"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["redlines_addline"].callback(id,position,coords,node,childMap)
if RBS_SM_2_getMocksByFunctionName()["redlines_addline"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["redlines_addline"].callback(id,position,coords,node,childMap)
return result
end if
line = CreateObject("roSGNode", "Rectangle")
line.setField("id", id)
end sub
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -180,17 +180,17 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`sub sayHello(a1, a2)
if RBS_SM_1_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["sayhello"].callback(a1,a2)
if RBS_SM_2_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["sayhello"].callback(a1,a2)
return
end if
print "hello"
end sub
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -211,17 +211,17 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`function person_utils_sayHello(a1, a2)
if RBS_SM_1_getMocksByFunctionName()["person_utils_sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["person_utils_sayhello"].callback(a1,a2)
if RBS_SM_2_getMocksByFunctionName()["person_utils_sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["person_utils_sayhello"].callback(a1,a2)
return result
end if
print "hello"
end function
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -242,17 +242,17 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`sub person_utils_sayHello(a1, a2)
if RBS_SM_1_getMocksByFunctionName()["person_utils_sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["person_utils_sayhello"].callback(a1,a2)
if RBS_SM_2_getMocksByFunctionName()["person_utils_sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["person_utils_sayhello"].callback(a1,a2)
return
end if
print "hello"
end sub
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -273,7 +273,7 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`function __Person_builder()
instance = {}
@@ -310,7 +310,7 @@ describe('MockUtil', () => {
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();
let a = getContents('source/code.brs');
let b = trimLeading(`function __beings_Person_builder()
instance = {}
@@ -328,22 +328,22 @@ describe('MockUtil', () => {
end function
function beings_sayHello()
if RBS_SM_1_getMocksByFunctionName()["beings_sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["beings_sayhello"].callback()
if RBS_SM_2_getMocksByFunctionName()["beings_sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["beings_sayhello"].callback()
return result
end if
print "hello2"
end function
function sayHello()
if RBS_SM_1_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_1_getMocksByFunctionName()["sayhello"].callback()
if RBS_SM_2_getMocksByFunctionName()["sayhello"] <> invalid
result = RBS_SM_2_getMocksByFunctionName()["sayhello"].callback()
return result
end if
print "hello3"
end function
function RBS_SM_1_getMocksByFunctionName()
function RBS_SM_2_getMocksByFunctionName()
if m._rMocksByFunctionName = invalid
m._rMocksByFunctionName = {}
end if
@@ -369,7 +369,7 @@ describe('MockUtil', () => {
program.setFile('source/code.coverageExcluded.bs', source);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
await builder.build();

let a = getContents('source/code.coverageExcluded.brs');
let b = `sub foo()
10 changes: 8 additions & 2 deletions bsc-plugin/src/lib/rooibos/MockUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { BrsFile, Editor, ProgramBuilder } from 'brighterscript';
import { Position, isClassStatement } from 'brighterscript';
import { Position, isClassStatement, isVariableExpression } from 'brighterscript';
import * as brighterscript from 'brighterscript';
import type { RooibosConfig } from './RooibosConfig';
import { RawCodeStatement } from './RawCodeStatement';
@@ -88,7 +88,13 @@ export class MockUtil {
}
const paramNames = functionStatement.func.parameters.map((param) => param.name.text).join(',');

const returnStatement = ((functionStatement.func.functionType?.kind === brighterscript.TokenKind.Sub && (functionStatement.func.returnTypeToken === undefined || functionStatement.func.returnTypeToken?.kind === brighterscript.TokenKind.Void)) || functionStatement.func.returnTypeToken?.kind === brighterscript.TokenKind.Void) ? 'return' : 'return result';
// const returnStatement = ((functionStatement.func.functionType?.kind === brighterscript.TokenKind.Sub && (functionStatement.func.returnTypeToken === undefined || functionStatement.func.returnTypeToken?.kind === brighterscript.TokenKind.Void)) || functionStatement.func.returnTypeToken?.kind === brighterscript.TokenKind.Void) ? 'return' : 'return result';
let returnStatement = 'return';
if (isVariableExpression(functionStatement?.func?.returnTypeExpression?.expression)) {
if ((functionStatement.func.returnTypeExpression.expression).name.text.toLowerCase() !== 'void') {
returnStatement = 'return result';
}
}
this.editor.addToArray(functionStatement.func.body.statements, 0, new RawCodeStatement(undent`
if RBS_SM_${this.fileId}_getMocksByFunctionName()["${methodName}"] <> invalid
result = RBS_SM_${this.fileId}_getMocksByFunctionName()["${methodName}"].callback(${paramNames})
30 changes: 0 additions & 30 deletions bsc-plugin/src/lib/rooibos/RawCodeExpression.ts

This file was deleted.

Loading

0 comments on commit cc3aec3

Please sign in to comment.