...

Source file src/github.com/go-openapi/validate/rexp_test.go

Documentation: github.com/go-openapi/validate

     1  // Copyright 2015 go-swagger maintainers
     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 untader the License.
    14  
    15  package validate
    16  
    17  import (
    18  	re "regexp"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  // Save repeated regexp compilation
    26  func Test_compileRegexp(t *testing.T) {
    27  	vrex := new(re.Regexp)
    28  
    29  	rex, err := compileRegexp(".*TestRegexp.*")
    30  	require.NoError(t, err)
    31  	assert.NotNil(t, rex)
    32  	assert.IsType(t, vrex, rex)
    33  
    34  	rex, err = compileRegexp(".*TestRegexp.*")
    35  	require.NoError(t, err)
    36  	assert.NotNil(t, rex)
    37  
    38  	irex, ierr := compileRegexp(".[*InvalidTestRegexp.*")
    39  	require.Error(t, ierr)
    40  	assert.Nil(t, irex)
    41  	assert.IsType(t, vrex, irex)
    42  }
    43  
    44  // Save repeated regexp compilation, with panic on error
    45  func testPanic() {
    46  	mustCompileRegexp(".[*InvalidTestRegexp.*")
    47  }
    48  
    49  func Test_mustCompileRegexp(t *testing.T) {
    50  	vrex := new(re.Regexp)
    51  
    52  	rex := mustCompileRegexp(".*TestRegexp2.*")
    53  	assert.NotNil(t, rex)
    54  	assert.IsType(t, vrex, rex)
    55  
    56  	rex = mustCompileRegexp(".*TestRegexp2.*")
    57  	assert.NotNil(t, rex)
    58  
    59  	assert.Panics(t, testPanic)
    60  }
    61  
    62  func TestRace_compileRegexp(t *testing.T) {
    63  	vrex := new(re.Regexp)
    64  
    65  	patterns := []string{
    66  		".*TestRegexp1.*",
    67  		".*TestRegexp2.*",
    68  		".*TestRegexp3.*",
    69  	}
    70  
    71  	comp := func(pattern string) {
    72  		rex, err := compileRegexp(pattern)
    73  		require.NoError(t, err)
    74  		assert.NotNil(t, rex)
    75  		assert.IsType(t, vrex, rex)
    76  	}
    77  
    78  	for i := 0; i < 20; i++ {
    79  		ii := i
    80  		t.Run(patterns[ii%3], func(t *testing.T) {
    81  			t.Parallel()
    82  			comp(patterns[ii%3])
    83  		})
    84  	}
    85  }
    86  
    87  func TestRace_mustCompileRegexp(t *testing.T) {
    88  	vrex := new(re.Regexp)
    89  
    90  	patterns := []string{
    91  		".*TestRegexp1.*",
    92  		".*TestRegexp2.*",
    93  		".*TestRegexp3.*",
    94  	}
    95  
    96  	comp := func(pattern string) {
    97  		rex := mustCompileRegexp(pattern)
    98  		assert.NotNil(t, rex)
    99  		assert.IsType(t, vrex, rex)
   100  	}
   101  
   102  	for i := 0; i < 20; i++ {
   103  		ii := i
   104  		t.Run(patterns[ii%3], func(t *testing.T) {
   105  			t.Parallel()
   106  			comp(patterns[ii%3])
   107  		})
   108  	}
   109  }
   110  

View as plain text