Skip to content

Commit 3af260c

Browse files
committed
chore: added more tests
Signed-off-by: ayushag <ayushag@nvidia.com>
1 parent 79decae commit 3af260c

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

lib/parsers/src/tool_calling/parsers.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,4 +1074,38 @@ Remember, San Francisco weather can be quite unpredictable, particularly with it
10741074
assert_eq!(args["from"], "New York");
10751075
assert_eq!(args["to"], "Los Angeles");
10761076
}
1077+
1078+
#[test]
1079+
fn test_pythonic_parser_basic_with_constants() {
1080+
let input = r#"[get_weather(location="San Francisco", unit="fahrenheit"), get_weather(location="New York", unit="fahrenheit")]"#;
1081+
let (result, content) = detect_and_parse_tool_call(input, Some("pythonic")).unwrap();
1082+
assert_eq!(content, Some("".to_string()));
1083+
assert_eq!(result.len(), 2);
1084+
let (name, args) = extract_name_and_args(result[0].clone());
1085+
assert_eq!(name, "get_weather");
1086+
assert_eq!(args["location"], "San Francisco");
1087+
assert_eq!(args["unit"], "fahrenheit");
1088+
let (name, args) = extract_name_and_args(result[1].clone());
1089+
assert_eq!(name, "get_weather");
1090+
assert_eq!(args["location"], "New York");
1091+
assert_eq!(args["unit"], "fahrenheit");
1092+
}
1093+
1094+
#[test]
1095+
#[ignore]
1096+
fn test_pythonic_parser_with_constants_and_normal_text() {
1097+
let input = r#"Hey How are you? [get_weather(location="San Francisco", unit="fahrenheit"), get_weather(location="New York", unit="fahrenheit")]"#;
1098+
let (result, content) = detect_and_parse_tool_call(input, Some("pythonic")).unwrap();
1099+
assert_eq!(content, Some("Hey How are you?".to_string()));
1100+
assert_eq!(result.len(), 2);
1101+
1102+
let (name, args) = extract_name_and_args(result[0].clone());
1103+
assert_eq!(name, "get_weather");
1104+
assert_eq!(args["location"], "San Francisco");
1105+
assert_eq!(args["unit"], "fahrenheit");
1106+
let (name, args) = extract_name_and_args(result[1].clone());
1107+
assert_eq!(name, "get_weather");
1108+
assert_eq!(args["location"], "New York");
1109+
assert_eq!(args["unit"], "fahrenheit");
1110+
}
10771111
}

lib/parsers/src/tool_calling/pythonic_parser.rs

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)