Skip to content

Commit 3645ecb

Browse files
Adam GibsonAdam Gibson
Adam Gibson
authored and
Adam Gibson
committed
add tests for config
1 parent 49903f3 commit 3645ecb

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

src/config.rs

+94-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct AutctConfig {
9292
#[arg(short('p'), long, required=false)]
9393
pub rpc_port: Option<i32>,
9494
/// Print additional information in the terminal
95-
#[arg(short('v'), long, required = false)]
95+
#[arg(long, required = false)]
9696
verbose: Option<bool>,
9797
/// Only required for prover, destination
9898
/// file for the binary string which is the proof
@@ -218,7 +218,100 @@ impl AutctConfig {
218218
}
219219

220220
pub fn get_context_labels_and_keysets(self) -> Result<(Vec<String>, Vec<String>), Box<dyn Error>> {
221+
if self.keysets.is_none(){
222+
return Err("Null keysets".into());
223+
}
221224
get_params_from_config_string(self.keysets.unwrap())
222225
}
223226

224227
}
228+
229+
#[cfg(test)]
230+
mod tests {
231+
use super::*;
232+
use clap::Parser;
233+
234+
// Test for `get_params_from_config_string`
235+
#[test]
236+
fn test_get_params_from_config_string_valid() {
237+
let input = "context1:keyset1,context2:keyset2".to_string();
238+
let result = get_params_from_config_string(input).unwrap();
239+
assert_eq!(result.0, vec!["context1".to_string(), "context2".to_string()]);
240+
assert_eq!(result.1, vec!["keyset1".to_string(), "keyset2".to_string()]);
241+
}
242+
243+
#[test]
244+
fn test_get_params_from_config_string_invalid_format() {
245+
let input = "context1keyset1,context2:keyset2".to_string(); // missing colon
246+
let result = get_params_from_config_string(input);
247+
assert!(result.is_err());
248+
}
249+
250+
#[test]
251+
fn test_get_params_from_config_string_empty_input() {
252+
let input = "".to_string();
253+
let result = get_params_from_config_string(input);
254+
assert!(result.is_err());
255+
}
256+
257+
// Tests for `AutctConfig`
258+
#[test]
259+
fn test_autctconfig_default_values() {
260+
let config = AutctConfig::default();
261+
assert_eq!(config.mode, Some("newkey".to_string()));
262+
assert_eq!(config.depth, Some(2));
263+
assert_eq!(config.branching_factor, Some(1024));
264+
assert_eq!(config.verbose, Some(true));
265+
assert_eq!(config.audit_range_min, Some(100_000_000u64));
266+
}
267+
268+
#[test]
269+
fn test_autctconfig_build_with_defaults() {
270+
// Ensure that the config can be built with default values.
271+
let result = AutctConfig::build();
272+
assert!(result.is_ok());
273+
}
274+
275+
#[test]
276+
fn test_autctconfig_command_line_parsing() {
277+
// Simulate passing arguments from the command line
278+
let args = vec![
279+
"test-app",
280+
"-M", "prove",
281+
"-k", "context1:keyset1",
282+
"-d", "4",
283+
"-b", "512",
284+
"--verbose", "true",
285+
"-H", "localhost",
286+
"-P", "test_proof_file"
287+
];
288+
let config = AutctConfig::try_parse_from(args).unwrap();
289+
assert_eq!(config.mode, Some("prove".to_string()));
290+
assert_eq!(config.depth, Some(4));
291+
assert_eq!(config.branching_factor, Some(512));
292+
assert_eq!(config.verbose, Some(true));
293+
assert_eq!(config.rpc_host, Some("localhost".to_string()));
294+
assert_eq!(config.proof_file_str, Some("test_proof_file".to_string()));
295+
}
296+
297+
#[test]
298+
fn test_autctconfig_get_context_labels_and_keysets() {
299+
let config = AutctConfig {
300+
keysets: Some("context1:keyset1,context2:keyset2".to_string()),
301+
..AutctConfig::default()
302+
};
303+
let result = config.get_context_labels_and_keysets().unwrap();
304+
assert_eq!(result.0, vec!["context1".to_string(), "context2".to_string()]);
305+
assert_eq!(result.1, vec!["keyset1".to_string(), "keyset2".to_string()]);
306+
}
307+
308+
#[test]
309+
fn test_autctconfig_missing_keysets() {
310+
let config = AutctConfig {
311+
keysets: None,
312+
..AutctConfig::default()
313+
};
314+
let result = config.get_context_labels_and_keysets();
315+
assert!(result.is_err());
316+
}
317+
}

0 commit comments

Comments
 (0)