Skip to content

Commit

Permalink
Add tests for inferring static type from imported arrays/dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jul 5, 2021
1 parent 1d26160 commit bbba319
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bbba319

Please sign in to comment.