From c94d3ffde4fae294b8f02960ffdb05bcca793506 Mon Sep 17 00:00:00 2001 From: darkdrag00n Date: Thu, 19 Oct 2023 22:28:04 +0530 Subject: [PATCH] Add test cases --- encoding/ccf/ccf_test.go | 68 ++++++++++++++++ runtime/convertValues_test.go | 144 ++++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) diff --git a/encoding/ccf/ccf_test.go b/encoding/ccf/ccf_test.go index 29c145678d..5970047e31 100644 --- a/encoding/ccf/ccf_test.go +++ b/encoding/ccf/ccf_test.go @@ -9319,6 +9319,74 @@ func TestEncodeType(t *testing.T) { 0x04, }, ) + + testEncodeAndDecode( + t, + cadence.TypeValue{ + StaticType: &cadence.FunctionType{ + TypeParameters: []cadence.TypeParameter{ + {Name: "T", TypeBound: cadence.HashableStructType{}}, + }, + Parameters: []cadence.Parameter{ + {Label: "qux", Identifier: "baz", Type: cadence.StringType{}}, + }, + ReturnType: cadence.IntType{}, + }, + }, + []byte{ + // language=json, format=json-cdc + // {"value":{"staticType":{"kind":"Function","typeParameters":[{"name":"T","typeBound":{"kind":"HashableStruct"}}],"parameters":[{"type":{"kind":"String"},"label":"qux","id":"baz"}],"return":{"kind":"Int"}}},"type":"Type"} + // + // language=edn, format=ccf + // 130([137(41), 193([[["T", 185(56)]], [["qux", "baz", 185(1)]], 185(4)])]) + // + // language=cbor, format=ccf + // tag + 0xd8, ccf.CBORTagTypeAndValue, + // array, 2 elements follow + 0x82, + // tag + 0xd8, ccf.CBORTagSimpleType, + // Meta type ID (41) + 0x18, 0x29, + // tag + 0xd8, ccf.CBORTagFunctionTypeValue, + // array, 3 elements follow + 0x83, + // array, 1 elements follow + 0x81, + // array, 2 elements follow + 0x82, + // string, 1 byte follows + 0x61, + // "T" + 0x54, + // tag + 0xd8, ccf.CBORTagSimpleTypeValue, + // HashableStruct type (56) + 0x18, 0x38, + // array, 1 elements follow + 0x81, + // array, 3 elements follow + 0x83, + // string, 3 bytes follow + 0x63, + // qux + 0x71, 0x75, 0x78, + // string, 3 bytes follow + 0x63, + // bax + 0x62, 0x61, 0x7a, + // tag + 0xd8, ccf.CBORTagSimpleTypeValue, + // String type (1) + 0x01, + // tag + 0xd8, ccf.CBORTagSimpleTypeValue, + // Int type ID (4) + 0x04, + }, + ) }) t.Run("with static function nil type bound", func(t *testing.T) { diff --git a/runtime/convertValues_test.go b/runtime/convertValues_test.go index a790573036..c72da0963b 100644 --- a/runtime/convertValues_test.go +++ b/runtime/convertValues_test.go @@ -217,6 +217,27 @@ func TestExportValue(t *testing.T) { ElementType: cadence.AnyStructType{}, }), }, + { + label: "Array (non-empty) with HashableStruct", + valueFactory: func(inter *interpreter.Interpreter) interpreter.Value { + return interpreter.NewArrayValue( + inter, + interpreter.EmptyLocationRange, + interpreter.VariableSizedStaticType{ + Type: interpreter.PrimitiveStaticTypeHashableStruct, + }, + common.ZeroAddress, + interpreter.NewUnmeteredIntValueFromInt64(42), + interpreter.NewUnmeteredStringValue("foo"), + ) + }, + expected: cadence.NewArray([]cadence.Value{ + cadence.NewInt(42), + cadence.String("foo"), + }).WithType(&cadence.VariableSizedArrayType{ + ElementType: cadence.HashableStructType{}, + }), + }, { label: "Dictionary", valueFactory: func(inter *interpreter.Interpreter) interpreter.Value { @@ -930,6 +951,11 @@ func TestImportRuntimeType(t *testing.T) { actual: cadence.AnyStructType{}, expected: interpreter.PrimitiveStaticTypeAnyStruct, }, + { + label: "HashableStruct", + actual: cadence.HashableStructType{}, + expected: interpreter.PrimitiveStaticTypeHashableStruct, + }, { label: "AnyResource", actual: cadence.AnyResourceType{}, @@ -2954,6 +2980,10 @@ func TestRuntimeComplexStructArgumentPassing(t *testing.T) { Identifier: "j", Type: cadence.AnyStructType{}, }, + { + Identifier: "k", + Type: cadence.HashableStructType{}, + }, }, }, @@ -2998,6 +3028,7 @@ func TestRuntimeComplexStructArgumentPassing(t *testing.T) { Identifier: "foo", }, cadence.String("foo"), + cadence.String("foo"), }, } @@ -3023,6 +3054,7 @@ func TestRuntimeComplexStructArgumentPassing(t *testing.T) { pub var h: PublicPath pub var i: PrivatePath pub var j: AnyStruct + pub var k: HashableStruct init() { self.a = "Hello" @@ -3035,6 +3067,7 @@ func TestRuntimeComplexStructArgumentPassing(t *testing.T) { self.h = /public/foo self.i = /private/foo self.j = nil + self.k = "hashable_struct_value" } } `, @@ -3160,6 +3193,117 @@ func TestRuntimeComplexStructWithAnyStructFields(t *testing.T) { assert.Equal(t, expected, actual) } +func TestRuntimeComplexStructWithHashableStructFields(t *testing.T) { + + t.Parallel() + + // Complex struct value + complexStructValue := cadence.Struct{ + StructType: &cadence.StructType{ + Location: common.ScriptLocation{}, + QualifiedIdentifier: "Foo", + Fields: []cadence.Field{ + { + Identifier: "a", + Type: &cadence.OptionalType{ + Type: cadence.HashableStructType{}, + }, + }, + { + Identifier: "b", + Type: &cadence.DictionaryType{ + KeyType: cadence.StringType{}, + ElementType: cadence.HashableStructType{}, + }, + }, + { + Identifier: "c", + Type: &cadence.VariableSizedArrayType{ + ElementType: cadence.HashableStructType{}, + }, + }, + { + Identifier: "d", + Type: &cadence.ConstantSizedArrayType{ + ElementType: cadence.HashableStructType{}, + Size: 2, + }, + }, + { + Identifier: "e", + Type: cadence.HashableStructType{}, + }, + }, + }, + + Fields: []cadence.Value{ + cadence.NewOptional(cadence.String("John")), + cadence.NewDictionary([]cadence.KeyValuePair{ + { + Key: cadence.String("name"), + Value: cadence.String("Doe"), + }, + }).WithType(&cadence.DictionaryType{ + KeyType: cadence.StringType{}, + ElementType: cadence.HashableStructType{}, + }), + cadence.NewArray([]cadence.Value{ + cadence.String("foo"), + cadence.String("bar"), + }).WithType(&cadence.VariableSizedArrayType{ + ElementType: cadence.HashableStructType{}, + }), + cadence.NewArray([]cadence.Value{ + cadence.String("foo"), + cadence.String("bar"), + }).WithType(&cadence.ConstantSizedArrayType{ + ElementType: cadence.HashableStructType{}, + Size: 2, + }), + cadence.Path{ + Domain: common.PathDomainStorage, + Identifier: "foo", + }, + }, + } + + script := fmt.Sprintf( + ` + pub fun main(arg: %[1]s): %[1]s { + + if !arg.isInstance(Type<%[1]s>()) { + panic("Not a %[1]s value") + } + + return arg + } + + pub struct Foo { + pub var a: HashableStruct? + pub var b: {String: HashableStruct} + pub var c: [HashableStruct] + pub var d: [HashableStruct; 2] + pub var e: HashableStruct + + init() { + self.a = "Hello" + self.b = {} + self.c = [] + self.d = ["foo", "bar"] + self.e = /storage/foo + } + } + `, + "Foo", + ) + + actual, err := executeTestScript(t, script, complexStructValue) + require.NoError(t, err) + + expected := cadence.ValueWithCachedTypeID(complexStructValue) + assert.Equal(t, expected, actual) +} + func TestRuntimeMalformedArgumentPassing(t *testing.T) { t.Parallel()