From 5574ec326770a7b555d98955b1e285c375c72ecb Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Mon, 1 Mar 2021 17:00:47 -0500 Subject: [PATCH] integration tests for value extraction --- src/values.rs | 29 ++------ .../file_integeration_test.rs | 71 +++++++++++++++++++ tests/integration_tests/main.rs | 8 +++ .../parser_integration_tests.rs | 0 4 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 tests/integration_tests/file_integeration_test.rs create mode 100644 tests/integration_tests/main.rs rename tests/{ => integration_tests}/parser_integration_tests.rs (100%) diff --git a/src/values.rs b/src/values.rs index 02482bf..ff2739f 100644 --- a/src/values.rs +++ b/src/values.rs @@ -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, + pub value: i64, + pub suffix: Option, } impl Display for Integer { @@ -599,26 +599,9 @@ impl TryFrom> for IntegerSuffix { /// foreground or background color. #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] pub struct Color { - foreground: Option, - background: Option, - attributes: Vec, -} - -impl Color { - /// Returns the foreground color, if any. - pub const fn foreground(&self) -> Option { - self.foreground - } - - /// Returns the background color, if any. - pub const fn background(&self) -> Option { - self.background - } - - /// Returns the list of text modifiers, if any. - pub fn attributes(&self) -> &[ColorAttribute] { - &self.attributes - } + pub foreground: Option, + pub background: Option, + pub attributes: Vec, } impl Display for Color { diff --git a/tests/integration_tests/file_integeration_test.rs b/tests/integration_tests/file_integeration_test.rs new file mode 100644 index 0000000..b2b068c --- /dev/null +++ b/tests/integration_tests/file_integeration_test.rs @@ -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> { + 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::("core", None, "bool-explicit")?, + Boolean::False(Cow::Borrowed("false")) + ); + + assert_eq!( + file.get_value::("core", None, "bool-implicit")?, + Boolean::True(TrueVariant::Implicit) + ); + + assert_eq!( + file.get_value::("core", None, "integer-no-prefix")?, + Integer { + value: 10, + suffix: None + } + ); + + assert_eq!( + file.get_value::("core", None, "integer-no-prefix")?, + Integer { + value: 10, + suffix: None + } + ); + + assert_eq!( + file.get_value::("core", None, "integer-prefix")?, + Integer { + value: 10, + suffix: Some(IntegerSuffix::Gibi), + } + ); + + assert_eq!( + file.get_value::("core", None, "color")?, + Color { + foreground: Some(ColorValue::BrightGreen), + background: Some(ColorValue::Red), + attributes: vec![ColorAttribute::Bold] + } + ); + + assert_eq!( + file.get_value::("core", None, "other")?, + Value::Other(Cow::Borrowed(b"hello world")) + ); + + Ok(()) +} diff --git a/tests/integration_tests/main.rs b/tests/integration_tests/main.rs new file mode 100644 index 0000000..93ed91f --- /dev/null +++ b/tests/integration_tests/main.rs @@ -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; diff --git a/tests/parser_integration_tests.rs b/tests/integration_tests/parser_integration_tests.rs similarity index 100% rename from tests/parser_integration_tests.rs rename to tests/integration_tests/parser_integration_tests.rs