go.arsenm.dev/pcre@v0.0.0-20220530205550-74594f6c8b0e/pcre_test.go (about)

     1  package pcre_test
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  	"testing"
     7  
     8  	"go.arsenm.dev/pcre"
     9  )
    10  
    11  func TestCompileError(t *testing.T) {
    12  	r, err := pcre.Compile("(")
    13  	if err == nil {
    14  		t.Error("expected error, got nil")
    15  	}
    16  	defer r.Close()
    17  }
    18  
    19  func TestMatch(t *testing.T) {
    20  	r := pcre.MustCompile(`\d+ (?=USD)`)
    21  	defer r.Close()
    22  
    23  	matches := r.MatchString("9000 USD")
    24  	if !matches {
    25  		t.Error("expected 9000 USD to match")
    26  	}
    27  
    28  	matches = r.MatchString("9000 RUB")
    29  	if matches {
    30  		t.Error("expected 9000 RUB not to match")
    31  	}
    32  
    33  	matches = r.MatchString("800 USD")
    34  	if !matches {
    35  		t.Error("expected 800 USD to match")
    36  	}
    37  
    38  	matches = r.MatchString("700 CAD")
    39  	if matches {
    40  		t.Error("expected 700 CAD not to match")
    41  	}
    42  
    43  	matches = r.Match([]byte("8 USD"))
    44  	if !matches {
    45  		t.Error("expected 8 USD to match")
    46  	}
    47  }
    48  
    49  func TestMatchUngreedy(t *testing.T) {
    50  	r := pcre.MustCompileOpts(`Hello, (.+)\.`, pcre.Ungreedy)
    51  	defer r.Close()
    52  
    53  	submatches := r.FindAllStringSubmatch("Hello, World. Hello, pcre2.", 1)
    54  	if submatches[0][1] != "World" {
    55  		t.Errorf("expected World, got %s", submatches[0][1])
    56  	}
    57  
    58  	matches := r.MatchString("hello, world.")
    59  	if matches {
    60  		t.Error("expected lowercase 'hello, world' not to match")
    61  	}
    62  }
    63  
    64  func TestReplace(t *testing.T) {
    65  	r := pcre.MustCompile(`(\d+)\.\d+`)
    66  	defer r.Close()
    67  
    68  	testStr := "123.54321 Test"
    69  
    70  	newStr := r.ReplaceAllString(testStr, "${1}.12345")
    71  	if newStr != "123.12345 Test" {
    72  		t.Errorf(`expected "123.12345 Test", got "%s"`, newStr)
    73  	}
    74  
    75  	newStr = r.ReplaceAllString(testStr, "${9}.12345")
    76  	if newStr != ".12345 Test" {
    77  		t.Errorf(`expected ".12345 Test", got "%s"`, newStr)
    78  	}
    79  
    80  	newStr = r.ReplaceAllString(testStr, "${hi}.12345")
    81  	if newStr != ".12345 Test" {
    82  		t.Errorf(`expected ".12345 Test", got "%s"`, newStr)
    83  	}
    84  
    85  	newStr = r.ReplaceAllLiteralString(testStr, "${1}.12345")
    86  	if newStr != "${1}.12345 Test" {
    87  		t.Errorf(`expected "${1}.12345 Test", got "%s"`, newStr)
    88  	}
    89  
    90  	newStr = r.ReplaceAllStringFunc(testStr, func(s string) string {
    91  		return strings.Replace(s, ".", ",", -1)
    92  	})
    93  	if newStr != "123,54321 Test" {
    94  		t.Errorf(`expected "123,54321 Test", got "%s"`, newStr)
    95  	}
    96  }
    97  
    98  func TestSplit(t *testing.T) {
    99  	r := pcre.MustCompile("a*")
   100  	defer r.Close()
   101  
   102  	split := r.Split("abaabaccadaaae", 5)
   103  	expected := [5]string{"", "b", "b", "c", "cadaaae"}
   104  
   105  	if *(*[5]string)(split) != expected {
   106  		t.Errorf("expected %v, got %v", expected, split)
   107  	}
   108  
   109  	split = r.Split("", 0)
   110  	if split != nil {
   111  		t.Errorf("expected nil, got %v", split)
   112  	}
   113  
   114  	split = r.Split("", 5)
   115  	if split[0] != "" {
   116  		t.Errorf(`expected []string{""}, got %v`, split)
   117  	}
   118  }
   119  
   120  func TestFind(t *testing.T) {
   121  	r := pcre.MustCompile(`(\d+)`)
   122  	defer r.Close()
   123  
   124  	testStr := "3 times 4 is 12"
   125  
   126  	matches := r.FindAllString(testStr, -1)
   127  	if len(matches) != 3 {
   128  		t.Errorf("expected length 3, got %d", len(matches))
   129  	}
   130  
   131  	matches = r.FindAllString(testStr, 2)
   132  	if len(matches) != 2 {
   133  		t.Errorf("expected length 2, got %d", len(matches))
   134  	}
   135  	if matches[0] != "3" || matches[1] != "4" {
   136  		t.Errorf("expected [3 4], got %v", matches)
   137  	}
   138  
   139  	index := r.FindStringIndex(testStr)
   140  	if index[0] != 0 || index[1] != 1 {
   141  		t.Errorf("expected [0 1], got %v", index)
   142  	}
   143  
   144  	submatch := r.FindStringSubmatch(testStr)
   145  	if submatch[0] != "3" {
   146  		t.Errorf("expected 3, got %s", submatch[0])
   147  	}
   148  
   149  	index = r.FindStringSubmatchIndex(testStr)
   150  	if *(*[4]int)(index) != [4]int{0, 1, 0, 1} {
   151  		t.Errorf("expected [0 1 0 1], got %v", index)
   152  	}
   153  
   154  	submatches := r.FindAllStringSubmatchIndex(testStr, 2)
   155  	if len(submatches) != 2 {
   156  		t.Errorf("expected length 2, got %d", len(submatches))
   157  	}
   158  
   159  	expected := [2][4]int{{0, 1, 0, 1}, {8, 9, 8, 9}}
   160  	if *(*[4]int)(submatches[0]) != expected[0] {
   161  		t.Errorf("expected %v, got %v", expected[0], submatches[0])
   162  	}
   163  	if *(*[4]int)(submatches[1]) != expected[1] {
   164  		t.Errorf("expected %v, got %v", expected[0], submatches[0])
   165  	}
   166  }
   167  
   168  func TestSubexpIndex(t *testing.T) {
   169  	r := pcre.MustCompile(`(?P<number>\d)`)
   170  	defer r.Close()
   171  
   172  	index := r.SubexpIndex("number")
   173  	if index != 1 {
   174  		t.Errorf("expected 1, got %d", index)
   175  	}
   176  
   177  	index = r.SubexpIndex("string")
   178  	if index != -1 {
   179  		t.Errorf("expected -1, got %d", index)
   180  	}
   181  
   182  	num := r.NumSubexp()
   183  	if num != 1 {
   184  		t.Errorf("expected 1, got %d", num)
   185  	}
   186  }
   187  
   188  func TestConcurrency(t *testing.T) {
   189  	r := pcre.MustCompile(`\d*`)
   190  	defer r.Close()
   191  
   192  	wg := &sync.WaitGroup{}
   193  
   194  	wg.Add(1)
   195  	go func() {
   196  		defer wg.Done()
   197  
   198  		found := r.FindString("Test string 12345")
   199  		if found != "12345" {
   200  			t.Errorf("expected 12345, got %s", found)
   201  		}
   202  	}()
   203  
   204  	wg.Add(1)
   205  	go func() {
   206  		defer wg.Done()
   207  
   208  		matched := r.MatchString("Hello")
   209  		if matched {
   210  			t.Errorf("Expected Hello not to match")
   211  		}
   212  	}()
   213  
   214  	wg.Add(1)
   215  	go func() {
   216  		defer wg.Done()
   217  
   218  		matched := r.MatchString("54321")
   219  		if !matched {
   220  			t.Errorf("expected 54321 to match")
   221  		}
   222  	}()
   223  
   224  	wg.Wait()
   225  }
   226  
   227  func TestString(t *testing.T) {
   228  	const expr = `()`
   229  
   230  	r := pcre.MustCompile(expr)
   231  	defer r.Close()
   232  
   233  	if r.String() != expr {
   234  		t.Errorf("expected %s, got %s", expr, r.String())
   235  	}
   236  }