Skip to content

Commit

Permalink
fix: bbob cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART committed Apr 23, 2024
1 parent 2d99220 commit 2906ee9
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 83 deletions.
7 changes: 7 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
},
"test": {
"dependsOn": [
"^build",
"^test"
]
},
"cover": {
"dependsOn": [
"^build",
"^cover"
]
},
"lint": {
"dependsOn": [
"^lint"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
"build:commonjs": "@/cross-env BABEL_ENV=commonjs NODE_ENV=production @/swc ./src/ --config-file ../../.swcrc-commonjs.json --out-dir lib --strip-leading-paths",
"build:es": "@/cross-env BABEL_ENV=es NODE_ENV=production @/swc ./src/ --config-file ../../.swcrc.json --out-dir es --strip-leading-paths",
"build:umd": "@/cross-env BABEL_ENV=rollup NODE_ENV=production @/rollup --config ../../rollup.config.mjs",
"test": "npm run build && @/jest",
"cover": "npm run build && @/jest --config ../../jest.config.js --coverage .",
"test": "@/jest",
"cover": "@/jest --config ../../jest.config.js --coverage .",
"lint": "@/eslint .",
"types": "@/tsc",
"bundlesize": "npm run build && @/cross-env NODE_ENV=production @/bundlesize .",
Expand Down
12 changes: 3 additions & 9 deletions packages/bbob-cli/bin/bbob → packages/bbob-cli/bin/bbob.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

const bbobHTML = require('@bbob/html').default
const presetHTML5 = require('@bbob/preset-html5').default
const html = require('@bbob/html').default;
const presetHTML5 = require('@bbob/preset-html5').default;

process.stdin.setEncoding('utf8');

Expand All @@ -17,11 +16,6 @@ process.stdin.on('readable', () => {

// Once there's no more data to read, reverse the input and write it to stdout
process.stdin.on('end', () => {
const reversedData = transform(inputData);
const reversedData = html(inputData, presetHTML5()).toString();
process.stdout.write(reversedData);
});

// Function to convert bbcode text to html
function transform(str) {
return bbobHTML(str, presetHTML5()).toString();
}
8 changes: 3 additions & 5 deletions packages/bbob-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "@bbob/cli",
"version": "3.0.2",
"description": "Comand line bbcode parser",
"main": "bin/bbob",
"main": "bin/bbob.js",
"bin": {
"bbob": "bin/bbob"
"bbob": "bin/bbob.js"
},
"dependencies": {
"@bbob/html": "*",
Expand All @@ -28,11 +28,9 @@
},
"homepage": "https://github.com/JiLiZART/bbob#readme",
"scripts": {
"build": "echo 'build @bbob/cli'",
"test": "pkg-task",
"cover": "pkg-task",
"lint": "pkg-task",
"prepublishOnly": "npm run build"
"lint": "pkg-task"
},
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
57 changes: 18 additions & 39 deletions packages/bbob-cli/test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,30 @@
const { exec } = require('child_process');
const { execSync } = require('child_process');
const path = require('path');

const pathToBin = path.resolve(__dirname, '../bin/bbob')
const pathToBin = path.resolve(__dirname, '../bin/bbob.js')

describe('@bbob/cli', () => {
console.log('pathToBin', pathToBin)
test('simple string', (done) => {
exec(`echo "hello" | ${pathToBin}`, (error, stdout, stderr) => {
if (error) {
done(error);
return;
}
expect(stdout.trim()).toBe('olleh');
done();
});
test('simple string', () => {
const stdout = execSync(`echo "hello" | ${pathToBin}`);

expect(String(stdout).trim()).toBe('hello');
});

test('string with bbcodes', (done) => {
exec(`echo "[i]Text[/i]" | ${pathToBin}`, (error, stdout, stderr) => {
if (error) {
done(error);
return;
}
expect(stdout.trim()).toBe('<span style="font-style: italic;">Text</span>');
done();
});
test('string with bbcodes', () => {
const stdout = execSync(`echo "[i]Text[/i]" | ${pathToBin}`);

expect(String(stdout).trim()).toBe('<span style="font-style: italic;">Text</span>');
});

test('empty string', (done) => {
exec(`echo "" | ${pathToBin}`, (error, stdout, stderr) => {
if (error) {
done(error);
return;
}
expect(stdout.trim()).toBe('');
done();
});
test('empty string', () => {
const stdout = execSync(`echo "" | ${pathToBin}`);

expect(String(stdout).trim()).toBe('');
});

test('multiline string', (done) => {
exec(`echo -e "[i]Text[/i]\n[i]Text[/i]" | ${pathToBin}`, (error, stdout, stderr) => {
if (error) {
done(error);
return;
}
expect(stdout.trim()).toBe('<span style="font-style: italic;">Text</span>\n<span style="font-style: italic;">Text</span>');
done();
});
test('multiline string', () => {
const stdout = execSync(`echo "[i]Text[/i]\n[i]Text[/i]" | ${pathToBin}`);

expect(String(stdout).trim()).toBe('<span style="font-style: italic;">Text</span>\n<span style="font-style: italic;">Text</span>');
});
});
3 changes: 0 additions & 3 deletions packages/bbob-core/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('@bbob/core utils', () => {
}];

const resultArr = iterate(testArr, node => {
console.log('iterate', node);
if (typeof node === 'object' && node !== null) {
return {
...node,
Expand All @@ -35,8 +34,6 @@ describe('@bbob/core utils', () => {
}
];

console.log('resultArr', resultArr, expected);

expect(resultArr).toEqual(expected);
});
test('match', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/bbob-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"browser": "dist/index.js",
"browserName": "BbobHtml",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./es/index.js",
"require": "./lib/index.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
}
},
"homepage": "https://github.com/JiLiZART/bbob",
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
22 changes: 8 additions & 14 deletions packages/bbob-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,27 @@ function renderNode(node?: TagNodeTree, options?: BBobHTMLOptions): string {
}

if (Array.isArray(node)) {
return renderNodes(node, options);
return render(node, options);
}

if (isTagNode(node)) {
if (stripTags) {
return renderNodes(node.content, options);
return render(node.content, options);
}

const attrs = attrsToString(node.attrs)

if (node.content === null) {
const tag = START_TAG + node.tag + attrs + SELFCLOSE_END_TAG

return tag
return START_TAG + node.tag + attrs + SELFCLOSE_END_TAG
}

const tag = START_TAG + node.tag + attrs + END_TAG + renderNodes(node.content, options) + CLOSE_START_TAG + node.tag + END_TAG

return tag;
return START_TAG + node.tag + attrs + END_TAG + render(node.content, options) + CLOSE_START_TAG + node.tag + END_TAG;
}

return '';
}

function renderNodes(nodes: TagNodeTree, options?: BBobHTMLOptions): string {
export function render(nodes: TagNodeTree, options?: BBobHTMLOptions): string {
if (Array.isArray(nodes)) {
return nodes.reduce<string>((r, node) => r + renderNode(node, options), '')
}
Expand All @@ -58,10 +54,8 @@ function renderNodes(nodes: TagNodeTree, options?: BBobHTMLOptions): string {
return ''
}

function toHTML<InputValue = string | TagNode[]>(source: InputValue, plugins: BBobPlugins, options?: BBobHTMLOptions) {
return core<InputValue, BBobHTMLOptions>(plugins).process(source, { ...options, render: renderNodes }).html
export function html<InputValue = string | TagNode[]>(source: InputValue, plugins: BBobPlugins, options?: BBobHTMLOptions) {
return core<InputValue, BBobHTMLOptions>(plugins).process(source, { ...options, render: render }).html
}

export const render = renderNodes;

export default toHTML;
export default html;
14 changes: 14 additions & 0 deletions packages/bbob-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
},
"./lexer": {
"types": "./types/lexer.d.ts",
"import": "./es/lexer.js",
"require": "./lib/lexer.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
},
"./parse": {
"types": "./types/parse.d.ts",
"import": "./es/parse.js",
"require": "./lib/parse.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
},
"./Token": {
"types": "./types/Token.d.ts",
"import": "./es/Token.js",
Expand Down
10 changes: 10 additions & 0 deletions packages/bbob-preset-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"jsnext:main": "es/index.js",
"browser": "dist/index.js",
"browserName": "BbobPresetReact",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./es/index.js",
"require": "./lib/index.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
}
},
"homepage": "https://github.com/JiLiZART/bbob",
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions packages/bbob-preset-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"jsnext:main": "es/index.js",
"browser": "dist/index.js",
"browserName": "BbobPresetVue",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./es/index.js",
"require": "./lib/index.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
}
},
"homepage": "https://github.com/JiLiZART/bbob",
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions packages/bbob-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
"jsnext:main": "es/index.js",
"browser": "dist/index.js",
"browserName": "BbobReact",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./es/index.js",
"require": "./lib/index.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
}
},
"homepage": "https://github.com/JiLiZART/bbob",
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions packages/bbob-vue2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
"jsnext:main": "es/index.js",
"browser": "dist/index.js",
"browserName": "BbobVue",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./es/index.js",
"require": "./lib/index.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
}
},
"homepage": "https://github.com/JiLiZART/bbob",
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions packages/bbob-vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
"jsnext:main": "es/index.js",
"browser": "dist/index.js",
"browserName": "BbobVue3",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./es/index.js",
"require": "./lib/index.js",
"browser": "./dist/index.min.js",
"umd": "./dist/index.min.js"
}
},
"homepage": "https://github.com/JiLiZART/bbob",
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
"license": "MIT",
Expand Down
8 changes: 2 additions & 6 deletions packages/bbob-vue3/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ function tagToVueNode(
node: TagNode,
index: number
) {
const { class: className, style, ...domProps } = node.attrs || {};

const content = isContentEmpty(node.content)
? undefined
: renderToVueNodes(createElement, node.content);
const { class: className, style, ...domProps } = node.attrs;

return createElement(
node.tag,
Expand All @@ -53,7 +49,7 @@ function tagToVueNode(
style,
...domProps,
},
content
isContentEmpty(node.content) ? undefined : renderToVueNodes(createElement, node.content),
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/bbob-vue3/test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('@bbob/vue3 render', () => {

expect(html).toStrictEqual([
{
"children": null,
"children": undefined,
"props": { "class": undefined, "key": 0, "style": undefined },
"tagName": "b"
}
Expand Down
6 changes: 2 additions & 4 deletions scripts/pkg-task
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ function runCommandByKey(key, command) {
}
}

if (args.length === 0 && event_name) {
if (event_name === key) {
return runCommand(command)
}
if (args.length === 0 && event_name && event_name === key) {
return runCommand(command)
}
}

Expand Down

0 comments on commit 2906ee9

Please sign in to comment.