Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ef92b07
add ts-node
notanengineercom Oct 11, 2020
3ceef2f
create linked list classes
notanengineercom Oct 11, 2020
990b743
minor refactoring
notanengineercom Oct 11, 2020
c2322c2
add root substitute graph
notanengineercom Oct 11, 2020
de52c77
replace context with contextNode
notanengineercom Oct 11, 2020
cc5f11b
add recorded arguments class
notanengineercom Oct 11, 2020
329a40a
add working returns example
notanengineercom Oct 11, 2020
08df650
Merge branch 'master' into v2-beta
notanengineercom Sep 11, 2021
6bf0838
update dependencies
notanengineercom Sep 11, 2021
612d7fa
add compile key to ava config
notanengineercom Sep 11, 2021
68517e4
refactor existing interfaces
notanengineercom Sep 12, 2021
fd08599
replace linked list implementations with recorder + node
notanengineercom Sep 12, 2021
53e0683
rework substitute implementation
notanengineercom Sep 12, 2021
a4342bf
refactor Arguments.ts
notanengineercom Sep 12, 2021
77a2032
Substitute v2-beta: Tests (#231)
notanengineercom Oct 3, 2021
636f3c5
Improve perfomance: implement RecordsSet (#232)
notanengineercom Oct 3, 2021
42a137d
Add clear substitute (#233)
notanengineercom Oct 3, 2021
0de266a
Merge branch 'master' into v2-beta
notanengineercom Oct 4, 2021
20f0808
2.0.0-beta.0
notanengineercom Oct 4, 2021
bfcb908
Merge branch 'master' into v2-beta
notanengineercom Oct 21, 2021
ebda33a
Merge branch 'master' into v2-beta
notanengineercom Oct 24, 2021
0bb0590
refactor and add recorder related specs (#236)
notanengineercom Jun 2, 2022
b553b3e
Update package.json
ffMathy Jun 2, 2022
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
Prev Previous commit
Next Next commit
replace context with contextNode
  • Loading branch information
notanengineercom committed Oct 11, 2020
commit de52c77402d806e813c82bcf42790778bd4c8ee0
128 changes: 0 additions & 128 deletions src/Context.ts

This file was deleted.

122 changes: 122 additions & 0 deletions src/ContextNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { Substitute } from './Substitute'
import { PropertyType, isSubstituteMethod } from './Utilities'
import { SubstituteJS as SubstituteBase, SubstituteException } from './SubstituteBase'
import { RecordedArguments } from './RecordedArguments'

import { Node } from './linked-list/Node'

export class ContextNode extends Node<Substitute, ContextNode> {
private _property: PropertyKey
private _propertyType: PropertyType = PropertyType.property
private _arguments: RecordedArguments = RecordedArguments.noArguments()

private _hasSubstitution: boolean = false
private _isSubstitution: boolean = false

private _proxy: typeof SubstituteBase

constructor(property: PropertyKey) {
super()

this._property = property
const nodeProxy: typeof SubstituteBase = new Proxy(SubstituteBase, {
apply: (_target, _this, rawArguments?: any[]) => {
this.handleMethod(rawArguments)
return this.returnProxyOrSubstitution()
},
getPrototypeOf() {
return SubstituteBase.prototype
},
// set: (_target, property, value) => (this.initialState.set(this, property, value), true),
get: (_target, property: PropertyKey) => {
if (property === 'prototype') Reflect.get(_target, property)

const newNode = this.createNewNode(property)
this.child = newNode

return newNode.proxy
}
})

this._proxy = nodeProxy
}

public labelAsSubstitution(): void {
this._isSubstitution = true
}

get isSubstitution(): boolean {
return this._isSubstitution
}

public enableSubstitution(): void {
this._hasSubstitution = true
}

get hasSubstitution(): boolean {
return this._hasSubstitution
}

get property() {
return this._property
}

get propertyType() {
return this._propertyType
}

get proxy() {
return this._proxy
}

get arguments() {
return this._arguments
}

public returnProxyOrSubstitution() {
const allSubstitutions = this.findAllSubstitutions()
const mostSuitableSubstitution = this.getMostSuitableSubstitution(allSubstitutions)

return mostSuitableSubstitution instanceof ContextNode ?
mostSuitableSubstitution.executeSubstitution() :
this.proxy
}

public executeSubstitution() {
return this.child.arguments.value[0] // Todo: support all substitutions, and multiple mockvalues
}

private createNewNode(property: PropertyKey): ContextNode {
const newNode = new ContextNode(property)
newNode.parent = this
newNode.head = this.head

return newNode
}

private handleMethod(rawArguments: any[]): void {
this._propertyType = PropertyType.method
this._arguments = RecordedArguments.from(rawArguments)

if (isSubstituteMethod(this.property)) {
this.labelAsSubstitution()
if (this.isIntermediateNode()) this.parent.enableSubstitution()
}
}

private findAllSubstitutions(): ContextNode[] {
const firstNode = this.head
const root = firstNode.parent

const allSiblings = root.getSiblingsOf(firstNode)
return allSiblings.filter(node => node.hasSubstitution &&
node.propertyType === this.propertyType &&
node.arguments.match(this.arguments)
)
}

private getMostSuitableSubstitution(nodes: ContextNode[]): ContextNode {
const sortedNodes = RecordedArguments.sort(nodes)
return sortedNodes[0]
}
}
2 changes: 1 addition & 1 deletion src/Substitute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContextNode } from './Context'
import { ContextNode } from './ContextNode'
import { Graph } from './linked-list/Graph'
import { ObjectSubstitute, OmitProxyMethods } from './Transformations'

Expand Down