From 1847ab320646da48ce5fec241428c93d50425258 Mon Sep 17 00:00:00 2001 From: Chunchun <14298407+appletreeisyellow@users.noreply.github.com> Date: Tue, 28 Feb 2023 13:04:24 -0600 Subject: [PATCH 1/2] test: add mock test for monaco --- jest.config.js | 1 + src/languageSupport/__mocks__/monacoMock.js | 11 ++++++ .../languages/influxql/influxql.test.ts | 19 +++++++++++ .../languages/influxql/testRunner.ts | 34 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 src/languageSupport/__mocks__/monacoMock.js create mode 100644 src/languageSupport/languages/influxql/influxql.test.ts create mode 100644 src/languageSupport/languages/influxql/testRunner.ts diff --git a/jest.config.js b/jest.config.js index d5d967edaa..6d4ca74aa6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -19,6 +19,7 @@ module.exports = { 'react-dnd$': 'react-dnd/dist/cjs', 'react-dnd-html5-backend$': 'react-dnd-html5-backend/dist/cjs', 'dnd-core$': 'dnd-core/dist/cjs', + 'monaco-editor': '/src/languageSupport/__mocks__/monacoMock.js', }, globals: { 'ts-jest': { diff --git a/src/languageSupport/__mocks__/monacoMock.js b/src/languageSupport/__mocks__/monacoMock.js new file mode 100644 index 0000000000..09b27427ef --- /dev/null +++ b/src/languageSupport/__mocks__/monacoMock.js @@ -0,0 +1,11 @@ +module.exports = { + languages: { + register: function(language) {}, + setMonarchTokensProvider: function(name, tokens) {}, + setLanguageConfiguration: function(name, config) {} + }, + editor: { + defineTheme: function(name, theme) {}, + tokenize: jest.fn().mockReturnValue([[]]) + } +}; diff --git a/src/languageSupport/languages/influxql/influxql.test.ts b/src/languageSupport/languages/influxql/influxql.test.ts new file mode 100644 index 0000000000..f8d82be867 --- /dev/null +++ b/src/languageSupport/languages/influxql/influxql.test.ts @@ -0,0 +1,19 @@ +import {testTokenization} from './testRunner' + +it('comment', () => { + testTokenization('influxql', [ + // Comments + [ + { + line: '-- a comment', + tokens: [{startIndex: 0, type: 'comment.influxql'}] + } + ], + [ + { + line: '---sticky -- comment', + tokens: [{ startIndex: 0, type: 'comment.sql' }] + } + ], + ]) +}) \ No newline at end of file diff --git a/src/languageSupport/languages/influxql/testRunner.ts b/src/languageSupport/languages/influxql/testRunner.ts new file mode 100644 index 0000000000..af1c88887a --- /dev/null +++ b/src/languageSupport/languages/influxql/testRunner.ts @@ -0,0 +1,34 @@ +import * as monaco from "monaco-editor"; // from 'src/languageSupport/__mocks__/monacoMock.js + +interface RelaxedToken { + startIndex: number; + type: string; +} + +interface TestItem { + line: string; + tokens: RelaxedToken[]; +} + +export const testTokenization = (_language: string, tests: TestItem[][]): void => { + tests.forEach((test) => runTest(_language, test)); +} + +function runTest(languageId: string, test: TestItem[]): void { + const text = test.map((t) => t.line).join('\n'); + const actualTokens = monaco.editor.tokenize(text, languageId); + const actual = actualTokens.map((lineTokens: any, index: number) => { + return { + line: test[index].line, + tokens: lineTokens.map((t) => { + return { + startIndex: t.offset, + type: t.type + }; + }) + }; + }); + + // eslint-disable-next-line no-console + console.log({languageId, test, test_tokens: test[0].tokens, text, actualTokens: actual[0].tokens}) +} \ No newline at end of file From 1ab4ced775f81536ecb42c7e6db8cb759e7d0bd4 Mon Sep 17 00:00:00 2001 From: Chunchun <14298407+appletreeisyellow@users.noreply.github.com> Date: Tue, 28 Feb 2023 13:08:05 -0600 Subject: [PATCH 2/2] chore: run prettier --- src/languageSupport/__mocks__/monacoMock.js | 14 ++--- .../languages/influxql/influxql.test.ts | 10 ++-- .../languages/influxql/testRunner.ts | 55 +++++++++++-------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/languageSupport/__mocks__/monacoMock.js b/src/languageSupport/__mocks__/monacoMock.js index 09b27427ef..45d90ddf09 100644 --- a/src/languageSupport/__mocks__/monacoMock.js +++ b/src/languageSupport/__mocks__/monacoMock.js @@ -1,11 +1,11 @@ module.exports = { languages: { - register: function(language) {}, - setMonarchTokensProvider: function(name, tokens) {}, - setLanguageConfiguration: function(name, config) {} + register: function (language) {}, + setMonarchTokensProvider: function (name, tokens) {}, + setLanguageConfiguration: function (name, config) {}, }, editor: { - defineTheme: function(name, theme) {}, - tokenize: jest.fn().mockReturnValue([[]]) - } -}; + defineTheme: function (name, theme) {}, + tokenize: jest.fn().mockReturnValue([[]]), + }, +} diff --git a/src/languageSupport/languages/influxql/influxql.test.ts b/src/languageSupport/languages/influxql/influxql.test.ts index f8d82be867..a35a83d766 100644 --- a/src/languageSupport/languages/influxql/influxql.test.ts +++ b/src/languageSupport/languages/influxql/influxql.test.ts @@ -6,14 +6,14 @@ it('comment', () => { [ { line: '-- a comment', - tokens: [{startIndex: 0, type: 'comment.influxql'}] - } + tokens: [{startIndex: 0, type: 'comment.influxql'}], + }, ], [ { line: '---sticky -- comment', - tokens: [{ startIndex: 0, type: 'comment.sql' }] - } + tokens: [{startIndex: 0, type: 'comment.sql'}], + }, ], ]) -}) \ No newline at end of file +}) diff --git a/src/languageSupport/languages/influxql/testRunner.ts b/src/languageSupport/languages/influxql/testRunner.ts index af1c88887a..445443ad7c 100644 --- a/src/languageSupport/languages/influxql/testRunner.ts +++ b/src/languageSupport/languages/influxql/testRunner.ts @@ -1,34 +1,43 @@ -import * as monaco from "monaco-editor"; // from 'src/languageSupport/__mocks__/monacoMock.js +import * as monaco from 'monaco-editor' // from src/languageSupport/__mocks__/monacoMock.js interface RelaxedToken { - startIndex: number; - type: string; + startIndex: number + type: string } interface TestItem { - line: string; - tokens: RelaxedToken[]; + line: string + tokens: RelaxedToken[] } -export const testTokenization = (_language: string, tests: TestItem[][]): void => { - tests.forEach((test) => runTest(_language, test)); +export const testTokenization = ( + _language: string, + tests: TestItem[][] +): void => { + tests.forEach(test => runTest(_language, test)) } function runTest(languageId: string, test: TestItem[]): void { - const text = test.map((t) => t.line).join('\n'); - const actualTokens = monaco.editor.tokenize(text, languageId); - const actual = actualTokens.map((lineTokens: any, index: number) => { - return { - line: test[index].line, - tokens: lineTokens.map((t) => { - return { - startIndex: t.offset, - type: t.type - }; - }) - }; - }); + const text = test.map(t => t.line).join('\n') + const actualTokens = monaco.editor.tokenize(text, languageId) + const actual = actualTokens.map((lineTokens: any, index: number) => { + return { + line: test[index].line, + tokens: lineTokens.map(t => { + return { + startIndex: t.offset, + type: t.type, + } + }), + } + }) - // eslint-disable-next-line no-console - console.log({languageId, test, test_tokens: test[0].tokens, text, actualTokens: actual[0].tokens}) -} \ No newline at end of file + // eslint-disable-next-line no-console + console.log({ + languageId, + test, + test_tokens: test[0].tokens, + text, + actualTokens: actual[0].tokens, + }) +}