github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/match-glob-test.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import matchGlob from 'nomad-ui/utils/match-glob';
     7  import { module, test } from 'qunit';
     8  
     9  module('Unit | Utility | match-glob', function () {
    10    module('#_doesMatchPattern', function () {
    11      const edgeCaseTest = 'this is a ϗѾ test';
    12  
    13      module('base cases', function () {
    14        test('it handles an empty pattern', function (assert) {
    15          // arrange
    16          const pattern = '';
    17          const emptyPath = '';
    18          const nonEmptyPath = 'a';
    19  
    20          // act
    21          const matchingResult = matchGlob(pattern, emptyPath);
    22          const nonMatchingResult = matchGlob(pattern, nonEmptyPath);
    23  
    24          // assert
    25          assert.ok(matchingResult, 'Empty pattern should match empty path');
    26          assert.notOk(
    27            nonMatchingResult,
    28            'Empty pattern should not match non-empty path'
    29          );
    30        });
    31  
    32        test('it handles an empty path', function (assert) {
    33          // arrange
    34          const emptyPath = '';
    35          const emptyPattern = '';
    36          const nonEmptyPattern = 'a';
    37  
    38          // act
    39          const matchingResult = matchGlob(emptyPattern, emptyPath);
    40          const nonMatchingResult = matchGlob(nonEmptyPattern, emptyPath);
    41  
    42          // assert
    43          assert.ok(matchingResult, 'Empty path should match empty pattern');
    44          assert.notOk(
    45            nonMatchingResult,
    46            'Empty path should not match non-empty pattern'
    47          );
    48        });
    49  
    50        test('it handles a pattern without a glob', function (assert) {
    51          // arrange
    52          const path = '/foo';
    53          const matchingPattern = '/foo';
    54          const nonMatchingPattern = '/bar';
    55  
    56          // act
    57          const matchingResult = matchGlob(matchingPattern, path);
    58          const nonMatchingResult = matchGlob(nonMatchingPattern, path);
    59  
    60          // assert
    61          assert.ok(matchingResult, 'Matches path correctly.');
    62          assert.notOk(nonMatchingResult, 'Does not match non-matching path.');
    63        });
    64  
    65        test('it handles a pattern that is a lone glob', function (assert) {
    66          // arrange
    67          const path = '/foo';
    68          const glob = '*';
    69  
    70          // act
    71          const matchingResult = matchGlob(glob, path);
    72  
    73          // assert
    74          assert.ok(matchingResult, 'Matches glob.');
    75        });
    76  
    77        test('it matches on leading glob', function (assert) {
    78          // arrange
    79          const pattern = '*bar';
    80          const matchingPath = 'footbar';
    81          const nonMatchingPath = 'rockthecasba';
    82  
    83          // act
    84          const matchingResult = matchGlob(pattern, matchingPath);
    85          const nonMatchingResult = matchGlob(pattern, nonMatchingPath);
    86  
    87          // assert
    88          assert.ok(
    89            matchingResult,
    90            'Correctly matches when leading glob and matching path.'
    91          );
    92          assert.notOk(
    93            nonMatchingResult,
    94            'Does not match when leading glob and non-matching path.'
    95          );
    96        });
    97  
    98        test('it matches on trailing glob', function (assert) {
    99          // arrange
   100          const pattern = 'foo*';
   101          const matchingPath = 'footbar';
   102          const nonMatchingPath = 'bar';
   103  
   104          // act
   105          const matchingResult = matchGlob(pattern, matchingPath);
   106          const nonMatchingResult = matchGlob(pattern, nonMatchingPath);
   107  
   108          // assert
   109          assert.ok(matchingResult, 'Correctly matches on trailing glob.');
   110          assert.notOk(
   111            nonMatchingResult,
   112            'Does not match on trailing glob if pattern does not match.'
   113          );
   114        });
   115  
   116        test('it matches when glob is in middle', function (assert) {
   117          // arrange
   118          const pattern = 'foo*bar';
   119          const matchingPath = 'footbar';
   120          const nonMatchingPath = 'footba';
   121  
   122          // act
   123          const matchingResult = matchGlob(pattern, matchingPath);
   124          const nonMatchingResult = matchGlob(pattern, nonMatchingPath);
   125  
   126          // assert
   127          assert.ok(
   128            matchingResult,
   129            'Correctly matches on glob in middle of path.'
   130          );
   131          assert.notOk(
   132            nonMatchingResult,
   133            'Does not match on glob in middle of path if not full pattern match.'
   134          );
   135        });
   136      });
   137  
   138      module('matching edge cases', function () {
   139        test('it matches when string is between globs', function (assert) {
   140          // arrange
   141          const pattern = '*is *';
   142  
   143          // act
   144          const result = matchGlob(pattern, edgeCaseTest);
   145  
   146          // assert
   147          assert.ok(result);
   148        });
   149  
   150        test('it handles many non-consective globs', function (assert) {
   151          // arrange
   152          const pattern = '*is*a*';
   153  
   154          // act
   155          const result = matchGlob(pattern, edgeCaseTest);
   156  
   157          // assert
   158          assert.ok(result);
   159        });
   160  
   161        test('it handles double globs', function (assert) {
   162          // arrange
   163          const pattern = '**test**';
   164  
   165          // act
   166          const result = matchGlob(pattern, edgeCaseTest);
   167  
   168          // assert
   169          assert.ok(result);
   170        });
   171  
   172        test('it handles many consecutive globs', function (assert) {
   173          // arrange
   174          const pattern = '**is**a***test*';
   175  
   176          // act
   177          const result = matchGlob(pattern, edgeCaseTest);
   178  
   179          // assert
   180          assert.ok(result);
   181        });
   182  
   183        test('it handles white space between globs', function (assert) {
   184          // arrange
   185          const pattern = '* *';
   186  
   187          // act
   188          const result = matchGlob(pattern, edgeCaseTest);
   189  
   190          // assert
   191          assert.ok(result);
   192        });
   193  
   194        test('it handles a pattern of only globs', function (assert) {
   195          // arrange
   196          const pattern = '**********';
   197  
   198          // act
   199          const result = matchGlob(pattern, edgeCaseTest);
   200  
   201          // assert
   202          assert.ok(result);
   203        });
   204  
   205        test('it handles unicode characters', function (assert) {
   206          // arrange
   207          const pattern = `*Ѿ*`;
   208  
   209          // act
   210          const result = matchGlob(pattern, edgeCaseTest);
   211  
   212          // assert
   213          assert.ok(result);
   214        });
   215  
   216        test('it handles mixed ASCII codes', function (assert) {
   217          // arrange
   218          const pattern = `*is a ϗѾ *`;
   219  
   220          // act
   221          const result = matchGlob(pattern, edgeCaseTest);
   222  
   223          // assert
   224          assert.ok(result);
   225        });
   226      });
   227  
   228      module('non-matching edge cases', function () {
   229        const failingCases = [
   230          {
   231            case: 'test*',
   232            message: 'Implicit substring match',
   233          },
   234          {
   235            case: '*is',
   236            message: 'Parial match',
   237          },
   238          {
   239            case: '*no*',
   240            message: 'Globs without match between them',
   241          },
   242          {
   243            case: ' ',
   244            message: 'Plain white space',
   245          },
   246          {
   247            case: '* ',
   248            message: 'Trailing white space',
   249          },
   250          {
   251            case: ' *',
   252            message: 'Leading white space',
   253          },
   254          {
   255            case: '*ʤ*',
   256            message: 'Non-matching unicode',
   257          },
   258          {
   259            case: 'this*this is a test',
   260            message: 'Repeated prefix',
   261          },
   262        ];
   263  
   264        failingCases.forEach(({ case: failingPattern, message }) => {
   265          test('should fail the specified cases', function (assert) {
   266            const result = matchGlob(failingPattern, edgeCaseTest);
   267            assert.notOk(result, `${message} should not match.`);
   268          });
   269        });
   270      });
   271    });
   272  });