Skip to content

Commit

Permalink
[ReleaseBranch] Substitute v2-beta: Rework substitution logic (#139)
Browse files Browse the repository at this point in the history
* add ts-node

* create linked list classes

* minor refactoring

* add root substitute graph

* replace context with contextNode

* add recorded arguments class

* add working returns example

* update dependencies

* add compile key to ava config

* refactor existing interfaces

This includes SubstituteBase, SubstituteException, Arguments, Utilities

* replace linked list implementations with recorder + node

* rework substitute implementation

* refactor Arguments.ts

* Substitute v2-beta: Tests (#231)

* move existing tests to regression folder

* add RecordedArguments spec

* create Utilities spec

* Improve perfomance: implement RecordsSet (#232)

* improve proxy creation function

* use node contexts to simplify node logic

* implement custom records set

RecordsSet implements the higher order filter and map methods which get applied when retrieving the iterator. This increases performance as it doesn't create arrays on each .map or .filter -> the iterator yields only the end values with one iteration

* Add clear substitute (#233)

resolves #46 

* implement clearSubstitute

* add clearSubstitute spec

* 2.0.0-beta.0

* refactor and add recorder related specs (#236)

* Update package.json

Co-authored-by: Mathias Lykkegaard Lorenzen <[email protected]>
  • Loading branch information
notanengineercom and ffMathy authored Jun 2, 2022
1 parent 52a6965 commit 6e3011a
Show file tree
Hide file tree
Showing 51 changed files with 2,431 additions and 2,039 deletions.
591 changes: 348 additions & 243 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fluffy-spoon/substitute",
"version": "1.0.0",
"version": "2.0.0-beta.1",
"description": "TypeScript port of NSubstitute, which aims to provide a much more fluent mocking opportunity for strong-typed languages",
"license": "MIT",
"funding": {
Expand Down Expand Up @@ -45,19 +45,20 @@
},
"dependencies": {},
"devDependencies": {
"@ava/typescript": "^1.1.0",
"@types/node": "^16.4.10",
"@ava/typescript": "^2.0.0",
"@types/node": "^12.20.24",
"ava": "^3.15.0",
"typescript": "^4.3.5"
"typescript": "^4.4.3"
},
"ava": {
"typescript": {
"rewritePaths": {
"/": "dist/"
}
},
"compile": false
},
"cache": false,
"failFast": true,
"failWithoutAssertions": true
}
}
}
111 changes: 0 additions & 111 deletions spec/Arguments.spec.ts

This file was deleted.

65 changes: 65 additions & 0 deletions spec/ClearSubstitute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import test from 'ava'

import { Substitute, SubstituteOf } from '../src'
import { SubstituteBase } from '../src/SubstituteBase'
import { SubstituteNode } from '../src/SubstituteNode'

interface Calculator {
add(a: number, b: number): number
subtract(a: number, b: number): number
divide(a: number, b: number): number
isEnabled: boolean
}

type InstanceReturningSubstitute<T> = SubstituteOf<T> & {
[SubstituteBase.instance]: Substitute
}

test('clears everything on a substitute', t => {
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
calculator.add(1, 1)
calculator.received().add(1, 1)
calculator.clearSubstitute()

t.is(calculator[Substitute.instance].recorder.records.size, 0)
t.is(calculator[Substitute.instance].recorder.indexedRecords.size, 0)

t.throws(() => calculator.received().add(1, 1))

// explicitly using 'all'
calculator.add(1, 1)
calculator.received().add(1, 1)
calculator.clearSubstitute('all')

t.is(calculator[Substitute.instance].recorder.records.size, 0)
t.is(calculator[Substitute.instance].recorder.indexedRecords.size, 0)

t.throws(() => calculator.received().add(1, 1))
})

test('clears received calls on a substitute', t => {
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
calculator.add(1, 1)
calculator.add(1, 1).returns(2)
calculator.clearSubstitute('receivedCalls')

t.is(calculator[Substitute.instance].recorder.records.size, 2)
t.is(calculator[Substitute.instance].recorder.indexedRecords.size, 2)

t.throws(() => calculator.received().add(1, 1))
t.is(calculator.add(1, 1), 2)
})

test('clears return values on a substitute', t => {
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
calculator.add(1, 1)
calculator.add(1, 1).returns(2)
calculator.clearSubstitute('substituteValues')

t.is(calculator[Substitute.instance].recorder.records.size, 2)
t.is(calculator[Substitute.instance].recorder.indexedRecords.size, 2)

t.notThrows(() => calculator.received().add(1, 1))
// @ts-expect-error
t.true(calculator.add(1, 1)[SubstituteBase.instance] instanceof SubstituteNode)
})
138 changes: 138 additions & 0 deletions spec/RecordedArguments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import test from 'ava'
import { inspect } from 'util'

import { Arg } from '../src'
import { RecordedArguments } from '../src/RecordedArguments'

const testObject = { 'foo': 'bar' }
const testArray = ['a', 1, true]

// #90: Infinite recursion in deepEqual https://github.com/ffMathy/FluffySpoon.JavaScript.Testing.Faking/blob/master/spec/issues/90.test.ts
const parent = {} as any
parent.child = parent
const root = {} as any
root.path = { to: { nested: root } }

const testFunc = () => { }
const testSymbol = Symbol()

test('records values and classifies them correctly', t => {
const emptyArguments = RecordedArguments.from([])
t.deepEqual(emptyArguments.value, [])
t.is(emptyArguments.argumentsClass, 'plain')
t.is(emptyArguments.hasNoArguments, false)

const primitivesOnlyArguments = RecordedArguments.from([1, 'Substitute', false, testSymbol, undefined, null, testFunc, {}])
t.deepEqual(primitivesOnlyArguments.value, [1, 'Substitute', false, testSymbol, undefined, null, testFunc, {}])
t.is(primitivesOnlyArguments.argumentsClass, 'plain')
t.is(primitivesOnlyArguments.hasNoArguments, false)

const anyArg = Arg.any('any')
const withSingleArgumentArguments = RecordedArguments.from([1, 'Substitute', false, testSymbol, undefined, null, testFunc, {}, anyArg])
t.deepEqual(withSingleArgumentArguments.value, [1, 'Substitute', false, testSymbol, undefined, null, testFunc, {}, anyArg])
t.is(withSingleArgumentArguments.argumentsClass, 'with-predicate')
t.is(withSingleArgumentArguments.hasNoArguments, false)

const allArg = Arg.all()
const allArgumentArguments = RecordedArguments.from([allArg])
t.deepEqual(allArgumentArguments.value, [allArg])
t.is(allArgumentArguments.argumentsClass, 'wildcard')
t.is(allArgumentArguments.hasNoArguments, false)
})

test('creates a valid instance for no arguments', t => {
const args = RecordedArguments.none()

t.is(args.value, undefined)
t.is(args.argumentsClass, undefined)
t.is(args.hasNoArguments, true)
})

test('sorts correctly objects with RecordedArguments', t => {
const plain1 = RecordedArguments.from([])
const plain2 = RecordedArguments.from([1, 2])
const withPredicate1 = RecordedArguments.from([1, Arg.any()])
const withPredicate2 = RecordedArguments.from([Arg.any()])
const wildcard1 = RecordedArguments.from([Arg.all()])
const wildcard2 = RecordedArguments.from([Arg.all()])

const wrapper = (recordedArguments: RecordedArguments[]) => recordedArguments.map(args => ({ recordedArguments: args }))
const sortedArgs1 = RecordedArguments.sort(wrapper([wildcard1, wildcard2, withPredicate1, withPredicate2, plain1, plain2]))
const sortedArgs2 = RecordedArguments.sort(wrapper([wildcard1, withPredicate1, plain1, withPredicate2, wildcard2, plain2]))

t.deepEqual(sortedArgs1, wrapper([plain1, plain2, withPredicate1, withPredicate2, wildcard1, wildcard2]))
t.deepEqual(sortedArgs2, wrapper([plain1, plain2, withPredicate1, withPredicate2, wildcard1, wildcard2]))
})

test('matches correctly with another RecordedArguments instance when none arguments are recorded', t => {
const args = RecordedArguments.none()

t.true(args.match(args))
t.true(args.match(RecordedArguments.none()))

t.false(args.match(RecordedArguments.from([])))
t.false(RecordedArguments.from([]).match(args))
t.false(args.match(RecordedArguments.from([undefined])))
})

test('matches correctly with another RecordedArguments instance when primitive arguments are recorded', t => {
// single
t.true(RecordedArguments.from([]).match(RecordedArguments.from([])))
t.true(RecordedArguments.from(['Substitute']).match(RecordedArguments.from(['Substitute'])))
t.true(RecordedArguments.from([0]).match(RecordedArguments.from([0])))
t.true(RecordedArguments.from([true]).match(RecordedArguments.from([true])))
t.true(RecordedArguments.from([false]).match(RecordedArguments.from([false])))
t.true(RecordedArguments.from([undefined]).match(RecordedArguments.from([undefined])))
t.true(RecordedArguments.from([null]).match(RecordedArguments.from([null])))
t.true(RecordedArguments.from([Symbol.for('test')]).match(RecordedArguments.from([Symbol.for('test')])))

t.false(RecordedArguments.from(['a']).match(RecordedArguments.from(['b'])))
t.false(RecordedArguments.from([1]).match(RecordedArguments.from([2])))
t.false(RecordedArguments.from([true]).match(RecordedArguments.from([false])))
t.false(RecordedArguments.from([undefined]).match(RecordedArguments.from([null])))
t.false(RecordedArguments.from(['1']).match(RecordedArguments.from([1])))

// multi
t.true(RecordedArguments.from([1, 2, 3]).match(RecordedArguments.from([1, 2, 3])))

t.false(RecordedArguments.from([1, 2, 3]).match(RecordedArguments.from([3, 2, 1])))
t.false(RecordedArguments.from([1, 2, 3]).match(RecordedArguments.from([1, 2, 3, 4])))
t.false(RecordedArguments.from([1, 2, 3, 4]).match(RecordedArguments.from([1, 2, 3])))
})

test('matches correctly with another RecordedArguments instance when object arguments are recorded', t => {
// same reference
t.true(RecordedArguments.from([testObject]).match(RecordedArguments.from([testObject])))
t.true(RecordedArguments.from([testArray]).match(RecordedArguments.from([testArray])))
t.true(RecordedArguments.from([testFunc]).match(RecordedArguments.from([testFunc])))
t.true(RecordedArguments.from([parent]).match(RecordedArguments.from([parent])))
t.true(RecordedArguments.from([root]).match(RecordedArguments.from([root])))

// deep equal
const objectWithSelfReference = { a: 1, b: 2 } as any
objectWithSelfReference.c = objectWithSelfReference
const anotherObjectWithSelfReference = { a: 1, b: 2 } as any
anotherObjectWithSelfReference.c = anotherObjectWithSelfReference

t.true(RecordedArguments.from([{ a: 1 }]).match(RecordedArguments.from([{ a: 1 }])))
t.true(RecordedArguments.from([[]]).match(RecordedArguments.from([[]])))
t.true(RecordedArguments.from([[1, 'a']]).match(RecordedArguments.from([[1, 'a']])))
t.true(RecordedArguments.from([objectWithSelfReference]).match(RecordedArguments.from([anotherObjectWithSelfReference])))
})

test('matches correctly with another RecordedArguments instance when using a wildcard argument', t => {
t.true(RecordedArguments.from([Arg.all()]).match(RecordedArguments.from([1, 2, 3])))
t.true(RecordedArguments.from(['Substitute', 'JS']).match(RecordedArguments.from([Arg.all()])))
})

test('matches correctly with another RecordedArguments instance when using predicate arguments', t => {
t.true(RecordedArguments.from([Arg.any(), Arg.any('number'), Arg.is((x: number) => x === 3), 4]).match(RecordedArguments.from([1, 2, 3, 4])))
t.true(RecordedArguments.from(['Substitute', 'JS']).match(RecordedArguments.from([Arg.is(x => typeof x === 'string'), Arg.any('string')])))
})

test('generates custom text representation', t => {
t.is(inspect(RecordedArguments.none()), '')
t.is(inspect(RecordedArguments.from([])), '()')
t.is(inspect(RecordedArguments.from([undefined])), 'undefined')
t.is(inspect(RecordedArguments.from([undefined, 1])), '(undefined, 1)')
})
Loading

0 comments on commit 6e3011a

Please sign in to comment.