def testEncodeInt(self):
     self._encodeAndDecodeAll([
         _EncodeTestParams(
             "Negative",
             cadence.Int(-42),
             '{"type": "Int", "value": "-42"}',
         ),
         _EncodeTestParams(
             "Zero",
             cadence.Int(0),
             '{"type": "Int", "value": "0"}',
         ),
         _EncodeTestParams(
             "Positive",
             cadence.Int(42),
             '{"type": "Int", "value": "42"}',
         ),
         _EncodeTestParams(
             "SmallerThanMinInt256",
             cadence.Int(
                 -57896044618658097711785492504343953926634992332820282019728792003956564819978
             ),
             """{"type": "Int", 
             "value": "-57896044618658097711785492504343953926634992332820282019728792003956564819978"}""",
         ),
         _EncodeTestParams(
             "LargerThanMaxUInt256",
             cadence.Int(
                 115792089237316195423570985008687907853269984665640564039457584007913129639945
             ),
             """{"type": "Int", 
             "value": "115792089237316195423570985008687907853269984665640564039457584007913129639945"}""",
         ),
     ])
 def testEncodeOptional(self):
     self._encodeAndDecodeAll([
         _EncodeTestParams("Nil", cadence.Optional(None),
                           '{"type": "Optional", "value": null}'),
         _EncodeTestParams(
             "Non-nil",
             cadence.Optional(cadence.Int(42)),
             '{"type": "Optional", "value": {"type": "Int", "value": "42"}}',
         ),
     ])
    def testEncodeArray(self):
        empty_array = _EncodeTestParams(
            "Empty",
            cadence.Array([]),
            '{"type":"Array","value":[]}',
        )

        int_array = _EncodeTestParams(
            "Integers",
            cadence.Array([
                cadence.Int(1),
                cadence.Int(2),
                cadence.Int(3),
            ]),
            """{"type":"Array",
                "value":[{"type":"Int","value":"1"},{"type":"Int","value":"2"},{"type":"Int","value":"3"}]}""",
        )

        resource_array = _EncodeTestParams(
            "Resources",
            cadence.Array([
                cadence.Resource([cadence.Int(1)], _foo_resource_type),
                cadence.Resource([cadence.Int(2)], _foo_resource_type),
                cadence.Resource([cadence.Int(3)], _foo_resource_type),
            ]),
            """{"type":"Array","value":[
                {"type":"Resource","value":{"id":"S.test.Foo",
                    "fields":[{"name":"bar","value":{"type":"Int","value":"1"}}]}},
                {"type":"Resource","value":{"id":"S.test.Foo",
                    "fields":[{"name":"bar","value":{"type":"Int","value":"2"}}]}},
                {"type":"Resource","value":{"id":"S.test.Foo",
                    "fields":[{"name":"bar","value":{"type":"Int","value":"3"}}]}}
            ]}""",
        )

        self._encodeAndDecodeAll([
            empty_array,
            int_array,
            resource_array,
        ])
    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])
    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])
    def testEncodeDictionary(self):
        simple_dict = _EncodeTestParams(
            "Simple",
            cadence.Dictionary([
                cadence.KeyValuePair(
                    cadence.String("a"),
                    cadence.Int(1),
                ),
                cadence.KeyValuePair(
                    cadence.String("b"),
                    cadence.Int(2),
                ),
                cadence.KeyValuePair(
                    cadence.String("c"),
                    cadence.Int(3),
                ),
            ]),
            """{"type":"Dictionary","value":[
                {"key":{"type":"String","value":"a"},"value":{"type":"Int","value":"1"}},
                {"key":{"type":"String","value":"b"},"value":{"type":"Int","value":"2"}},
                {"key":{"type":"String","value":"c"},"value":{"type":"Int","value":"3"}}
            ]}""",
        )

        nested_dict = _EncodeTestParams(
            "Nested",
            cadence.Dictionary([
                cadence.KeyValuePair(
                    cadence.String("a"),
                    cadence.Dictionary([
                        cadence.KeyValuePair(
                            cadence.String("1"),
                            cadence.Int(1),
                        ),
                    ]),
                ),
                cadence.KeyValuePair(
                    cadence.String("b"),
                    cadence.Dictionary([
                        cadence.KeyValuePair(
                            cadence.String("2"),
                            cadence.Int(2),
                        ),
                    ]),
                ),
                cadence.KeyValuePair(
                    cadence.String("c"),
                    cadence.Dictionary([
                        cadence.KeyValuePair(
                            cadence.String("3"),
                            cadence.Int(3),
                        ),
                    ]),
                ),
            ]),
            """
            {
                "type": "Dictionary",
                "value": [
                {
                  "key": { "type": "String", "value": "a" },
                  "value": {
                    "type": "Dictionary",
                    "value": [
                      {
                        "key": { "type": "String", "value": "1" },
                        "value": { "type": "Int", "value": "1" }
                      }
                    ]
                  }
                },
                {
                  "key": { "type": "String", "value": "b" },
                  "value": {
                    "type": "Dictionary",
                    "value": [
                      {
                        "key": { "type": "String", "value": "2" },
                        "value": { "type": "Int", "value": "2" }
                      }
                    ]
                  }
                },
                {
                  "key": { "type": "String", "value": "c" },
                  "value": {
                    "type": "Dictionary",
                    "value": [
                      {
                        "key": { "type": "String", "value": "3" },
                        "value": { "type": "Int", "value": "3" }
                      }
                    ]
                  }
                }
                ]
            }
            """,
        )

        resource_dict = _EncodeTestParams(
            "Resources",
            cadence.Dictionary([
                cadence.KeyValuePair(
                    cadence.String("a"),
                    cadence.Resource([cadence.Int(1)], _foo_resource_type),
                ),
                cadence.KeyValuePair(
                    cadence.String("b"),
                    cadence.Resource([cadence.Int(2)], _foo_resource_type),
                ),
                cadence.KeyValuePair(
                    cadence.String("c"),
                    cadence.Resource([cadence.Int(3)], _foo_resource_type),
                ),
            ]),
            """
                {
                  "type": "Dictionary",
                  "value": [
                    {
                      "key": { "type": "String", "value": "a" },
                      "value": {
                        "type": "Resource",
                        "value": {
                          "id": "S.test.Foo",
                          "fields": [
                            { "name": "bar", "value": { "type": "Int", "value": "1" } }
                          ]
                        }
                      }
                    },
                    {
                      "key": { "type": "String", "value": "b" },
                      "value": {
                        "type": "Resource",
                        "value": {
                          "id": "S.test.Foo",
                          "fields": [
                            { "name": "bar", "value": { "type": "Int", "value": "2" } }
                          ]
                        }
                      }
                    },
                    {
                      "key": { "type": "String", "value": "c" },
                      "value": {
                        "type": "Resource",
                        "value": {
                          "id": "S.test.Foo",
                          "fields": [
                            { "name": "bar", "value": { "type": "Int", "value": "3" } }
                          ]
                        }
                      }
                    }
                  ]
                }
            """,
        )

        self._encodeAndDecodeAll([
            simple_dict,
            nested_dict,
            resource_dict,
        ])