-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle unpure generated values (1st commit)
- Loading branch information
Showing
14 changed files
with
414 additions
and
13 deletions.
There are no files selected for viewing
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 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,44 @@ | ||
import { cloneMethod } from '../symbols'; | ||
import { constant } from './ConstantArbitrary'; | ||
import { Arbitrary } from './definition/Arbitrary'; | ||
|
||
/** | ||
* Interface for IContext instances | ||
*/ | ||
export interface IContext { | ||
/** | ||
* Log execution details during a test. | ||
* Very helpful when troubleshooting failures | ||
* @param data Data to be logged into the current context | ||
*/ | ||
log(data: string): void; | ||
/** | ||
* Number of logs already logged into current context | ||
*/ | ||
size(): number; | ||
} | ||
|
||
/** @hidden */ | ||
class ContextImplem implements IContext { | ||
private readonly receivedLogs: string[]; | ||
constructor() { | ||
this.receivedLogs = []; | ||
} | ||
log(data: string): void { | ||
this.receivedLogs.push(data); | ||
} | ||
size(): number { | ||
return this.receivedLogs.length; | ||
} | ||
toString() { | ||
return JSON.stringify({ logs: this.receivedLogs }); | ||
} | ||
[cloneMethod]() { | ||
return new ContextImplem(); | ||
} | ||
} | ||
|
||
/** | ||
* Produce a {@link IContext} instance | ||
*/ | ||
export const context = () => constant(new ContextImplem()) as Arbitrary<IContext>; |
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 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 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 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 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 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,25 @@ | ||
/** | ||
* Generated instances having a method [cloneMethod] | ||
* will be automatically cloned whenever necessary | ||
* | ||
* This is pretty useful for statefull generated values. | ||
* For instance, whenever you use a Stream you directly impact it. | ||
* Implementing [cloneMethod] on the generated Stream would force | ||
* the framework to clone it whenever it has to re-use it | ||
* (mainly required for chrinking process) | ||
*/ | ||
export const cloneMethod = Symbol.for('fast-check/cloneMethod'); | ||
|
||
/** @hidden */ | ||
export interface WithCloneMethod<T> { | ||
[cloneMethod]: () => T; | ||
} | ||
|
||
/** @hidden */ | ||
export const hasCloneMethod = <T>(instance: T | WithCloneMethod<T>): instance is WithCloneMethod<T> => { | ||
// Valid values for `instanceof Object`: | ||
// [], {}, () => {}, function() {}, async () => {}, async function() {} | ||
// Invalid ones: | ||
// 1, "", Symbol(), null, undefined | ||
return instance instanceof Object && typeof (instance as any)[cloneMethod] === 'function'; | ||
}; |
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
Oops, something went wrong.