Skip to content

Commit

Permalink
Add test for exporting and type infering for DeploymentResult
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Sep 7, 2023
1 parent 2e852b4 commit d03e039
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
150 changes: 150 additions & 0 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5242,3 +5242,153 @@ func TestRuntimeDestroyedResourceReferenceExport(t *testing.T) {
require.Error(t, err)
require.ErrorAs(t, err, &interpreter.DestroyedResourceError{})
}

func TestRuntimeDeploymentResultValueImportExport(t *testing.T) {

t.Parallel()

t.Run("import", func(t *testing.T) {

t.Parallel()

script := `
access(all) fun main(v: DeploymentResult) {}
`

rt := newTestInterpreterRuntime()
runtimeInterface := &testRuntimeInterface{}

_, err := rt.ExecuteScript(
Script{
Source: []byte(script),
},
Context{
Interface: runtimeInterface,
Location: common.ScriptLocation{},
},
)

RequireError(t, err)

var notImportableError *ScriptParameterTypeNotImportableError
require.ErrorAs(t, err, &notImportableError)
})

t.Run("export", func(t *testing.T) {

t.Parallel()

script := `
access(all) fun main(): DeploymentResult? {
return nil
}
`

rt := newTestInterpreterRuntime()
runtimeInterface := &testRuntimeInterface{}

_, err := rt.ExecuteScript(
Script{
Source: []byte(script),
},
Context{
Interface: runtimeInterface,
Location: common.ScriptLocation{},
},
)

RequireError(t, err)

var invalidReturnTypeError *InvalidScriptReturnTypeError
require.ErrorAs(t, err, &invalidReturnTypeError)
})
}

func TestRuntimeDeploymentResultTypeImportExport(t *testing.T) {

t.Parallel()

t.Run("import", func(t *testing.T) {

t.Parallel()

script := `
access(all) fun main(v: Type) {
assert(v == Type<DeploymentResult>())
}
`

rt := newTestInterpreterRuntime()

typeValue := cadence.NewTypeValue(&cadence.StructType{
QualifiedIdentifier: "DeploymentResult",
Fields: []cadence.Field{
{
Type: cadence.NewOptionalType(cadence.DeployedContractType),
Identifier: "deployedContract",
},
},
})

encodedArg, err := json.Encode(typeValue)
require.NoError(t, err)

runtimeInterface := &testRuntimeInterface{}

runtimeInterface.decodeArgument = func(b []byte, t cadence.Type) (value cadence.Value, err error) {
return json.Decode(runtimeInterface, b)
}

_, err = rt.ExecuteScript(
Script{
Source: []byte(script),
Arguments: [][]byte{encodedArg},
},
Context{
Interface: runtimeInterface,
Location: common.ScriptLocation{},
},
)

require.NoError(t, err)
})

t.Run("export", func(t *testing.T) {

t.Parallel()

script := `
access(all) fun main(): Type {
return Type<DeploymentResult>()
}
`

rt := newTestInterpreterRuntime()
runtimeInterface := &testRuntimeInterface{}

result, err := rt.ExecuteScript(
Script{
Source: []byte(script),
},
Context{
Interface: runtimeInterface,
Location: common.ScriptLocation{},
},
)

require.NoError(t, err)

assert.Equal(t,
cadence.NewTypeValue(&cadence.StructType{
QualifiedIdentifier: "DeploymentResult",
Fields: []cadence.Field{
{
Type: cadence.NewOptionalType(cadence.DeployedContractType),
Identifier: "deployedContract",
},
},
}),
result,
)
})
}
28 changes: 28 additions & 0 deletions runtime/tests/checker/type_inference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,31 @@ func TestCheckCompositeSupertypeInference(t *testing.T) {
assert.Equal(t, expectedType.ID(), intersectionType.ID())
})
}

func TestCheckDeploymentResultInference(t *testing.T) {

t.Parallel()

code := `
let x: DeploymentResult = getDeploymentResult()
let y: DeploymentResult = getDeploymentResult()
// Function is just to get a 'DeploymentResult' return type.
fun getDeploymentResult(): DeploymentResult {
let v: DeploymentResult? = nil
return v!
}
let z = [x, y]
`

checker, err := ParseAndCheck(t, code)
require.NoError(t, err)

zType := RequireGlobalValue(t, checker.Elaboration, "z")

require.IsType(t, &sema.VariableSizedType{}, zType)
variableSizedType := zType.(*sema.VariableSizedType)

assert.Equal(t, sema.DeploymentResultType, variableSizedType.Type)
}

0 comments on commit d03e039

Please sign in to comment.