vitess.io/vitess@v0.16.2/web/vtadmin/src/util/tokenize.test.ts (about)

     1  /**
     2   * Copyright 2021 The Vitess Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  import { Token, tokenizeSearch, SearchToken, SearchTokenTypes } from './tokenize';
    17  
    18  describe('tokenize', () => {
    19      describe('tokenizeSearch', () => {
    20          const tests: {
    21              name: string;
    22              input: string;
    23              expected: SearchToken[];
    24          }[] = [
    25              {
    26                  name: 'parses fuzzy strings',
    27                  input: 'hello',
    28                  expected: [{ type: SearchTokenTypes.FUZZY, value: 'hello' }],
    29              },
    30              {
    31                  name: 'parses exact strings',
    32                  input: '"hello"',
    33                  expected: [{ type: SearchTokenTypes.EXACT, value: 'hello' }],
    34              },
    35              {
    36                  name: 'parses key/values',
    37                  input: 'hello:moon',
    38                  expected: [{ type: SearchTokenTypes.KEY_VALUE, key: 'hello', value: 'moon' }],
    39              },
    40              {
    41                  name: 'parses multiple tokens',
    42                  input: 'hello "moon" goodbye:world',
    43                  expected: [
    44                      { type: SearchTokenTypes.FUZZY, value: 'hello' },
    45                      { type: SearchTokenTypes.EXACT, value: 'moon' },
    46                      { type: SearchTokenTypes.KEY_VALUE, key: 'goodbye', value: 'world' },
    47                  ],
    48              },
    49              {
    50                  name: 'parses numbers and symbols',
    51                  input: 'hello-123 "moon-456" goodbye:world-789',
    52                  expected: [
    53                      { type: SearchTokenTypes.FUZZY, value: 'hello-123' },
    54                      { type: SearchTokenTypes.EXACT, value: 'moon-456' },
    55                      { type: SearchTokenTypes.KEY_VALUE, key: 'goodbye', value: 'world-789' },
    56                  ],
    57              },
    58          ];
    59  
    60          test.each(tests.map(Object.values))('%s', (name: string, input: string, expected: Token[]) => {
    61              const result = tokenizeSearch(input);
    62              expect(result).toEqual(expected);
    63          });
    64      });
    65  });