Skip to content

Commit

Permalink
Add test case with multiple imports in the same test file
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Oct 4, 2023
1 parent fda31d8 commit c1e14d2
Showing 1 changed file with 97 additions and 2 deletions.
99 changes: 97 additions & 2 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,6 @@ func TestImportContract(t *testing.T) {
import BarContract from "./BarContract"
import FooContract from "./FooContract"
pub let account = Test.getAccount(0x0000000000000005)
pub fun setup() {
var err = Test.deployContract(
name: "BarContract",
Expand Down Expand Up @@ -586,6 +584,103 @@ func TestImportContract(t *testing.T) {
require.NoError(t, result.Error)
})

t.Run("multiple imports", func(t *testing.T) {
t.Parallel()

const code = `
import Test
import FooContract from "./FooContract"
import BarContract from "./BarContract"
pub fun setup() {
var err = Test.deployContract(
name: "BarContract",
path: "./BarContract",
arguments: []
)
Test.expect(err, Test.beNil())
err = Test.deployContract(
name: "FooContract",
path: "./FooContract",
arguments: []
)
Test.expect(err, Test.beNil())
}
pub fun test() {
Test.assertEqual("Hi from BarContract", BarContract.sayHi())
Test.assertEqual("Hi from FooContract", FooContract.sayHi())
}
`

const fooContract = `
pub contract FooContract {
init() {}
pub fun sayHi(): String {
return "Hi from FooContract"
}
}
`

const barContract = `
pub contract BarContract {
init() {}
pub fun sayHi(): String {
return "Hi from BarContract"
}
}
`

importResolver := func(location common.Location) (string, error) {
switch location := location.(type) {
case common.AddressLocation:
if location.Name == "FooContract" {
return fooContract, nil
}
if location.Name == "BarContract" {
return barContract, nil
}
case common.StringLocation:
if location == "./FooContract" {
return fooContract, nil
}
if location == "./BarContract" {
return barContract, nil
}
}

return "", fmt.Errorf("unsupported import %s", location)
}

fileResolver := func(path string) (string, error) {
switch path {
case "./FooContract":
return fooContract, nil
case "./BarContract":
return barContract, nil
default:
return "", fmt.Errorf("cannot find file path: %s", path)
}
}

contracts := map[string]common.Address{
"BarContract": {0, 0, 0, 0, 0, 0, 0, 5},
"FooContract": {0, 0, 0, 0, 0, 0, 0, 5},
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithContracts(contracts).
WithFileResolver(fileResolver)

result, err := runner.RunTest(code, "test")
require.NoError(t, err)
require.NoError(t, result.Error)
})

t.Run("undeployed contract", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c1e14d2

Please sign in to comment.