From 19cb3117eb4d3db169abfcb473370708cfa9be21 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Wed, 24 Feb 2021 15:47:35 -0500 Subject: [PATCH] add tests for boolean --- src/config.rs | 8 +++--- src/lib.rs | 2 +- src/parser.rs | 8 +++--- src/values.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index 1f4b1b1..f99c91a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -200,7 +200,7 @@ impl<'a> GitConfig<'a> { /// `d`, since the last valid config value is `a = d`: /// /// ``` - /// # use serde_git_config::config::GitConfig; + /// # use git_config::config::GitConfig; /// # use std::borrow::Cow; /// # let git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap(); /// assert_eq!(git_config.get_raw_value("core", None, "a"), Ok(&Cow::Borrowed("d".into()))); @@ -275,7 +275,7 @@ impl<'a> GitConfig<'a> { /// `d`, since the last valid config value is `a = d`: /// /// ``` - /// # use serde_git_config::config::{GitConfig, GitConfigError}; + /// # use git_config::config::{GitConfig, GitConfigError}; /// # use std::borrow::Cow; /// # use bstr::BStr; /// # let mut git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap(); @@ -354,7 +354,7 @@ impl<'a> GitConfig<'a> { /// Attempting to get all values of `a` yields the following: /// /// ``` - /// # use serde_git_config::config::GitConfig; + /// # use git_config::config::GitConfig; /// # use std::borrow::Cow; /// # let git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap(); /// assert_eq!( @@ -419,7 +419,7 @@ impl<'a> GitConfig<'a> { /// Attempting to get all values of `a` yields the following: /// /// ``` - /// # use serde_git_config::config::{GitConfig, GitConfigError}; + /// # use git_config::config::{GitConfig, GitConfigError}; /// # use std::borrow::Cow; /// # use bstr::BStr; /// # let mut git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap(); diff --git a/src/lib.rs b/src/lib.rs index d27f159..6607b8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,9 @@ extern crate serde_crate as serde; // mod de; +// mod ser; pub mod config; mod error; -// mod ser; pub mod parser; pub mod values; diff --git a/src/parser.rs b/src/parser.rs index 4c4f409..a0418fb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -249,7 +249,7 @@ impl<'a> From>> for ParserError<'a> { /// non-significant events that occur in addition to the ones you may expect: /// /// ``` -/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; +/// # use git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; /// # use std::borrow::Cow; /// # let section_header = ParsedSectionHeader { /// # name: Cow::Borrowed("core".into()), @@ -288,7 +288,7 @@ impl<'a> From>> for ParserError<'a> { /// which means that the corresponding event won't appear either: /// /// ``` -/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; +/// # use git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; /// # use std::borrow::Cow; /// # use bstr::BStr; /// # let section_header = ParsedSectionHeader { @@ -323,7 +323,7 @@ impl<'a> From>> for ParserError<'a> { /// relevant event stream emitted is thus emitted as: /// /// ``` -/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; +/// # use git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; /// # use std::borrow::Cow; /// # let section_header = ParsedSectionHeader { /// # name: Cow::Borrowed("core".into()), @@ -360,7 +360,7 @@ impl<'a> From>> for ParserError<'a> { /// split value accordingly: /// /// ``` -/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; +/// # use git_config::parser::{Event, ParsedSectionHeader, parse_from_str}; /// # use std::borrow::Cow; /// # let section_header = ParsedSectionHeader { /// # name: Cow::Borrowed("some-section".into()), diff --git a/src/values.rs b/src/values.rs index c677c8e..5202ffb 100644 --- a/src/values.rs +++ b/src/values.rs @@ -79,6 +79,15 @@ impl Display for Boolean<'_> { } } +impl Into for Boolean<'_> { + fn into(self) -> bool { + match self { + Boolean::True(_) => true, + Boolean::False(_) => false, + } + } +} + #[cfg(feature = "serde")] impl Serialize for Boolean<'_> { fn serialize(&self, serializer: S) -> Result @@ -583,6 +592,72 @@ impl FromStr for ColorAttribute { } } +#[cfg(test)] +mod boolean { + use super::*; + + #[test] + fn from_str_false() { + assert_eq!( + Boolean::from_str("no"), + Ok(Boolean::False(FalseVariant("no"))) + ); + assert_eq!( + Boolean::from_str("off"), + Ok(Boolean::False(FalseVariant("off"))) + ); + assert_eq!( + Boolean::from_str("false"), + Ok(Boolean::False(FalseVariant("false"))) + ); + assert_eq!( + Boolean::from_str("zero"), + Ok(Boolean::False(FalseVariant("zero"))) + ); + assert_eq!( + Boolean::from_str("\"\""), + Ok(Boolean::False(FalseVariant("\"\""))) + ); + } + + #[test] + fn from_str_true() { + assert_eq!( + Boolean::from_str("yes"), + Ok(Boolean::True(TrueVariant::Explicit("yes"))) + ); + assert_eq!( + Boolean::from_str("on"), + Ok(Boolean::True(TrueVariant::Explicit("on"))) + ); + assert_eq!( + Boolean::from_str("true"), + Ok(Boolean::True(TrueVariant::Explicit("true"))) + ); + assert_eq!( + Boolean::from_str("one"), + Ok(Boolean::True(TrueVariant::Explicit("one"))) + ); + } + + #[test] + fn ignores_case() { + // Random subset + for word in &["no", "yes", "off", "true", "zero"] { + let first: bool = Boolean::from_str(word).unwrap().into(); + let second: bool = Boolean::from_str(&word.to_uppercase()).unwrap().into(); + assert_eq!(first, second); + } + } + + #[test] + fn from_str_err() { + assert!(Boolean::from_str("yesn't").is_err()); + assert!(Boolean::from_str("yesno").is_err()); + assert!(Boolean::from_str("").is_err()); + } +} + #[cfg(test)] mod integer { use super::*;