integration tests for value extraction

master
Edward Shen 2021-03-01 17:00:47 -05:00
parent e9598831cc
commit 5574ec3267
Signed by: edward
GPG Key ID: 19182661E818369F
4 changed files with 85 additions and 23 deletions

View File

@ -111,7 +111,7 @@ pub fn normalize_cow(input: Cow<'_, [u8]>) -> Cow<'_, [u8]> {
}
}
owned.extend(dbg!(&input[last_index..]));
owned.extend(&input[last_index..]);
if owned.is_empty() {
input
} else {
@ -431,8 +431,8 @@ impl Serialize for TrueVariant<'_> {
/// [`bitwise_offset`]: IntegerSuffix::bitwise_offset
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Integer {
value: i64,
suffix: Option<IntegerSuffix>,
pub value: i64,
pub suffix: Option<IntegerSuffix>,
}
impl Display for Integer {
@ -599,26 +599,9 @@ impl TryFrom<Vec<u8>> for IntegerSuffix {
/// foreground or background color.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct Color {
foreground: Option<ColorValue>,
background: Option<ColorValue>,
attributes: Vec<ColorAttribute>,
}
impl Color {
/// Returns the foreground color, if any.
pub const fn foreground(&self) -> Option<ColorValue> {
self.foreground
}
/// Returns the background color, if any.
pub const fn background(&self) -> Option<ColorValue> {
self.background
}
/// Returns the list of text modifiers, if any.
pub fn attributes(&self) -> &[ColorAttribute] {
&self.attributes
}
pub foreground: Option<ColorValue>,
pub background: Option<ColorValue>,
pub attributes: Vec<ColorAttribute>,
}
impl Display for Color {

View File

@ -0,0 +1,71 @@
use git_config::file::GitConfig;
use git_config::values::*;
use std::borrow::Cow;
use std::convert::TryFrom;
/// Asserts we can cast into all variants of our type
#[test]
fn get_value_for_all_provided_values() -> Result<(), Box<dyn std::error::Error>> {
let config = r#"
[core]
bool-explicit = false
bool-implicit
integer-no-prefix = 10
integer-prefix = 10g
color = brightgreen red \
bold
other = hello world
"#;
let file = GitConfig::try_from(config)?;
assert_eq!(
file.get_value::<Boolean>("core", None, "bool-explicit")?,
Boolean::False(Cow::Borrowed("false"))
);
assert_eq!(
file.get_value::<Boolean>("core", None, "bool-implicit")?,
Boolean::True(TrueVariant::Implicit)
);
assert_eq!(
file.get_value::<Integer>("core", None, "integer-no-prefix")?,
Integer {
value: 10,
suffix: None
}
);
assert_eq!(
file.get_value::<Integer>("core", None, "integer-no-prefix")?,
Integer {
value: 10,
suffix: None
}
);
assert_eq!(
file.get_value::<Integer>("core", None, "integer-prefix")?,
Integer {
value: 10,
suffix: Some(IntegerSuffix::Gibi),
}
);
assert_eq!(
file.get_value::<Color>("core", None, "color")?,
Color {
foreground: Some(ColorValue::BrightGreen),
background: Some(ColorValue::Red),
attributes: vec![ColorAttribute::Bold]
}
);
assert_eq!(
file.get_value::<Value>("core", None, "other")?,
Value::Other(Cow::Borrowed(b"hello world"))
);
Ok(())
}

View File

@ -0,0 +1,8 @@
// See https://matklad.github.io/2021/02/27/delete-cargo-integration-tests.html
// for an explanation of why the integration tests are laid out like this.
//
// TL;DR single mod makes integration tests faster to compile, test, and with
// less build artifacts.
mod file_integeration_test;
mod parser_integration_tests;