use crate::command::traits::CommandBuilder;
use std::convert::AsRef;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
#[derive(Clone, Debug, Default)]
pub struct PgTestTimingBuilder {
program_dir: Option<PathBuf>,
duration: Option<OsString>,
}
impl PgTestTimingBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.program_dir = Some(path.into());
self
}
pub fn duration<S: AsRef<OsStr>>(mut self, duration: S) -> Self {
self.duration = Some(duration.as_ref().to_os_string());
self
}
}
impl CommandBuilder for PgTestTimingBuilder {
fn get_program(&self) -> &'static OsStr {
"pg_test_timing".as_ref()
}
fn get_program_dir(&self) -> &Option<PathBuf> {
&self.program_dir
}
fn get_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = Vec::new();
if let Some(duration) = &self.duration {
args.push("-d".into());
args.push(duration.into());
}
args
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command::traits::CommandToString;
use test_log::test;
#[test]
fn test_builder_new() {
let command = PgTestTimingBuilder::new().program_dir(".").build();
assert_eq!(
PathBuf::from(".").join("pg_test_timing"),
PathBuf::from(command.to_command_string().replace("\"", ""))
);
}
#[test]
fn test_builder() {
let command = PgTestTimingBuilder::new().duration("10").build();
assert_eq!(r#""pg_test_timing" "-d" "10""#, command.to_command_string());
}
}