mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	convert es6 tests to ts
This commit is contained in:
		
							parent
							
								
									615f15756b
								
							
						
					
					
						commit
						17fac31cd1
					
				@ -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"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
				
			|||||||
@ -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');
 | 
				
			||||||
@ -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}`);
 | 
				
			||||||
							
								
								
									
										7
									
								
								src/public/app/services/attribute_parser.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/public/app/services/attribute_parser.d.ts
									
									
									
									
										vendored
									
									
										Normal 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[]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -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
 | 
				
			||||||
 | 
				
			|||||||
@ -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,
 | 
				
			||||||
 | 
				
			|||||||
@ -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
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user