github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/config/regexp_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/lyft/flytestdlib/internal/utils"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRegexp_MarshalJSON(t *testing.T) {
    15  	validRegexps := []string{
    16  		"",
    17  		".*",
    18  		"^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$",
    19  	}
    20  
    21  	for i, validRegexp := range validRegexps {
    22  		t.Run(fmt.Sprintf("Valid %v", i), func(t *testing.T) {
    23  			expected := Regexp{Regexp: utils.MustCompileRegexp(validRegexp)}
    24  
    25  			b, err := expected.MarshalJSON()
    26  			assert.NoError(t, err)
    27  
    28  			actual := Regexp{}
    29  			err = actual.UnmarshalJSON(b)
    30  			assert.NoError(t, err)
    31  
    32  			assert.True(t, reflect.DeepEqual(expected, actual))
    33  		})
    34  	}
    35  }
    36  
    37  func TestRegexp_UnmarshalJSON(t *testing.T) {
    38  	invalidValues := []interface{}{
    39  		"^(",
    40  		123,
    41  		true,
    42  	}
    43  	for i, invalidRegexp := range invalidValues {
    44  		t.Run(fmt.Sprintf("Invalid %v", i), func(t *testing.T) {
    45  			raw, err := json.Marshal(invalidRegexp)
    46  			assert.NoError(t, err)
    47  
    48  			actual := Regexp{}
    49  			err = actual.UnmarshalJSON(raw)
    50  			assert.Error(t, err)
    51  		})
    52  	}
    53  
    54  	t.Run("Empty regexp", func(t *testing.T) {
    55  		expected := Regexp{}
    56  
    57  		actual := Regexp{}
    58  		err := actual.UnmarshalJSON([]byte{})
    59  		assert.NoError(t, err)
    60  		assert.True(t, reflect.DeepEqual(expected, actual))
    61  	})
    62  }