-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ReleaseBranch] Substitute v2-beta: Rework substitution logic (#139)
* 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
1 parent
52a6965
commit 6e3011a
Showing
51 changed files
with
2,431 additions
and
2,039 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)') | ||
}) |
Oops, something went wrong.