Compare commits

...

2 commits

Author SHA1 Message Date
a53a056cea
Add tests for ColorAttribute 2021-02-23 20:45:13 -05:00
6c2385d394
Fix docs 2021-02-23 20:22:15 -05:00
2 changed files with 99 additions and 5 deletions

View file

@ -23,7 +23,7 @@ use std::fmt::Display;
use std::iter::FusedIterator; use std::iter::FusedIterator;
/// Syntactic events that occurs in the config. Despite all these variants /// Syntactic events that occurs in the config. Despite all these variants
/// holding a [`Cow`] instead over a [`&str`], the parser will only emit /// holding a [`Cow`] instead over a simple reference, the parser will only emit
/// borrowed `Cow` variants. /// borrowed `Cow` variants.
/// ///
/// The `Cow` smart pointer is used here for ease of inserting events in a /// The `Cow` smart pointer is used here for ease of inserting events in a
@ -31,7 +31,6 @@ use std::iter::FusedIterator;
/// struct when adding values. /// struct when adding values.
/// ///
/// [`Cow`]: std::borrow::Cow /// [`Cow`]: std::borrow::Cow
/// [`&str`]: std::str
/// [`GitConfig`]: crate::config::GitConfig /// [`GitConfig`]: crate::config::GitConfig
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Event<'a> { pub enum Event<'a> {

View file

@ -529,10 +529,15 @@ impl FromStr for ColorAttribute {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let inverted = s.starts_with("no"); let inverted = s.starts_with("no");
let mut parsed = &s[2..]; let mut parsed = s;
if inverted {
parsed = &parsed[2..];
if parsed.starts_with("-") { if parsed.starts_with("-") {
parsed = &parsed[1..]; parsed = &parsed[1..];
} }
}
match parsed { match parsed {
"bold" if !inverted => Ok(Self::Bold), "bold" if !inverted => Ok(Self::Bold),
@ -615,3 +620,93 @@ mod integer {
assert!(Integer::from_str("gg").is_err()); assert!(Integer::from_str("gg").is_err());
} }
} }
#[cfg(test)]
mod color_attribute {
use super::ColorAttribute;
use std::str::FromStr;
#[test]
fn non_inverted() {
assert_eq!(ColorAttribute::from_str("bold"), Ok(ColorAttribute::Bold));
assert_eq!(ColorAttribute::from_str("dim"), Ok(ColorAttribute::Dim));
assert_eq!(ColorAttribute::from_str("ul"), Ok(ColorAttribute::Ul));
assert_eq!(ColorAttribute::from_str("blink"), Ok(ColorAttribute::Blink));
assert_eq!(
ColorAttribute::from_str("reverse"),
Ok(ColorAttribute::Reverse)
);
assert_eq!(
ColorAttribute::from_str("italic"),
Ok(ColorAttribute::Italic)
);
assert_eq!(
ColorAttribute::from_str("strike"),
Ok(ColorAttribute::Strike)
);
}
#[test]
fn inverted_no_dash() {
assert_eq!(
ColorAttribute::from_str("nobold"),
Ok(ColorAttribute::NoBold)
);
assert_eq!(ColorAttribute::from_str("nodim"), Ok(ColorAttribute::NoDim));
assert_eq!(ColorAttribute::from_str("noul"), Ok(ColorAttribute::NoUl));
assert_eq!(
ColorAttribute::from_str("noblink"),
Ok(ColorAttribute::NoBlink)
);
assert_eq!(
ColorAttribute::from_str("noreverse"),
Ok(ColorAttribute::NoReverse)
);
assert_eq!(
ColorAttribute::from_str("noitalic"),
Ok(ColorAttribute::NoItalic)
);
assert_eq!(
ColorAttribute::from_str("nostrike"),
Ok(ColorAttribute::NoStrike)
);
}
#[test]
fn inverted_dashed() {
assert_eq!(
ColorAttribute::from_str("no-bold"),
Ok(ColorAttribute::NoBold)
);
assert_eq!(
ColorAttribute::from_str("no-dim"),
Ok(ColorAttribute::NoDim)
);
assert_eq!(ColorAttribute::from_str("no-ul"), Ok(ColorAttribute::NoUl));
assert_eq!(
ColorAttribute::from_str("no-blink"),
Ok(ColorAttribute::NoBlink)
);
assert_eq!(
ColorAttribute::from_str("no-reverse"),
Ok(ColorAttribute::NoReverse)
);
assert_eq!(
ColorAttribute::from_str("no-italic"),
Ok(ColorAttribute::NoItalic)
);
assert_eq!(
ColorAttribute::from_str("no-strike"),
Ok(ColorAttribute::NoStrike)
);
}
#[test]
fn invalid() {
assert!(ColorAttribute::from_str("a").is_err());
assert!(ColorAttribute::from_str("no bold").is_err());
assert!(ColorAttribute::from_str("").is_err());
assert!(ColorAttribute::from_str("no").is_err());
assert!(ColorAttribute::from_str("no-").is_err());
}
}