[wip] unit tests

- strip unneeded mod `test_util::mock_ids::tests`
- coverage for `sharry::uri`
This commit is contained in:
Jörn-Michael Miehe 2025-07-14 15:20:21 +00:00
parent 037e59df4f
commit 1ac4db28ed
2 changed files with 86 additions and 61 deletions

View file

@ -28,7 +28,7 @@ impl AsRef<[u8]> for Uri {
fn captured(caps: &Captures, name: &str) -> String {
caps.name(name)
.unwrap_or_else(|| panic!("{name} not captured"))
.unwrap_or_else(|| panic!("{name:?} not captured"))
.as_str()
.to_string()
}
@ -103,6 +103,36 @@ mod tests {
test_util::check_trait,
};
#[test]
fn check_captured_works() {
let cases = [
// simple http host
("http://example.com", "http", "example.com"),
// https host with port
("https://my-host:8080", "https", "my-host:8080"),
// trailing slash
("scheme://host/", "scheme", "host"),
// with path
("scheme://host/path/to/whatever", "scheme", "host"),
// custom scheme
("custom+scheme://host", "custom+scheme", "host"),
];
for (good, scheme, host) in cases {
let caps = SHARRY_URI_RE.captures(good).unwrap();
assert_eq!(captured(&caps, "scheme"), scheme);
assert_eq!(captured(&caps, "host"), host);
}
}
#[test]
#[should_panic = "\"foo\" not captured"]
fn make_captured_panic() {
let caps = SHARRY_URI_RE.captures("http://example.com").unwrap();
captured(&caps, "foo");
}
#[test]
fn basic_traits_working() {
let cases = [

View file

@ -109,14 +109,8 @@ impl From<bool> for FileID {
}
}
#[cfg(test)]
mod tests {
use crate::Parameter;
use super::*;
#[test]
fn true_makes_valids() {
#[test]
fn true_makes_valids() {
let uri = Uri::from(true);
let alias_id = AliasID::from(true);
let share_id = ShareID::from(true);
@ -125,10 +119,10 @@ mod tests {
assert!((&uri, &alias_id).check().is_ok());
assert!(share_id.check().is_ok());
assert!((&share_id, &file_id).check().is_ok());
}
}
#[test]
fn default_is_valid() {
#[test]
fn default_is_valid() {
let uri = Uri::default();
let alias_id = AliasID::from(true); // no `impl Default`
let share_id = ShareID::default();
@ -137,10 +131,12 @@ mod tests {
assert!((&uri, &alias_id).check().is_ok());
assert!(share_id.check().is_ok());
assert!((&share_id, &file_id).check().is_ok());
}
}
#[test]
fn false_makes_invalids() {
use crate::Parameter;
#[test]
fn false_makes_invalids() {
fn test_check(value: impl CheckID, callback: impl FnOnce(&Parameter) -> bool) {
let check = value.check().unwrap_err();
let p = check.get_invalid_param().unwrap();
@ -177,5 +173,4 @@ mod tests {
test_check((&share_id_i, &file_id_i), is_share_id_i);
test_check((&share_id_i, &file_id), is_share_id_i);
test_check((&share_id, &file_id_i), is_file_id_i);
}
}