github.com/enetx/g@v1.0.80/tests/string_regexp_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"reflect"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/enetx/g"
     9  )
    10  
    11  func TestReplaceRegexp(t *testing.T) {
    12  	testCases := []struct {
    13  		input     g.String
    14  		pattern   *regexp.Regexp
    15  		newString g.String
    16  		expected  g.String
    17  	}{
    18  		// Test case 1: Regular replacement
    19  		{
    20  			input:     "Hello, world!",
    21  			pattern:   regexp.MustCompile(`\bworld\b`),
    22  			newString: "universe",
    23  			expected:  "Hello, universe!",
    24  		},
    25  		// Test case 2: Replacement with empty string
    26  		{
    27  			input:     "apple, orange, apple, banana",
    28  			pattern:   regexp.MustCompile(`\bapple\b`),
    29  			newString: "",
    30  			expected:  ", orange, , banana",
    31  		},
    32  		// Test case 3: Replacement with special characters
    33  		{
    34  			input:     "1 + 2 = 3",
    35  			pattern:   regexp.MustCompile(`\d`),
    36  			newString: "x",
    37  			expected:  "x + x = x",
    38  		},
    39  		// Test case 4: No match
    40  		{
    41  			input:     "Hello, world!",
    42  			pattern:   regexp.MustCompile(`\buniverse\b`),
    43  			newString: "galaxy",
    44  			expected:  "Hello, world!",
    45  		},
    46  		// Test case 5: Empty input
    47  		{
    48  			input:     "",
    49  			pattern:   regexp.MustCompile(`\d`),
    50  			newString: "x",
    51  			expected:  "",
    52  		},
    53  	}
    54  
    55  	for _, tc := range testCases {
    56  		result := tc.input.ReplaceRegexp(tc.pattern, tc.newString)
    57  		if result != tc.expected {
    58  			t.Errorf("Expected %s, but got %s for input %s", tc.expected, result, tc.input)
    59  		}
    60  	}
    61  }
    62  
    63  func TestFindRegexp(t *testing.T) {
    64  	testCases := []struct {
    65  		pattern  *regexp.Regexp
    66  		expected g.Option[g.String]
    67  		input    g.String
    68  	}{
    69  		// Test case 1: Regular match
    70  		{
    71  			input:    "Hello, world!",
    72  			pattern:  regexp.MustCompile(`\bworld\b`),
    73  			expected: g.Some[g.String]("world"),
    74  		},
    75  		// Test case 2: Match with special characters
    76  		{
    77  			input:    "Hello, 12345!",
    78  			pattern:  regexp.MustCompile(`\d+`),
    79  			expected: g.Some[g.String]("12345"),
    80  		},
    81  		// Test case 3: No match
    82  		{
    83  			input:    "Hello, world!",
    84  			pattern:  regexp.MustCompile(`\buniverse\b`),
    85  			expected: g.None[g.String](),
    86  		},
    87  		// Test case 4: Empty input
    88  		{
    89  			input:    "",
    90  			pattern:  regexp.MustCompile(`\d`),
    91  			expected: g.None[g.String](),
    92  		},
    93  	}
    94  
    95  	for _, tc := range testCases {
    96  		result := tc.input.FindRegexp(tc.pattern)
    97  		if result.IsSome() {
    98  			if result.Some() != tc.expected.Some() {
    99  				t.Errorf("Expected %s, but got %s for input %s", tc.expected.Some(), result.Some(), tc.input)
   100  			}
   101  		} else {
   102  			if result.IsSome() != tc.expected.IsSome() {
   103  				t.Errorf("Expected None, but got Some for input %s", tc.input)
   104  			}
   105  		}
   106  	}
   107  }
   108  
   109  func TestContainsRegexp(t *testing.T) {
   110  	testCases := []struct {
   111  		pattern  g.String
   112  		input    g.String
   113  		expected bool
   114  	}{
   115  		// Test case 1: Regular match
   116  		{
   117  			input:    "Hello, world!",
   118  			pattern:  `\bworld\b`,
   119  			expected: true,
   120  		},
   121  		// Test case 2: Match with special characters
   122  		{
   123  			input:    "Hello, 12345!",
   124  			pattern:  `\d+`,
   125  			expected: true,
   126  		},
   127  		// Test case 3: No match
   128  		{
   129  			input:    "Hello, world!",
   130  			pattern:  `\buniverse\b`,
   131  			expected: false,
   132  		},
   133  		// Test case 4: Empty input
   134  		{
   135  			input:    "",
   136  			pattern:  `\d`,
   137  			expected: false,
   138  		},
   139  	}
   140  
   141  	for _, tc := range testCases {
   142  		result := tc.input.ContainsRegexp(tc.pattern).Ok()
   143  		if result != tc.expected {
   144  			t.Errorf("Expected %v, but got %v for input %s", tc.expected, result, tc.input)
   145  		}
   146  	}
   147  }
   148  
   149  func TestContainsRegexpAny(t *testing.T) {
   150  	testCases := []struct {
   151  		input    g.String
   152  		patterns g.Slice[g.String]
   153  		expected bool
   154  	}{
   155  		// Test case 1: Regular match
   156  		{
   157  			input:    "Hello, world!",
   158  			patterns: g.Slice[g.String]{`\bworld\b`},
   159  			expected: true,
   160  		},
   161  		// Test case 2: Multiple patterns, one matches
   162  		{
   163  			input:    "Hello, world!",
   164  			patterns: g.Slice[g.String]{`\bworld\b`, `\d+`},
   165  			expected: true,
   166  		},
   167  		// Test case 3: Multiple patterns, none matches
   168  		{
   169  			input:    "Hello, world!",
   170  			patterns: g.Slice[g.String]{`\buniverse\b`, `\d`},
   171  			expected: false,
   172  		},
   173  		// Test case 4: Empty input
   174  		{
   175  			input:    "",
   176  			patterns: g.Slice[g.String]{`\d`},
   177  			expected: false,
   178  		},
   179  	}
   180  
   181  	for _, tc := range testCases {
   182  		result := tc.input.ContainsRegexpAny(tc.patterns...).Ok()
   183  		if result != tc.expected {
   184  			t.Errorf("Expected %v, but got %v for input %s", tc.expected, result, tc.input)
   185  		}
   186  	}
   187  }
   188  
   189  func TestContainsRegexpAll(t *testing.T) {
   190  	testCases := []struct {
   191  		input    g.String
   192  		patterns g.Slice[g.String]
   193  		expected bool
   194  	}{
   195  		// Test case 1: Regular match
   196  		{
   197  			input:    "Hello, world!",
   198  			patterns: g.Slice[g.String]{`\bworld\b`},
   199  			expected: true,
   200  		},
   201  		// Test case 2: Multiple patterns, all match
   202  		{
   203  			input:    "Hello, 12345!",
   204  			patterns: g.Slice[g.String]{`\bHello\b`, `\d+`},
   205  			expected: true,
   206  		},
   207  		// Test case 3: Multiple patterns, some match
   208  		{
   209  			input:    "Hello, world!",
   210  			patterns: g.Slice[g.String]{`\bworld\b`, `\d`},
   211  			expected: false,
   212  		},
   213  		// Test case 4: Empty input
   214  		{
   215  			input:    "",
   216  			patterns: g.Slice[g.String]{`\d`},
   217  			expected: false,
   218  		},
   219  	}
   220  
   221  	for _, tc := range testCases {
   222  		result := tc.input.ContainsRegexpAll(tc.patterns...).Ok()
   223  		if result != tc.expected {
   224  			t.Errorf("Expected %v, but got %v for input %s", tc.expected, result, tc.input)
   225  		}
   226  	}
   227  }
   228  
   229  func TestSplitRegexp(t *testing.T) {
   230  	testCases := []struct {
   231  		input    g.String
   232  		expected g.Slice[g.String]
   233  		pattern  regexp.Regexp
   234  	}{
   235  		// Test case 1: Regular split
   236  		{
   237  			input:    "one,two,three",
   238  			pattern:  *regexp.MustCompile(`,`),
   239  			expected: g.Slice[g.String]{"one", "two", "three"},
   240  		},
   241  		// Test case 2: Split with multiple patterns
   242  		{
   243  			input:    "1, 2, 3, 4",
   244  			pattern:  *regexp.MustCompile(`\s*,\s*`),
   245  			expected: g.Slice[g.String]{"1", "2", "3", "4"},
   246  		},
   247  		// Test case 3: Empty input
   248  		{
   249  			input:    "",
   250  			pattern:  *regexp.MustCompile(`,`),
   251  			expected: g.Slice[g.String]{""},
   252  		},
   253  		// Test case 4: No match
   254  		{
   255  			input:    "abcdefgh",
   256  			pattern:  *regexp.MustCompile(`,`),
   257  			expected: g.Slice[g.String]{"abcdefgh"},
   258  		},
   259  	}
   260  
   261  	for _, tc := range testCases {
   262  		result := tc.input.SplitRegexp(tc.pattern)
   263  		if !reflect.DeepEqual(result, tc.expected) {
   264  			t.Errorf("Expected %v, but got %v for input %s", tc.expected, result, tc.input)
   265  		}
   266  	}
   267  }
   268  
   269  func TestSplitRegexpN(t *testing.T) {
   270  	testCases := []struct {
   271  		expected g.Option[g.Slice[g.String]]
   272  		input    g.String
   273  		pattern  regexp.Regexp
   274  		n        g.Int
   275  	}{
   276  		// Test case 1: Regular split with n = 2
   277  		{
   278  			input:    "one,two,three",
   279  			pattern:  *regexp.MustCompile(`,`),
   280  			n:        2,
   281  			expected: g.Some(g.Slice[g.String]{"one", "two,three"}),
   282  		},
   283  		// Test case 2: Split with multiple patterns with n = 0
   284  		{
   285  			input:    "1, 2, 3, 4",
   286  			pattern:  *regexp.MustCompile(`\s*,\s*`),
   287  			n:        0,
   288  			expected: g.None[g.Slice[g.String]](),
   289  		},
   290  		// Test case 3: Empty input with n = 1
   291  		{
   292  			input:    "",
   293  			pattern:  *regexp.MustCompile(`,`),
   294  			n:        1,
   295  			expected: g.Some(g.Slice[g.String]{""}),
   296  		},
   297  		// Test case 4: No match with n = -1
   298  		{
   299  			input:    "abcdefgh",
   300  			pattern:  *regexp.MustCompile(`,`),
   301  			n:        -1,
   302  			expected: g.Some(g.Slice[g.String]{"abcdefgh"}),
   303  		},
   304  	}
   305  
   306  	for _, tc := range testCases {
   307  		result := tc.input.SplitRegexpN(tc.pattern, tc.n)
   308  		if !reflect.DeepEqual(result, tc.expected) {
   309  			t.Errorf("Expected %v, but got %v for input %s with n = %d", tc.expected, result, tc.input, tc.n)
   310  		}
   311  	}
   312  }
   313  
   314  func TestIndexRegexp(t *testing.T) {
   315  	testCases := []struct {
   316  		expected g.Option[g.Slice[g.Int]]
   317  		input    g.String
   318  		pattern  regexp.Regexp
   319  	}{
   320  		// Test case 1: Regular match
   321  		{
   322  			input:    "Hello, World!",
   323  			pattern:  *regexp.MustCompile(`World`),
   324  			expected: g.Some(g.Slice[g.Int]{7, 12}),
   325  		},
   326  		// Test case 2: No match
   327  		{
   328  			input:    "Hello, World!",
   329  			pattern:  *regexp.MustCompile(`Earth`),
   330  			expected: g.None[g.Slice[g.Int]](),
   331  		},
   332  		// Test case 3: Empty input
   333  		{
   334  			input:    "",
   335  			pattern:  *regexp.MustCompile(`World`),
   336  			expected: g.None[g.Slice[g.Int]](),
   337  		},
   338  	}
   339  
   340  	for _, tc := range testCases {
   341  		result := tc.input.IndexRegexp(&tc.pattern)
   342  		if !reflect.DeepEqual(result, tc.expected) {
   343  			t.Errorf(
   344  				"Expected %v, but got %v for input %s with pattern %s",
   345  				tc.expected,
   346  				result,
   347  				tc.input,
   348  				tc.pattern.String(),
   349  			)
   350  		}
   351  	}
   352  }
   353  
   354  func TestFindAllRegexp(t *testing.T) {
   355  	testCases := []struct {
   356  		expected g.Option[g.Slice[g.String]]
   357  		input    g.String
   358  		pattern  regexp.Regexp
   359  	}{
   360  		// Test case 1: Regular matches
   361  		{
   362  			input:    "Hello, World! Hello, Universe!",
   363  			pattern:  *regexp.MustCompile(`Hello`),
   364  			expected: g.Some(g.Slice[g.String]{"Hello", "Hello"}),
   365  		},
   366  		// Test case 2: No match
   367  		{
   368  			input:    "Hello, World!",
   369  			pattern:  *regexp.MustCompile(`Earth`),
   370  			expected: g.None[g.Slice[g.String]](),
   371  		},
   372  		// Test case 3: Empty input
   373  		{
   374  			input:    "",
   375  			pattern:  *regexp.MustCompile(`Hello`),
   376  			expected: g.None[g.Slice[g.String]](),
   377  		},
   378  	}
   379  
   380  	for _, tc := range testCases {
   381  		result := tc.input.FindAllRegexp(&tc.pattern)
   382  		if !reflect.DeepEqual(result, tc.expected) {
   383  			t.Errorf(
   384  				"Expected %v, but got %v for input %s with pattern %s",
   385  				tc.expected,
   386  				result,
   387  				tc.input,
   388  				tc.pattern.String(),
   389  			)
   390  		}
   391  	}
   392  }
   393  
   394  func TestFindAllRegexpN(t *testing.T) {
   395  	testCases := []struct {
   396  		expected g.Option[g.Slice[g.String]]
   397  		input    g.String
   398  		pattern  regexp.Regexp
   399  		n        g.Int
   400  	}{
   401  		// Test case 1: Regular matches with n = 2
   402  		{
   403  			input:    "Hello, World! Hello, Universe!",
   404  			pattern:  *regexp.MustCompile(`Hello`),
   405  			n:        2,
   406  			expected: g.Some(g.Slice[g.String]{"Hello", "Hello"}),
   407  		},
   408  		// Test case 2: No match with n = -1
   409  		{
   410  			input:    "Hello, World!",
   411  			pattern:  *regexp.MustCompile(`Earth`),
   412  			n:        -1,
   413  			expected: g.None[g.Slice[g.String]](),
   414  		},
   415  		// Test case 3: Empty input with n = 1
   416  		{
   417  			input:    "",
   418  			pattern:  *regexp.MustCompile(`Hello`),
   419  			n:        1,
   420  			expected: g.None[g.Slice[g.String]](),
   421  		},
   422  	}
   423  
   424  	for _, tc := range testCases {
   425  		result := tc.input.FindAllRegexpN(&tc.pattern, tc.n)
   426  		if !reflect.DeepEqual(result, tc.expected) {
   427  			t.Errorf(
   428  				"Expected %v, but got %v for input %s with pattern %s and n = %d",
   429  				tc.expected,
   430  				result,
   431  				tc.input,
   432  				tc.pattern.String(),
   433  				tc.n,
   434  			)
   435  		}
   436  	}
   437  }
   438  
   439  func TestFindSubmatchRegexp(t *testing.T) {
   440  	testCases := []struct {
   441  		expected g.Option[g.Slice[g.String]]
   442  		input    g.String
   443  		pattern  regexp.Regexp
   444  	}{
   445  		// Test case 1: Regular match
   446  		{
   447  			input:    "Hello, World!",
   448  			pattern:  *regexp.MustCompile(`Hello, (\w+)!`),
   449  			expected: g.Some(g.Slice[g.String]{"Hello, World!", "World"}),
   450  		},
   451  		// Test case 2: No match
   452  		{
   453  			input:    "Hello, World!",
   454  			pattern:  *regexp.MustCompile(`Earth`),
   455  			expected: g.None[g.Slice[g.String]](),
   456  		},
   457  		// Test case 3: Empty input
   458  		{
   459  			input:    "",
   460  			pattern:  *regexp.MustCompile(`Hello`),
   461  			expected: g.None[g.Slice[g.String]](),
   462  		},
   463  	}
   464  
   465  	for _, tc := range testCases {
   466  		result := tc.input.FindSubmatchRegexp(&tc.pattern)
   467  		if !reflect.DeepEqual(result, tc.expected) {
   468  			t.Errorf(
   469  				"Expected %v, but got %v for input %s with pattern %s",
   470  				tc.expected,
   471  				result,
   472  				tc.input,
   473  				tc.pattern.String(),
   474  			)
   475  		}
   476  	}
   477  }
   478  
   479  func TestFindAllSubmatchRegexp(t *testing.T) {
   480  	testCases := []struct {
   481  		expected g.Option[g.Slice[g.Slice[g.String]]]
   482  		input    g.String
   483  		pattern  regexp.Regexp
   484  	}{
   485  		// Test case 1: Regular matches
   486  		{
   487  			input:    "Hello, World! Hello, Universe!",
   488  			pattern:  *regexp.MustCompile(`Hello, (\w+)!`),
   489  			expected: g.Some(g.Slice[g.Slice[g.String]]{{"Hello, World!", "World"}, {"Hello, Universe!", "Universe"}}),
   490  		},
   491  		// Test case 2: No match
   492  		{
   493  			input:    "Hello, World!",
   494  			pattern:  *regexp.MustCompile(`Earth`),
   495  			expected: g.None[g.Slice[g.Slice[g.String]]](),
   496  		},
   497  		// Test case 3: Empty input
   498  		{
   499  			input:    "",
   500  			pattern:  *regexp.MustCompile(`Hello`),
   501  			expected: g.None[g.Slice[g.Slice[g.String]]](),
   502  		},
   503  	}
   504  
   505  	for _, tc := range testCases {
   506  		result := tc.input.FindAllSubmatchRegexp(&tc.pattern)
   507  		if !reflect.DeepEqual(result, tc.expected) {
   508  			t.Errorf(
   509  				"Expected %v, but got %v for input %s with pattern %s",
   510  				tc.expected,
   511  				result,
   512  				tc.input,
   513  				tc.pattern.String(),
   514  			)
   515  		}
   516  	}
   517  }
   518  
   519  func TestFindAllSubmatchRegexpN(t *testing.T) {
   520  	testCases := []struct {
   521  		expected g.Option[g.Slice[g.Slice[g.String]]]
   522  		input    g.String
   523  		pattern  regexp.Regexp
   524  		n        g.Int
   525  	}{
   526  		// Test case 1: Regular matches with n = 2
   527  		{
   528  			input:    "Hello, World! Hello, Universe!",
   529  			pattern:  *regexp.MustCompile(`Hello, (\w+)!`),
   530  			n:        2,
   531  			expected: g.Some(g.Slice[g.Slice[g.String]]{{"Hello, World!", "World"}, {"Hello, Universe!", "Universe"}}),
   532  		},
   533  		// Test case 2: No match with n = -1
   534  		{
   535  			input:    "Hello, World!",
   536  			pattern:  *regexp.MustCompile(`Earth`),
   537  			n:        -1,
   538  			expected: g.None[g.Slice[g.Slice[g.String]]](),
   539  		},
   540  		// Test case 3: Empty input with n = 1
   541  		{
   542  			input:    "",
   543  			pattern:  *regexp.MustCompile(`Hello`),
   544  			n:        1,
   545  			expected: g.None[g.Slice[g.Slice[g.String]]](),
   546  		},
   547  	}
   548  
   549  	for _, tc := range testCases {
   550  		result := tc.input.FindAllSubmatchRegexpN(&tc.pattern, tc.n)
   551  		if !reflect.DeepEqual(result, tc.expected) {
   552  			t.Errorf(
   553  				"Expected %v, but got %v for input %s with pattern %s and n = %d",
   554  				tc.expected,
   555  				result,
   556  				tc.input,
   557  				tc.pattern.String(),
   558  				tc.n,
   559  			)
   560  		}
   561  	}
   562  }