Skip to content

Commit 4b4ac8a

Browse files
committed
refactor baseline tests
Also, add iformation about serialization precision - usually there may be some difference when serializing a parsed URL back into a string, but ideally it leads to the same outcome.
1 parent e9aa690 commit 4b4ac8a

File tree

1 file changed

+46
-54
lines changed

1 file changed

+46
-54
lines changed

gix-url/tests/baseline.rs

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn run() {
99

1010
let mut count = 0;
1111
let mut failures = Vec::new();
12+
let (mut failed_roundtrips, mut serialized_url_does_not_match_input) = (0, 0);
1213
for (url, expected) in baseline::URLS.iter() {
1314
count += 1;
1415
let actual = match gix_url::parse(url) {
@@ -18,9 +19,12 @@ fn run() {
1819
continue;
1920
}
2021
};
22+
let url_as_string = actual.to_bstring();
23+
serialized_url_does_not_match_input += usize::from(url_as_string != *url);
24+
failed_roundtrips += usize::from(gix_url::parse(url_as_string.as_ref()).ok().as_ref() != Some(&actual));
2125
let result = std::panic::catch_unwind(|| assert_urls_equal(expected, &actual)).map_err(|panic| {
2226
match downcast_panic_to_str(&panic) {
23-
Some(s) => format!("{url}: {s}\nexpected: {expected:?}\nactual: {actual:?}").into(),
27+
Some(s) => format!("{url}: {s}\nexpected: {expected:?}\nactual: {actual:?}"),
2428
None => format!("{url}: expected: {expected:?}\nactual: {actual:?}"),
2529
}
2630
});
@@ -42,7 +46,13 @@ fn run() {
4246
failures.len(),
4347
count - failures.len()
4448
);
49+
assert!(
50+
serialized_url_does_not_match_input <= 126,
51+
"we shouldn't get worse when serializing to match our input URL"
52+
);
53+
4554
let kind = baseline::Kind::new();
55+
assert_eq!(failed_roundtrips, 0);
4656
assert!(
4757
failures.len() <= kind.max_num_failures(),
4858
"Expected no more than {} failures, but got {} - this should get better, not worse",
@@ -70,30 +80,29 @@ fn downcast_panic_to_str<'a>(panic: &'a Box<dyn Any + Send + 'static>) -> Option
7080

7181
fn assert_urls_equal(expected: &baseline::GitDiagUrl<'_>, actual: &gix_url::Url) {
7282
assert_eq!(
83+
actual.scheme,
7384
gix_url::Scheme::from(expected.protocol.to_str().unwrap()),
74-
actual.scheme
7585
);
7686

7787
match expected.host {
7888
baseline::GitDiagHost::NonSsh { host_and_port } => match host_and_port {
79-
Some(host_and_port) if !host_and_port.is_empty() => {
89+
Some(expected_host_and_port) if !expected_host_and_port.is_empty() => {
8090
assert!(actual.host().is_some());
8191

82-
let mut gix_host_and_port = String::with_capacity(host_and_port.len());
83-
92+
let mut actual_host_and_port = String::new();
8493
if let Some(user) = actual.user() {
85-
gix_host_and_port.push_str(user);
86-
gix_host_and_port.push('@');
94+
actual_host_and_port.push_str(user);
95+
actual_host_and_port.push('@');
8796
}
8897

89-
gix_host_and_port.push_str(actual.host().unwrap());
98+
actual_host_and_port.push_str(actual.host().unwrap());
9099

91100
if let Some(port) = actual.port {
92-
gix_host_and_port.push(':');
93-
gix_host_and_port.push_str(&port.to_string());
101+
actual_host_and_port.push(':');
102+
actual_host_and_port.push_str(&port.to_string());
94103
}
95104

96-
assert_eq!(host_and_port, gix_host_and_port);
105+
assert_eq!(actual_host_and_port, expected_host_and_port);
97106
}
98107
_ => {
99108
assert!(actual.host().is_none());
@@ -102,44 +111,28 @@ fn assert_urls_equal(expected: &baseline::GitDiagUrl<'_>, actual: &gix_url::Url)
102111
},
103112
baseline::GitDiagHost::Ssh { user_and_host, port } => {
104113
match user_and_host {
105-
Some(user_and_host) => {
114+
Some(expected_user_and_host) => {
106115
assert!(actual.host().is_some());
107116

108-
let mut gix_user_and_host = String::with_capacity(user_and_host.len());
117+
let mut actual_user_and_host = String::new();
109118
if let Some(user) = actual.user() {
110-
gix_user_and_host.push_str(user);
111-
gix_user_and_host.push('@');
119+
actual_user_and_host.push_str(user);
120+
actual_user_and_host.push('@');
112121
}
113-
gix_user_and_host.push_str(actual.host().unwrap());
122+
actual_user_and_host.push_str(actual.host().unwrap());
114123

115-
assert_eq!(user_and_host, gix_user_and_host);
124+
assert_eq!(actual_user_and_host, expected_user_and_host);
116125
}
117126
None => {
118127
assert!(actual.host().is_none());
119128
assert!(actual.user().is_none());
120129
}
121130
}
122-
match port {
123-
Some(port) => {
124-
assert!(actual.port.is_some());
125-
assert_eq!(port, actual.port.unwrap().to_string());
126-
}
127-
None => {
128-
assert!(actual.port.is_none());
129-
}
130-
}
131+
assert_eq!(actual.port.map(|p| p.to_string()), port.map(ToString::to_string));
131132
}
132133
}
133134

134-
match expected.path {
135-
Some(path) => {
136-
assert_eq!(path, actual.path);
137-
}
138-
None => {
139-
// I guess? This case does not happen a single time in the current fixtures...
140-
assert!(actual.path.is_empty());
141-
}
142-
}
135+
assert_eq!(actual.path, expected.path.unwrap_or_default());
143136
}
144137

145138
mod baseline {
@@ -177,20 +170,20 @@ mod baseline {
177170

178171
static BASELINE: Lazy<BString> = Lazy::new(|| {
179172
let base = gix_testtools::scripted_fixture_read_only("make_baseline.sh").unwrap();
180-
BString::from(
181-
std::fs::read(base.join(format!("git-baseline.{}", Kind::new().extension()))).expect("fixture file exists"),
182-
)
173+
std::fs::read(base.join(format!("git-baseline.{}", Kind::new().extension())))
174+
.expect("fixture file exists")
175+
.into()
183176
});
184177

185178
pub static URLS: Lazy<Vec<(&'static BStr, GitDiagUrl<'static>)>> = Lazy::new(|| {
186179
let mut out = Vec::new();
187180

188-
let url_block = BASELINE
181+
let blocks = BASELINE
189182
.split(|c| c == &b';')
190-
.filter(|url| !url.is_empty())
183+
.filter(|block| !block.is_empty())
191184
.map(ByteSlice::trim);
192185

193-
for block in url_block {
186+
for block in blocks {
194187
let (url, diag_url) = GitDiagUrl::parse(block.as_bstr());
195188
out.push((url, diag_url));
196189
}
@@ -208,8 +201,15 @@ mod baseline {
208201
/// Parses the given string into a [GitDiagUrl] according to the format
209202
/// specified in [Git's `connect.c`][git_src].
210203
///
211-
/// [git_src]: https://github.com/git/git/blob/master/connect.c#L1415
204+
/// [git_src]: https://github.com/git/git/blob/bcb6cae2966cc407ca1afc77413b3ef11103c175/connect.c#L1415
212205
fn parse(diag_url: &BStr) -> (&'_ BStr, GitDiagUrl<'_>) {
206+
fn null_is_none(input: &BStr) -> Option<&BStr> {
207+
if input == "NULL" || input == "NONE" {
208+
None
209+
} else {
210+
Some(input)
211+
}
212+
}
213213
let mut lines = diag_url.lines().map(ByteSlice::trim);
214214
let mut next_attr = |name: &str| {
215215
lines
@@ -227,21 +227,13 @@ mod baseline {
227227
let user_and_host = next_attr("userandhost");
228228
let port = next_attr("port");
229229
GitDiagHost::Ssh {
230-
user_and_host: if user_and_host == "NULL" {
231-
None
232-
} else {
233-
Some(user_and_host)
234-
},
235-
port: if port == "NONE" { None } else { Some(port) },
230+
user_and_host: null_is_none(user_and_host),
231+
port: null_is_none(port),
236232
}
237233
} else {
238234
let host_and_port = next_attr("hostandport");
239235
GitDiagHost::NonSsh {
240-
host_and_port: if host_and_port == "NULL" {
241-
None
242-
} else {
243-
Some(host_and_port)
244-
},
236+
host_and_port: null_is_none(host_and_port),
245237
}
246238
};
247239

@@ -252,7 +244,7 @@ mod baseline {
252244
GitDiagUrl {
253245
protocol,
254246
host,
255-
path: if path == "NULL" { None } else { Some(path) },
247+
path: null_is_none(path),
256248
},
257249
)
258250
}

0 commit comments

Comments
 (0)