use ahk::builtins::names;
use ahk::classes;
use ahk::dialect::{self, Dialect};
use ahk::variables;
use std::collections::BTreeSet;
use std::sync::OnceLock;
const SURFACE: &str = include_str!("../reference/ahk-v2.0.32.surface.tsv");
const SURFACE_KS: &str = include_str!("../reference/ahk-ks.surface.tsv");
const PENDING: &str = include_str!("../reference/ahk-v2.0.32.pending.tsv");
const SURFACE_V1: &str = include_str!("../reference/ahk-v1.1.37.02.surface.tsv");
const PENDING_V1: &str = include_str!("../reference/ahk-v1.1.37.02.pending.tsv");
const DIALECT: &[&str] = &["Screenshot", "IsSet"];
const REASONS: &[&str] = &["windows", "hotkey", "class", "variable", "not-yet"];
const KINDS: &[&str] = &["command", "function", "variable", "method", "class"];
const KINDS_V1: &[&str] = &["command", "function", "variable", "directive"];
const REASONS_V1: &[&str] = &["not-yet"];
fn rows(table: &str) -> Vec<Vec<&str>> {
let mut out: Vec<Vec<&str>> = Vec::new();
for line in table.lines() {
let line = line.trim_end_matches('\r');
if line.is_empty() || line.starts_with('#') {
continue;
}
let fields: Vec<&str> = line.split('\t').collect();
if fields[0] == "kind" {
continue;
}
out.push(fields);
}
out
}
fn surface_names() -> BTreeSet<&'static str> {
rows(SURFACE).into_iter().map(|row| row[2]).collect()
}
fn builtin_names() -> &'static BTreeSet<String> {
static NAMES: OnceLock<BTreeSet<String>> = OnceLock::new();
NAMES.get_or_init(|| names().map(|name| name.to_lowercase()).collect())
}
fn pending_names() -> BTreeSet<&'static str> {
rows(PENDING).into_iter().map(|row| row[2]).collect()
}
fn places(table: &str) -> BTreeSet<(String, String, String)> {
rows(table)
.into_iter()
.map(|row| (row[0].to_string(), row[1].to_string(), row[2].to_string()))
.collect()
}
fn carries(kind: &str, class: &str, name: &str) -> bool {
match kind {
"command" | "function" => builtin_names().contains(&name.to_lowercase()),
"variable" => variables::carries(name),
"method" => classes::carries(class, name),
"class" => classes::is_a_name_of_a_class(name),
_ => false,
}
}
fn carries_of(dialect: &dyn Dialect, kind: &str, class: &str, name: &str) -> bool {
match kind {
"command" | "function" => dialect
.builtins()
.iter()
.any(|builtin| builtin.name.eq_ignore_ascii_case(name)),
"variable" => dialect.carries_variable(name),
"method" => classes::carries(class, name),
"class" => classes::is_a_name_of_a_class(name),
"directive" => dialect.carries_directive(name.trim_start_matches('#')),
_ => false,
}
}
fn counts(surface: usize, pending: usize) -> String {
format!(
"the surface of v2 has {surface} names, and the pending table lists {pending} \
of them"
)
}
fn counts_v1(surface: usize, pending: usize) -> String {
format!(
"the surface of v1 has {surface} names, and the pending table lists {pending} \
of them"
)
}
#[test]
fn every_implemented_name_is_a_name_of_v2() {
let surface = surface_names();
let invented: Vec<&str> = names()
.filter(|name| !surface.contains(name) && !DIALECT.contains(name))
.collect();
assert!(
invented.is_empty(),
"the tree carries names that v2 does not have: {invented:?}; {}",
counts(surface.len(), pending_names().len())
);
}
#[test]
fn no_name_is_both_carried_and_pending() {
let mut both: Vec<String> = Vec::new();
for row in rows(PENDING) {
let (kind, class, name) = (row[0], row[1], row[2]);
if carries(kind, class, name) {
both.push(format!("{kind} {class} {name}"));
}
}
assert!(
both.is_empty(),
"these names are carried and pending at once: {both:?}; {}",
counts(surface_names().len(), pending_names().len())
);
}
#[test]
fn every_name_of_v2_is_carried_or_pending() {
let surface = surface_names();
let pending = places(PENDING);
let lost: Vec<String> = rows(SURFACE)
.into_iter()
.filter(|row| !carries(row[0], row[1], row[2]))
.filter(|row| {
!pending.contains(&(row[0].to_string(), row[1].to_string(), row[2].to_string()))
})
.map(|row| format!("{} {} {}", row[0], row[1], row[2]))
.collect();
assert!(
lost.is_empty(),
"these names of v2 are neither carried nor pending: {lost:?}; {}",
counts(surface.len(), pending.len())
);
}
#[test]
fn every_pending_row_has_a_reason_and_a_kind() {
let pending = rows(PENDING);
let mut bad: Vec<String> = Vec::new();
for row in &pending {
let kind = row[0];
let name = if row[1].is_empty() {
row[2]
} else {
"a method"
};
if !KINDS.contains(&kind) {
bad.push(format!("{name}: the kind {kind} is not one of {KINDS:?}"));
}
if row[3].is_empty() || !REASONS.contains(&row[3]) {
bad.push(format!(
"{name}: the reason {:?} is not one of {REASONS:?}",
row[3]
));
}
}
assert!(
bad.is_empty(),
"{bad:?}; {}",
counts(surface_names().len(), pending_names().len())
);
}
#[test]
fn every_pending_row_is_a_row_of_the_surface() {
let surface = places(SURFACE);
let pending = places(PENDING);
let invented: Vec<(String, String, String)> = pending.difference(&surface).cloned().collect();
assert!(
invented.is_empty(),
"the pending table names that v2 does not have: {invented:?}; {}",
counts(surface_names().len(), pending.len())
);
}
#[test]
fn the_kind_and_the_arity_of_a_name_are_the_ones_of_the_binary() {
let mut wrong: Vec<String> = Vec::new();
for row in rows(SURFACE) {
let (kind, name) = (row[0], row[2]);
if kind != "command" && kind != "function" {
continue;
}
let Some(builtin) = ahk::builtins::find(name) else {
continue;
};
let ours = match builtin.kind {
ahk::builtins::BuiltinKind::Command => "command",
ahk::builtins::BuiltinKind::Function => "function",
};
if ours != kind {
wrong.push(format!("{name}: v2 says {kind}, the tree says {ours}"));
}
if row[4].is_empty() {
continue;
}
let low: u32 = row[4].parse().unwrap_or(u32::MAX);
let high: u32 = row[5].parse().unwrap_or(u32::MAX);
if builtin.min as u32 != low {
wrong.push(format!(
"{name}: v2 says at least {low}, the tree says {}",
builtin.min
));
}
if builtin.max as u32 != high {
wrong.push(format!(
"{name}: v2 says at most {high}, the tree says {}",
builtin.max
));
}
}
assert!(wrong.is_empty(), "the rows that drifted: {wrong:?}");
}
#[test]
fn every_class_type_of_the_tree_is_a_class_of_v2() {
let surface = surface_names();
const THE_CLASSES_THE_EXTRACTION_MISSED: [&str; 1] = ["varref"];
let invented: Vec<String> = classes::the_class_types()
.into_iter()
.filter(|name| !surface.contains(name.as_str()))
.filter(|name| {
!THE_CLASSES_THE_EXTRACTION_MISSED
.iter()
.any(|missed| name.eq_ignore_ascii_case(missed))
})
.collect();
assert!(
invented.is_empty(),
"the tree carries classes that v2 does not have: {invented:?}; {}",
counts(surface.len(), pending_names().len())
);
}
#[test]
fn every_name_of_v2_is_carried() {
let lost: Vec<String> = rows(SURFACE)
.into_iter()
.filter(|row| !carries(row[0], row[1], row[2]))
.map(|row| format!("{} {} {}", row[0], row[1], row[2]))
.collect();
assert!(
lost.is_empty(),
"these names of v2 are not carried: {lost:?}; {}",
counts(surface_names().len(), pending_names().len())
);
}
#[test]
fn every_name_of_the_v1_is_carried_or_pending() {
let dialect = dialect::v1();
let surface = places(SURFACE_V1);
let pending = places(PENDING_V1);
let lost: Vec<String> = rows(SURFACE_V1)
.into_iter()
.filter(|row| !carries_of(dialect, row[0], row[1], row[2]))
.filter(|row| {
!pending.contains(&(row[0].to_string(), row[1].to_string(), row[2].to_string()))
})
.map(|row| format!("{} {} {}", row[0], row[1], row[2]))
.collect();
assert!(
lost.is_empty(),
"these names of the v1 are neither carried nor pending: {lost:?}; {}",
counts_v1(surface.len(), pending.len())
);
}
#[test]
fn no_name_of_the_v1_is_both_carried_and_pending() {
let dialect = dialect::v1();
let mut both: Vec<String> = Vec::new();
for row in rows(PENDING_V1) {
let (kind, class, name) = (row[0], row[1], row[2]);
if carries_of(dialect, kind, class, name) {
both.push(format!("{kind} {class} {name}"));
}
}
assert!(
both.is_empty(),
"these names of the v1 are carried and pending at once: {both:?}; {}",
counts_v1(rows(SURFACE_V1).len(), rows(PENDING_V1).len())
);
}
#[test]
fn every_pending_row_of_the_v1_is_a_row_of_the_surface_with_a_reason() {
let surface = places(SURFACE_V1);
let mut bad: Vec<String> = Vec::new();
for row in rows(PENDING_V1) {
let (kind, class, name) = (row[0], row[1], row[2]);
if !KINDS_V1.contains(&kind) {
bad.push(format!(
"{name}: the kind {kind} is not one of {KINDS_V1:?}"
));
}
if row[3].is_empty() || !REASONS_V1.contains(&row[3]) {
bad.push(format!(
"{name}: the reason {:?} is not one of {REASONS_V1:?}",
row[3]
));
}
if !surface.contains(&(kind.to_string(), class.to_string(), name.to_string())) {
bad.push(format!(
"{name}: the pending table names that the v1 does not have"
));
}
}
assert!(
bad.is_empty(),
"{bad:?}; {}",
counts_v1(rows(SURFACE_V1).len(), rows(PENDING_V1).len())
);
}
#[test]
fn every_name_of_the_table_of_the_v1_is_a_name_of_the_surface() {
let surface = surface_names_of_the_v1();
let invented: Vec<String> = dialect::v1()
.builtins()
.iter()
.map(|builtin| builtin.name.to_string())
.filter(|name| !surface.contains(name.as_str()))
.collect();
assert!(
invented.is_empty(),
"the table of the v1 names that the surface does not have: {invented:?}; {}",
counts_v1(surface.len(), rows(PENDING_V1).len())
);
}
fn surface_names_of_the_v1() -> BTreeSet<&'static str> {
rows(SURFACE_V1).into_iter().map(|row| row[2]).collect()
}
#[test]
fn every_name_of_the_surface_of_the_ks_is_carried() {
let mut carried: BTreeSet<String> = BTreeSet::new();
for dialect in [dialect::ks(), dialect::v1()] {
for entry in ahk::surface::entries_of(dialect) {
if entry.state == ahk::surface::State::Value {
carried.insert(entry.full_name().to_lowercase());
}
}
}
let lost: Vec<String> = rows(SURFACE_KS)
.into_iter()
.filter(|row| {
let name = if row[1].is_empty() {
row[2].to_lowercase()
} else {
format!("{}.{}", row[1].to_lowercase(), row[2].to_lowercase())
};
!carried.contains(&name)
})
.map(|row| format!("{} {} {}", row[0], row[1], row[2]))
.collect();
assert!(
lost.is_empty(),
"these names of the surface of KeySharp are not carried: {lost:?}; the table has {} \
names",
rows(SURFACE_KS).len()
);
}