浏览代码

[frontend] Add support for parser tests next to the jison definition

This allows us to move the test cases for the autocomplete parser next to the corresponding jison files to make development of parsers easier and to reduce duplication of test cases.

Before this change the tests were specified for each parser even for parsers that were referencing the same jison file. For changes to a specific jison file the tests would have to be updated in multiple places. Now the exact same tests will be ran for each parser that references the same jison file greatly reducing duplication.

This commit also move the alter table tests for the generic parser into the new setup.
Johan Åhlén 3 年之前
父节点
当前提交
426669d26b

+ 28 - 0
desktop/core/src/desktop/js/parse/sql/generic/genericAutocompleteParser.test.ts

@@ -0,0 +1,28 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// 'License'); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import genericAutocompleteParser from './genericAutocompleteParser';
+import structure from './jison/structure.json';
+import { AutocompleteParser } from '../../types';
+import { extractTestCases, runTestCases } from '../testUtils';
+
+const jisonFolder = 'desktop/core/src/desktop/js/parse/sql/generic/jison';
+const groupedTestCases = extractTestCases(jisonFolder, structure.autocomplete);
+
+describe('genericAutocompleteParser', () => {
+  // TODO: Fix the types
+  runTestCases(genericAutocompleteParser as unknown as AutocompleteParser, groupedTestCases);
+});

+ 43 - 0
desktop/core/src/desktop/js/parse/sql/generic/jison/alter/alter_table.test.json

@@ -0,0 +1,43 @@
+[
+  {
+    "namePrefix": "should suggest keywords",
+    "beforeCursor": "ALTER ",
+    "afterCursor": "",
+    "containsKeywords": [
+      "TABLE"
+    ],
+    "expectedResult": {
+      "lowerCase": false
+    }
+  },
+  {
+    "namePrefix": "should suggest tables",
+    "beforeCursor": "ALTER TABLE ",
+    "afterCursor": "",
+    "expectedResult": {
+      "lowerCase": false,
+      "suggestTables": {
+        "onlyTables": true
+      },
+      "suggestDatabases": {
+        "appendDot": true
+      }
+    }
+  },
+  {
+    "namePrefix": "should suggest tables",
+    "beforeCursor": "ALTER TABLE foo.",
+    "afterCursor": "",
+    "expectedResult": {
+      "lowerCase": false,
+      "suggestTables": {
+        "identifierChain": [
+          {
+            "name": "foo"
+          }
+        ],
+        "onlyTables": true
+      }
+    }
+  }
+]

+ 0 - 36
desktop/core/src/desktop/js/parse/sql/generic/test/genericAutocompleteParser.Alter.test.js

@@ -35,42 +35,6 @@ describe('genericAutocompleteParser.js ALTER statements', () => {
     ).toEqualDefinition(testDefinition);
   };
 
-  describe('ALTER TABLE', () => {
-    it('should suggest keywords for "ALTER |"', () => {
-      assertAutoComplete({
-        beforeCursor: 'ALTER ',
-        afterCursor: '',
-        containsKeywords: ['TABLE'],
-        expectedResult: {
-          lowerCase: false
-        }
-      });
-    });
-
-    it('should suggest tables for "ALTER TABLE |"', () => {
-      assertAutoComplete({
-        beforeCursor: 'ALTER TABLE ',
-        afterCursor: '',
-        expectedResult: {
-          lowerCase: false,
-          suggestTables: { onlyTables: true },
-          suggestDatabases: { appendDot: true }
-        }
-      });
-    });
-
-    it('should suggest tables for "ALTER TABLE foo.|"', () => {
-      assertAutoComplete({
-        beforeCursor: 'ALTER TABLE foo.',
-        afterCursor: '',
-        expectedResult: {
-          lowerCase: false,
-          suggestTables: { identifierChain: [{ name: 'foo' }], onlyTables: true }
-        }
-      });
-    });
-  });
-
   describe('ALTER VIEW', () => {
     it('should handle "ALTER VIEW baa.boo AS SELECT * FROM bla;|"', () => {
       assertAutoComplete({

+ 105 - 0
desktop/core/src/desktop/js/parse/sql/testUtils.ts

@@ -0,0 +1,105 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// 'License'); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import fs from 'fs';
+
+import { AutocompleteParser } from '../types';
+
+interface TestCase {
+  namePrefix: string; // ex. "should suggest keywords"
+  beforeCursor: string;
+  afterCursor: string;
+  containsKeywords?: string[];
+  noErrors?: boolean;
+  expectedResult: {
+    lowerCase?: boolean;
+    suggestTables?: {
+      identifierChain?: { name: string }[];
+      onlyTables?: boolean;
+    };
+    suggestDatabases?: {
+      appendDot?: boolean;
+    };
+  };
+}
+
+interface GroupedTestCases {
+  jisonFile: string;
+  testCases: TestCase[];
+}
+
+/**
+ * Finds and parses x.test.json files given a list of jison files.
+ * For example, if alter_table.jison is part of the structure it will look for alter_table.test.json and if it does
+ * it'll parser it (TestCase[]). Test cases are grouped per found .jison file.
+ */
+export const extractTestCases = (
+  jisonFolder: string,
+  structureFiles: string[]
+): GroupedTestCases[] => {
+  const groupedTestCases: GroupedTestCases[] = [];
+  structureFiles.forEach(jisonFile => {
+    const testFile = `${jisonFolder}/${jisonFile.replace('.jison', '.test.json')}`;
+    if (fs.existsSync(testFile)) {
+      const fileJson = fs.readFileSync(testFile).toString();
+      const testCases: TestCase[] = [];
+      JSON.parse(fileJson).forEach((testCase: TestCase) => {
+        testCases.push(testCase);
+      });
+      groupedTestCases.push({ jisonFile, testCases });
+    }
+  });
+  return groupedTestCases;
+};
+
+const createAssertForAutocomplete = (
+  parser: AutocompleteParser,
+  debug = false
+): ((testCase: TestCase) => void) => {
+  const assertAutocomplete = (testCase: TestCase) => {
+    expect(parser.parseSql(testCase.beforeCursor, testCase.afterCursor, debug)).toEqualDefinition(
+      testCase
+    );
+  };
+  return assertAutocomplete;
+};
+
+export const runTestCases = (
+  autocompleteParser: AutocompleteParser,
+  groupedTestCases: GroupedTestCases[]
+): void => {
+  beforeAll(() => {
+    // This guarantees that any parse errors are actually thrown
+    (
+      autocompleteParser as unknown as { yy: { parseError: (msg?: string) => void } }
+    ).yy.parseError = msg => {
+      throw Error(msg);
+    };
+  });
+
+  const assertTestCase = createAssertForAutocomplete(autocompleteParser);
+
+  groupedTestCases.forEach(({ jisonFile, testCases }) => {
+    // Each group (jison file) gets its own describe
+    describe(jisonFile, () => {
+      testCases.forEach(testCase => {
+        it(`${testCase.namePrefix} given "${testCase.beforeCursor}|${testCase.afterCursor}"`, () => {
+          assertTestCase(testCase);
+        });
+      });
+    });
+  });
+};

+ 1 - 1
desktop/core/src/desktop/js/parse/types.ts

@@ -195,7 +195,7 @@ export interface SqlStatementsParser {
 }
 
 export interface AutocompleteParser {
-  parseSql(beforeCursor: string, afterCursor: string): AutocompleteParseResult;
+  parseSql(beforeCursor: string, afterCursor: string, debug?: boolean): AutocompleteParseResult;
 }
 
 export interface SyntaxParser {