text: // 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 { assertPartials } from 'parse/sql/sharedParserTests';
  import dasksqlAutocompleteParser from '../dasksqlAutocompleteParser';
  
  describe('dasksqlAutocompleteParser.js', () => {
    beforeAll(() => {
      dasksqlAutocompleteParser.yy.parseError = function (msg) {
        throw Error(msg);
      };
    });
  
    const assertAutoComplete = testDefinition => {
      const debug = false;
  
      expect(
        dasksqlAutocompleteParser.parseSql(
          testDefinition.beforeCursor,
          testDefinition.afterCursor,
          debug
        )
      ).toEqualDefinition(testDefinition);
    };
  
    it('should suggest keywords for ";;|"', () => {
      assertAutoComplete({
        beforeCursor: ';;',
        afterCursor: '',
        containsKeywords: ['CREATE', 'DESCRIBE', 'SELECT', 'SHOW'],
        expectedResult: {
          lowerCase: false
        }
      });
    });
  
    it('should suggest keywords for ";|;"', () => {
      assertAutoComplete({
        beforeCursor: ';',
        afterCursor: ';',
        containsKeywords: ['SELECT'],
        expectedResult: {
          lowerCase: false
        }
      });
    });
  
    it('should suggest keywords for "|;;;;', () => {
      assertAutoComplete({
        beforeCursor: '',
        afterCursor: ';;;;',
        containsKeywords: ['SELECT'],
        expectedResult: {
          lowerCase: false
        }
      });
    });
  
    it('should suggest keywords for "foo|bar"', () => {
      assertAutoComplete({
        beforeCursor: 'foo',
        afterCursor: 'bar',
        containsKeywords: ['SELECT'],
        expectedResult: {
          lowerCase: false
        }
      });
    });
  
    describe('Error Handling', () => {
      it('should suggest keywords for "bla; |"', () => {
        assertAutoComplete({
          beforeCursor: 'bla; ',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "bla bla bla;bla; |"', () => {
        assertAutoComplete({
          beforeCursor: 'bla bla bla;bla; ',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "Åäö; |"', () => {
        assertAutoComplete({
          beforeCursor: 'Åäö; ',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "bla bla bla;bla;\\n|;bladiblaa blaa"', () => {
        assertAutoComplete({
          beforeCursor: 'bla bla bla;bla;\n',
          afterCursor: ';bladiblaa blaa',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "FROM; |"', () => {
        assertAutoComplete({
          beforeCursor: 'FROM; ',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "INTO USE; |"', () => {
        assertAutoComplete({
          beforeCursor: 'INTO USE; ',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "INTO SELECT; OR FROM FROM; |"', () => {
        assertAutoComplete({
          beforeCursor: 'INTO SELECT; OR FROM FROM;',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "INTO SELECT; OR FROM FROM; |;BLAAA; AND;"', () => {
        assertAutoComplete({
          beforeCursor: 'INTO SELECT; OR FROM FROM;',
          afterCursor: ';BLAAA; AND;',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "INTO bla bla;AND booo; |"', () => {
        assertAutoComplete({
          beforeCursor: 'INTO bla bla;AND booo;',
          afterCursor: '',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "|; SELECT LIMIT 10"', () => {
        assertAutoComplete({
          beforeCursor: '',
          afterCursor: '; SELECT LIMIT 10',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "| * FROM boo; SELECT LIMIT 10"', () => {
        assertAutoComplete({
          beforeCursor: '',
          afterCursor: ' * FROM boo; SELECT LIMIT 10',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
  
      it('should suggest keywords for "bla| * FROM boo; SELECT LIMIT 10"', () => {
        assertAutoComplete({
          beforeCursor: 'bla',
          afterCursor: ' * FROM boo; SELECT LIMIT 10',
          containsKeywords: ['SELECT'],
          expectedResult: {
            lowerCase: false
          }
        });
      });
    });
  
    describe('partial removal', () => {
      it('should identify part lengths', () => {
        assertPartials(dasksqlAutocompleteParser);
      });
    });
  });
  