From bbba3191e719a31151fa3e5faf08c98f468a00f3 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Mon, 5 Jul 2021 16:01:15 +0530 Subject: [PATCH] Add tests for inferring static type from imported arrays/dictionaries --- runtime/convertValues_test.go | 132 ++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/runtime/convertValues_test.go b/runtime/convertValues_test.go index d548b2699f..ca1717f529 100644 --- a/runtime/convertValues_test.go +++ b/runtime/convertValues_test.go @@ -1837,6 +1837,59 @@ func TestImportExportArrayValue(t *testing.T) { actual, ) }) + + t.Run("import nested array with broader expected type", func(t *testing.T) { + + t.Parallel() + + value := cadence.NewArray([]cadence.Value{ + cadence.NewArray([]cadence.Value{ + cadence.NewInt8(4), + cadence.NewInt8(3), + }), + cadence.NewArray([]cadence.Value{ + cadence.NewInt8(42), + cadence.NewInt8(54), + }), + }) + + inter, err := interpreter.NewInterpreter( + nil, + utils.TestLocation, + ) + require.NoError(t, err) + + actual := importValue( + inter, + value, + sema.AnyStructType, + ) + + assert.Equal(t, + interpreter.NewArrayValueUnownedNonCopying( + interpreter.VariableSizedStaticType{ + Type: interpreter.VariableSizedStaticType{ + Type: interpreter.PrimitiveStaticTypeInt8, + }, + }, + interpreter.NewArrayValueUnownedNonCopying( + interpreter.VariableSizedStaticType{ + Type: interpreter.PrimitiveStaticTypeInt8, + }, + interpreter.Int8Value(4), + interpreter.Int8Value(3), + ), + interpreter.NewArrayValueUnownedNonCopying( + interpreter.VariableSizedStaticType{ + Type: interpreter.PrimitiveStaticTypeInt8, + }, + interpreter.Int8Value(42), + interpreter.Int8Value(54), + ), + ), + actual, + ) + }) } func TestImportExportDictionaryValue(t *testing.T) { @@ -1950,6 +2003,85 @@ func TestImportExportDictionaryValue(t *testing.T) { actual, ) }) + + t.Run("import nested dictionary with broader expected type", func(t *testing.T) { + + t.Parallel() + + value := cadence.NewDictionary([]cadence.KeyValuePair{ + { + Key: cadence.NewString("a"), + Value: cadence.NewDictionary([]cadence.KeyValuePair{ + { + Key: cadence.NewInt(1), + Value: cadence.NewInt(100), + }, + { + Key: cadence.NewInt(2), + Value: cadence.NewString("hello"), + }, + }), + }, + { + Key: cadence.NewString("b"), + Value: cadence.NewDictionary([]cadence.KeyValuePair{ + { + Key: cadence.NewInt(1), + Value: cadence.NewString("foo"), + }, + { + Key: cadence.NewInt(2), + Value: cadence.NewInt(50), + }, + }), + }, + }) + + inter, err := interpreter.NewInterpreter( + nil, + utils.TestLocation, + ) + require.NoError(t, err) + + actual := importValue( + inter, + value, + sema.AnyStructType, + ) + + assert.Equal(t, + interpreter.NewDictionaryValueUnownedNonCopying( + interpreter.DictionaryStaticType{ + KeyType: interpreter.PrimitiveStaticTypeString, + ValueType: interpreter.DictionaryStaticType{ + KeyType: interpreter.PrimitiveStaticTypeInt, + ValueType: interpreter.PrimitiveStaticTypeAnyStruct, + }, + }, + + interpreter.NewStringValue("a"), + interpreter.NewDictionaryValueUnownedNonCopying( + interpreter.DictionaryStaticType{ + KeyType: interpreter.PrimitiveStaticTypeInt, + ValueType: interpreter.PrimitiveStaticTypeAnyStruct, + }, + interpreter.NewIntValueFromInt64(1), interpreter.NewIntValueFromInt64(100), + interpreter.NewIntValueFromInt64(2), interpreter.NewStringValue("hello"), + ), + + interpreter.NewStringValue("b"), + interpreter.NewDictionaryValueUnownedNonCopying( + interpreter.DictionaryStaticType{ + KeyType: interpreter.PrimitiveStaticTypeInt, + ValueType: interpreter.PrimitiveStaticTypeAnyStruct, + }, + interpreter.NewIntValueFromInt64(1), interpreter.NewStringValue("foo"), + interpreter.NewIntValueFromInt64(2), interpreter.NewIntValueFromInt64(50), + ), + ), + actual, + ) + }) } func TestStringValueImport(t *testing.T) {