Skip to content

Commit

Permalink
fix: handle empty list in getCVTypeString (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedger authored and reedrosenbluth committed Jul 26, 2021
1 parent 7e0fcba commit 1ff5b03
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/transactions/src/clarity/clarityValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function getCVTypeString(val: ClarityValue): string {
case ClarityType.PrincipalContract:
return 'principal';
case ClarityType.List:
return `(list ${val.list.length} ${getCVTypeString(val.list[0])})`;
return `(list ${val.list.length} ${val.list.length ? getCVTypeString(val.list[0]) : "UnknownType"})`;
case ClarityType.Tuple:
return `(tuple ${Object.keys(val.data)
.map(key => `(${key} ${getCVTypeString(val.data[key])})`)
Expand Down
46 changes: 45 additions & 1 deletion packages/transactions/tests/clarity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
StandardPrincipalCV,
} from '../src/clarity';
import { BufferReader } from '../src/bufferReader';
import { cvToString, cvToJSON } from '../src/clarity/clarityValue';
import { cvToString, cvToJSON, getCVTypeString } from '../src/clarity/clarityValue';

const ADDRESS = 'SP2JXKMSH007NPYAQHKJPQMAQYAD90NQGTVJVQ02B';

Expand Down Expand Up @@ -545,4 +545,48 @@ describe('Clarity Types', () => {
);
});
});

describe('Clarity Types to String', () => {
test('Complex Tuple', () => {
const tuple = tupleCV({
a: intCV(-1),
b: uintCV(1),
c: bufferCV(Buffer.from('test')),
d: trueCV(),
e: someCV(trueCV()),
f: noneCV(),
g: standardPrincipalCV(ADDRESS),
h: contractPrincipalCV(ADDRESS, 'test'),
i: responseOkCV(trueCV()),
j: responseErrorCV(falseCV()),
k: listCV([trueCV(), falseCV()]),
l: tupleCV({
a: trueCV(),
b: falseCV(),
}),
m: stringAsciiCV('hello world'),
n: stringUtf8CV('hello \u{1234}'),
o: listCV([])
});
const typeString = getCVTypeString(tuple)
expect(typeString).toEqual(oneLineTrim`
(tuple
(a int)
(b uint)
(c (buff 4))
(d bool)
(e (optional bool))
(f (optional none))
(g principal)
(h principal)
(i (response bool UnknownType))
(j (response UnknownType bool))
(k (list 2 bool))
(l (tuple (a bool) (b bool)))
(m (string-ascii 11))
(n (string-utf8 9))
(o (list 0 UnknownType)))
`)
})
})
});

0 comments on commit 1ff5b03

Please sign in to comment.