github.com/Tyktechnologies/tyk@v2.9.5+incompatible/regexp/regexp_test.go (about)

     1  package regexp
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestCompile(t *testing.T) {
    10  	ResetCache(defaultCacheItemTTL, true)
    11  	// 1st miss
    12  	rx, err := Compile("^abc.*$")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	if rx.FromCache {
    17  		t.Error("Regexp shoul not be from cache")
    18  	}
    19  	if !rx.MatchString("abcxyz") {
    20  		t.Error("String didn't match to compiled regexp: ", rx.String())
    21  	}
    22  	// 2nd hit
    23  	rx2, err := Compile("^abc.*$")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	if !rx2.FromCache {
    28  		t.Error("Regexp shoul be from cache")
    29  	}
    30  	if !rx2.MatchString("abcxyz") {
    31  		t.Error("String didn't match to compiled regexp: ", rx2.String())
    32  	}
    33  }
    34  
    35  func BenchmarkRegExpCompile(b *testing.B) {
    36  	ResetCache(defaultCacheItemTTL, true)
    37  
    38  	b.ReportAllocs()
    39  
    40  	var rx *Regexp
    41  	var err error
    42  
    43  	for i := 0; i < b.N; i++ {
    44  		rx, err = Compile("^abc.*$")
    45  		if err != nil {
    46  			b.Fatal(err)
    47  		}
    48  	}
    49  
    50  	b.Log(rx)
    51  }
    52  
    53  func TestCompilePOSIX(t *testing.T) {
    54  	ResetCache(defaultCacheItemTTL, true)
    55  	// 1st miss
    56  	rx, err := CompilePOSIX("^abc.*$")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if rx.FromCache {
    61  		t.Error("Regexp shoul not be from cache")
    62  	}
    63  	if !rx.MatchString("abcxyz") {
    64  		t.Error("String didn't match to compiled regexp: ", rx.String())
    65  	}
    66  	// 2nd hit
    67  	rx2, err := CompilePOSIX("^abc.*$")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if !rx2.FromCache {
    72  		t.Error("Regexp shoul be from cache")
    73  	}
    74  	if !rx2.MatchString("abcxyz") {
    75  		t.Error("String didn't match to compiled regexp: ", rx2.String())
    76  	}
    77  }
    78  
    79  func BenchmarkRegExpCompilePOSIX(b *testing.B) {
    80  	ResetCache(defaultCacheItemTTL, true)
    81  
    82  	b.ReportAllocs()
    83  
    84  	var rx *Regexp
    85  	var err error
    86  
    87  	for i := 0; i < b.N; i++ {
    88  		rx, err = CompilePOSIX("^abc.*$")
    89  		if err != nil {
    90  			b.Fatal(err)
    91  		}
    92  	}
    93  
    94  	b.Log(rx)
    95  }
    96  
    97  func TestMustCompile(t *testing.T) {
    98  	ResetCache(defaultCacheItemTTL, true)
    99  	// 1st miss
   100  	rx := MustCompile("^abc.*$")
   101  	if rx.FromCache {
   102  		t.Error("Regexp shoul not be from cache")
   103  	}
   104  	if !rx.MatchString("abcxyz") {
   105  		t.Error("String didn't match to compiled regexp: ", rx.String())
   106  	}
   107  	// 2nd hit
   108  	rx2 := MustCompile("^abc.*$")
   109  	if !rx2.FromCache {
   110  		t.Error("Regexp shoul be from cache")
   111  	}
   112  	if !rx2.MatchString("abcxyz") {
   113  		t.Error("String didn't match to compiled regexp: ", rx2.String())
   114  	}
   115  	// catch panic
   116  	panicked := false
   117  	func() {
   118  		defer func() {
   119  			panicked = recover() != nil
   120  		}()
   121  		MustCompile("*")
   122  	}()
   123  	if !panicked {
   124  		t.Error("Expected panic but it didn't happen")
   125  	}
   126  }
   127  
   128  func BenchmarkRegExpMustCompile(b *testing.B) {
   129  	ResetCache(defaultCacheItemTTL, true)
   130  
   131  	b.ReportAllocs()
   132  
   133  	var rx *Regexp
   134  
   135  	for i := 0; i < b.N; i++ {
   136  		rx = MustCompile("^abc.*$")
   137  	}
   138  
   139  	b.Log(rx)
   140  }
   141  
   142  func TestMustCompilePOSIX(t *testing.T) {
   143  	ResetCache(defaultCacheItemTTL, true)
   144  	// 1st miss
   145  	rx := MustCompilePOSIX("^abc.*$")
   146  	if rx.FromCache {
   147  		t.Error("Regexp shoul not be from cache")
   148  	}
   149  	if !rx.MatchString("abcxyz") {
   150  		t.Error("String didn't match to compiled regexp: ", rx.String())
   151  	}
   152  	// 2nd hit
   153  	rx2 := MustCompilePOSIX("^abc.*$")
   154  	if !rx2.FromCache {
   155  		t.Error("Regexp shoul be from cache")
   156  	}
   157  	if !rx2.MatchString("abcxyz") {
   158  		t.Error("String didn't match to compiled regexp: ", rx2.String())
   159  	}
   160  	// catch panic
   161  	panicked := false
   162  	func() {
   163  		defer func() {
   164  			panicked = recover() != nil
   165  		}()
   166  		MustCompilePOSIX("*")
   167  	}()
   168  	if !panicked {
   169  		t.Error("Expected panic but it didn't happen")
   170  	}
   171  }
   172  
   173  func BenchmarkRegExpMustCompilePOSIX(b *testing.B) {
   174  	ResetCache(defaultCacheItemTTL, true)
   175  	b.ReportAllocs()
   176  
   177  	var rx *Regexp
   178  
   179  	for i := 0; i < b.N; i++ {
   180  		rx = MustCompilePOSIX("^abc.*$")
   181  	}
   182  
   183  	b.Log(rx)
   184  }
   185  
   186  func TestMatchString(t *testing.T) {
   187  	ResetCache(defaultCacheItemTTL, true)
   188  	// 1st miss
   189  	matched, err := MatchString("^abc.*$", "abcedfxyz")
   190  	if err != nil {
   191  		t.Fatal(err)
   192  	}
   193  	if !matched {
   194  		t.Error("String didn't match")
   195  	}
   196  	// 2nd hit
   197  	matched, err = MatchString("^abc.*$", "abcedfxyz")
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  	if !matched {
   202  		t.Error("String didn't match")
   203  	}
   204  }
   205  
   206  func TestMatchStringFailed(t *testing.T) {
   207  	ResetCache(defaultCacheItemTTL, true)
   208  	_, err := MatchString("*", "abcedfxyz")
   209  	if err == nil {
   210  		t.Error("Expected error")
   211  	}
   212  }
   213  
   214  func TestMatchStringRegexpNotSet(t *testing.T) {
   215  	ResetCache(defaultCacheItemTTL, true)
   216  	rx := Regexp{}
   217  	matched := rx.MatchString("abcdefxyz")
   218  	if matched {
   219  		t.Error("Expected not to match")
   220  	}
   221  }
   222  
   223  func BenchmarkRegExpMatchString(b *testing.B) {
   224  	ResetCache(defaultCacheItemTTL, true)
   225  	b.ReportAllocs()
   226  
   227  	matched := false
   228  	var err error
   229  
   230  	for i := 0; i < b.N; i++ {
   231  		matched, err = MatchString("^abc.*$", "abcdefxyz")
   232  		if err != nil {
   233  			b.Fatal(err)
   234  		}
   235  		if !matched {
   236  			b.Error("String didn't match")
   237  		}
   238  	}
   239  }
   240  
   241  func TestMatch(t *testing.T) {
   242  	ResetCache(defaultCacheItemTTL, true)
   243  	data := []byte("abcdefxyz")
   244  	// 1st miss
   245  	matched, err := Match("^abc.*$", data)
   246  	if err != nil {
   247  		t.Fatal(err)
   248  	}
   249  	if !matched {
   250  		t.Error("String didn't match")
   251  	}
   252  	// 2nd hit
   253  	matched, err = Match("^abc.*$", data)
   254  	if err != nil {
   255  		t.Fatal(err)
   256  	}
   257  	if !matched {
   258  		t.Error("String didn't match")
   259  	}
   260  }
   261  
   262  func TestMatchFailed(t *testing.T) {
   263  	ResetCache(defaultCacheItemTTL, true)
   264  	_, err := Match("*", []byte("abcdefxyz"))
   265  	if err == nil {
   266  		t.Error("Expected error")
   267  	}
   268  }
   269  
   270  func TestMatchRegexpNotSet(t *testing.T) {
   271  	ResetCache(defaultCacheItemTTL, true)
   272  	rx := Regexp{}
   273  	matched := rx.Match([]byte("abcdefxyz"))
   274  	if matched {
   275  		t.Error("Expected not to match")
   276  	}
   277  }
   278  
   279  func BenchmarkRegExpMatch(b *testing.B) {
   280  	ResetCache(defaultCacheItemTTL, true)
   281  
   282  	b.ReportAllocs()
   283  
   284  	data := []byte("abcdefxyz")
   285  
   286  	matched := false
   287  	var err error
   288  
   289  	for i := 0; i < b.N; i++ {
   290  		matched, err = Match("^abc.*$", data)
   291  		if err != nil {
   292  			b.Fatal(err)
   293  		}
   294  		if !matched {
   295  			b.Error("Data didn't match")
   296  		}
   297  	}
   298  }
   299  
   300  func TestString(t *testing.T) {
   301  	rx, err := Compile("^abc.*$")
   302  	if err != nil {
   303  		t.Fatal(err)
   304  	}
   305  	if rx.String() != "^abc.*$" {
   306  		t.Error("String didn't match to compiled regexp expression")
   307  	}
   308  }
   309  
   310  func TestStringRegexpNotSet(t *testing.T) {
   311  	rx := Regexp{}
   312  	if rx.String() != "" {
   313  		t.Error("Empty string expected")
   314  	}
   315  }
   316  
   317  func BenchmarkRegExpString(b *testing.B) {
   318  	b.ReportAllocs()
   319  
   320  	rx, err := Compile("^abc.*$")
   321  	if err != nil {
   322  		b.Fatal(err)
   323  	}
   324  	str := ""
   325  
   326  	for i := 0; i < b.N; i++ {
   327  		str = rx.String()
   328  	}
   329  
   330  	b.Log(str)
   331  }
   332  
   333  func TestCopy(t *testing.T) {
   334  	rx, err := Compile("^abc.*$")
   335  	if err != nil {
   336  		t.Fatal(err)
   337  	}
   338  	rxCopy := rx.Copy()
   339  	if rx.FromCache != rxCopy.FromCache {
   340  		t.Error("Copy's FromCache is not equal")
   341  	}
   342  	if rx.String() != rxCopy.String() {
   343  		t.Error("Copy's Regexp is not equal")
   344  	}
   345  }
   346  
   347  func TestReplaceAllString(t *testing.T) {
   348  	ResetCache(defaultCacheItemTTL, true)
   349  	// 1st miss
   350  	rx := MustCompile("abc")
   351  	newStr := rx.ReplaceAllString("qweabcxyz", "123")
   352  	if newStr != "qwe123xyz" {
   353  		t.Error("Expected 'qwe123xyz'. Got:", newStr)
   354  	}
   355  	// 2nd hit
   356  	newStr2 := rx.ReplaceAllString("qweabcxyz", "123")
   357  	if newStr2 != "qwe123xyz" {
   358  		t.Error("Expected 'qwe123xyz'. Got:", newStr2)
   359  	}
   360  }
   361  
   362  func TestReplaceAllStringRegexpNotSet(t *testing.T) {
   363  	ResetCache(defaultCacheItemTTL, true)
   364  	rx := &Regexp{}
   365  	newStr := rx.ReplaceAllString("qweabcxyz", "123")
   366  	if newStr != "" {
   367  		t.Error("Expected empty string")
   368  	}
   369  }
   370  
   371  func BenchmarkRegexpReplaceAllString(b *testing.B) {
   372  	b.ReportAllocs()
   373  
   374  	ResetCache(defaultCacheItemTTL, true)
   375  
   376  	rx := MustCompile("abc")
   377  	str := ""
   378  
   379  	for i := 0; i < b.N; i++ {
   380  		str = rx.ReplaceAllString("qweabcxyz", "123")
   381  	}
   382  
   383  	b.Log(str)
   384  
   385  }
   386  
   387  func TestReplaceAllLiteralString(t *testing.T) {
   388  	ResetCache(defaultCacheItemTTL, true)
   389  	// 1st miss
   390  	rx := MustCompile("abc")
   391  	newStr := rx.ReplaceAllLiteralString("qweabcxyz", "123")
   392  	if newStr != "qwe123xyz" {
   393  		t.Error("Expected 'qwe123xyz'. Got:", newStr)
   394  	}
   395  	// 2nd hit
   396  	newStr2 := rx.ReplaceAllLiteralString("qweabcxyz", "123")
   397  	if newStr2 != "qwe123xyz" {
   398  		t.Error("Expected 'qwe123xyz'. Got:", newStr2)
   399  	}
   400  }
   401  
   402  func TestReplaceAllLiteralStringRegexpNotSet(t *testing.T) {
   403  	ResetCache(defaultCacheItemTTL, true)
   404  	rx := &Regexp{}
   405  	newStr := rx.ReplaceAllLiteralString("qweabcxyz", "123")
   406  	if newStr != "" {
   407  		t.Error("Expected empty string")
   408  	}
   409  }
   410  
   411  func BenchmarkRegexpReplaceAllLiteralString(b *testing.B) {
   412  	b.ReportAllocs()
   413  
   414  	ResetCache(defaultCacheItemTTL, true)
   415  
   416  	rx := MustCompile("abc")
   417  	str := ""
   418  
   419  	for i := 0; i < b.N; i++ {
   420  		str = rx.ReplaceAllLiteralString("qweabcxyz", "123")
   421  	}
   422  
   423  	b.Log(str)
   424  
   425  }
   426  
   427  func TestReplaceAllStringFunc(t *testing.T) {
   428  	ResetCache(defaultCacheItemTTL, true)
   429  
   430  	f := func(s string) string {
   431  		return strings.ToUpper(s)
   432  	}
   433  
   434  	// 1st miss
   435  	rx := MustCompile("abc")
   436  	newStr := rx.ReplaceAllStringFunc("qweabcxyz", f)
   437  	if newStr != "qweABCxyz" {
   438  		t.Error("Expected 'qweABCxyz'. Got:", newStr)
   439  	}
   440  	// 2nd hit
   441  	newStr2 := rx.ReplaceAllStringFunc("qweabcxyz", f)
   442  	if newStr2 != "qweABCxyz" {
   443  		t.Error("Expected 'qweABCxyz'. Got:", newStr2)
   444  	}
   445  }
   446  
   447  func TestReplaceAllStringFuncRegexpNotSet(t *testing.T) {
   448  	ResetCache(defaultCacheItemTTL, true)
   449  
   450  	f := func(s string) string {
   451  		return strings.ToUpper(s)
   452  	}
   453  
   454  	rx := &Regexp{}
   455  	newStr := rx.ReplaceAllStringFunc("qweabcxyz", f)
   456  	if newStr != "" {
   457  		t.Error("Expected empty string returned")
   458  	}
   459  }
   460  
   461  func BenchmarkRegexpReplaceAllStringFunc(b *testing.B) {
   462  	b.ReportAllocs()
   463  
   464  	ResetCache(defaultCacheItemTTL, true)
   465  
   466  	f := func(s string) string {
   467  		return strings.ToUpper(s)
   468  	}
   469  
   470  	rx := MustCompile("abc")
   471  	str := ""
   472  
   473  	for i := 0; i < b.N; i++ {
   474  		str = rx.ReplaceAllStringFunc("qweabcxyz", f)
   475  	}
   476  
   477  	b.Log(str)
   478  
   479  }
   480  
   481  func TestFindAllString(t *testing.T) {
   482  	ResetCache(defaultCacheItemTTL, true)
   483  
   484  	// 1st miss
   485  	rx := MustCompile("abc")
   486  	res := rx.FindAllString("qweabcxyzabc123abcz", -1)
   487  	expectedRes := []string{"abc", "abc", "abc"}
   488  	if !reflect.DeepEqual(res, expectedRes) {
   489  		t.Error("Expected :", expectedRes, " Got:", res)
   490  	}
   491  	// 2nd hit
   492  	res2 := rx.FindAllString("qweabcxyzabc123abcz", -1)
   493  	if !reflect.DeepEqual(res2, expectedRes) {
   494  		t.Error("Expected :", expectedRes, " Got:", res2)
   495  	}
   496  }
   497  
   498  func TestFindAllStringRegexpNotSet(t *testing.T) {
   499  	ResetCache(defaultCacheItemTTL, true)
   500  
   501  	rx := &Regexp{}
   502  
   503  	res := rx.FindAllString("qweabcxyzabc123abcz", -1)
   504  	if len(res) > 0 {
   505  		t.Error("Expected 0 length slice returned. Got:", res)
   506  	}
   507  }
   508  
   509  func BenchmarkRegexpFindAllString(b *testing.B) {
   510  	b.ReportAllocs()
   511  
   512  	ResetCache(defaultCacheItemTTL, true)
   513  
   514  	rx := MustCompile("abc")
   515  	var res []string
   516  
   517  	for i := 0; i < b.N; i++ {
   518  		res = rx.FindAllString("qweabcxyzabc123abcz", -1)
   519  	}
   520  
   521  	b.Log(res)
   522  }
   523  
   524  func TestFindAllStringSubmatch(t *testing.T) {
   525  	ResetCache(defaultCacheItemTTL, true)
   526  
   527  	// 1st miss
   528  	rx := MustCompile("abc")
   529  	res := rx.FindAllStringSubmatch("qweabcxyzabc123abcz", -1)
   530  	expectedRes := [][]string{
   531  		{"abc"},
   532  		{"abc"},
   533  		{"abc"},
   534  	}
   535  	if !reflect.DeepEqual(res, expectedRes) {
   536  		t.Error("Expected :", expectedRes, " Got:", res)
   537  	}
   538  	// 2nd hit
   539  	res2 := rx.FindAllStringSubmatch("qweabcxyzabc123abcz", -1)
   540  	if !reflect.DeepEqual(res2, expectedRes) {
   541  		t.Error("Expected :", expectedRes, " Got:", res2)
   542  	}
   543  }
   544  
   545  func TestTestFindAllStringSubmatchRegexpNotSet(t *testing.T) {
   546  	ResetCache(defaultCacheItemTTL, true)
   547  
   548  	rx := &Regexp{}
   549  
   550  	res := rx.FindAllStringSubmatch("qweabcxyzabc123abcz", -1)
   551  	if len(res) > 0 {
   552  		t.Error("Expected 0 length slice returned. Got:", res)
   553  	}
   554  }
   555  
   556  func BenchmarkRegexpFindAllStringSubmatch(b *testing.B) {
   557  	b.ReportAllocs()
   558  
   559  	ResetCache(defaultCacheItemTTL, true)
   560  
   561  	rx := MustCompile("abc")
   562  	var res [][]string
   563  
   564  	for i := 0; i < b.N; i++ {
   565  		res = rx.FindAllStringSubmatch("qweabcxyzabc123abcz", -1)
   566  	}
   567  
   568  	b.Log(res)
   569  }
   570  
   571  func TestFindStringSubmatch(t *testing.T) {
   572  	ResetCache(defaultCacheItemTTL, true)
   573  
   574  	// 1st miss
   575  	rx := MustCompile("abc(\\w)")
   576  	res := rx.FindStringSubmatch("qweabcxyzabc123abcz")
   577  	expectedRes := []string{"abcx", "x"}
   578  
   579  	if !reflect.DeepEqual(res, expectedRes) {
   580  		t.Error("Expected :", expectedRes, " Got:", res)
   581  	}
   582  	// 2nd hit
   583  	res2 := rx.FindStringSubmatch("qweabcxyzabc123abcz")
   584  	if !reflect.DeepEqual(res2, expectedRes) {
   585  		t.Error("Expected :", expectedRes, " Got:", res2)
   586  	}
   587  }
   588  
   589  func TestTestFindStringSubmatchRegexpNotSet(t *testing.T) {
   590  	ResetCache(defaultCacheItemTTL, true)
   591  
   592  	rx := &Regexp{}
   593  
   594  	res := rx.FindStringSubmatch("qweabcxyzabc123abcz")
   595  	if len(res) > 0 {
   596  		t.Error("Expected 0 length slice returned. Got:", res)
   597  	}
   598  }
   599  
   600  func BenchmarkRegexpFindStringSubmatch(b *testing.B) {
   601  	b.ReportAllocs()
   602  
   603  	ResetCache(defaultCacheItemTTL, true)
   604  
   605  	rx := MustCompile("abc(\\w)")
   606  	var res []string
   607  
   608  	for i := 0; i < b.N; i++ {
   609  		res = rx.FindStringSubmatch("qweabcxyzabc123abcz")
   610  	}
   611  
   612  	b.Log(res)
   613  }