go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/generic_libs/tools/string_utils/string_utils.test.ts (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { getLongestCommonPrefix } from './string_utils';
    16  
    17  describe('getLongestCommonPrefix', () => {
    18    it('should work with empty array', () => {
    19      expect(getLongestCommonPrefix([])).toEqual('');
    20    });
    21  
    22    it('should work with single string', () => {
    23      expect(getLongestCommonPrefix(['a_string'])).toEqual('a_string');
    24    });
    25  
    26    it('should work with multiple strings', () => {
    27      expect(
    28        getLongestCommonPrefix(['string1', 'string2', 'string3', 'string4']),
    29      ).toEqual('string');
    30    });
    31  
    32    it('prefix is a member', () => {
    33      expect(
    34        getLongestCommonPrefix(['stringabc', 'string', 'stringdd', 'stringz']),
    35      ).toEqual('string');
    36    });
    37  
    38    it('no shared prefix', () => {
    39      expect(getLongestCommonPrefix(['abc', 'abce', 'aad', 'ze'])).toEqual('');
    40    });
    41  
    42    it('same strings', () => {
    43      expect(getLongestCommonPrefix(['abc', 'abc', 'abc', 'abc'])).toEqual('abc');
    44    });
    45  
    46    it('has empty string', () => {
    47      expect(getLongestCommonPrefix(['', 'aaa', 'aaab', 'aaac'])).toEqual('');
    48    });
    49  });