-
-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! This module defines the [Git] flag. To set it up from [ArgMatches], a [Config] and its | ||
//! [Default] value, use the [configure_from](Configurable::configure_from) method. | ||
use super::Configurable; | ||
|
||
use crate::config_file::Config; | ||
|
||
use clap::ArgMatches; | ||
|
||
/// The flag showing whether to display block gits. | ||
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)] | ||
pub struct Git(pub bool); | ||
|
||
impl Configurable<Self> for Git { | ||
/// Get a potential `Git` value from [ArgMatches]. | ||
/// | ||
/// If the "git" argument is passed, this returns a `Git` with value `true` in a | ||
/// [Some]. Otherwise this returns [None]. | ||
fn from_arg_matches(matches: &ArgMatches) -> Option<Self> { | ||
if !cfg!(feature = "no-git") && matches.get_one("git") == Some(&true) { | ||
Some(Self(true)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Get a potential `Git` value from a [Config]. | ||
/// | ||
/// If the `Config::git` has value, | ||
/// this returns it as the value of the `Git`, in a [Some]. | ||
/// Otherwise this returns [None]. | ||
fn from_config(config: &Config) -> Option<Self> { | ||
config.git.map(Self) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "no-git"))] | ||
#[cfg(test)] | ||
mod test { | ||
use super::Git; | ||
|
||
use crate::app; | ||
use crate::config_file::Config; | ||
use crate::flags::Configurable; | ||
|
||
#[test] | ||
fn test_from_arg_matches_none() { | ||
let argv = ["lsd"]; | ||
let matches = app::build().try_get_matches_from(argv).unwrap(); | ||
assert_eq!(None, Git::from_arg_matches(&matches)); | ||
} | ||
|
||
#[test] | ||
fn test_from_arg_matches_true() { | ||
let argv = ["lsd", "--git"]; | ||
let matches = app::build().try_get_matches_from(argv).unwrap(); | ||
assert_eq!(Some(Git(true)), Git::from_arg_matches(&matches)); | ||
} | ||
|
||
#[test] | ||
fn test_from_config_none() { | ||
assert_eq!(None, Git::from_config(&Config::with_none())); | ||
} | ||
|
||
#[test] | ||
fn test_from_config_true() { | ||
let mut c = Config::with_none(); | ||
c.git = Some(true); | ||
assert_eq!(Some(Git(true)), Git::from_config(&c)); | ||
} | ||
|
||
#[test] | ||
fn test_from_config_false() { | ||
let mut c = Config::with_none(); | ||
c.git = Some(false); | ||
assert_eq!(Some(Git(false)), Git::from_config(&c)); | ||
} | ||
} |