add tests for boolean

This commit is contained in:
Edward Shen 2021-02-24 15:47:35 -05:00
parent 48d7e1b65b
commit 19cb3117eb
Signed by: edward
GPG key ID: 19182661E818369F
4 changed files with 84 additions and 9 deletions

View file

@ -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();

View file

@ -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;

View file

@ -249,7 +249,7 @@ impl<'a> From<nom::Err<NomError<&'a [u8]>>> 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<nom::Err<NomError<&'a [u8]>>> 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<nom::Err<NomError<&'a [u8]>>> 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<nom::Err<NomError<&'a [u8]>>> 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()),

View file

@ -79,6 +79,15 @@ impl Display for Boolean<'_> {
}
}
impl Into<bool> for Boolean<'_> {
fn into(self) -> bool {
match self {
Boolean::True(_) => true,
Boolean::False(_) => false,
}
}
}
#[cfg(feature = "serde")]
impl Serialize for Boolean<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@ -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::*;