Skip to content

Commit

Permalink
Merge pull request #3576 from onflow/supun/export-tests
Browse files Browse the repository at this point in the history
Add tests for exporting interface type
  • Loading branch information
SupunS authored Sep 10, 2024
2 parents ac0c01c + 7a0ff08 commit 83742cc
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5447,3 +5447,108 @@ func TestRuntimeDeploymentResultTypeImportExport(t *testing.T) {
)
})
}

func TestRuntimeExportInterfaceType(t *testing.T) {

t.Parallel()

t.Run("exportable interface, exportable implementation", func(t *testing.T) {

t.Parallel()

script := `
access(all) struct interface I {}
access(all) struct S: I {}
access(all) fun main(): {I} {
return S()
}
`

actual := exportValueFromScript(t, script)
expected := cadence.NewStruct([]cadence.Value{}).
WithType(cadence.NewStructType(
common.ScriptLocation{},
"S",
[]cadence.Field{},
nil,
))

assert.Equal(t, expected, actual)
})

t.Run("exportable interface, non exportable implementation", func(t *testing.T) {

t.Parallel()

script := `
access(all) struct interface I {}
access(all) struct S: I {
access(self) var a: Block?
init() {
self.a = nil
}
}
access(all) fun main(): {I} {
return S()
}
`

rt := NewTestInterpreterRuntime()

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

// Dynamically validated
notExportableError := &ValueNotExportableError{}
require.ErrorAs(t, err, &notExportableError)
})

t.Run("non exportable interface, non exportable implementation", func(t *testing.T) {

t.Parallel()

script := `
access(all) struct interface I {
access(all) var a: Block?
}
access(all) struct S: I {
access(all) var a: Block?
init() {
self.a = nil
}
}
access(all) fun main(): {I} {
return S()
}
`

rt := NewTestInterpreterRuntime()

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

// Statically validated
invalidReturnType := &InvalidScriptReturnTypeError{}
require.ErrorAs(t, err, &invalidReturnType)
})
}

0 comments on commit 83742cc

Please sign in to comment.