Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
35 changes: 26 additions & 9 deletions test/node-test-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ describe('Bin', () => {
assert.strictEqual(e.stderr,
`Usage: cborg <command> <args>
Valid commands:
\tbin2diag [binary input]
\tbin2diag [--width <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 <width>] [hex input]
\thex2json [--pretty] [hex input]
\tjson2bin '[json input]'
\tjson2diag '[json input]'
\tjson2diag [--width <width>] '[json input]'
\tjson2hex '[json input]'
Input may either be supplied as an argument or piped via stdin
`)
Expand All @@ -124,17 +124,17 @@ Input may either be supplied as an argument or piped via stdin
`Unknown command: 'blip'
Usage: cborg <command> <args>
Valid commands:
\tbin2diag [binary input]
\tbin2diag [--width <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 <width>] [hex input]
\thex2json [--pretty] [hex input]
\tjson2bin '[json input]'
\tjson2diag '[json input]'
\tjson2diag [--width <width>] '[json input]'
\tjson2hex '[json input]'
Input may either be supplied as an argument or piped via stdin
`)
Expand All @@ -147,17 +147,17 @@ Input may either be supplied as an argument or piped via stdin
assert.strictEqual(stderr,
`Usage: cborg <command> <args>
Valid commands:
\tbin2diag [binary input]
\tbin2diag [--width <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 <width>] [hex input]
\thex2json [--pretty] [hex input]
\tjson2bin '[json input]'
\tjson2diag '[json input]'
\tjson2diag [--width <width>] '[json input]'
\tjson2hex '[json input]'
Input may either be supplied as an argument or piped via stdin
`)
Expand Down Expand Up @@ -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, '')
Expand Down
2 changes: 1 addition & 1 deletion types/lib/byte-utils.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/lib/diagnostic.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.