convert es6 tests to ts

This commit is contained in:
Alex 2024-06-09 12:16:09 +02:00
parent 615f15756b
commit 17fac31cd1
7 changed files with 35 additions and 27 deletions

View File

@ -29,7 +29,7 @@
"build-docs": "npm run build-backend-docs && npm run build-frontend-docs", "build-docs": "npm run build-backend-docs && npm run build-frontend-docs",
"webpack": "webpack -c webpack.config.js", "webpack": "webpack -c webpack.config.js",
"test-jasmine": "TRILIUM_DATA_DIR=./data-test ts-node ./node_modules/.bin/jasmine", "test-jasmine": "TRILIUM_DATA_DIR=./data-test ts-node ./node_modules/.bin/jasmine",
"test-es6": "node -r esm spec-es6/attribute_parser.spec.js ", "test-es6": "ts-node spec-es6/attribute_parser.spec.ts ",
"test": "npm run test-jasmine && npm run test-es6", "test": "npm run test-jasmine && npm run test-es6",
"postinstall": "rimraf ./node_modules/canvas" "postinstall": "rimraf ./node_modules/canvas"
}, },

View File

@ -1,27 +1,28 @@
import attributeParser from '../src/public/app/services/attribute_parser.js'; import * as attributeParser from '../src/public/app/services/attribute_parser.js';
import {describe, it, expect, execute} from './mini_test.js';
import {describe, it, expect, execute} from './mini_test';
describe("Lexing", () => { describe("Lexing", () => {
it("simple label", () => { it("simple label", () => {
expect(attributeParser.lex("#label").map(t => t.text)) expect(attributeParser.lex("#label").map((t: any) => t.text))
.toEqual(["#label"]); .toEqual(["#label"]);
}); });
it("simple label with trailing spaces", () => { it("simple label with trailing spaces", () => {
expect(attributeParser.lex(" #label ").map(t => t.text)) expect(attributeParser.lex(" #label ").map((t: any) => t.text))
.toEqual(["#label"]); .toEqual(["#label"]);
}); });
it("inherited label", () => { it("inherited label", () => {
expect(attributeParser.lex("#label(inheritable)").map(t => t.text)) expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text))
.toEqual(["#label", "(", "inheritable", ")"]); .toEqual(["#label", "(", "inheritable", ")"]);
expect(attributeParser.lex("#label ( inheritable ) ").map(t => t.text)) expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text))
.toEqual(["#label", "(", "inheritable", ")"]); .toEqual(["#label", "(", "inheritable", ")"]);
}); });
it("label with value", () => { it("label with value", () => {
expect(attributeParser.lex("#label=Hallo").map(t => t.text)) expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text))
.toEqual(["#label", "=", "Hallo"]); .toEqual(["#label", "=", "Hallo"]);
}); });
@ -32,25 +33,25 @@ describe("Lexing", () => {
}); });
it("relation with value", () => { it("relation with value", () => {
expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map(t => t.text)) expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map((t: any) => t.text))
.toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]); .toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
}); });
it("use quotes to define value", () => { it("use quotes to define value", () => {
expect(attributeParser.lex("#'label a'='hello\"` world'").map(t => t.text)) expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text))
.toEqual(["#label a", "=", 'hello"` world']); .toEqual(["#label a", "=", 'hello"` world']);
expect(attributeParser.lex('#"label a" = "hello\'` world"').map(t => t.text)) expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text))
.toEqual(["#label a", "=", "hello'` world"]); .toEqual(["#label a", "=", "hello'` world"]);
expect(attributeParser.lex('#`label a` = `hello\'" world`').map(t => t.text)) expect(attributeParser.lex('#`label a` = `hello\'" world`').map((t: any) => t.text))
.toEqual(["#label a", "=", "hello'\" world"]); .toEqual(["#label a", "=", "hello'\" world"]);
}); });
}); });
describe("Parser", () => { describe("Parser", () => {
it("simple label", () => { it("simple label", () => {
const attrs = attributeParser.parse(["#token"].map(t => ({text: t}))); const attrs = attributeParser.parse(["#token"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1); expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label'); expect(attrs[0].type).toEqual('label');
@ -60,7 +61,7 @@ describe("Parser", () => {
}); });
it("inherited label", () => { it("inherited label", () => {
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map(t => ({text: t}))); const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1); expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label'); expect(attrs[0].type).toEqual('label');
@ -70,7 +71,7 @@ describe("Parser", () => {
}); });
it("label with value", () => { it("label with value", () => {
const attrs = attributeParser.parse(["#token", "=", "val"].map(t => ({text: t}))); const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1); expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label'); expect(attrs[0].type).toEqual('label');
@ -79,14 +80,14 @@ describe("Parser", () => {
}); });
it("relation", () => { it("relation", () => {
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map(t => ({text: t}))); let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1); expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('relation'); expect(attrs[0].type).toEqual('relation');
expect(attrs[0].name).toEqual("token"); expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual('NFi2gL4xtPxM'); expect(attrs[0].value).toEqual('NFi2gL4xtPxM');
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map(t => ({text: t}))); attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1); expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('relation'); expect(attrs[0].type).toEqual('relation');

View File

@ -1,10 +1,10 @@
export function describe(name, cb) { export function describe(name: string, cb: () => any) {
console.log(`Running ${name}`); console.log(`Running ${name}`);
cb(); cb();
} }
export async function it(name, cb) { export async function it(name: string, cb: () => any) {
console.log(` Running ${name}`); console.log(` Running ${name}`);
cb(); cb();
@ -12,9 +12,9 @@ export async function it(name, cb) {
let errorCount = 0; let errorCount = 0;
export function expect(val) { export function expect(val: any) {
return { return {
toEqual: comparedVal => { toEqual: (comparedVal: any) => {
const jsonVal = JSON.stringify(val); const jsonVal = JSON.stringify(val);
const comparedJsonVal = JSON.stringify(comparedVal); const comparedJsonVal = JSON.stringify(comparedVal);
@ -44,11 +44,11 @@ export function expect(val) {
errorCount++; errorCount++;
} }
}, },
toThrow: errorMessage => { toThrow: (errorMessage: any) => {
try { try {
val(); val();
} }
catch (e) { catch (e: any) {
if (e.message !== errorMessage) { if (e.message !== errorMessage) {
console.trace("toThrow caught exception, but messages differ"); console.trace("toThrow caught exception, but messages differ");
console.error(`expected: ${errorMessage}`); console.error(`expected: ${errorMessage}`);

View File

@ -0,0 +1,7 @@
declare module 'attribute_parser';
export function lex(str: string): any[]
export function parse(tokens: any[], str?: string, allowEmptyRelations?: boolean): any[]
export function lexAndParse(str: string, allowEmptyRelations?: boolean): any[]

View File

@ -1,4 +1,4 @@
import utils from "./utils.js"; const utils = require("./utils.js");
function lex(str) { function lex(str) {
str = str.trim(); str = str.trim();
@ -222,7 +222,7 @@ function lexAndParse(str, allowEmptyRelations = false) {
return parse(tokens, str, allowEmptyRelations); return parse(tokens, str, allowEmptyRelations);
} }
export default { module.exports = {
lex, lex,
parse, parse,
lexAndParse lexAndParse

View File

@ -505,7 +505,7 @@ function createImageSrcUrl(note) {
return `api/images/${note.noteId}/${encodeURIComponent(note.title)}?timestamp=${Date.now()}`; return `api/images/${note.noteId}/${encodeURIComponent(note.title)}?timestamp=${Date.now()}`;
} }
export default { module.exports = {
reloadFrontendApp, reloadFrontendApp,
parseDate, parseDate,
formatDateISO, formatDateISO,

View File

@ -11,7 +11,7 @@
"downlevelIteration": true, "downlevelIteration": true,
"skipLibCheck": true "skipLibCheck": true
}, },
"include": ["./src/**/*.js", "./src/**/*.ts", "./*.ts", "./spec/**/*.ts"], "include": ["./src/**/*.js", "./src/**/*.ts", "./*.ts", "./spec/**/*.ts", "./spec-es6/**/*.ts"],
"exclude": ["./node_modules/**/*"], "exclude": ["./node_modules/**/*"],
"ts-node": { "ts-node": {
"files": true "files": true