42 lines
2.4 KiB
TypeScript
42 lines
2.4 KiB
TypeScript
import type { PropertyRow } from './types'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { toBlockViews } from '../../../src/codeReader/blockView'
|
|
import { createPythonAdapter } from '../../../src/codeReader/languages/python'
|
|
import { WASM_DIR, withBlocks } from '../../../src/codeReader/testSupport'
|
|
import { pythonZhTranslator } from '../../../src/codeReader/translators/pythonZh'
|
|
import { subRowsLayout, tableCells, tableTemplate } from './subRows'
|
|
|
|
const x = { text: 'x', role: 'variable' as const, offset: 4 }
|
|
|
|
describe('属性行下面的子行怎么排', () => {
|
|
it('给了列名的排成表格,没给的排成列表,没有子行的不展开', () => {
|
|
const items = [{ cells: [[x], []] }]
|
|
expect(subRowsLayout({ label: '输入参数', value: [], items, columns: ['名称', '类型', '默认值'] })).toBe('table')
|
|
expect(subRowsLayout({ label: '导入项', value: [], items })).toBe('list')
|
|
expect(subRowsLayout({ label: '输入参数', value: [], items: [], columns: ['名称', '类型', '默认值'] })).toBeUndefined()
|
|
expect(subRowsLayout({ label: '名称', value: [x] })).toBeUndefined()
|
|
})
|
|
|
|
it('表格的格子按列名的列数取:少给的留空,多给的不显示', () => {
|
|
expect(tableCells({ cells: [[x]] }, ['名称', '类型', '默认值'])).toEqual([[x], [], []])
|
|
expect(tableCells({ cells: [[x], [x], [x], [x]] }, ['名称', '类型'])).toEqual([[x], [x]])
|
|
})
|
|
|
|
it('表格的列宽按整列内容定:前几列最宽到不换行,最后一列分剩下的、至少留一段', () => {
|
|
expect(tableTemplate(2)).toBe('minmax(0, max-content) minmax(min(20%, 8em), 1fr)')
|
|
expect(tableTemplate(4)).toBe('minmax(0, max-content) minmax(0, max-content) minmax(0, max-content) minmax(min(20%, 8em), 1fr)')
|
|
})
|
|
|
|
it('真实的 Python 函数:输入参数排成表格,导入项、字典的各项排成列表', async () => {
|
|
const source = [
|
|
'from m import a',
|
|
'def f(k: int, v=1): pass',
|
|
'D = {"k": 1}',
|
|
].join('\n')
|
|
const py = createPythonAdapter(WASM_DIR)
|
|
const views = await withBlocks(py, 'python', source, blocks => toBlockViews(blocks, { source, language: py, markers: [] }, pythonZhTranslator))
|
|
const layouts = views.flatMap(view => 'properties' in view ? view.properties.flatMap((row: PropertyRow) => subRowsLayout(row) ? [[row.label, subRowsLayout(row)]] : []) : [])
|
|
expect(layouts).toEqual([['导入项', 'list'], ['输入参数', 'table'], ['值', 'list']])
|
|
})
|
|
})
|