tweak describe

This commit is contained in:
Chris Wanstrath 2025-10-29 12:28:51 -07:00
parent a21ba54ad7
commit 1a3f1c6c43
2 changed files with 7 additions and 5 deletions

View File

@ -32,7 +32,7 @@ export const globals = {
inspect: (v: any) => formatValue(toValue(v)), inspect: (v: any) => formatValue(toValue(v)),
describe: (v: any) => { describe: (v: any) => {
const val = toValue(v) const val = toValue(v)
return { [val.type]: formatValue(toValue(v)) } return `#<${val.type}: ${formatValue(val)}>`
}, },
length: (v: any) => { length: (v: any) => {
const value = toValue(v) const value = toValue(v)
@ -122,13 +122,15 @@ export function formatValue(value: Value, inner = false): string {
return `${colors.dim}null${colors.reset}` return `${colors.dim}null${colors.reset}`
case 'array': { case 'array': {
const items = value.value.map(x => formatValue(x, true)).join(' ') const items = value.value.map(x => formatValue(x, true)).join(' ')
return `${inner ? '(' : ''}${colors.blue}list${colors.reset} ${items}${inner ? ')' : ''}` return `${colors.blue}[${colors.reset}${items}${colors.blue}]${colors.reset}`
} }
case 'dict': { case 'dict': {
const entries = Array.from(value.value.entries()) const entries = Array.from(value.value.entries())
.map(([k, v]) => `${k}=${formatValue(v, true)}`) .map(([k, v]) => `${k}${colors.blue}=${colors.reset}${formatValue(v, true)}`)
.join(' ') .join(' ')
return `${inner ? '(' : ''}${colors.magenta}dict${colors.reset} ${entries}${inner ? ')' : ''}` if (entries.length === 0)
return `${colors.blue}[=]${colors.reset}`
return `${colors.blue}[${colors.reset}${entries}${colors.blue}]${colors.reset}`
} }
case 'function': { case 'function': {
const params = value.params.length ? '(' + value.params.join(' ') + ')' : '' const params = value.params.length ? '(' + value.params.join(' ') + ')' : ''

View File

@ -190,7 +190,7 @@ describe('introspection', () => {
test('describe describes values', async () => { test('describe describes values', async () => {
// Just test that inspect returns something for now // Just test that inspect returns something for now
// (we'd need more complex assertion to check the actual format) // (we'd need more complex assertion to check the actual format)
await expect(`describe 'hello'`).toEvaluateTo({ string: "\u001b[32m'hello\u001b[32m'\u001b[0m" }, globals) await expect(`describe 'hello'`).toEvaluateTo("#<string: \u001b[32m'hello\u001b[32m'\u001b[0m>", globals)
}) })
}) })