Skip to content

Commit

Permalink
fix: rename some options
Browse files Browse the repository at this point in the history
  • Loading branch information
dstallenberg committed Aug 7, 2023
1 parent 839a766 commit 7e26cd0
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
constantPoolManager: ConstantPoolManager,
constantPoolEnabled: boolean,
constantPoolProbability: number,
typePoolEnabled: boolean,
typePoolProbability: number,
statementPoolEnabled: boolean,
statementPoolProbability: number,
typeInferenceMode: string,
randomTypeProbability: number,
incorporateExecutionInformation: boolean,
Expand All @@ -69,15 +73,17 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
stringMaxLength: number,
resampleGeneProbability: number,
deltaMutationProbability: number,
exploreIllegalValues: boolean,
reuseStatementProbability: number,
useMockedObjectProbability: number
exploreIllegalValues: boolean
) {
super(
subject,
constantPoolManager,
constantPoolEnabled,
constantPoolProbability,
typePoolEnabled,
typePoolProbability,
statementPoolEnabled,
statementPoolProbability,
typeInferenceMode,
randomTypeProbability,
incorporateExecutionInformation,
Expand All @@ -86,9 +92,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
stringMaxLength,
resampleGeneProbability,
deltaMutationProbability,
exploreIllegalValues,
reuseStatementProbability,
useMockedObjectProbability
exploreIllegalValues
);
}

Expand Down Expand Up @@ -473,10 +477,16 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
}

// take from pool
const statementFromPool = this.statementPool.getRandomStatement(chosenType);

if (statementFromPool && prng.nextBoolean(this.reuseStatementProbability)) {
return statementFromPool;
if (this.statementPoolEnabled) {
const statementFromPool =
this.statementPool.getRandomStatement(chosenType);

if (
statementFromPool &&
prng.nextBoolean(this.statementPoolProbability)
) {
return statementFromPool;
}
}

switch (chosenType) {
Expand Down Expand Up @@ -514,88 +524,88 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
.getTypeModel()
.getObjectDescription(typeId);

const typeFromTypePool = this.rootContext
.getTypePool()
// .getRandomMatchingType(typeObject)
// TODO this prevents the sampling of function return types
// maybe we want this actually?
.getRandomMatchingType(
typeObject,
(type_) => type_.kind !== DiscoveredObjectKind.FUNCTION
);
if (this.typePoolEnabled) {
// TODO maybe we should sample from the typepool for the other stuff as well (move this to sample arg for example)
const typeFromTypePool = this.rootContext
.getTypePool()
// .getRandomMatchingType(typeObject)
// TODO this prevents the sampling of function return types
// maybe we want this actually?
.getRandomMatchingType(
typeObject,
(type_) => type_.kind !== DiscoveredObjectKind.FUNCTION
);

if (
typeFromTypePool &&
prng.nextBoolean(1 - this.useMockedObjectProbability)
) {
// always prefer type from type pool
switch (typeFromTypePool.kind) {
case DiscoveredObjectKind.CLASS: {
// find constructor of class
const targets = this.rootContext.getSubTargets(
typeFromTypePool.id.split(":")[0]
);
const constructor_ = <MethodTarget>(
targets.find(
(target) =>
target.type === TargetType.METHOD &&
(<MethodTarget>target).methodType === "constructor" &&
(<MethodTarget>target).classId === typeFromTypePool.id
)
);
if (typeFromTypePool && prng.nextBoolean(this.typePoolProbability)) {
// always prefer type from type pool
switch (typeFromTypePool.kind) {
case DiscoveredObjectKind.CLASS: {
// find constructor of class
const targets = this.rootContext.getSubTargets(
typeFromTypePool.id.split(":")[0]
);
const constructor_ = <MethodTarget>(
targets.find(
(target) =>
target.type === TargetType.METHOD &&
(<MethodTarget>target).methodType === "constructor" &&
(<MethodTarget>target).classId === typeFromTypePool.id
)
);

if (constructor_) {
return this.constructorCallGenerator.generate(
depth,
id, // variable id
constructor_.typeId, // constructor call id
typeFromTypePool.id, // class export id
name,
this.statementPool
);
}

if (constructor_) {
return this.constructorCallGenerator.generate(
depth,
id, // variable id
constructor_.typeId, // constructor call id
typeFromTypePool.id, // constructor call id
typeFromTypePool.id, // class export id
name,
this.statementPool
);
}

return this.constructorCallGenerator.generate(
depth,
id, // variable id
typeFromTypePool.id, // constructor call id
typeFromTypePool.id, // class export id
name,
this.statementPool
);
}
case DiscoveredObjectKind.FUNCTION: {
return this.functionCallGenerator.generate(
depth,
id,
typeFromTypePool.id,
typeFromTypePool.id,
name,
this.statementPool
);
}
case DiscoveredObjectKind.INTERFACE: {
// TODO
return this.constructorCallGenerator.generate(
depth,
id,
typeFromTypePool.id,
typeFromTypePool.id,
name,
this.statementPool
);
}
case DiscoveredObjectKind.OBJECT: {
return this.constantObjectGenerator.generate(
depth,
id,
typeFromTypePool.id,
typeFromTypePool.id,
name,
this.statementPool
);
case DiscoveredObjectKind.FUNCTION: {
return this.functionCallGenerator.generate(
depth,
id,
typeFromTypePool.id,
typeFromTypePool.id,
name,
this.statementPool
);
}
case DiscoveredObjectKind.INTERFACE: {
// TODO
return this.constructorCallGenerator.generate(
depth,
id,
typeFromTypePool.id,
typeFromTypePool.id,
name,
this.statementPool
);
}
case DiscoveredObjectKind.OBJECT: {
return this.constantObjectGenerator.generate(
depth,
id,
typeFromTypePool.id,
typeFromTypePool.id,
name,
this.statementPool
);
}
// No default
}
// No default
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
private _constantPoolManager: ConstantPoolManager;
private _constantPoolEnabled: boolean;
private _constantPoolProbability: number;

private _typePoolEnabled: boolean;
private _typePoolProbability: number;

private _statementPoolEnabled: boolean;
private _statementPoolProbability: number;

private _typeInferenceMode: string;
private _randomTypeProbability: number;
private _incorporateExecutionInformation: boolean;
Expand All @@ -69,8 +76,6 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
private _resampleGeneProbability: number;
private _deltaMutationProbability: number;
private _exploreIllegalValues: boolean;
private _reuseStatementProbability: number;
private _useMockedObjectProbability: number;

private _statementPool: StatementPool | null;

Expand All @@ -89,6 +94,10 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
constantPoolManager: ConstantPoolManager,
constantPoolEnabled: boolean,
constantPoolProbability: number,
typePoolEnabled: boolean,
typePoolProbability: number,
statementPoolEnabled: boolean,
statementPoolProbability: number,
typeInferenceMode: string,
randomTypeProbability: number,
incorporateExecutionInformation: boolean,
Expand All @@ -97,14 +106,19 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
stringMaxLength: number,
resampleGeneProbability: number,
deltaMutationProbability: number,
exploreIllegalValues: boolean,
reuseStatementProbability: number,
useMockedObjectProbability: number
exploreIllegalValues: boolean
) {
super(subject);
this._constantPoolManager = constantPoolManager;
this._constantPoolEnabled = constantPoolEnabled;
this._constantPoolProbability = constantPoolProbability;

this._typePoolEnabled = typePoolEnabled;
this._typePoolProbability = typePoolProbability;

this._statementPoolEnabled = statementPoolEnabled;
this._statementPoolProbability = statementPoolProbability;

this._typeInferenceMode = typeInferenceMode;
this._randomTypeProbability = randomTypeProbability;
this._incorporateExecutionInformation = incorporateExecutionInformation;
Expand All @@ -114,8 +128,6 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
this._resampleGeneProbability = resampleGeneProbability;
this._deltaMutationProbability = deltaMutationProbability;
this._exploreIllegalValues = exploreIllegalValues;
this._reuseStatementProbability = reuseStatementProbability;
this._useMockedObjectProbability = useMockedObjectProbability;
}

get rootContext() {
Expand All @@ -128,37 +140,44 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
this._functionCallGenerator = new FunctionCallGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
this._constructorCallGenerator = new ConstructorCallGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
this._methodCallGenerator = new MethodCallGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
this._getterGenerator = new GetterGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
this._setterGenerator = new SetterGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
this._constantObjectGenerator = new ConstantObjectGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
this._objectFunctionCallGenerator = new ObjectFunctionCallGenerator(
this,
rootContext,
this.reuseStatementProbability
this._statementPoolEnabled,
this._statementPoolProbability
);
}

Expand Down Expand Up @@ -284,6 +303,22 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
return this._constantPoolProbability;
}

get typePoolEnabled(): boolean {
return this._typePoolEnabled;
}

get typePoolProbability(): number {
return this._typePoolProbability;
}

get statementPoolEnabled(): boolean {
return this._statementPoolEnabled;
}

get statementPoolProbability(): number {
return this._statementPoolProbability;
}

get typeInferenceMode(): string {
return this._typeInferenceMode;
}
Expand Down Expand Up @@ -319,12 +354,4 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
get exploreIllegalValues(): boolean {
return this._exploreIllegalValues;
}

get reuseStatementProbability(): number {
return this._reuseStatementProbability;
}

get useMockedObjectProbability(): number {
return this._useMockedObjectProbability;
}
}
Loading

0 comments on commit 7e26cd0

Please sign in to comment.