add some more tests

This commit is contained in:
ms2k 2026-03-31 22:32:41 +09:00
commit dade4e204b
2 changed files with 112 additions and 0 deletions

View file

@ -308,6 +308,55 @@ fn names_match(display_name: &str, status_country: &str) -> bool {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::countries::Country;
fn country(name: &str) -> Country {
Country { name: name.to_string(), nordvpn_key: name.replace(' ', "_") }
}
// ── build_list_rows ─────────────────────────────────────────────────────
#[test]
fn list_rows_no_favs() {
let countries = [country("Germany"), country("Japan")];
let refs: Vec<&Country> = countries.iter().collect();
let rows = build_list_rows(&[], &refs);
// No header, no space — just two Country rows
assert!(matches!(rows[0], ListRow::Country(c) if c.name == "Germany"));
assert!(matches!(rows[1], ListRow::Country(c) if c.name == "Japan"));
assert_eq!(rows.len(), 2);
}
#[test]
fn list_rows_favs_only() {
let countries = [country("Japan")];
let refs: Vec<&Country> = countries.iter().collect();
let rows = build_list_rows(&refs, &[]);
assert!(matches!(rows[0], ListRow::Header("★ Favorites")));
assert!(matches!(rows[1], ListRow::Country(c) if c.name == "Japan"));
assert!(matches!(rows[2], ListRow::Space));
assert_eq!(rows.len(), 3);
}
#[test]
fn list_rows_favs_and_rest() {
let fav = country("Japan");
let rest = country("Germany");
let rows = build_list_rows(&[&fav], &[&rest]);
// Header + Japan + Space + "All" header + Germany
assert!(matches!(rows[0], ListRow::Header("★ Favorites")));
assert!(matches!(rows[1], ListRow::Country(c) if c.name == "Japan"));
assert!(matches!(rows[2], ListRow::Space));
assert!(matches!(rows[3], ListRow::Header("All")));
assert!(matches!(rows[4], ListRow::Country(c) if c.name == "Germany"));
assert_eq!(rows.len(), 5);
}
#[test]
fn list_rows_empty() {
let rows = build_list_rows(&[], &[]);
assert!(rows.is_empty());
}
#[test] #[test]
fn names_match_exact() { fn names_match_exact() {

View file

@ -331,3 +331,66 @@ fn norm(s: &str) -> String {
.map(|c| c.to_ascii_lowercase()) .map(|c| c.to_ascii_lowercase())
.collect() .collect()
} }
#[cfg(test)]
mod tests {
use super::*;
// ── point_in_polygon ────────────────────────────────────────────────────
fn unit_square() -> Vec<[f64; 2]> {
vec![[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]
}
#[test]
fn pip_inside() {
assert!(point_in_polygon([0.5, 0.5], &unit_square()));
}
#[test]
fn pip_outside() {
assert!(!point_in_polygon([2.0, 2.0], &unit_square()));
assert!(!point_in_polygon([-1.0, 0.5], &unit_square()));
}
#[test]
fn pip_triangle() {
let tri = vec![[0.0, 0.0], [4.0, 0.0], [2.0, 4.0]];
assert!(point_in_polygon([2.0, 1.0], &tri));
assert!(!point_in_polygon([0.0, 3.0], &tri));
}
#[test]
fn pip_concave_polygon() {
// L-shape: point inside the L, point in the concave notch (outside)
let l_shape = vec![
[0.0, 0.0], [2.0, 0.0], [2.0, 1.0],
[1.0, 1.0], [1.0, 2.0], [0.0, 2.0],
];
assert!(point_in_polygon([0.5, 0.5], &l_shape)); // inside
assert!(!point_in_polygon([1.5, 1.5], &l_shape)); // in the notch
}
// ── norm ────────────────────────────────────────────────────────────────
#[test]
fn norm_strips_spaces_and_lowercases() {
assert_eq!(norm("United States"), "unitedstates");
}
#[test]
fn norm_strips_punctuation() {
assert_eq!(norm("Lao People's Democratic Republic"), "laopeoplesdemocraticrepublic");
}
#[test]
fn norm_us_prefix_match() {
// Ensures "United States of America" starts with norm("United States")
assert!(norm("United States of America").starts_with(&norm("United States")));
}
#[test]
fn norm_no_false_prefix() {
assert!(!norm("United Kingdom").starts_with(&norm("United States")));
}
}