storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/ellipses/ellipses_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     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  
    17  package ellipses
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  )
    23  
    24  // Test tests args with ellipses.
    25  func TestHasEllipses(t *testing.T) {
    26  	testCases := []struct {
    27  		args       []string
    28  		expectedOk bool
    29  	}{
    30  		// Tests for all args without ellipses.
    31  		{
    32  			[]string{"64"},
    33  			false,
    34  		},
    35  		// Found flower braces, still attempt to parse and throw an error.
    36  		{
    37  			[]string{"{1..64}"},
    38  			true,
    39  		},
    40  		{
    41  			[]string{"{1..2..}"},
    42  			true,
    43  		},
    44  		// Test for valid input.
    45  		{
    46  			[]string{"1...64"},
    47  			true,
    48  		},
    49  		{
    50  			[]string{"{1...2O}"},
    51  			true,
    52  		},
    53  		{
    54  			[]string{"..."},
    55  			true,
    56  		},
    57  		{
    58  			[]string{"{-1...1}"},
    59  			true,
    60  		},
    61  		{
    62  			[]string{"{0...-1}"},
    63  			true,
    64  		},
    65  		{
    66  			[]string{"{1....4}"},
    67  			true,
    68  		},
    69  		{
    70  			[]string{"{1...64}"},
    71  			true,
    72  		},
    73  		{
    74  			[]string{"{...}"},
    75  			true,
    76  		},
    77  		{
    78  			[]string{"{1...64}", "{65...128}"},
    79  			true,
    80  		},
    81  		{
    82  			[]string{"http://minio{2...3}/export/set{1...64}"},
    83  			true,
    84  		},
    85  		{
    86  			[]string{
    87  				"http://minio{2...3}/export/set{1...64}",
    88  				"http://minio{2...3}/export/set{65...128}",
    89  			},
    90  			true,
    91  		},
    92  		{
    93  			[]string{
    94  				"mydisk-{a...z}{1...20}",
    95  			},
    96  			true,
    97  		},
    98  		{
    99  
   100  			[]string{
   101  				"mydisk-{1...4}{1..2.}",
   102  			},
   103  			true,
   104  		},
   105  	}
   106  
   107  	for i, testCase := range testCases {
   108  		t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
   109  			gotOk := HasEllipses(testCase.args...)
   110  			if gotOk != testCase.expectedOk {
   111  				t.Errorf("Expected %t, got %t", testCase.expectedOk, gotOk)
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  // Test tests find ellipses patterns.
   118  func TestFindEllipsesPatterns(t *testing.T) {
   119  	testCases := []struct {
   120  		pattern       string
   121  		success       bool
   122  		expectedCount int
   123  	}{
   124  		// Tests for all invalid inputs
   125  		{
   126  			"{1..64}",
   127  			false,
   128  			0,
   129  		},
   130  		{
   131  			"1...64",
   132  			false,
   133  			0,
   134  		},
   135  		{
   136  			"...",
   137  			false,
   138  			0,
   139  		},
   140  		{
   141  			"{1...",
   142  			false,
   143  			0,
   144  		},
   145  		{
   146  			"...64}",
   147  			false,
   148  			0,
   149  		},
   150  		{
   151  			"{...}",
   152  			false,
   153  			0,
   154  		},
   155  		{
   156  			"{-1...1}",
   157  			false,
   158  			0,
   159  		},
   160  		{
   161  			"{0...-1}",
   162  			false,
   163  			0,
   164  		},
   165  		{
   166  			"{1...2O}",
   167  			false,
   168  			0,
   169  		},
   170  		{
   171  			"{64...1}",
   172  			false,
   173  			0,
   174  		},
   175  		{
   176  			"{1....4}",
   177  			false,
   178  			0,
   179  		},
   180  		{
   181  			"mydisk-{a...z}{1...20}",
   182  			false,
   183  			0,
   184  		},
   185  		{
   186  			"mydisk-{1...4}{1..2.}",
   187  			false,
   188  			0,
   189  		},
   190  		{
   191  			"{1..2.}-mydisk-{1...4}",
   192  			false,
   193  			0,
   194  		},
   195  		{
   196  			"{{1...4}}",
   197  			false,
   198  			0,
   199  		},
   200  		{
   201  			"{4...02}",
   202  			false,
   203  			0,
   204  		},
   205  		{
   206  			"{f...z}",
   207  			false,
   208  			0,
   209  		},
   210  		// Test for valid input.
   211  		{
   212  			"{1...64}",
   213  			true,
   214  			64,
   215  		},
   216  		{
   217  			"{1...64} {65...128}",
   218  			true,
   219  			4096,
   220  		},
   221  		{
   222  			"{01...036}",
   223  			true,
   224  			36,
   225  		},
   226  		{
   227  			"{001...036}",
   228  			true,
   229  			36,
   230  		},
   231  		{
   232  			"{1...a}",
   233  			true,
   234  			10,
   235  		},
   236  	}
   237  
   238  	for i, testCase := range testCases {
   239  		t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
   240  			argP, err := FindEllipsesPatterns(testCase.pattern)
   241  			if err != nil && testCase.success {
   242  				t.Errorf("Expected success but failed instead %s", err)
   243  			}
   244  			if err == nil && !testCase.success {
   245  				t.Errorf("Expected failure but passed instead")
   246  			}
   247  			if err == nil {
   248  				gotCount := len(argP.Expand())
   249  				if gotCount != testCase.expectedCount {
   250  					t.Errorf("Expected %d, got %d", testCase.expectedCount, gotCount)
   251  				}
   252  			}
   253  		})
   254  	}
   255  }