@@ -20,17 +20,17 @@ public void testCalculateOffsets_SimpleExample() {
2020 String text = "user@domain.com" ;
2121 String pattern = "(?<username>\\ w+)@(?<domain>\\ w+)\\ .(?<tld>\\ w+)" ;
2222
23- String result = RexOffsetFunction .calculateOffsets (text , pattern );
24- // user=0-3, domain=5-10, com=12-14 (but order is reversed)
25- assertEquals ("tld=12-14 &domain=5-10&username=0-3 " , result );
23+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
24+ // Groups appear in pattern order: username, domain, tld
25+ assertEquals ("username=0-3 &domain=5-10&tld=12-14 " , result );
2626 }
2727
2828 @ Test
2929 public void testCalculateOffsets_SingleNamedGroup () {
3030 String text = "hello world" ;
3131 String pattern = "(?<word>\\ w+)" ;
3232
33- String result = RexOffsetFunction .calculateOffsets (text , pattern );
33+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
3434 assertEquals ("word=0-4" , result );
3535 }
3636
@@ -39,31 +39,31 @@ public void testCalculateOffsets_TwoGroups() {
3939 String text = "abc123" ;
4040 String pattern = "(?<letters>[a-z]+)(?<numbers>\\ d+)" ;
4141
42- String result = RexOffsetFunction .calculateOffsets (text , pattern );
43- assertEquals ("numbers=3-5& letters=0-2" , result );
42+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
43+ assertEquals ("letters=0-2&numbers=3-5 " , result );
4444 }
4545
4646 @ Test
4747 public void testCalculateOffsets_NoMatches () {
4848 String text = "This text has no digits" ;
4949 String pattern = "(?<digit>\\ d+)" ;
5050
51- String result = RexOffsetFunction .calculateOffsets (text , pattern );
51+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
5252 assertNull (result );
5353 }
5454
5555 @ Test
5656 public void testCalculateOffsets_NullInputs () {
5757 // Null text
58- String result = RexOffsetFunction .calculateOffsets (null , "(?<test>\\ w+)" );
58+ String result = RexOffsetFunction .calculateOffsets (null , "(?<test>\\ w+)" , 1 );
5959 assertNull (result );
6060
6161 // Null pattern
62- result = RexOffsetFunction .calculateOffsets ("test text" , null );
62+ result = RexOffsetFunction .calculateOffsets ("test text" , null , 1 );
6363 assertNull (result );
6464
6565 // Both null
66- result = RexOffsetFunction .calculateOffsets (null , null );
66+ result = RexOffsetFunction .calculateOffsets (null , null , 1 );
6767 assertNull (result );
6868 }
6969
@@ -72,7 +72,7 @@ public void testCalculateOffsets_InvalidPattern() {
7272 String text = "test string" ;
7373 String invalidPattern = "[unclosed" ;
7474
75- String result = RexOffsetFunction .calculateOffsets (text , invalidPattern );
75+ String result = RexOffsetFunction .calculateOffsets (text , invalidPattern , 1 );
7676 assertNull (result );
7777 }
7878
@@ -81,7 +81,7 @@ public void testCalculateOffsets_EmptyString() {
8181 String text = "" ;
8282 String pattern = "(?<word>\\ w+)" ;
8383
84- String result = RexOffsetFunction .calculateOffsets (text , pattern );
84+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
8585 assertNull (result );
8686 }
8787
@@ -90,7 +90,7 @@ public void testCalculateOffsets_PatternWithoutNamedGroups() {
9090 String text = "test123" ;
9191 String pattern = "(\\ w+)(\\ d+)" ;
9292
93- String result = RexOffsetFunction .calculateOffsets (text , pattern );
93+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
9494 assertNull (result );
9595 }
9696
@@ -99,7 +99,7 @@ public void testCalculateOffsets_SingleCharacterMatch() {
9999 String text = "a" ;
100100 String pattern = "(?<char>[a-z])" ;
101101
102- String result = RexOffsetFunction .calculateOffsets (text , pattern );
102+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
103103 assertEquals ("char=0-0" , result );
104104 }
105105
@@ -108,17 +108,17 @@ public void testCalculateOffsets_DigitsPattern() {
108108 String text = "year 2023 month 12" ;
109109 String pattern = "(?<year>\\ d{4}).*(?<month>\\ d{2})" ;
110110
111- String result = RexOffsetFunction .calculateOffsets (text , pattern );
112- assertEquals ("month=16-17& year=5-8" , result );
111+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
112+ assertEquals ("year=5-8&month=16-17 " , result );
113113 }
114114
115115 @ Test
116116 public void testCalculateOffsets_EmailExample () {
117117 String text = "email: john@example.org" ;
118118 String pattern = "(?<name>\\ w+)@(?<domain>\\ w+)\\ .(?<ext>\\ w+)" ;
119119
120- String result = RexOffsetFunction .calculateOffsets (text , pattern );
121- assertEquals ("ext=20-22 &domain=12-18&name=7-10 " , result );
120+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 1 );
121+ assertEquals ("name=7-10 &domain=12-18&ext=20-22 " , result );
122122 }
123123
124124 @ Test
@@ -136,4 +136,31 @@ public void testFunctionConstructor() {
136136 RexOffsetFunction testFunction = new RexOffsetFunction ();
137137 assertNotNull (testFunction , "Function should be properly initialized" );
138138 }
139+
140+ @ Test
141+ public void testCalculateOffsets_MaxMatchMultiple () {
142+ String text = "880 Holmes Lane" ;
143+ String pattern = "(?<digit>\\ d)" ;
144+
145+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 2 );
146+ assertEquals ("digit=0-0&digit=1-1" , result );
147+ }
148+
149+ @ Test
150+ public void testCalculateOffsets_MaxMatchUnlimited () {
151+ String text = "880 Holmes Lane" ;
152+ String pattern = "(?<digit>\\ d)" ;
153+
154+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 0 );
155+ assertEquals ("digit=0-0&digit=1-1&digit=2-2" , result );
156+ }
157+
158+ @ Test
159+ public void testCalculateOffsets_MaxMatchExceedsAvailable () {
160+ String text = "880" ;
161+ String pattern = "(?<digit>\\ d)" ;
162+
163+ String result = RexOffsetFunction .calculateOffsets (text , pattern , 10 );
164+ assertEquals ("digit=0-0&digit=1-1&digit=2-2" , result );
165+ }
139166}
0 commit comments