Skip to content

Commit

Permalink
fixup! Add the Test.assertError() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Aug 7, 2023
1 parent aee08f0 commit 600ed65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
11 changes: 6 additions & 5 deletions runtime/stdlib/contracts/test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -451,23 +451,24 @@ pub contract Test {
/// scripts & transactions, has errored and the error message
/// contains the given sub-string.
///
pub fun assertError(_ result: {Result}, substr: String) {
pub fun assertError(_ result: {Result}, errorMessage: String) {
pre {
result.status == ResultStatus.failed: "no error was found"
}

var found: Bool = false
var found = false
let msg = result.error!.message
let msgLength = msg.length - substr.length + 1
let msgLength = msg.length - errorMessage.length + 1
var i = 0
while i < msgLength {
if msg[i] == substr[0] && msg.slice(from: i, upTo: i + substr.length) == substr {
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 substring")
assert(found, message: "the error message did not contain the given sub-string")
}

}
16 changes: 8 additions & 8 deletions runtime/stdlib/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {
error: Test.Error("computation exceeding limit")
)
Test.assertError(result, substr: "exceeding limit")
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoMatch() {
Expand All @@ -1181,7 +1181,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {
error: Test.Error("computation exceeding memory")
)
Test.assertError(result, substr: "exceeding limit")
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoError() {
Expand All @@ -1191,7 +1191,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {
error: nil
)
Test.assertError(result, substr: "exceeding limit")
Test.assertError(result, errorMessage: "exceeding limit")
}
`

Expand All @@ -1203,7 +1203,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {

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

_, err = inter.Invoke("testNoError")
require.Error(t, err)
Expand All @@ -1222,7 +1222,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {
error: Test.Error("computation exceeding limit")
)
Test.assertError(result, substr: "exceeding limit")
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoMatch() {
Expand All @@ -1231,7 +1231,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {
error: Test.Error("computation exceeding memory")
)
Test.assertError(result, substr: "exceeding limit")
Test.assertError(result, errorMessage: "exceeding limit")
}
pub fun testNoError() {
Expand All @@ -1240,7 +1240,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {
error: nil
)
Test.assertError(result, substr: "exceeding limit")
Test.assertError(result, errorMessage: "exceeding limit")
}
`

Expand All @@ -1252,7 +1252,7 @@ func TestTestAssertErrorMatcher(t *testing.T) {

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

_, err = inter.Invoke("testNoError")
require.Error(t, err)
Expand Down

0 comments on commit 600ed65

Please sign in to comment.