github.com/leanovate/gopter@v0.2.9/gen/regex_test.go (about)

     1  package gen_test
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/leanovate/gopter/gen"
     9  )
    10  
    11  func TestRegexMatch(t *testing.T) {
    12  	regexs := []string{
    13  		"[a-z][0-9a-zA-Z]*",
    14  		"AB[0-9]+",
    15  		"1?(zero|one)0",
    16  		"ABCD.+1234",
    17  		"^[0-9]{3}[A-Z]{5,}[a-z]{10,20}$",
    18  		"(?s)[^0-9]*ABCD.*1234",
    19  	}
    20  	for _, regex := range regexs {
    21  		pattern, err := regexp.Compile(regex)
    22  		if err != nil {
    23  			t.Error("Invalid regex", err)
    24  		}
    25  		commonGeneratorTest(t, fmt.Sprintf("matches for %s", regex), gen.RegexMatch(regex), func(value interface{}) bool {
    26  			str, ok := value.(string)
    27  			return ok && pattern.MatchString(str)
    28  		})
    29  	}
    30  
    31  	gen := gen.RegexMatch("]]}})Invalid{]]]")
    32  	value, ok := gen.Sample()
    33  	if ok || value != nil {
    34  		t.Errorf("Invalid value: %#v", value)
    35  	}
    36  }