Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: integrate typepool in sampler
  • Loading branch information
dstallenberg committed Jul 31, 2023
commit 5299f3d55ef65ee4bc173ab3a00f3646b9027f76
20 changes: 16 additions & 4 deletions libraries/analysis-javascript/lib/RootContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { TypeModel } from "./type/resolving/TypeModel";
import { Element } from "./type/discovery/element/Element";
import { DiscoveredObjectType } from "./type/discovery/object/DiscoveredType";
import { Relation } from "./type/discovery/relation/Relation";
import { TypePool } from "./type/resolving/TypePool";

export class RootContext extends CoreRootContext<t.Node> {
protected _exportFactory: ExportFactory;
Expand All @@ -45,6 +46,7 @@ export class RootContext extends CoreRootContext<t.Node> {
protected _objectMap: Map<string, DiscoveredObjectType>;

protected _typeModel: TypeModel;
protected _typePool: TypePool;

// Mapping: filepath -> target name -> Exports
protected _exportMap: Map<string, Export[]>;
Expand Down Expand Up @@ -138,25 +140,35 @@ export class RootContext extends CoreRootContext<t.Node> {
}

resolveTypes(): void {
if (!this._typeModel) {
if (!this._elementMap || !this._relationMap || !this._objectMap) {
this.extractTypes();
}

if (!this._typeModel) {
this._typeModel = this._typeResolver.resolveTypes(
this._elementMap,
this._relationMap
); //, this._objectMap);
);
this._typePool = new TypePool(this._objectMap);
}
}

getTypeModel(): TypeModel {
if (!this._typeModel) {
this.extractTypes();
this.resolveTypes();
// or should this always be done beforehand?
}

return this._typeModel;
}

getTypePool(): TypePool {
if (!this._typePool) {
this.resolveTypes();
}

return this._typePool;
}

getElement(id: string): Element {
if (!this._elementMap || !this._elementMap.has(id)) {
this.extractTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import { prng } from "@syntest/search";
import { prng } from "@syntest/prng";
import { DiscoveredObjectType } from "../discovery/object/DiscoveredType";
import { ObjectType } from "./Type";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import {
ClassTarget,
DiscoveredObjectKind,
FunctionTarget,
getRelationName,
isExported,
Expand Down Expand Up @@ -664,6 +665,37 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
.getObjectDescription(type.split("<>")[0])
: this._rootContext.getTypeModel().getObjectDescription(id);

const typeFromTypePool = this._rootContext
.getTypePool()
.getRandomMatchingType(typeObject);
if (typeFromTypePool) {
// always prefer type from type pool
switch (typeFromTypePool.kind) {
case DiscoveredObjectKind.CLASS: {
return this.sampleSpecificClass(depth + 1, typeFromTypePool.id, name);
}
case DiscoveredObjectKind.FUNCTION: {
return this.sampleSpecificFunctionCall(
depth + 1,
typeFromTypePool.id,
name
);
}
case DiscoveredObjectKind.INTERFACE: {
// TODO
return this.sampleSpecificClass(depth + 1, typeFromTypePool.id, name);
}
case DiscoveredObjectKind.OBJECT: {
return this.sampleSpecificRootObject(
depth + 1,
typeFromTypePool.id,
name
);
}
// No default
}
}

const object_: { [key: string]: Statement } = {};

for (const [key, id] of typeObject.properties.entries()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ArrowFunctionStatement } from "../statements/complex/ArrowFunctionState
import { ArrayStatement } from "../statements/complex/ArrayStatement";
import { ObjectStatement } from "../statements/complex/ObjectStatement";
import { IntegerStatement } from "../statements/primitive/IntegerStatement";
import { FunctionCall } from "../statements/root/FunctionCall";

/**
* JavaScriptRandomSampler class
Expand Down Expand Up @@ -118,7 +119,7 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
id: string,
name: string,
type: string
): ObjectStatement;
): ObjectStatement | ConstructorCall | RootObject | FunctionCall;

abstract sampleArray(
depth: number,
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.