Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Oct 19, 2023
1 parent 047939d commit c94d3ff
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
68 changes: 68 additions & 0 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
144 changes: 144 additions & 0 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -2954,6 +2980,10 @@ func TestRuntimeComplexStructArgumentPassing(t *testing.T) {
Identifier: "j",
Type: cadence.AnyStructType{},
},
{
Identifier: "k",
Type: cadence.HashableStructType{},
},
},
},

Expand Down Expand Up @@ -2998,6 +3028,7 @@ func TestRuntimeComplexStructArgumentPassing(t *testing.T) {
Identifier: "foo",
},
cadence.String("foo"),
cadence.String("foo"),
},
}

Expand All @@ -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"
Expand All @@ -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"
}
}
`,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c94d3ff

Please sign in to comment.