Skip to content

feat: improve targetting #188

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions libraries/search-javascript/lib/search/JavaScriptSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -181,19 +188,38 @@ export class JavaScriptSubject extends SearchSubject<JavaScriptTestCase> {
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 &&
(<MethodTarget>target).methodType !== "constructor" &&
isExported(
this._target.subTargets.find(
(classTarget) =>
classTarget.id === (<MethodTarget>target).classId
)
)) || // check whether parent class is exported
(target.type === TargetType.OBJECT_FUNCTION &&
isExported(
this._target.subTargets.find(
(objectTarget) =>
objectTarget.id === (<ObjectFunctionTarget>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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

sampleRoot(): ActionStatement {
const targets = (<JavaScriptSubject>this._subject).getActionableTargets();

if (this.statementPoolEnabled) {
const constructor_ = this.statementPool.getRandomConstructor();

Expand Down Expand Up @@ -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 &&
(<MethodTarget>target).methodType !== "constructor" &&
isExported(
targets.find(
(classTarget) =>
classTarget.id === (<MethodTarget>target).classId
)
)) || // check whether parent class is exported
(target.type === TargetType.OBJECT_FUNCTION &&
isExported(
targets.find(
(objectTarget) =>
objectTarget.id === (<ObjectFunctionTarget>target).objectId
)
)) // check whether parent object is exported
)
(<JavaScriptSubject>this._subject).getActions()
);

switch (action.type) {
Expand All @@ -196,9 +174,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
// get a random function
const function_ = <FunctionTarget>(
prng.pickOne(
(<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.FUNCTION)
.filter((target) => isExported(target))
(<JavaScriptSubject>this._subject).getActionByType(TargetType.FUNCTION)
)
);

Expand All @@ -216,7 +192,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
if (id) {
const result = <ClassTarget>(
(<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.CLASS)
.getActionByType(TargetType.CLASS)
.find((target) => (<ClassTarget>target).id === id)
);
if (!result) {
Expand All @@ -230,9 +206,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
// random
return <ClassTarget>(
prng.pickOne(
(<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.CLASS)
.filter((target) => isExported(target))
(<JavaScriptSubject>this._subject).getActionByType(TargetType.CLASS)
)
);
}
Expand All @@ -243,7 +217,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {

// get the constructor of the class
const constructor_ = (<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.METHOD)
.getActionByType(TargetType.METHOD)
.filter(
(method) =>
(<MethodTarget>method).classId === class_.id &&
Expand Down Expand Up @@ -283,19 +257,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

override sampleClassAction(depth: number): MethodCall | Getter | Setter {
const targets = (<JavaScriptSubject>this._subject).getActionableTargets();

const methods = (<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.METHOD)
.filter(
(method) =>
(<MethodTarget>method).methodType !== "constructor" &&
isExported(
targets.find(
(classTarget) => classTarget.id === (<MethodTarget>method).classId
)
)
);
.getActionByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType !== "constructor");

const randomMethod = <MethodTarget>prng.pickOne(methods);
switch (randomMethod.methodType) {
Expand All @@ -316,18 +280,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

override sampleMethodCall(depth: number): MethodCall {
const targets = (<JavaScriptSubject>this._subject).getActionableTargets();

const methods = (<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType === "method")
.filter((target) =>
isExported(
targets.find(
(objectTarget) => objectTarget.id === (<MethodTarget>target).classId
)
)
);
.getActionByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType === "method");

const method = <MethodTarget>prng.pickOne(methods);
const class_ = this._getClass(method.classId);
Expand All @@ -343,18 +298,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

sampleGetter(depth: number): Getter {
const targets = (<JavaScriptSubject>this._subject).getActionableTargets();

const methods = (<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType === "get")
.filter((target) =>
isExported(
targets.find(
(objectTarget) => objectTarget.id === (<MethodTarget>target).classId
)
)
);
.getActionByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType === "get");

const method = <MethodTarget>prng.pickOne(methods);
const class_ = this._getClass(method.classId);
Expand All @@ -370,18 +316,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

sampleSetter(depth: number): Setter {
const targets = (<JavaScriptSubject>this._subject).getActionableTargets();

const methods = (<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType === "set")
.filter((target) =>
isExported(
targets.find(
(objectTarget) => objectTarget.id === (<MethodTarget>target).classId
)
)
);
.getActionByType(TargetType.METHOD)
.filter((method) => (<MethodTarget>method).methodType === "set");

const method = <MethodTarget>prng.pickOne(methods);
const class_ = this._getClass(method.classId);
Expand All @@ -400,7 +337,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
if (id) {
const result = <ObjectTarget>(
(<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.OBJECT)
.getActionByType(TargetType.OBJECT)
.find((target) => (<ObjectTarget>target).id === id)
);
if (!result) {
Expand All @@ -414,9 +351,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
// random
return <ObjectTarget>(
prng.pickOne(
(<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.OBJECT)
.filter((target) => isExported(target))
(<JavaScriptSubject>this._subject).getActionByType(TargetType.OBJECT)
)
);
}
Expand All @@ -436,18 +371,9 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

sampleObjectFunctionCall(depth: number): ObjectFunctionCall {
const targets = (<JavaScriptSubject>this._subject).getActionableTargets();

const functions = (<JavaScriptSubject>this._subject)
.getActionableTargetsByType(TargetType.OBJECT_FUNCTION)
.filter((target) =>
isExported(
targets.find(
(objectTarget) =>
objectTarget.id === (<ObjectFunctionTarget>target).objectId
)
)
);
const functions = (<JavaScriptSubject>this._subject).getActionByType(
TargetType.OBJECT_FUNCTION
);

const randomFunction = <ObjectFunctionTarget>prng.pickOne(functions);
const object_ = this._getObject(randomFunction.objectId);
Expand Down
7 changes: 3 additions & 4 deletions tools/javascript/lib/JavaScriptLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
DependencyFactory,
ExportFactory,
InferenceTypeModelFactory,
isExported,
RootContext,
Target,
TargetFactory,
Expand Down Expand Up @@ -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(
Expand Down