diff --git a/lib/diagnostic.js b/lib/diagnostic.js index 776b96c..ca1dc98 100644 --- a/lib/diagnostic.js +++ b/lib/diagnostic.js @@ -47,9 +47,16 @@ function * tokensToDiagnostic (inp, width = 100) { case 'bytes': case 'map': case 'array': - // for bytes and string, we want to print out the length part of the value prefix if it - // exists - it exists for short lengths (<24) but does for longer lengths - multilen = token.type.name === 'string' ? utf8Encoder.encode(token.value).length : token.value.length + // print the length bytes if they exist (length >= 24) + // for string/bytes, token.value is the content; for array/map, token.value IS the count + if (token.type.name === 'string') { + multilen = utf8Encoder.encode(token.value).length + } else if (token.type.name === 'bytes') { + multilen = token.value.length + } else { + // array or map - value is the count directly + multilen = token.value + } if (multilen >= uintBoundaries[0]) { if (multilen < uintBoundaries[1]) { outp += ` ${slc(1, 1)}` diff --git a/package.json b/package.json index 571605b..64e730c 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,9 @@ "build:types": "tsc --build", "prepublishOnly": "npm run build", "test:node": "c8 --check-coverage --exclude=test/** mocha test/test-*.js", + "test:node-bin": "mocha test/node-test-bin.js", "test:browser": "polendina --cleanup test/test-*.js", - "test": "npm run lint && npm run build && npm run test:node && npm run test:browser", + "test": "npm run lint && npm run build && npm run test:node && npm run test:node-bin && npm run test:browser", "test:ci": "npm run test", "coverage": "c8 --reporter=html --reporter=text mocha test/test-*.js && npx st -d coverage -p 8888" }, diff --git a/test/node-test-bin.js b/test/node-test-bin.js index fa558a3..6c9f889 100644 --- a/test/node-test-bin.js +++ b/test/node-test-bin.js @@ -97,17 +97,17 @@ describe('Bin', () => { assert.strictEqual(e.stderr, `Usage: cborg Valid commands: -\tbin2diag [binary input] +\tbin2diag [--width ] [binary input] \tbin2hex [binary input] \tbin2json [--pretty] [binary input] \tdiag2bin [diagnostic input] \tdiag2hex [diagnostic input] \tdiag2json [--pretty] [diagnostic input] \thex2bin [hex input] -\thex2diag [hex input] +\thex2diag [--width ] [hex input] \thex2json [--pretty] [hex input] \tjson2bin '[json input]' -\tjson2diag '[json input]' +\tjson2diag [--width ] '[json input]' \tjson2hex '[json input]' Input may either be supplied as an argument or piped via stdin `) @@ -124,17 +124,17 @@ Input may either be supplied as an argument or piped via stdin `Unknown command: 'blip' Usage: cborg Valid commands: -\tbin2diag [binary input] +\tbin2diag [--width ] [binary input] \tbin2hex [binary input] \tbin2json [--pretty] [binary input] \tdiag2bin [diagnostic input] \tdiag2hex [diagnostic input] \tdiag2json [--pretty] [diagnostic input] \thex2bin [hex input] -\thex2diag [hex input] +\thex2diag [--width ] [hex input] \thex2json [--pretty] [hex input] \tjson2bin '[json input]' -\tjson2diag '[json input]' +\tjson2diag [--width ] '[json input]' \tjson2hex '[json input]' Input may either be supplied as an argument or piped via stdin `) @@ -147,17 +147,17 @@ Input may either be supplied as an argument or piped via stdin assert.strictEqual(stderr, `Usage: cborg Valid commands: -\tbin2diag [binary input] +\tbin2diag [--width ] [binary input] \tbin2hex [binary input] \tbin2json [--pretty] [binary input] \tdiag2bin [diagnostic input] \tdiag2hex [diagnostic input] \tdiag2json [--pretty] [diagnostic input] \thex2bin [hex input] -\thex2diag [hex input] +\thex2diag [--width ] [hex input] \thex2json [--pretty] [hex input] \tjson2bin '[json input]' -\tjson2diag '[json input]' +\tjson2diag [--width ] '[json input]' \tjson2hex '[json input]' Input may either be supplied as an argument or piped via stdin `) @@ -341,6 +341,23 @@ Input may either be supplied as an argument or piped via stdin }) describe('diag length bytes', () => { + // issue #137 - array and map length bytes were missing for lengths >= 24 + it('array with 24 elements', async () => { + // 98 18 = array(24), then 24 uint(1) values + const hex = '9818' + '01'.repeat(24) + const { stdout, stderr } = await execBin(`hex2diag ${hex}`) + assert.strictEqual(stderr, '') + assert.ok(stdout.startsWith('98 18 # array(24)\n')) + }) + + it('map with 24 entries', async () => { + // b8 18 = map(24), then 24 pairs of string('a')=uint(1) + const hex = 'b818' + '616101'.repeat(24) + const { stdout, stderr } = await execBin(`hex2diag ${hex}`) + assert.strictEqual(stderr, '') + assert.ok(stdout.startsWith('b8 18 # map(24)\n')) + }) + it('compact', async () => { const { stdout, stderr } = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaa"') assert.strictEqual(stderr, '') diff --git a/types/lib/byte-utils.d.ts.map b/types/lib/byte-utils.d.ts.map index 4fb60af..ca012ab 100644 --- a/types/lib/byte-utils.d.ts.map +++ b/types/lib/byte-utils.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"byte-utils.d.ts","sourceRoot":"","sources":["../../lib/byte-utils.js"],"names":[],"mappings":"AAuBA;;;GAGG;AACH,2BAHW,UAAU,GAAC,MAAM,EAAE,GACjB,UAAU,CAQtB;AAoMD;;;;GAIG;AACH,4BAJW,UAAU,MACV,UAAU,GACR,MAAM,CAgBlB;AA6CD;;;GAGG;AACH,kDAHW,MAAM,EAAE,GACN,MAAM,CAkBlB;AArTD,gCAMkD;AAyBlD,mCAGe,MAAM,iDAYN,MAAM,6CAIhB;AAOE,+BAHI,MAAM,EAAE,GACN,UAAU,CAItB;AAIG;;;;GAIG;AAEH,6BALW,UAAU,SACV,MAAM,OACN,MAAM,2BAQhB;AAcD;;;;GAIG;AACH,+BAJW,UAAU,EAAE,UACZ,MAAM,GACJ,UAAU,CActB;AAwBD;;;GAGG;AACH,4BAHW,MAAM,GACJ,UAAU,CAMtB;AAaD;;;GAGG;AACH,yBAHW,UAAU,GACR,MAAM,CAQlB;AAiBH;;;GAGG;AACD,6BAHS,MAAM,GAAC,UAAU,GACf,UAAU,CAQpB"} \ No newline at end of file +{"version":3,"file":"byte-utils.d.ts","sourceRoot":"","sources":["../../lib/byte-utils.js"],"names":[],"mappings":"AAuBA;;;GAGG;AACH,2BAHW,UAAU,GAAC,MAAM,EAAE,GACjB,UAAU,CAQtB;AA0MD;;;;GAIG;AACH,4BAJW,UAAU,MACV,UAAU,GACR,MAAM,CAgBlB;AA6CD;;;GAGG;AACH,kDAHW,MAAM,EAAE,GACN,MAAM,CAkBlB;AA3TD,gCAMkD;AA+BlD,mCAGe,MAAM,iDAYN,MAAM,6CAIhB;AAOE,+BAHI,MAAM,EAAE,GACN,UAAU,CAItB;AAIG;;;;GAIG;AAEH,6BALW,UAAU,SACV,MAAM,OACN,MAAM,2BAQhB;AAcD;;;;GAIG;AACH,+BAJW,UAAU,EAAE,UACZ,MAAM,GACJ,UAAU,CActB;AAwBD;;;GAGG;AACH,4BAHW,MAAM,GACJ,UAAU,CAMtB;AAaD;;;GAGG;AACH,yBAHW,UAAU,GACR,MAAM,CAQlB;AAiBH;;;GAGG;AACD,6BAHS,MAAM,GAAC,UAAU,GACf,UAAU,CAQpB"} \ No newline at end of file diff --git a/types/lib/diagnostic.d.ts.map b/types/lib/diagnostic.d.ts.map index 14c2985..625b750 100644 --- a/types/lib/diagnostic.d.ts.map +++ b/types/lib/diagnostic.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"diagnostic.d.ts","sourceRoot":"","sources":["../../lib/diagnostic.js"],"names":[],"mappings":"AAOA;;;GAGG;AACH,wCAHW,UAAU,UACV,MAAM,oCA4HhB;AAED;;;;GAIG;AACH,gCAHW,MAAM,GACJ,UAAU,CAatB"} \ No newline at end of file +{"version":3,"file":"diagnostic.d.ts","sourceRoot":"","sources":["../../lib/diagnostic.js"],"names":[],"mappings":"AAOA;;;GAGG;AACH,wCAHW,UAAU,UACV,MAAM,oCAmIhB;AAED;;;;GAIG;AACH,gCAHW,MAAM,GACJ,UAAU,CAatB"} \ No newline at end of file