Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b28df2

Browse files
committed
port tests over as well (#450)
1 parent edb1162 commit 9b28df2

7 files changed

Lines changed: 459 additions & 2 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ check: ## Build all code in suitable configurations
108108
&& cargo check --features cache-efficiency-debug
109109
cd git-commitgraph && cargo check --all-features \
110110
&& cargo check
111+
cd git-config-value && cargo check --all-features \
112+
&& cargo check
111113
cd git-config && cargo check --all-features \
112114
&& cargo check
113115
cd git-transport && cargo check \
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::b;
2+
use git_config_value::Boolean;
3+
use std::convert::TryFrom;
4+
5+
#[test]
6+
fn from_str_false() -> crate::Result {
7+
assert!(!Boolean::try_from(b("no"))?.0);
8+
assert!(!Boolean::try_from(b("off"))?.0);
9+
assert!(!Boolean::try_from(b("false"))?.0);
10+
assert!(!Boolean::try_from(b("0"))?.0);
11+
assert!(!Boolean::try_from(b(""))?.0);
12+
Ok(())
13+
}
14+
15+
#[test]
16+
fn from_str_true() -> crate::Result {
17+
assert_eq!(Boolean::try_from(b("yes")).map(Into::into), Ok(true));
18+
assert_eq!(Boolean::try_from(b("on")), Ok(Boolean(true)));
19+
assert_eq!(Boolean::try_from(b("true")), Ok(Boolean(true)));
20+
assert!(Boolean::try_from(b("1"))?.0);
21+
assert!(Boolean::try_from(b("+10"))?.0);
22+
assert!(Boolean::try_from(b("-1"))?.0);
23+
Ok(())
24+
}
25+
26+
#[test]
27+
fn ignores_case() {
28+
// Random subset
29+
for word in &["no", "yes", "on", "off", "true", "false"] {
30+
let first: bool = Boolean::try_from(b(word)).unwrap().into();
31+
let second: bool = Boolean::try_from(b(&*word.to_uppercase())).unwrap().into();
32+
assert_eq!(first, second);
33+
}
34+
}
35+
36+
#[test]
37+
fn from_str_err() {
38+
assert!(Boolean::try_from(b("yesn't")).is_err());
39+
assert!(Boolean::try_from(b("yesno")).is_err());
40+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
mod name {
2+
use git_config_value::color::Name;
3+
use std::str::FromStr;
4+
5+
#[test]
6+
fn non_bright() {
7+
assert_eq!(Name::from_str("normal"), Ok(Name::Normal));
8+
assert_eq!(Name::from_str("-1"), Ok(Name::Normal));
9+
assert_eq!(Name::from_str("default"), Ok(Name::Default));
10+
assert_eq!(Name::from_str("black"), Ok(Name::Black));
11+
assert_eq!(Name::from_str("red"), Ok(Name::Red));
12+
assert_eq!(Name::from_str("green"), Ok(Name::Green));
13+
assert_eq!(Name::from_str("yellow"), Ok(Name::Yellow));
14+
assert_eq!(Name::from_str("blue"), Ok(Name::Blue));
15+
assert_eq!(Name::from_str("magenta"), Ok(Name::Magenta));
16+
assert_eq!(Name::from_str("cyan"), Ok(Name::Cyan));
17+
assert_eq!(Name::from_str("white"), Ok(Name::White));
18+
}
19+
20+
#[test]
21+
fn bright() {
22+
assert_eq!(Name::from_str("brightblack"), Ok(Name::BrightBlack));
23+
assert_eq!(Name::from_str("brightred"), Ok(Name::BrightRed));
24+
assert_eq!(Name::from_str("brightgreen"), Ok(Name::BrightGreen));
25+
assert_eq!(Name::from_str("brightyellow"), Ok(Name::BrightYellow));
26+
assert_eq!(Name::from_str("brightblue"), Ok(Name::BrightBlue));
27+
assert_eq!(Name::from_str("brightmagenta"), Ok(Name::BrightMagenta));
28+
assert_eq!(Name::from_str("brightcyan"), Ok(Name::BrightCyan));
29+
assert_eq!(Name::from_str("brightwhite"), Ok(Name::BrightWhite));
30+
}
31+
32+
#[test]
33+
fn ansi() {
34+
assert_eq!(Name::from_str("255"), Ok(Name::Ansi(255)));
35+
assert_eq!(Name::from_str("0"), Ok(Name::Ansi(0)));
36+
}
37+
38+
#[test]
39+
fn hex() {
40+
assert_eq!(Name::from_str("#ff0010"), Ok(Name::Rgb(255, 0, 16)));
41+
assert_eq!(Name::from_str("#ffffff"), Ok(Name::Rgb(255, 255, 255)));
42+
assert_eq!(Name::from_str("#000000"), Ok(Name::Rgb(0, 0, 0)));
43+
}
44+
45+
#[test]
46+
fn invalid() {
47+
assert!(Name::from_str("-2").is_err());
48+
assert!(Name::from_str("brightnormal").is_err());
49+
assert!(Name::from_str("brightdefault").is_err());
50+
assert!(Name::from_str("").is_err());
51+
assert!(Name::from_str("bright").is_err());
52+
assert!(Name::from_str("256").is_err());
53+
assert!(Name::from_str("#").is_err());
54+
assert!(Name::from_str("#fff").is_err());
55+
assert!(Name::from_str("#gggggg").is_err());
56+
}
57+
}
58+
59+
mod attribute {
60+
use std::str::FromStr;
61+
62+
use git_config_value::color::Attribute;
63+
64+
#[test]
65+
fn non_inverted() {
66+
assert_eq!(Attribute::from_str("reset"), Ok(Attribute::RESET));
67+
assert_eq!(Attribute::from_str("bold"), Ok(Attribute::BOLD));
68+
assert_eq!(Attribute::from_str("dim"), Ok(Attribute::DIM));
69+
assert_eq!(Attribute::from_str("ul"), Ok(Attribute::UL));
70+
assert_eq!(Attribute::from_str("blink"), Ok(Attribute::BLINK));
71+
assert_eq!(Attribute::from_str("reverse"), Ok(Attribute::REVERSE));
72+
assert_eq!(Attribute::from_str("italic"), Ok(Attribute::ITALIC));
73+
assert_eq!(Attribute::from_str("strike"), Ok(Attribute::STRIKE));
74+
}
75+
76+
#[test]
77+
fn inverted_no_dash() {
78+
assert_eq!(Attribute::from_str("nobold"), Ok(Attribute::NO_BOLD));
79+
assert_eq!(Attribute::from_str("nodim"), Ok(Attribute::NO_DIM));
80+
assert_eq!(Attribute::from_str("noul"), Ok(Attribute::NO_UL));
81+
assert_eq!(Attribute::from_str("noblink"), Ok(Attribute::NO_BLINK));
82+
assert_eq!(Attribute::from_str("noreverse"), Ok(Attribute::NO_REVERSE));
83+
assert_eq!(Attribute::from_str("noitalic"), Ok(Attribute::NO_ITALIC));
84+
assert_eq!(Attribute::from_str("nostrike"), Ok(Attribute::NO_STRIKE));
85+
}
86+
87+
#[test]
88+
fn inverted_dashed() {
89+
assert_eq!(Attribute::from_str("no-bold"), Ok(Attribute::NO_BOLD));
90+
assert_eq!(Attribute::from_str("no-dim"), Ok(Attribute::NO_DIM));
91+
assert_eq!(Attribute::from_str("no-ul"), Ok(Attribute::NO_UL));
92+
assert_eq!(Attribute::from_str("no-blink"), Ok(Attribute::NO_BLINK));
93+
assert_eq!(Attribute::from_str("no-reverse"), Ok(Attribute::NO_REVERSE));
94+
assert_eq!(Attribute::from_str("no-italic"), Ok(Attribute::NO_ITALIC));
95+
assert_eq!(Attribute::from_str("no-strike"), Ok(Attribute::NO_STRIKE));
96+
}
97+
98+
#[test]
99+
fn invalid() {
100+
assert!(Attribute::from_str("no-reset").is_err());
101+
assert!(Attribute::from_str("noreset").is_err());
102+
assert!(Attribute::from_str("a").is_err());
103+
assert!(Attribute::from_str("no bold").is_err());
104+
assert!(Attribute::from_str("").is_err());
105+
assert!(Attribute::from_str("no").is_err());
106+
assert!(Attribute::from_str("no-").is_err());
107+
}
108+
}
109+
110+
mod from_git {
111+
use std::convert::TryFrom;
112+
113+
use bstr::BStr;
114+
use git_config_value::Color;
115+
116+
#[test]
117+
fn reset() {
118+
assert_eq!(color("reset"), "reset");
119+
}
120+
121+
#[test]
122+
fn empty() {
123+
assert_eq!(color(""), "");
124+
}
125+
126+
#[test]
127+
fn at_most_two_colors() {
128+
assert!(try_color("red green blue").is_err());
129+
}
130+
131+
#[test]
132+
fn attribute_before_color_name() {
133+
assert_eq!(color("bold red"), "red bold");
134+
}
135+
136+
#[test]
137+
fn color_name_before_attribute() {
138+
assert_eq!(color("red bold"), "red bold");
139+
}
140+
141+
#[test]
142+
fn attribute_fg_bg() {
143+
assert_eq!(color("ul blue red"), "blue red ul");
144+
}
145+
146+
#[test]
147+
fn fg_bg_attribute() {
148+
assert_eq!(color("blue red ul"), "blue red ul");
149+
}
150+
151+
#[test]
152+
fn multiple_attributes() {
153+
assert_eq!(
154+
color("blue bold dim ul blink reverse"),
155+
"blue bold dim ul blink reverse"
156+
);
157+
}
158+
159+
#[test]
160+
fn reset_then_multiple_attributes() {
161+
assert_eq!(
162+
color("blue bold dim ul blink reverse reset"),
163+
"blue bold dim ul blink reverse reset"
164+
);
165+
}
166+
167+
#[test]
168+
fn long_color_spec() {
169+
assert_eq!(
170+
color("254 255 bold dim ul blink reverse"),
171+
"254 255 bold dim ul blink reverse"
172+
);
173+
let input = "#ffffff #ffffff bold nobold dim nodim italic noitalic ul noul blink noblink reverse noreverse strike nostrike";
174+
let expected = "#ffffff #ffffff bold dim italic ul blink reverse strike nodim nobold noitalic noul noblink noreverse nostrike";
175+
assert_eq!(color(input), expected);
176+
}
177+
178+
#[test]
179+
fn normal_default_can_clear_backgrounds() {
180+
assert_eq!(color("normal default"), "normal default");
181+
}
182+
183+
#[test]
184+
fn default_can_combine_with_attributes() {
185+
assert_eq!(
186+
color("default default no-reverse bold"),
187+
"default default bold noreverse"
188+
);
189+
}
190+
191+
fn color<'a>(name: impl Into<&'a BStr>) -> String {
192+
try_color(name).expect("input color is expected to be valid")
193+
}
194+
195+
fn try_color<'a>(name: impl Into<&'a BStr>) -> crate::Result<String> {
196+
Ok(Color::try_from(name.into())?.to_string())
197+
}
198+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::convert::TryFrom;
2+
3+
use crate::b;
4+
use git_config_value::integer::Suffix;
5+
use git_config_value::Integer;
6+
7+
#[test]
8+
fn from_str_no_suffix() {
9+
assert_eq!(Integer::try_from(b("1")).unwrap(), Integer { value: 1, suffix: None });
10+
11+
assert_eq!(
12+
Integer::try_from(b("-1")).unwrap(),
13+
Integer {
14+
value: -1,
15+
suffix: None
16+
}
17+
);
18+
}
19+
20+
#[test]
21+
fn from_str_with_suffix() {
22+
assert_eq!(
23+
Integer::try_from(b("1k")).unwrap(),
24+
Integer {
25+
value: 1,
26+
suffix: Some(Suffix::Kibi),
27+
}
28+
);
29+
30+
assert_eq!(
31+
Integer::try_from(b("1m")).unwrap(),
32+
Integer {
33+
value: 1,
34+
suffix: Some(Suffix::Mebi),
35+
}
36+
);
37+
38+
assert_eq!(
39+
Integer::try_from(b("1g")).unwrap(),
40+
Integer {
41+
value: 1,
42+
suffix: Some(Suffix::Gibi),
43+
}
44+
);
45+
}
46+
47+
#[test]
48+
fn invalid_from_str() {
49+
assert!(Integer::try_from(b("")).is_err());
50+
assert!(Integer::try_from(b("-")).is_err());
51+
assert!(Integer::try_from(b("k")).is_err());
52+
assert!(Integer::try_from(b("m")).is_err());
53+
assert!(Integer::try_from(b("g")).is_err());
54+
assert!(Integer::try_from(b("123123123123123123123123")).is_err());
55+
assert!(Integer::try_from(b("gg")).is_err());
56+
}
57+
58+
#[test]
59+
fn as_decimal() {
60+
fn decimal(input: &str) -> Option<i64> {
61+
Integer::try_from(b(input)).unwrap().to_decimal()
62+
}
63+
64+
assert_eq!(decimal("12"), Some(12), "works without suffix");
65+
assert_eq!(decimal("13k"), Some(13 * 1024), "works with kilobyte suffix");
66+
assert_eq!(decimal("13K"), Some(13 * 1024), "works with Kilobyte suffix");
67+
assert_eq!(decimal("14m"), Some(14 * 1_048_576), "works with megabyte suffix");
68+
assert_eq!(decimal("14M"), Some(14 * 1_048_576), "works with Megabyte suffix");
69+
assert_eq!(decimal("15g"), Some(15 * 1_073_741_824), "works with gigabyte suffix");
70+
assert_eq!(decimal("15G"), Some(15 * 1_073_741_824), "works with Gigabyte suffix");
71+
72+
assert_eq!(decimal(&format!("{}g", i64::MAX)), None, "overflow results in None");
73+
assert_eq!(decimal(&format!("{}g", i64::MIN)), None, "underflow results in None");
74+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use bstr::{BStr, ByteSlice};
2+
use std::borrow::Cow;
3+
4+
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
5+
fn b(s: &str) -> &bstr::BStr {
6+
s.into()
7+
}
8+
9+
pub fn cow_str(s: &str) -> Cow<'_, BStr> {
10+
Cow::Borrowed(s.as_bytes().as_bstr())
11+
}
12+
13+
mod boolean;
14+
mod color;
15+
mod integer;
16+
mod path;

0 commit comments

Comments
 (0)