Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Nov 7, 2023
1 parent 5bdcb9b commit 1906080
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 4 deletions.
11 changes: 9 additions & 2 deletions migrations/account_type/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (m *AccountTypeMigration) migrateValue(value interpreter.Value) (newValue i

// Value was migrated
if keyValue.newValue != nil {
value = keyValue.newValue
// Always wrap with an optional, when inserting to the dictionary.
value = interpreter.NewUnmeteredSomeValueNonCopying(keyValue.newValue)
}

dictionary.SetKey(m.interpreter, locationRange, key, value)
Expand Down Expand Up @@ -227,7 +228,7 @@ func (m *AccountTypeMigration) maybeConvertAccountType(staticType interpreter.St
convertedKeyType := m.maybeConvertAccountType(staticType.KeyType)
convertedValueType := m.maybeConvertAccountType(staticType.ValueType)
if convertedKeyType != nil && convertedValueType != nil {
return interpreter.NewDictionaryStaticType(nil, staticType.KeyType, staticType.ValueType)
return interpreter.NewDictionaryStaticType(nil, convertedKeyType, convertedValueType)
}
if convertedKeyType != nil {
return interpreter.NewDictionaryStaticType(nil, convertedKeyType, staticType.ValueType)
Expand Down Expand Up @@ -275,6 +276,12 @@ func (m *AccountTypeMigration) maybeConvertAccountType(staticType interpreter.St
*interpreter.InterfaceStaticType:
// Nothing to do

case primitiveStaticTypeWrapper:
// This is for testing the migration.
// i.e: wrapper was only to make it possible to use as a dictionary-key.
// Ignore the wrapper, and continue with the inner type.
return m.maybeConvertAccountType(staticType.PrimitiveStaticType)

default:
// Is it safe to do so?
switch staticType {
Expand Down
134 changes: 132 additions & 2 deletions migrations/account_type/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestMigration(t *testing.T) {
"variable_sized_string_array": {
storedType: interpreter.NewVariableSizedStaticType(nil, stringType),
},
"dictionary": {
"dictionary_with_account_type_value": {
storedType: interpreter.NewDictionaryStaticType(
nil,
stringType,
Expand All @@ -163,6 +163,30 @@ func TestMigration(t *testing.T) {
authAccountReferenceType,
),
},
"dictionary_with_account_type_key": {
storedType: interpreter.NewDictionaryStaticType(
nil,
authAccountType,
stringType,
),
expectedType: interpreter.NewDictionaryStaticType(
nil,
authAccountReferenceType,
stringType,
),
},
"dictionary_with_account_type_key_and_value": {
storedType: interpreter.NewDictionaryStaticType(
nil,
authAccountType,
authAccountType,
),
expectedType: interpreter.NewDictionaryStaticType(
nil,
authAccountReferenceType,
authAccountReferenceType,
),
},
"string_dictionary": {
storedType: interpreter.NewDictionaryStaticType(
nil,
Expand Down Expand Up @@ -473,7 +497,7 @@ func TestNestedTypeValueMigration(t *testing.T) {
interpreter.PrimitiveStaticTypeAnyStruct,
),
interpreter.NewUnmeteredInt8Value(4),
interpreter.NewUnmeteredSomeValueNonCopying(storedAccountTypeValue),
storedAccountTypeValue,
),
expectedValue: interpreter.NewDictionaryValue(
inter,
Expand All @@ -484,9 +508,115 @@ func TestNestedTypeValueMigration(t *testing.T) {
interpreter.PrimitiveStaticTypeAnyStruct,
),
interpreter.NewUnmeteredInt8Value(4),
expectedAccountTypeValue,
),
},
"dictionary_with_optional_account_type_value": {
storedValue: interpreter.NewDictionaryValue(
inter,
locationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeInt8,
interpreter.NewOptionalStaticType(nil, interpreter.PrimitiveStaticTypeMetaType),
),
interpreter.NewUnmeteredInt8Value(4),
interpreter.NewUnmeteredSomeValueNonCopying(storedAccountTypeValue),
),
expectedValue: interpreter.NewDictionaryValue(
inter,
locationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeInt8,
interpreter.NewOptionalStaticType(nil, interpreter.PrimitiveStaticTypeMetaType),
),
interpreter.NewUnmeteredInt8Value(4),
interpreter.NewUnmeteredSomeValueNonCopying(expectedAccountTypeValue),
),
},
"dictionary_with_account_type_key": {
storedValue: interpreter.NewDictionaryValue(
inter,
locationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeMetaType,
interpreter.PrimitiveStaticTypeInt8,
),
interpreter.NewTypeValue(
nil,
primitiveStaticTypeWrapper{
PrimitiveStaticType: interpreter.PrimitiveStaticTypePublicAccount,
},
),
interpreter.NewUnmeteredInt8Value(4),
),
expectedValue: interpreter.NewDictionaryValue(
inter,
locationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeMetaType,
interpreter.PrimitiveStaticTypeInt8,
),
expectedAccountTypeValue,
interpreter.NewUnmeteredInt8Value(4),
),
},
"dictionary_with_account_type_key_and_value": {
storedValue: interpreter.NewDictionaryValue(
inter,
locationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeMetaType,
interpreter.PrimitiveStaticTypeMetaType,
),
interpreter.NewTypeValue(
nil,
primitiveStaticTypeWrapper{
PrimitiveStaticType: interpreter.PrimitiveStaticTypePublicAccount,
},
),
storedAccountTypeValue,
),
expectedValue: interpreter.NewDictionaryValue(
inter,
locationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeMetaType,
interpreter.PrimitiveStaticTypeMetaType,
),
expectedAccountTypeValue,
expectedAccountTypeValue,
),
},
"composite_with_account_type": {
storedValue: interpreter.NewCompositeValue(
inter,
interpreter.EmptyLocationRange,
common.NewAddressLocation(nil, common.Address{0x42}, "Foo"),
"Bar",
common.CompositeKindResource,
[]interpreter.CompositeField{
interpreter.NewUnmeteredCompositeField("field", storedAccountTypeValue),
},
common.Address{},
),
expectedValue: interpreter.NewCompositeValue(
inter,
interpreter.EmptyLocationRange,
common.NewAddressLocation(nil, common.Address{0x42}, "Foo"),
"Bar",
common.CompositeKindResource,
[]interpreter.CompositeField{
interpreter.NewUnmeteredCompositeField("field", expectedAccountTypeValue),
},
common.Address{},
),
},
}

// Store values
Expand Down
37 changes: 37 additions & 0 deletions migrations/account_type/primitive_static_type_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package account_type

import (
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
)

// primitiveStaticTypeWrapper is just a wrapper for `interpreter.PrimitiveStaticType`
// with a custom `ID` function.
// This is only for testing the migration, since the `ID` function of `interpreter.PrimitiveStaticType`
// for deprecated types no longer work the original `ID` function relies on the `sema.Type`,
// but corresponding `sema.Type` for the deprecated primitive types are no longer available.
type primitiveStaticTypeWrapper struct {
interpreter.PrimitiveStaticType
}

func (t primitiveStaticTypeWrapper) ID() common.TypeID {
return common.TypeID(t.String())
}

0 comments on commit 1906080

Please sign in to comment.