Skip to content

Commit

Permalink
Add the Test.assertError() helper function
Browse files Browse the repository at this point in the history
This matcher function asserts that the result of an executed operation,
such as scripts and transactions, resulted in an error that contains
a given sub-string.
  • Loading branch information
m-Peter committed Aug 8, 2023
1 parent 346aebf commit 09caabb
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
28 changes: 28 additions & 0 deletions runtime/stdlib/contracts/test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ pub contract Test {
/// The resulted status of an executed operation.
///
pub let status: ResultStatus

/// The optionally resulted error of an executed operation.
///
pub let error: Error?
}

/// The result of a transaction execution.
Expand Down Expand Up @@ -346,4 +350,28 @@ pub contract Test {
})
}

/// Asserts that the result of an executed operation, such as
/// scripts & transactions, has errored and the error message
/// contains the given sub-string.
///
pub fun assertError(_ result: {Result}, errorMessage: String) {
pre {
result.status == ResultStatus.failed: "no error was found"
}

var found = false
let msg = result.error!.message
let msgLength = msg.length - errorMessage.length + 1
var i = 0
while i < msgLength {
if msg.slice(from: i, upTo: i + errorMessage.length) == errorMessage {
found = true
break
}
i = i + 1
}

assert(found, message: "the error message did not contain the given sub-string")
}

}
106 changes: 106 additions & 0 deletions runtime/stdlib/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,112 @@ func TestTestBeFailedMatcher(t *testing.T) {
})
}

func TestTestAssertErrorMatcher(t *testing.T) {

t.Parallel()

t.Run("with ScriptResult", func(t *testing.T) {
t.Parallel()

const script = `
import Test
pub fun testMatch() {
let result = Test.ScriptResult(
status: Test.ResultStatus.failed,
returnValue: nil,
error: Test.Error("computation exceeding limit")
)
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoMatch() {
let result = Test.ScriptResult(
status: Test.ResultStatus.failed,
returnValue: nil,
error: Test.Error("computation exceeding memory")
)
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoError() {
let result = Test.ScriptResult(
status: Test.ResultStatus.succeeded,
returnValue: 42,
error: nil
)
Test.assertError(result, errorMessage: "exceeding limit")
}
`

inter, err := newTestContractInterpreter(t, script)
require.NoError(t, err)

_, err = inter.Invoke("testMatch")
require.NoError(t, err)

_, err = inter.Invoke("testNoMatch")
require.Error(t, err)
assert.ErrorContains(t, err, "the error message did not contain the given sub-string")

_, err = inter.Invoke("testNoError")
require.Error(t, err)
assert.ErrorContains(t, err, "no error was found")
})

t.Run("with TransactionResult", func(t *testing.T) {
t.Parallel()

const script = `
import Test
pub fun testMatch() {
let result = Test.TransactionResult(
status: Test.ResultStatus.failed,
error: Test.Error("computation exceeding limit")
)
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoMatch() {
let result = Test.TransactionResult(
status: Test.ResultStatus.failed,
error: Test.Error("computation exceeding memory")
)
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoError() {
let result = Test.TransactionResult(
status: Test.ResultStatus.succeeded,
error: nil
)
Test.assertError(result, errorMessage: "exceeding limit")
}
`

inter, err := newTestContractInterpreter(t, script)
require.NoError(t, err)

_, err = inter.Invoke("testMatch")
require.NoError(t, err)

_, err = inter.Invoke("testNoMatch")
require.Error(t, err)
assert.ErrorContains(t, err, "the error message did not contain the given sub-string")

_, err = inter.Invoke("testNoError")
require.Error(t, err)
assert.ErrorContains(t, err, "no error was found")
})
}

func TestTestBeNilMatcher(t *testing.T) {

t.Parallel()
Expand Down

0 comments on commit 09caabb

Please sign in to comment.