|
| 1 | +from ic.candid import * |
| 2 | + |
| 3 | +# TODO |
| 4 | +class TestCandid: |
| 5 | + |
| 6 | + def test_nat_encode(self): |
| 7 | + nat = NatClass() |
| 8 | + res = encode([{'type':nat, 'value':10000000000}]) |
| 9 | + assert res.hex() == "4449444c00017d80c8afa025" |
| 10 | + |
| 11 | + def test_nat_decode(self): |
| 12 | + data = bytes.fromhex("4449444c00017d80c8afa025") |
| 13 | + res = decode(data) |
| 14 | + assert len(res) == 1 |
| 15 | + assert res[0]["type"] == 'nat' |
| 16 | + assert res[0]["value"] == 10000000000 |
| 17 | + |
| 18 | + def test_principal_encode(self): |
| 19 | + principal = PrincipalClass() |
| 20 | + res = encode([{'type': principal, 'value':'aaaaa-aa'}]) |
| 21 | + assert res.hex() == "4449444c0001680100" |
| 22 | + |
| 23 | + def test_principal_decode(self): |
| 24 | + data = bytes.fromhex("4449444c0001680100") |
| 25 | + res = decode(data) |
| 26 | + assert len(res) == 1 |
| 27 | + assert res[0]["type"] == 'principal' |
| 28 | + assert res[0]["value"].to_str() == 'aaaaa-aa' |
| 29 | + |
| 30 | + # data = b'DIDL\x00\x01q\x08XTC Test' |
| 31 | + # print('decode data: {}'.format(data)) |
| 32 | + # out = decode(data) |
| 33 | + # print(out) |
| 34 | + |
| 35 | + # data = b'DIDL\x00\x01}\xe2\x82\xac\xe2\x82\xac\xe2\x80' |
| 36 | + # print('decode data: {}'.format(data)) |
| 37 | + # out = decode(data) |
| 38 | + # print(out) |
| 39 | + |
| 40 | + def test_record_encode(self): |
| 41 | + record = Types.Record({'foo':Types.Text, 'bar': Types.Int}) |
| 42 | + res = encode([{'type': record, 'value':{'foo': '💩', 'bar': 42}}]) |
| 43 | + assert res.hex() == '4449444c016c02d3e3aa027c868eb7027101002a04f09f92a9' |
| 44 | + |
| 45 | + def test_record_decode(self): |
| 46 | + data = bytes.fromhex('4449444c016c02d3e3aa027c868eb7027101002a04f09f92a9') |
| 47 | + res = decode(data) |
| 48 | + assert len(res) == 1 |
| 49 | + assert res[0]["type"].startswith('rec') |
| 50 | + assert res[0]['value'] == {'_4895187': 42, '_5097222': '💩'} |
| 51 | + |
| 52 | + # def test_tuple_encode(self): |
| 53 | + # tup = Types.Tuple(Types.Int, Types.Text) |
| 54 | + # res = encode([{'type': tup, 'value': [42, '💩']}]) |
| 55 | + # assert res.hex() == '4449444c016c02007c017101002a04f09f92a9' |
| 56 | + |
| 57 | + |
| 58 | + # # variant |
| 59 | + # tup = Types.Variant({'ok': Types.Text, 'err': Types.Text}) |
| 60 | + # res = encode([{'type': tup, 'value': {'ok': 'good'} }]) |
| 61 | + # print('expected:', '4449444c016b03017e9cc20171e58eb4027101000104676f6f64') |
| 62 | + # print('current:', res.hex()) |
| 63 | + # print(decode(res, tup)) |
| 64 | + |
| 65 | + # # tuple(variant) |
| 66 | + # tup = Types.Tuple(Types.Variant({'ok': Types.Text, 'err': Types.Text})) |
| 67 | + # res = encode([{'type': tup, 'value': [{'ok': 'good'}] }]) |
| 68 | + # print('expected:', '4449444c026b029cc20171e58eb402716c01000001010004676f6f64') |
| 69 | + # print('current:', res.hex()) |
| 70 | + # print(decode(res, tup)) |
| 71 | + |
| 72 | + # # Vec |
| 73 | + # vec = Types.Vec(Types.Nat64) |
| 74 | + # param = [0, 1, 2, 3] |
| 75 | + # res = encode([{'type': vec, 'value': param}]) |
| 76 | + # print('expected:', '4449444c016d7c01000400010203') |
| 77 | + # print('current :', res.hex()) |
| 78 | + # print('decode Vec:', decode(res, vec)) |
| 79 | + |
| 80 | + # # Principle |
| 81 | + # Prin = Types.Principal |
| 82 | + # param = 'expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae' |
| 83 | + # res = encode([{'type': Prin, 'value': param}]) |
| 84 | + # print('current :', res.hex()) |
| 85 | + # print('decode Principal:', decode(res)) |
| 86 | + |
| 87 | + # # Opt principal |
| 88 | + # Prin = Types.Opt(Types.Principal) |
| 89 | + # param = ['expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae'] |
| 90 | + # res = encode([{'type': Prin, 'value': param}]) |
| 91 | + # print('current :', res.hex()) |
| 92 | + # print('decode Principal:', decode(res, Prin)) |
| 93 | + |
| 94 | + # # NULL |
| 95 | + # Prin = Types.Null |
| 96 | + # param = None |
| 97 | + # res = encode([{'type': Prin, 'value': param}]) |
| 98 | + # print('current :', res.hex()) |
| 99 | + # print('decode Null:', decode(res, Prin)) |
0 commit comments