-
Notifications
You must be signed in to change notification settings - Fork 12
/
auto_test.rs
126 lines (108 loc) · 3.96 KB
/
auto_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-License-Identifier: MIT
// Akira Moroo <[email protected]> 2023
use async_openai::{
types::{
ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs,
CreateChatCompletionRequestArgs,
},
Client,
};
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use std::collections::HashSet;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, parse_str, Ident, Token,
};
use tokio::runtime::Runtime;
use super::utils;
/// Parses a list of test function names separated by commas.
///
/// test_valid, test_div_by_zero
///
/// The function name is used to generate the test function name.
struct Args {
test_names: HashSet<Ident>,
}
impl Parse for Args {
fn parse(input: ParseStream) -> syn::parse::Result<Self> {
let test_names = input.parse_terminated(Ident::parse, Token![,])?;
Ok(Args {
test_names: test_names.into_iter().collect(),
})
}
}
struct AutoTest {
token_stream: proc_macro2::TokenStream,
}
impl AutoTest {
fn new(token_stream: proc_macro2::TokenStream) -> Self {
Self { token_stream }
}
async fn completion(&mut self, args: Args) -> Result<TokenStream, Box<dyn std::error::Error>> {
let mut output = self.token_stream.clone();
let mut messages =
vec![
ChatCompletionRequestSystemMessageArgs::default()
.content(
"You are a Rust expert who can generate perfect tests for the given function.",
)
.build()?.into(),
ChatCompletionRequestUserMessageArgs::default()
.content(format!(
"Read this Rust function:\n```rust\n{}\n```",
self.token_stream
))
.build()?.into(),
];
if args.test_names.is_empty() {
messages.push(
ChatCompletionRequestUserMessageArgs::default()
.content(
"Write a test case for the function as much as possible in Markdown code snippet style. Your response must start with code block '```rust'.",
)
.build()?.into(),
);
} else {
for test_name in args.test_names {
messages.push(
ChatCompletionRequestUserMessageArgs::default()
.content(
format!(
"Write a test case `{}` for the function in Markdown code snippet style. Your response must start with code block '```rust'.",
test_name
)
)
.build()?.into(),
);
}
}
let request = CreateChatCompletionRequestArgs::default()
.model("gpt-3.5-turbo")
.messages(messages)
.build()?;
let client = Client::new();
let response = client.chat().create(request).await?;
let test_case = self.parse_str(&utils::extract_code(&response)?)?;
test_case.to_tokens(&mut output);
Ok(TokenStream::from(output))
}
fn parse_str(&self, s: &str) -> Result<proc_macro2::TokenStream, Box<dyn std::error::Error>> {
let expanded = if let Ok(test_case) = parse_str::<proc_macro2::TokenStream>(s) {
quote! {
#test_case
}
} else {
return Err(format!("Failed to parse the response as Rust code:\n{}\n", s).into());
};
Ok(expanded)
}
}
pub fn auto_test_impl(args: TokenStream, input: TokenStream) -> TokenStream {
// Parse the list of test function names that should be generated.
let args = parse_macro_input!(args as Args);
let mut auto_test = AutoTest::new(input.into());
let rt = Runtime::new().expect("Failed to create a runtime.");
rt.block_on(auto_test.completion(args))
.unwrap_or_else(|e| panic!("{}", e))
}