@@ -134,7 +134,61 @@ mod tests {
134134 }
135135
136136 #[ test]
137- fn test_get_regex_matches ( ) {
137+ fn test_strip_text ( ) {
138+ let message = "Hello, world!" ;
139+ let stripped = strip_text ( message) ;
140+ assert_eq ! ( stripped, "Hello, world!" ) ;
141+
142+ let message = "<|python_start|>foo(a=1, b=2)<|python_end|>" ;
143+ let stripped = strip_text ( message) ;
144+ assert_eq ! ( stripped, "foo(a=1, b=2)" ) ;
145+
146+ let message = "<|python_start|>foo(a=1, b=2)" ;
147+ let stripped = strip_text ( message) ;
148+ assert_eq ! ( stripped, "foo(a=1, b=2)" ) ;
149+
150+ let message = "foo(a=1, b=2)<|python_end|>" ;
151+ let stripped = strip_text ( message) ;
152+ assert_eq ! ( stripped, "foo(a=1, b=2)" ) ;
153+ }
154+
155+ #[ test]
156+ fn test_get_regex_matches_simple_case ( ) {
157+ // Simple Case
158+ let message = "[foo(a=1, b=2), bar(x=3)]" ;
159+ let matches = get_regex_matches ( message) ;
160+ assert_eq ! ( matches. len( ) , 1 ) ;
161+ assert_eq ! ( matches[ 0 ] , "[foo(a=1, b=2), bar(x=3)]" ) ;
162+ }
163+
164+ #[ test]
165+ fn test_get_regex_matches_text_before_and_after ( ) {
166+ // Spacing in arg and value and text before and after
167+ let message = "Hey yo ! [foo(a=1, b=2), bar(x= 3)] Hey yo" ;
168+ let matches = get_regex_matches ( message) ;
169+ assert_eq ! ( matches. len( ) , 1 ) ;
170+ assert_eq ! ( matches[ 0 ] , "[foo(a=1, b=2), bar(x= 3)]" ) ;
171+ }
172+
173+ #[ test]
174+ fn test_get_regex_matches_new_line_in_arg_and_value ( ) {
175+ // New Line in Arg and value
176+ let message = "Hey \n yo ! [foo(a=1,b=2), \n bar(x=3)] Hey yo" ;
177+ let matches = get_regex_matches ( message) ;
178+ assert_eq ! ( matches. len( ) , 1 ) ;
179+ assert_eq ! ( matches[ 0 ] , "[foo(a=1,b=2), \n bar(x=3)]" ) ;
180+ }
181+
182+ #[ test]
183+ fn test_get_regex_matches_no_call ( ) {
184+ // No Call
185+ let message = "Hey yo !" ;
186+ let matches = get_regex_matches ( message) ;
187+ assert_eq ! ( matches. len( ) , 0 ) ;
188+ }
189+
190+ #[ test]
191+ fn test_parse_tool_call_parse_pythonic_basic ( ) {
138192 let message = "[foo(a=1, b=2), bar(x=3)]" ;
139193 let ( result, _) = try_tool_call_parse_pythonic ( message) . unwrap ( ) ;
140194 assert ! ( !result. is_empty( ) ) ;
@@ -147,4 +201,74 @@ mod tests {
147201 assert_eq ! ( name, "bar" ) ;
148202 assert_eq ! ( args[ "x" ] , "3" ) ;
149203 }
204+
205+ #[ test]
206+ fn test_parse_tool_call_parse_pythonic_with_text ( ) {
207+ let message = "Hey yo ! [foo(a=1, b=2), bar(x=3)] Hey yo" ;
208+ let ( result, _) = try_tool_call_parse_pythonic ( message) . unwrap ( ) ;
209+ assert ! ( !result. is_empty( ) ) ;
210+ assert_eq ! ( result. len( ) , 2 ) ;
211+ let ( name, args) = extract_name_and_args ( result[ 0 ] . clone ( ) ) ;
212+ assert_eq ! ( name, "foo" ) ;
213+ assert_eq ! ( args[ "a" ] , "1" ) ;
214+ assert_eq ! ( args[ "b" ] , "2" ) ;
215+ let ( name, args) = extract_name_and_args ( result[ 1 ] . clone ( ) ) ;
216+ assert_eq ! ( name, "bar" ) ;
217+ assert_eq ! ( args[ "x" ] , "3" ) ;
218+ }
219+
220+ #[ test]
221+ fn test_parse_tool_call_parse_pythonic_with_text_and_new_line ( ) {
222+ let message = "Hey \n yo ! [foo(a=1, b=2), bar(x=3)] Hey yo" ;
223+ let ( result, _) = try_tool_call_parse_pythonic ( message) . unwrap ( ) ;
224+ assert ! ( !result. is_empty( ) ) ;
225+ assert_eq ! ( result. len( ) , 2 ) ;
226+ let ( name, args) = extract_name_and_args ( result[ 0 ] . clone ( ) ) ;
227+ assert_eq ! ( name, "foo" ) ;
228+ assert_eq ! ( args[ "a" ] , "1" ) ;
229+ assert_eq ! ( args[ "b" ] , "2" ) ;
230+ let ( name, args) = extract_name_and_args ( result[ 1 ] . clone ( ) ) ;
231+ assert_eq ! ( name, "bar" ) ;
232+ assert_eq ! ( args[ "x" ] , "3" ) ;
233+ }
234+
235+ #[ test]
236+ fn test_parse_tool_call_parse_pythonic_with_no_calls ( ) {
237+ let message = "Hey \n yo !" ;
238+ let ( result, _) = try_tool_call_parse_pythonic ( message) . unwrap ( ) ;
239+ assert ! ( result. is_empty( ) ) ;
240+ assert_eq ! ( result. len( ) , 0 )
241+ }
242+
243+ #[ test]
244+ #[ ignore]
245+ fn test_parse_tool_call_parse_pythonic_with_list_arg_values ( ) {
246+ let message = "[foo(a=[1, 2, 3], b=2), bar(x=[3, 4, 5])]" ;
247+ let ( result, _) = try_tool_call_parse_pythonic ( message) . unwrap ( ) ;
248+ assert ! ( !result. is_empty( ) ) ;
249+ assert_eq ! ( result. len( ) , 2 ) ;
250+ let ( name, args) = extract_name_and_args ( result[ 0 ] . clone ( ) ) ;
251+ assert_eq ! ( name, "foo" ) ;
252+ assert_eq ! ( args[ "a" ] , "[1, 2, 3]" ) ;
253+ assert_eq ! ( args[ "b" ] , "2" ) ;
254+ let ( name, args) = extract_name_and_args ( result[ 1 ] . clone ( ) ) ;
255+ assert_eq ! ( name, "bar" ) ;
256+ assert_eq ! ( args[ "x" ] , "[3, 4, 5]" ) ;
257+ }
258+
259+ #[ test]
260+ #[ ignore]
261+ fn test_parse_tool_call_parse_pythonic_with_dict_arg_values ( ) {
262+ let message = "[foo(a={'a': 1, 'b': 2}, b=2), bar(x={'x': 3, 'y': 4})]" ;
263+ let ( result, _) = try_tool_call_parse_pythonic ( message) . unwrap ( ) ;
264+ assert ! ( !result. is_empty( ) ) ;
265+ assert_eq ! ( result. len( ) , 2 ) ;
266+ let ( name, args) = extract_name_and_args ( result[ 0 ] . clone ( ) ) ;
267+ assert_eq ! ( name, "foo" ) ;
268+ assert_eq ! ( args[ "a" ] , "{'a': 1, 'b': 2}" ) ;
269+ assert_eq ! ( args[ "b" ] , "2" ) ;
270+ let ( name, args) = extract_name_and_args ( result[ 1 ] . clone ( ) ) ;
271+ assert_eq ! ( name, "bar" ) ;
272+ assert_eq ! ( args[ "x" ] , "{'x': 3, 'y': 4}" ) ;
273+ }
150274}
0 commit comments