2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-14 18:42:24 +03:00
Files
bbob/packages/bbob-core/test/index.test.ts
T
Nikolay Kost 8797f7f363 feat: typescript support (#185)
* feat: initial typescript support

* feat: typescript support

* feat(plugin-helper): move files to typescript

* chore: update lock files

* feat: preset types

* fix: build

* fix: benchmark

* fix: remove pnpm cache

* fix: bench action

* fix: pnpm recursive install

* fix: nx cache

* fix: lock file

* fix: workflows

* fix: lerna support in pnpm

* fix: pnpm workspace

* fix: remove unused files

* fix: pnpm lock file

* fix: update lerna for support pnpm

* fix: lerna bootstrap

* fix: rollup build

* fix: update nx

* fix: build

* fix: add nx dep target

* fix: remove nx cache

* fix: workflow run on push only for master

* fix: test workflow run on push only for master

* fix: remove parallel for gen types

* fix: benchmark

* fix: benchmark imports

* fix: pnpm

* fix: types errors and pnpm

* fix: types

* fix: types

* refactor: parser

* fix(parser): tests

* fix: preset tests

* fix: react types

* fix: react type declarations

* fix: pnpm lock file

* fix: react preset types

* fix: lock file

* fix: vue2 types

* feat: dev container support

* fix: types

* fix: types

* refactor: rewrite pkg-task, add nx gen-types deps, fix react/render.ts

* refactor: types

* fix: types

* fix: rename gen-types to types

* fix: nx build order

* fix: nx reset

* fix: define nx deps explicit

* fix: build

* fix: nx

* fix: nx order build

* fix: nx deps

* fix: bbob cli tests

* fix: tests

* fix: cli tests and import

* fix: test cover

* fix: cli cover
2024-04-23 21:11:14 +02:00

157 lines
3.5 KiB
TypeScript

import { TagNode } from '@bbob/parser'
import core, { BBobPluginFunction, BBobPlugins } from '../src'
import { isTagNode } from "@bbob/plugin-helper";
const stringify = (val: unknown) => JSON.stringify(val);
const process = (plugins: BBobPlugins, input: string) => core(plugins).process(input, { render: stringify });
describe('@bbob/core', () => {
test('parse bbcode string to ast and html', () => {
const res = process([], '[style size="15px"]Large Text[/style]');
const ast = res.tree;
expect(res.html).toBe('[{"tag":"style","attrs":{"size":"15px"},"content":["Large"," ","Text"]}]');
expect(ast).toBeInstanceOf(Array);
expect(stringify(ast)).toEqual(stringify([
{
tag: 'style',
attrs: { size: '15px' },
content: ["Large", " ", "Text"]
}
]))
});
test('plugin walk api node', () => {
const testPlugin = () => {
const plugin: BBobPluginFunction = (tree) => tree.walk(node => {
if (isTagNode(node)) {
if (node?.tag === 'mytag') {
node.attrs = {
pass: 1
};
if (Array.isArray(node.content)) {
node.content.push('Test');
}
}
}
return node
});
return plugin
}
const res = process([testPlugin()], '[mytag size="15px"]Large Text[/mytag]');
const ast = res.tree;
expect(ast).toBeInstanceOf(Array);
expect(ast.walk).toBeInstanceOf(Function);
expect(stringify(ast)).toEqual(stringify([
{
tag: 'mytag',
attrs: {
pass: 1
},
content: [
'Large',
' ',
'Text',
'Test'
]
}
]));
});
test('plugin walk api string', () => {
const testPlugin = () => {
const plugin: BBobPluginFunction = (tree) => tree.walk(node => {
if (node === ':)') {
return TagNode.create('test-tag')
}
return node
})
return plugin
};
const res = process([testPlugin()], '[mytag]Large Text :)[/mytag]');
const ast = res.tree;
expect(ast).toBeInstanceOf(Array);
expect(ast.walk).toBeInstanceOf(Function);
expect(stringify(ast)).toEqual(stringify([
{
tag: 'mytag',
attrs: {},
content: [
'Large',
' ',
'Text',
' ',
{
tag: 'test-tag',
attrs: {},
content: [],
}
]
}
]));
});
test('plugin match api', () => {
const testPlugin = () => {
const plugin: BBobPluginFunction = (tree) => tree.match([{ tag: 'mytag1' }, { tag: 'mytag2' }], node => {
if (isTagNode(node) && node.attrs) {
node.attrs['pass'] = 1
}
return node
})
return plugin
};
const res = process([testPlugin()], `[mytag1 size="15"]Tag1[/mytag1][mytag2 size="16"]Tag2[/mytag2][mytag3]Tag3[/mytag3]`);
const ast = res.tree;
expect(ast).toBeInstanceOf(Array);
expect(ast.walk).toBeInstanceOf(Function);
expect(stringify(ast)).toEqual(stringify([
{
tag: 'mytag1',
attrs: {
size: '15',
pass: 1
},
content: [
'Tag1'
]
},
{
tag: 'mytag2',
attrs: {
size: '16',
pass: 1
},
content: [
'Tag2'
]
},
{
tag: 'mytag3',
attrs: {},
content: [
'Tag3'
]
}
]));
})
});