From 7a0ff0802551821f28c3b10a53ba03c231c196c4 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Mon, 9 Sep 2024 14:27:46 -0700 Subject: [PATCH] Add tests for exporting interface type --- runtime/convertValues_test.go | 105 ++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/runtime/convertValues_test.go b/runtime/convertValues_test.go index 70690fd80f..770fff2f16 100644 --- a/runtime/convertValues_test.go +++ b/runtime/convertValues_test.go @@ -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, ¬ExportableError) + }) + + 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) + }) +}