Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more built-in matchers to the Cadence Testing Framework #2420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 49 additions & 3 deletions runtime/stdlib/contracts/test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,30 @@ pub contract Test {
pub case failed
}

/// Result is the interface to be implemented by the various execution
/// operations, such as transactions and scripts.
///
pub struct interface Result {
/// The resulted status of an executed operation.
///
pub let status: ResultStatus
}

/// The result of a transaction execution.
///
pub struct TransactionResult {
pub struct TransactionResult: Result {
pub let status: ResultStatus
pub let error: Error?

init(status: ResultStatus, error: Error) {
init(status: ResultStatus, error: Error?) {
self.status = status
self.error = error
}
}

/// The result of a script execution.
///
pub struct ScriptResult {
pub struct ScriptResult: Result {
pub let status: ResultStatus
pub let returnValue: AnyStruct?
pub let error: Error?
Expand Down Expand Up @@ -250,4 +259,41 @@ pub contract Test {
///
pub fun useConfiguration(_ configuration: Configuration)
}

/// Returns a new matcher that negates the test of the given matcher.
///
pub fun not(_ matcher: Matcher): Matcher {
return Matcher(test: fun (value: AnyStruct): Bool {
return !matcher.test(value)
})
}

/// Returns a new matcher that checks if the given test value is either
/// a ScriptResult or TransactionResult and the ResultStatus is succeeded.
/// Returns false in any other case.
///
pub fun beSucceeded(): Matcher {
return Matcher(test: fun (value: AnyStruct): Bool {
return (value as! {Result}).status == ResultStatus.succeeded
})
}

/// Returns a new matcher that checks if the given test value is either
/// a ScriptResult or TransactionResult and the ResultStatus is failed.
/// Returns false in any other case.
///
pub fun beFailed(): Matcher {
return Matcher(test: fun (value: AnyStruct): Bool {
return (value as! {Result}).status == ResultStatus.failed
})
}

/// Returns a new matcher that checks if the given test value is nil.
///
pub fun beNil(): Matcher {
return Matcher(test: fun (value: AnyStruct): Bool {
return value == nil
})
}

}
Loading