diff options
author | Daniel Mueller <deso@posteo.net> | 2019-08-31 10:34:34 -0700 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-08-31 10:34:34 -0700 |
commit | 7e2adff71e293eef2570a6a8e43127bfab569cb3 (patch) | |
tree | 6e8d0ae565e480c5540b2975384882497130ed5b /nitrocli/src/tests/mod.rs | |
parent | fb55bcb2e1330086c3c39f5f9dcbd67aaf6fd1b3 (diff) | |
download | nitrocli-7e2adff71e293eef2570a6a8e43127bfab569cb3.tar.gz nitrocli-7e2adff71e293eef2570a6a8e43127bfab569cb3.tar.bz2 |
Introduce builder infrastructure for assembling 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 introduces a Builder
struct that can be used to create such an instance in an idiomatic way.
Diffstat (limited to 'nitrocli/src/tests/mod.rs')
-rw-r--r-- | nitrocli/src/tests/mod.rs | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/nitrocli/src/tests/mod.rs b/nitrocli/src/tests/mod.rs index 1e2fe26..bc76d6d 100644 --- a/nitrocli/src/tests/mod.rs +++ b/nitrocli/src/tests/mod.rs @@ -76,6 +76,30 @@ where } } +struct Builder(Nitrocli); + +impl Builder { + /// Set the model to use. + fn model(mut self, model: nitrokey::Model) -> Self { + self.0.model = Some(model); + self + } + + /// Set the password to use for certain operations. + fn password<P>(mut self, password: P) -> Self + where + P: Into<ffi::OsString>, + { + self.0.password = Some(password.into()); + self + } + + /// Build the final `Nitrocli` object. + fn build(self) -> Nitrocli { + self.0 + } +} + struct Nitrocli { model: Option<nitrokey::Model>, admin_pin: Option<ffi::OsString>, @@ -97,18 +121,8 @@ impl Nitrocli { } } - pub fn with_model<M>(model: M) -> Self - where - M: Into<nitrokey::Model>, - { - 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()), - } + pub fn make() -> Builder { + Builder(Self::new()) } pub fn admin_pin(&mut self, pin: impl Into<ffi::OsString>) { |