diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
commit | 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch) | |
tree | bdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/gix-config-value/tests/value | |
parent | Releasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff) | |
download | rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix-config-value/tests/value')
-rw-r--r-- | vendor/gix-config-value/tests/value/boolean.rs | 42 | ||||
-rw-r--r-- | vendor/gix-config-value/tests/value/color.rs | 199 | ||||
-rw-r--r-- | vendor/gix-config-value/tests/value/integer.rs | 74 | ||||
-rw-r--r-- | vendor/gix-config-value/tests/value/main.rs | 17 | ||||
-rw-r--r-- | vendor/gix-config-value/tests/value/path.rs | 129 |
5 files changed, 461 insertions, 0 deletions
diff --git a/vendor/gix-config-value/tests/value/boolean.rs b/vendor/gix-config-value/tests/value/boolean.rs new file mode 100644 index 000000000..8fe8b43f0 --- /dev/null +++ b/vendor/gix-config-value/tests/value/boolean.rs @@ -0,0 +1,42 @@ +use std::convert::TryFrom; + +use gix_config_value::Boolean; + +use crate::b; + +#[test] +fn from_str_false() -> crate::Result { + assert!(!Boolean::try_from(b("no"))?.0); + assert!(!Boolean::try_from(b("off"))?.0); + assert!(!Boolean::try_from(b("false"))?.0); + assert!(!Boolean::try_from(b("0"))?.0); + assert!(!Boolean::try_from(b(""))?.0); + Ok(()) +} + +#[test] +fn from_str_true() -> crate::Result { + assert_eq!(Boolean::try_from(b("yes")).map(Into::into), Ok(true)); + assert_eq!(Boolean::try_from(b("on")), Ok(Boolean(true))); + assert_eq!(Boolean::try_from(b("true")), Ok(Boolean(true))); + assert!(Boolean::try_from(b("1"))?.0); + assert!(Boolean::try_from(b("+10"))?.0); + assert!(Boolean::try_from(b("-1"))?.0); + Ok(()) +} + +#[test] +fn ignores_case() { + // Random subset + for word in &["no", "yes", "on", "off", "true", "false"] { + let first: bool = Boolean::try_from(b(word)).unwrap().into(); + let second: bool = Boolean::try_from(b(&word.to_uppercase())).unwrap().into(); + assert_eq!(first, second); + } +} + +#[test] +fn from_str_err() { + assert!(Boolean::try_from(b("yesn't")).is_err()); + assert!(Boolean::try_from(b("yesno")).is_err()); +} diff --git a/vendor/gix-config-value/tests/value/color.rs b/vendor/gix-config-value/tests/value/color.rs new file mode 100644 index 000000000..1db7fe2e0 --- /dev/null +++ b/vendor/gix-config-value/tests/value/color.rs @@ -0,0 +1,199 @@ +mod name { + use std::str::FromStr; + + use gix_config_value::color::Name; + + #[test] + fn non_bright() { + assert_eq!(Name::from_str("normal"), Ok(Name::Normal)); + assert_eq!(Name::from_str("-1"), Ok(Name::Normal)); + assert_eq!(Name::from_str("default"), Ok(Name::Default)); + assert_eq!(Name::from_str("black"), Ok(Name::Black)); + assert_eq!(Name::from_str("red"), Ok(Name::Red)); + assert_eq!(Name::from_str("green"), Ok(Name::Green)); + assert_eq!(Name::from_str("yellow"), Ok(Name::Yellow)); + assert_eq!(Name::from_str("blue"), Ok(Name::Blue)); + assert_eq!(Name::from_str("magenta"), Ok(Name::Magenta)); + assert_eq!(Name::from_str("cyan"), Ok(Name::Cyan)); + assert_eq!(Name::from_str("white"), Ok(Name::White)); + } + + #[test] + fn bright() { + assert_eq!(Name::from_str("brightblack"), Ok(Name::BrightBlack)); + assert_eq!(Name::from_str("brightred"), Ok(Name::BrightRed)); + assert_eq!(Name::from_str("brightgreen"), Ok(Name::BrightGreen)); + assert_eq!(Name::from_str("brightyellow"), Ok(Name::BrightYellow)); + assert_eq!(Name::from_str("brightblue"), Ok(Name::BrightBlue)); + assert_eq!(Name::from_str("brightmagenta"), Ok(Name::BrightMagenta)); + assert_eq!(Name::from_str("brightcyan"), Ok(Name::BrightCyan)); + assert_eq!(Name::from_str("brightwhite"), Ok(Name::BrightWhite)); + } + + #[test] + fn ansi() { + assert_eq!(Name::from_str("255"), Ok(Name::Ansi(255))); + assert_eq!(Name::from_str("0"), Ok(Name::Ansi(0))); + } + + #[test] + fn hex() { + assert_eq!(Name::from_str("#ff0010"), Ok(Name::Rgb(255, 0, 16))); + assert_eq!(Name::from_str("#ffffff"), Ok(Name::Rgb(255, 255, 255))); + assert_eq!(Name::from_str("#000000"), Ok(Name::Rgb(0, 0, 0))); + } + + #[test] + fn invalid() { + assert!(Name::from_str("-2").is_err()); + assert!(Name::from_str("brightnormal").is_err()); + assert!(Name::from_str("brightdefault").is_err()); + assert!(Name::from_str("").is_err()); + assert!(Name::from_str("bright").is_err()); + assert!(Name::from_str("256").is_err()); + assert!(Name::from_str("#").is_err()); + assert!(Name::from_str("#fff").is_err()); + assert!(Name::from_str("#gggggg").is_err()); + } +} + +mod attribute { + use std::str::FromStr; + + use gix_config_value::color::Attribute; + + #[test] + fn non_inverted() { + assert_eq!(Attribute::from_str("reset"), Ok(Attribute::RESET)); + assert_eq!(Attribute::from_str("bold"), Ok(Attribute::BOLD)); + assert_eq!(Attribute::from_str("dim"), Ok(Attribute::DIM)); + assert_eq!(Attribute::from_str("ul"), Ok(Attribute::UL)); + assert_eq!(Attribute::from_str("blink"), Ok(Attribute::BLINK)); + assert_eq!(Attribute::from_str("reverse"), Ok(Attribute::REVERSE)); + assert_eq!(Attribute::from_str("italic"), Ok(Attribute::ITALIC)); + assert_eq!(Attribute::from_str("strike"), Ok(Attribute::STRIKE)); + } + + #[test] + fn inverted_no_dash() { + assert_eq!(Attribute::from_str("nobold"), Ok(Attribute::NO_BOLD)); + assert_eq!(Attribute::from_str("nodim"), Ok(Attribute::NO_DIM)); + assert_eq!(Attribute::from_str("noul"), Ok(Attribute::NO_UL)); + assert_eq!(Attribute::from_str("noblink"), Ok(Attribute::NO_BLINK)); + assert_eq!(Attribute::from_str("noreverse"), Ok(Attribute::NO_REVERSE)); + assert_eq!(Attribute::from_str("noitalic"), Ok(Attribute::NO_ITALIC)); + assert_eq!(Attribute::from_str("nostrike"), Ok(Attribute::NO_STRIKE)); + } + + #[test] + fn inverted_dashed() { + assert_eq!(Attribute::from_str("no-bold"), Ok(Attribute::NO_BOLD)); + assert_eq!(Attribute::from_str("no-dim"), Ok(Attribute::NO_DIM)); + assert_eq!(Attribute::from_str("no-ul"), Ok(Attribute::NO_UL)); + assert_eq!(Attribute::from_str("no-blink"), Ok(Attribute::NO_BLINK)); + assert_eq!(Attribute::from_str("no-reverse"), Ok(Attribute::NO_REVERSE)); + assert_eq!(Attribute::from_str("no-italic"), Ok(Attribute::NO_ITALIC)); + assert_eq!(Attribute::from_str("no-strike"), Ok(Attribute::NO_STRIKE)); + } + + #[test] + fn invalid() { + assert!(Attribute::from_str("no-reset").is_err()); + assert!(Attribute::from_str("noreset").is_err()); + assert!(Attribute::from_str("a").is_err()); + assert!(Attribute::from_str("no bold").is_err()); + assert!(Attribute::from_str("").is_err()); + assert!(Attribute::from_str("no").is_err()); + assert!(Attribute::from_str("no-").is_err()); + } +} + +mod from_git { + use std::convert::TryFrom; + + use bstr::BStr; + use gix_config_value::Color; + + #[test] + fn reset() { + assert_eq!(color("reset"), "reset"); + } + + #[test] + fn empty() { + assert_eq!(color(""), ""); + } + + #[test] + fn at_most_two_colors() { + assert!(try_color("red green blue").is_err()); + } + + #[test] + fn attribute_before_color_name() { + assert_eq!(color("bold red"), "red bold"); + } + + #[test] + fn color_name_before_attribute() { + assert_eq!(color("red bold"), "red bold"); + } + + #[test] + fn attribute_fg_bg() { + assert_eq!(color("ul blue red"), "blue red ul"); + } + + #[test] + fn fg_bg_attribute() { + assert_eq!(color("blue red ul"), "blue red ul"); + } + + #[test] + fn multiple_attributes() { + assert_eq!( + color("blue bold dim ul blink reverse"), + "blue bold dim ul blink reverse" + ); + } + + #[test] + fn reset_then_multiple_attributes() { + assert_eq!( + color("blue bold dim ul blink reverse reset"), + "blue bold dim ul blink reverse reset" + ); + } + + #[test] + fn long_color_spec() { + assert_eq!( + color("254 255 bold dim ul blink reverse"), + "254 255 bold dim ul blink reverse" + ); + let input = "#ffffff #ffffff bold nobold dim nodim italic noitalic ul noul blink noblink reverse noreverse strike nostrike"; + let expected = "#ffffff #ffffff bold dim italic ul blink reverse strike nodim nobold noitalic noul noblink noreverse nostrike"; + assert_eq!(color(input), expected); + } + + #[test] + fn normal_default_can_clear_backgrounds() { + assert_eq!(color("normal default"), "normal default"); + } + + #[test] + fn default_can_combine_with_attributes() { + assert_eq!( + color("default default no-reverse bold"), + "default default bold noreverse" + ); + } + + fn color<'a>(name: impl Into<&'a BStr>) -> String { + try_color(name).expect("input color is expected to be valid") + } + + fn try_color<'a>(name: impl Into<&'a BStr>) -> crate::Result<String> { + Ok(Color::try_from(name.into())?.to_string()) + } +} diff --git a/vendor/gix-config-value/tests/value/integer.rs b/vendor/gix-config-value/tests/value/integer.rs new file mode 100644 index 000000000..9de2ab1ca --- /dev/null +++ b/vendor/gix-config-value/tests/value/integer.rs @@ -0,0 +1,74 @@ +use std::convert::TryFrom; + +use gix_config_value::{integer::Suffix, Integer}; + +use crate::b; + +#[test] +fn from_str_no_suffix() { + assert_eq!(Integer::try_from(b("1")).unwrap(), Integer { value: 1, suffix: None }); + + assert_eq!( + Integer::try_from(b("-1")).unwrap(), + Integer { + value: -1, + suffix: None + } + ); +} + +#[test] +fn from_str_with_suffix() { + assert_eq!( + Integer::try_from(b("1k")).unwrap(), + Integer { + value: 1, + suffix: Some(Suffix::Kibi), + } + ); + + assert_eq!( + Integer::try_from(b("1m")).unwrap(), + Integer { + value: 1, + suffix: Some(Suffix::Mebi), + } + ); + + assert_eq!( + Integer::try_from(b("1g")).unwrap(), + Integer { + value: 1, + suffix: Some(Suffix::Gibi), + } + ); +} + +#[test] +fn invalid_from_str() { + assert!(Integer::try_from(b("")).is_err()); + assert!(Integer::try_from(b("-")).is_err()); + assert!(Integer::try_from(b("k")).is_err()); + assert!(Integer::try_from(b("m")).is_err()); + assert!(Integer::try_from(b("g")).is_err()); + assert!(Integer::try_from(b("123123123123123123123123")).is_err()); + assert!(Integer::try_from(b("gg")).is_err()); +} + +#[test] +fn as_decimal() { + fn decimal(input: &str) -> Option<i64> { + Integer::try_from(b(input)).unwrap().to_decimal() + } + + assert_eq!(decimal("12"), Some(12), "works without suffix"); + assert_eq!(decimal("13k"), Some(13 * 1024), "works with kilobyte suffix"); + assert_eq!(decimal("13K"), Some(13 * 1024), "works with Kilobyte suffix"); + assert_eq!(decimal("14m"), Some(14 * 1_048_576), "works with megabyte suffix"); + assert_eq!(decimal("14M"), Some(14 * 1_048_576), "works with Megabyte suffix"); + assert_eq!(decimal("15g"), Some(15 * 1_073_741_824), "works with gigabyte suffix"); + assert_eq!(decimal("15G"), Some(15 * 1_073_741_824), "works with Gigabyte suffix"); + + assert_eq!(decimal(&format!("{}g", i64::MAX)), None, "overflow results in None"); + assert_eq!(decimal(&format!("{}g", i64::MIN)), None, "underflow results in None"); +} diff --git a/vendor/gix-config-value/tests/value/main.rs b/vendor/gix-config-value/tests/value/main.rs new file mode 100644 index 000000000..13a33c9d8 --- /dev/null +++ b/vendor/gix-config-value/tests/value/main.rs @@ -0,0 +1,17 @@ +use std::borrow::Cow; + +use bstr::{BStr, ByteSlice}; + +type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>; +fn b(s: &str) -> &bstr::BStr { + s.into() +} + +pub fn cow_str(s: &str) -> Cow<'_, BStr> { + Cow::Borrowed(s.as_bytes().as_bstr()) +} + +mod boolean; +mod color; +mod integer; +mod path; diff --git a/vendor/gix-config-value/tests/value/path.rs b/vendor/gix-config-value/tests/value/path.rs new file mode 100644 index 000000000..10970296d --- /dev/null +++ b/vendor/gix-config-value/tests/value/path.rs @@ -0,0 +1,129 @@ +mod interpolate { + use std::{ + borrow::Cow, + path::{Path, PathBuf}, + }; + + use gix_config_value::path; + + use crate::{b, cow_str}; + + #[test] + fn backslash_is_not_special_and_they_are_not_escaping_anything() -> crate::Result { + for path in ["C:\\foo\\bar", "/foo/bar"] { + let actual = gix_config_value::Path::from(Cow::Borrowed(b(path))).interpolate(Default::default())?; + assert_eq!(actual, Path::new(path)); + assert!( + matches!(actual, Cow::Borrowed(_)), + "it does not unnecessarily copy values" + ); + } + Ok(()) + } + + #[test] + fn empty_path_is_error() { + assert!(matches!( + interpolate_without_context(""), + Err(path::interpolate::Error::Missing { what: "path" }) + )); + } + + #[test] + fn prefix_substitutes_git_install_dir() { + for git_install_dir in &["/tmp/git", "C:\\git"] { + for (val, expected) in &[("%(prefix)/foo/bar", "foo/bar"), ("%(prefix)/foo\\bar", "foo\\bar")] { + let expected = + std::path::PathBuf::from(format!("{}{}{}", git_install_dir, std::path::MAIN_SEPARATOR, expected)); + assert_eq!( + gix_config_value::Path::from(cow_str(val)) + .interpolate(path::interpolate::Context { + git_install_dir: Path::new(git_install_dir).into(), + ..Default::default() + }) + .unwrap(), + expected, + "prefix interpolation keeps separators as they are" + ); + } + } + } + + #[test] + fn prefix_substitution_skipped_with_dot_slash() { + let path = "./%(prefix)/foo/bar"; + let git_install_dir = "/tmp/git"; + assert_eq!( + gix_config_value::Path::from(Cow::Borrowed(b(path))) + .interpolate(path::interpolate::Context { + git_install_dir: Path::new(git_install_dir).into(), + ..Default::default() + }) + .unwrap(), + Path::new(path) + ); + } + + #[test] + fn tilde_alone_does_not_interpolate() -> crate::Result { + assert_eq!(interpolate_without_context("~")?, Path::new("~")); + Ok(()) + } + + #[test] + fn tilde_slash_substitutes_current_user() -> crate::Result { + let path = "~/user/bar"; + let home = std::env::current_dir()?; + let expected = home.join("user").join("bar"); + assert_eq!( + gix_config_value::Path::from(cow_str(path)) + .interpolate(path::interpolate::Context { + home_dir: Some(&home), + home_for_user: Some(home_for_user), + ..Default::default() + }) + .unwrap() + .as_ref(), + expected + ); + Ok(()) + } + + #[cfg(windows)] + #[test] + fn tilde_with_given_user_is_unsupported_on_windows() { + assert!(matches!( + interpolate_without_context("~baz/foo/bar"), + Err(gix_config_value::path::interpolate::Error::UserInterpolationUnsupported) + )); + } + + #[cfg(not(windows))] + #[test] + fn tilde_with_given_user() -> crate::Result { + let home = std::env::current_dir()?; + + for path_suffix in &["foo/bar", "foo\\bar", ""] { + let path = format!("~user{}{}", std::path::MAIN_SEPARATOR, path_suffix); + let expected = home.join("user").join(path_suffix); + + assert_eq!(interpolate_without_context(path)?, expected); + } + Ok(()) + } + + fn interpolate_without_context( + path: impl AsRef<str>, + ) -> Result<Cow<'static, Path>, gix_config_value::path::interpolate::Error> { + gix_config_value::Path::from(Cow::Owned(path.as_ref().to_owned().into())).interpolate( + path::interpolate::Context { + home_for_user: Some(home_for_user), + ..Default::default() + }, + ) + } + + fn home_for_user(name: &str) -> Option<PathBuf> { + std::env::current_dir().unwrap().join(name).into() + } +} |