Skip to content

Commit

Permalink
Add native function declarations for all revelent Test contract funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
m-Peter committed Sep 28, 2023
1 parent 7c7eb74 commit b290058
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
67 changes: 67 additions & 0 deletions runtime/stdlib/contracts/test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,71 @@ access(all) contract Test {
assert(found, message: "the error message did not contain the given sub-string")
}

/// Creates a matcher with a test function.
/// The test function is of type '((T): Bool)',
/// where 'T' is bound to 'AnyStruct'.
///
access(all)
native fun newMatcher<T: AnyStruct>(_ test: ((T): Bool)): Test.Matcher {}

/// Wraps a function call in a closure, and expects it to fail with
/// an error message that contains the given error message portion.
///
access(all)
native fun expectFailure(
_ functionWrapper: ((): Void),
errorMessageSubstring: String
) {}

/// Expect function tests a value against a matcher
/// and fails the test if it's not a match.
///
access(all)
native fun expect<T: AnyStruct>(_ value: T, _ matcher: Test.Matcher) {}

/// Returns a matcher that succeeds if the tested
/// value is equal to the given value.
///
access(all)
native fun equal<T: AnyStruct>(_ value: T): Test.Matcher {}

/// Fails the test-case if the given values are not equal, and
/// reports a message which explains how the two values differ.
///
access(all)
native fun assertEqual(_ expected: AnyStruct, _ actual: AnyStruct) {}

/// Returns a matcher that succeeds if the tested value is
/// an array or dictionary and the tested value contains
/// no elements.
///
access(all)
native fun beEmpty(): Test.Matcher {}

/// Returns a matcher that succeeds if the tested value is
/// an array or dictionary and has the given number of elements.
///
access(all)
native fun haveElementCount(_ count: Int): Test.Matcher {}

/// Returns a matcher that succeeds if the tested value is
/// an array that contains a value that is equal to the given
/// value, or the tested value is a dictionary that contains
/// an entry where the key is equal to the given value.
///
access(all)
native fun contain(_ element: AnyStruct): Test.Matcher {}

/// Returns a matcher that succeeds if the tested value
/// is a number and greater than the given number.
///
access(all)
native fun beGreaterThan(_ value: Number): Test.Matcher {}

/// Returns a matcher that succeeds if the tested value
/// is a number and less than the given number.
///
access(all)
native fun beLessThan(_ value: Number): Test.Matcher {}

}
10 changes: 7 additions & 3 deletions runtime/stdlib/test_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,10 @@ func newTestContractType() *TestContractType {
program, err := parser.ParseProgram(
nil,
contracts.TestContract,
parser.Config{},
parser.Config{
NativeModifierEnabled: true,
TypeParametersEnabled: true,
},
)
if err != nil {
panic(err)
Expand All @@ -965,8 +968,9 @@ func newTestContractType() *TestContractType {
TestContractLocation,
nil,
&sema.Config{
BaseValueActivation: activation,
AccessCheckMode: sema.AccessCheckModeStrict,
BaseValueActivation: activation,
AccessCheckMode: sema.AccessCheckModeStrict,
AllowNativeDeclarations: true,
},
)
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions runtime/stdlib/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func newTestContractInterpreterWithTestFramework(
program, err := parser.ParseProgram(
nil,
[]byte(code),
parser.Config{},
parser.Config{
NativeModifierEnabled: true,
TypeParametersEnabled: true,
},
)
require.NoError(t, err)

Expand All @@ -70,8 +73,9 @@ func newTestContractInterpreterWithTestFramework(
utils.TestLocation,
nil,
&sema.Config{
BaseValueActivation: activation,
AccessCheckMode: sema.AccessCheckModeStrict,
BaseValueActivation: activation,
AccessCheckMode: sema.AccessCheckModeStrict,
AllowNativeDeclarations: true,
ImportHandler: func(
checker *sema.Checker,
importedLocation common.Location,
Expand Down Expand Up @@ -931,7 +935,7 @@ func TestAssertEqual(t *testing.T) {
pub fun test() {
let foo = Foo()
let bar <- create Bar()
Test.expect(foo, Test.equal(<-bar))
Test.assertEqual(foo, <-bar)
}
pub struct Foo {}
Expand Down

0 comments on commit b290058

Please sign in to comment.