Ejemplo n.º 1
0
    def testEncodeResource(self):
        foo_resource_type = cadence.ResourceType(
            _test_location,
            "Foo",
            [
                cadence.Field(
                    "uuid",
                    type(cadence.UInt64),
                ),
                cadence.Field(
                    "bar",
                    type(cadence.Int),
                ),
            ],
        )

        simple_resource = _EncodeTestParams(
            "Simple",
            cadence.Resource(
                [cadence.UInt64(0), cadence.Int(42)], foo_resource_type),
            """
            {
              "type": "Resource",
              "value": {
                "id": "S.test.Foo",
                "fields": [
                  { "name": "uuid", "value": { "type": "UInt64", "value": "0" } },
                  { "name": "bar", "value": { "type": "Int", "value": "42" } }
                ]
              }
            }
            """,
        )

        bar_resource_type = cadence.ResourceType(
            _test_location,
            "Bar",
            [
                cadence.Field(
                    "uuid",
                    type(cadence.UInt64),
                ),
                cadence.Field(
                    "x",
                    type(cadence.Int),
                ),
            ],
        )

        foo_resource_type = cadence.ResourceType(
            _test_location,
            "Foo",
            [
                cadence.Field(
                    "uuid",
                    type(cadence.UInt64),
                ),
                cadence.Field(
                    "bar",
                    bar_resource_type,
                ),
            ],
        )

        nested_resource = _EncodeTestParams(
            "Nested resource",
            cadence.Resource(
                [
                    cadence.UInt64(0),
                    cadence.Resource([cadence.UInt64(0),
                                      cadence.Int(42)], bar_resource_type),
                ],
                foo_resource_type,
            ),
            """
            {
              "type": "Resource",
              "value": {
                "id": "S.test.Foo",
                "fields": [
                  { "name": "uuid", "value": { "type": "UInt64", "value": "0" } },
                  {
                    "name": "bar",
                    "value": {
                      "type": "Resource",
                      "value": {
                        "id": "S.test.Bar",
                        "fields": [
                          { "name": "uuid", "value": { "type": "UInt64", "value": "0" } },
                          { "name": "x", "value": { "type": "Int", "value": "42" } }
                        ]
                      }
                    }
                  }
                ]
              }
            }
            """,
        )

        self._encodeAndDecodeAll([simple_resource, nested_resource])
Ejemplo n.º 2
0
    def testEncodeContract(self):
        simple_contract_type = cadence.ContractType(
            _test_location,
            "FooContract",
            [
                cadence.Field(
                    "a",
                    type(cadence.Int),
                ),
                cadence.Field(
                    "b",
                    type(cadence.String),
                ),
            ],
        )
        simple_contract = _EncodeTestParams(
            "Simple",
            cadence.Contract(
                [cadence.Int(1), cadence.String("foo")], simple_contract_type),
            """
            {
              "type": "Contract",
              "value": {
                "id": "S.test.FooContract",
                "fields": [
                  { "name": "a", "value": { "type": "Int", "value": "1" } },
                  { "name": "b", "value": { "type": "String", "value": "foo" } }
                ]
              }
            }
            """,
        )

        resource_contract_type = cadence.ContractType(
            _test_location,
            "FooContract",
            [
                cadence.Field(
                    "a",
                    type(cadence.Int),
                ),
                cadence.Field(
                    "b",
                    _foo_resource_type,
                ),
            ],
        )

        resource_contract = _EncodeTestParams(
            "Resources",
            cadence.Contract(
                [
                    cadence.String("foo"),
                    cadence.Resource([cadence.Int(42)], _foo_resource_type),
                ],
                resource_contract_type,
            ),
            """
            {
              "type": "Contract",
              "value": {
                "id": "S.test.FooContract",
                "fields": [
                  { "name": "a", "value": { "type": "String", "value": "foo" } },
                  {
                    "name": "b",
                    "value": {
                      "type": "Resource",
                      "value": {
                        "id": "S.test.Foo",
                        "fields": [
                          { "name": "bar", "value": { "type": "Int", "value": "42" } }
                        ]
                      }
                    }
                  }
                ]
              }
            }
            """,
        )

        self._encodeAndDecodeAll([simple_contract, resource_contract])
Ejemplo n.º 3
0
        self._encodeAndDecodeAll([capability])


# func TestEncodeCapability(t *testing.T) {
#
# 	t.Parallel()
#
# 	testEncodeAndDecode(
# 		t,
# 		cadence.Capability{
# 			Path:       cadence.Path{Domain: "storage", Identifier: "foo"},
# 			Address:    cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
# 			BorrowType: "Int",
# 		},
# 		`{"type":"Capability","value":{"path":{"type":"Path","value":{"domain":"storage","identifier":"foo"}},"borrowType":"Int","address":"0x0000000102030405"}}`,
# 	)
# }

_test_location = cadence.StringLocation("test")

_foo_resource_type = cadence.ResourceType(
    _test_location,
    "Foo",
    [
        cadence.Field(
            "bar",
            type(cadence.Int),
        ),
    ],
)