diff --git a/libraries/search-javascript/lib/search/JavaScriptSubject.ts b/libraries/search-javascript/lib/search/JavaScriptSubject.ts index db303fb24..a7b30fee1 100644 --- a/libraries/search-javascript/lib/search/JavaScriptSubject.ts +++ b/libraries/search-javascript/lib/search/JavaScriptSubject.ts @@ -16,7 +16,14 @@ * limitations under the License. */ import { TargetType } from "@syntest/analysis"; -import { RootContext, SubTarget, Target } from "@syntest/analysis-javascript"; +import { + isExported, + MethodTarget, + ObjectFunctionTarget, + RootContext, + SubTarget, + Target, +} from "@syntest/analysis-javascript"; import { ControlFlowGraph, Edge, EdgeType } from "@syntest/cfg"; import { ApproachLevel, @@ -181,19 +188,38 @@ export class JavaScriptSubject extends SearchSubject { return childObjectives; } - getActionableTargets(): SubTarget[] { - return this._target.subTargets.filter((t) => { - return ( - t.type === TargetType.FUNCTION || - t.type === TargetType.CLASS || - t.type === TargetType.METHOD || - t.type === TargetType.OBJECT || - t.type === TargetType.OBJECT_FUNCTION - ); - }); + private _actions: SubTarget[]; + + getActions(): SubTarget[] { + if (!this._actions) { + this._actions = this._target.subTargets.filter((target) => { + return ( + (target.type === TargetType.FUNCTION && isExported(target)) || + (target.type === TargetType.CLASS && isExported(target)) || + (target.type === TargetType.OBJECT && isExported(target)) || + (target.type === TargetType.METHOD && + (target).methodType !== "constructor" && + isExported( + this._target.subTargets.find( + (classTarget) => + classTarget.id === (target).classId + ) + )) || // check whether parent class is exported + (target.type === TargetType.OBJECT_FUNCTION && + isExported( + this._target.subTargets.find( + (objectTarget) => + objectTarget.id === (target).objectId + ) + )) // check whether parent object is exported + ); + }); + } + + return this._actions; } - getActionableTargetsByType(type: TargetType): SubTarget[] { - return this.getActionableTargets().filter((t) => t.type === type); + getActionByType(type: TargetType): SubTarget[] { + return this.getActions().filter((action) => action.type === type); } } diff --git a/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts b/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts index d0c6f921d..02644adab 100644 --- a/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts +++ b/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts @@ -109,8 +109,6 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } sampleRoot(): ActionStatement { - const targets = (this._subject).getActionableTargets(); - if (this.statementPoolEnabled) { const constructor_ = this.statementPool.getRandomConstructor(); @@ -150,27 +148,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } const action = prng.pickOne( - targets.filter( - (target) => - (target.type === TargetType.FUNCTION && isExported(target)) || - (target.type === TargetType.CLASS && isExported(target)) || - (target.type === TargetType.OBJECT && isExported(target)) || - (target.type === TargetType.METHOD && - (target).methodType !== "constructor" && - isExported( - targets.find( - (classTarget) => - classTarget.id === (target).classId - ) - )) || // check whether parent class is exported - (target.type === TargetType.OBJECT_FUNCTION && - isExported( - targets.find( - (objectTarget) => - objectTarget.id === (target).objectId - ) - )) // check whether parent object is exported - ) + (this._subject).getActions() ); switch (action.type) { @@ -196,9 +174,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { // get a random function const function_ = ( prng.pickOne( - (this._subject) - .getActionableTargetsByType(TargetType.FUNCTION) - .filter((target) => isExported(target)) + (this._subject).getActionByType(TargetType.FUNCTION) ) ); @@ -216,7 +192,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { if (id) { const result = ( (this._subject) - .getActionableTargetsByType(TargetType.CLASS) + .getActionByType(TargetType.CLASS) .find((target) => (target).id === id) ); if (!result) { @@ -230,9 +206,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { // random return ( prng.pickOne( - (this._subject) - .getActionableTargetsByType(TargetType.CLASS) - .filter((target) => isExported(target)) + (this._subject).getActionByType(TargetType.CLASS) ) ); } @@ -243,7 +217,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { // get the constructor of the class const constructor_ = (this._subject) - .getActionableTargetsByType(TargetType.METHOD) + .getActionByType(TargetType.METHOD) .filter( (method) => (method).classId === class_.id && @@ -283,19 +257,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } override sampleClassAction(depth: number): MethodCall | Getter | Setter { - const targets = (this._subject).getActionableTargets(); - const methods = (this._subject) - .getActionableTargetsByType(TargetType.METHOD) - .filter( - (method) => - (method).methodType !== "constructor" && - isExported( - targets.find( - (classTarget) => classTarget.id === (method).classId - ) - ) - ); + .getActionByType(TargetType.METHOD) + .filter((method) => (method).methodType !== "constructor"); const randomMethod = prng.pickOne(methods); switch (randomMethod.methodType) { @@ -316,18 +280,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } override sampleMethodCall(depth: number): MethodCall { - const targets = (this._subject).getActionableTargets(); - const methods = (this._subject) - .getActionableTargetsByType(TargetType.METHOD) - .filter((method) => (method).methodType === "method") - .filter((target) => - isExported( - targets.find( - (objectTarget) => objectTarget.id === (target).classId - ) - ) - ); + .getActionByType(TargetType.METHOD) + .filter((method) => (method).methodType === "method"); const method = prng.pickOne(methods); const class_ = this._getClass(method.classId); @@ -343,18 +298,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } sampleGetter(depth: number): Getter { - const targets = (this._subject).getActionableTargets(); - const methods = (this._subject) - .getActionableTargetsByType(TargetType.METHOD) - .filter((method) => (method).methodType === "get") - .filter((target) => - isExported( - targets.find( - (objectTarget) => objectTarget.id === (target).classId - ) - ) - ); + .getActionByType(TargetType.METHOD) + .filter((method) => (method).methodType === "get"); const method = prng.pickOne(methods); const class_ = this._getClass(method.classId); @@ -370,18 +316,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } sampleSetter(depth: number): Setter { - const targets = (this._subject).getActionableTargets(); - const methods = (this._subject) - .getActionableTargetsByType(TargetType.METHOD) - .filter((method) => (method).methodType === "set") - .filter((target) => - isExported( - targets.find( - (objectTarget) => objectTarget.id === (target).classId - ) - ) - ); + .getActionByType(TargetType.METHOD) + .filter((method) => (method).methodType === "set"); const method = prng.pickOne(methods); const class_ = this._getClass(method.classId); @@ -400,7 +337,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { if (id) { const result = ( (this._subject) - .getActionableTargetsByType(TargetType.OBJECT) + .getActionByType(TargetType.OBJECT) .find((target) => (target).id === id) ); if (!result) { @@ -414,9 +351,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { // random return ( prng.pickOne( - (this._subject) - .getActionableTargetsByType(TargetType.OBJECT) - .filter((target) => isExported(target)) + (this._subject).getActionByType(TargetType.OBJECT) ) ); } @@ -436,18 +371,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } sampleObjectFunctionCall(depth: number): ObjectFunctionCall { - const targets = (this._subject).getActionableTargets(); - - const functions = (this._subject) - .getActionableTargetsByType(TargetType.OBJECT_FUNCTION) - .filter((target) => - isExported( - targets.find( - (objectTarget) => - objectTarget.id === (target).objectId - ) - ) - ); + const functions = (this._subject).getActionByType( + TargetType.OBJECT_FUNCTION + ); const randomFunction = prng.pickOne(functions); const object_ = this._getObject(randomFunction.objectId); diff --git a/tools/javascript/lib/JavaScriptLauncher.ts b/tools/javascript/lib/JavaScriptLauncher.ts index 2c7b343a7..007b78827 100644 --- a/tools/javascript/lib/JavaScriptLauncher.ts +++ b/tools/javascript/lib/JavaScriptLauncher.ts @@ -25,7 +25,6 @@ import { DependencyFactory, ExportFactory, InferenceTypeModelFactory, - isExported, RootContext, Target, TargetFactory, @@ -740,9 +739,9 @@ export class JavaScriptLauncher extends Launcher { this.arguments_.stringAlphabet ); - const rootTargets = currentSubject - .getActionableTargets() - .filter((target) => isExported(target)); + const rootTargets = currentSubject.getActions(); + + console.log(rootTargets); if (rootTargets.length === 0) { JavaScriptLauncher.LOGGER.info(