From 0cc4371d138d5e98cd22813f10689cb089eef378 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Mon, 24 Aug 2020 18:04:20 -0700 Subject: Introduce builder-inspired way for configuring Nitrocli instance In the future we would like to provide more ways for tests to create a Nitrocli instance. In order to prevent explosion of with_XXX methods for each possible combination of arguments, this change allows for an easier configuration of an existing object with builder-pattern-inspired modifier methods. --- src/tests/mod.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'src/tests/mod.rs') diff --git a/src/tests/mod.rs b/src/tests/mod.rs index d31348b..631ffb0 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -41,34 +41,36 @@ impl Nitrocli { } } - pub fn with_model(model: M) -> Self - where - M: Into, - { - Self { - model: Some(model.into()), - admin_pin: Some(nitrokey::DEFAULT_ADMIN_PIN.into()), - user_pin: Some(nitrokey::DEFAULT_USER_PIN.into()), - new_admin_pin: None, - new_user_pin: None, - password: Some("1234567".into()), - } + /// Set the model to use. + fn model(mut self, model: nitrokey::Model) -> Self { + self.model = Some(model); + self + } + + /// Set the password to use for certain operations. + fn password(mut self, password: impl Into) -> Self { + self.password = Some(password.into()); + self } - pub fn admin_pin(&mut self, pin: impl Into) { - self.admin_pin = Some(pin.into()) + pub fn admin_pin(mut self, pin: impl Into) -> Self { + self.admin_pin = Some(pin.into()); + self } - pub fn new_admin_pin(&mut self, pin: impl Into) { - self.new_admin_pin = Some(pin.into()) + pub fn new_admin_pin(mut self, pin: impl Into) -> Self { + self.new_admin_pin = Some(pin.into()); + self } - pub fn user_pin(&mut self, pin: impl Into) { - self.user_pin = Some(pin.into()) + pub fn user_pin(mut self, pin: impl Into) -> Self { + self.user_pin = Some(pin.into()); + self } - pub fn new_user_pin(&mut self, pin: impl Into) { - self.new_user_pin = Some(pin.into()) + pub fn new_user_pin(mut self, pin: impl Into) -> Self { + self.new_user_pin = Some(pin.into()); + self } fn model_to_arg(model: nitrokey::Model) -> &'static str { -- cgit v1.2.3