Skip to content

Commit

Permalink
refactor: improve version check and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushuo committed Oct 22, 2024
1 parent 57b7b90 commit 2a8b2a2
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,11 @@ pub fn read(path: String) -> Config {
}

fn check_if_rescript11_or_higher(version: &str) -> bool {
// Non stable rescript versions might contain non-numeric characters
let filter_non_number_chars = |s: &str| s.chars().filter(|c| c.is_numeric()).collect::<String>();

version
.split('.')
.next()
.map(filter_non_number_chars)
.unwrap()
.parse::<usize>()
.unwrap()
>= 11
if let Some(major) = version.split('.').next().and_then(|s| s.parse::<usize>().ok()) {
major >= 11
} else {
false
}
}

fn namespace_from_package_name(package_name: &str) -> String {
Expand Down Expand Up @@ -433,4 +427,23 @@ mod tests {
assert_eq!(config.gentype_config.is_some(), true);
assert_eq!(config.get_gentype_arg(), vec!["-bs-gentype".to_string()]);
}

#[test]
fn test_check_if_rescript11_or_higher() {
assert_eq!(check_if_rescript11_or_higher("11.0.0"), true);
assert_eq!(check_if_rescript11_or_higher("11.0.1"), true);
assert_eq!(check_if_rescript11_or_higher("11.1.0"), true);

assert_eq!(check_if_rescript11_or_higher("12.0.0"), true);

assert_eq!(check_if_rescript11_or_higher("10.0.0"), false);
assert_eq!(check_if_rescript11_or_higher("9.0.0"), false);
}

#[test]
fn test_check_if_rescript11_or_higher_misc() {
assert_eq!(check_if_rescript11_or_higher("11"), true);
assert_eq!(check_if_rescript11_or_higher("*"), false);
assert_eq!(check_if_rescript11_or_higher("12.0.0-alpha.4"), true);
}
}

0 comments on commit 2a8b2a2

Please sign in to comment.