use ahk::desktop::{
Clipboard, Com, Desktop, Dialogs, Files, Hotkeys, Keyboard, Machine, Native, Network, Pictures,
Pointer, Processes, Screen, Sound, Timers, Windows,
};
use ahk::parse;
use ahk::runtime::{Report, Runtime};
use std::time::Duration;
const SURFACE: &str = include_str!("../reference/ahk-v2.0.32.surface.tsv");
const REACHABLE: &[(&str, &str)] = &[
("Array", "x := Array()"),
("Buffer", "x := Buffer(4)"),
("File", "x := FileOpen(\"probe.txt\", \"w\")"),
("Func", "x := Abs"),
("Gui", "x := Gui()"),
("Gui.Menu", "x := Menu()"),
("Hotkey", "x := Hotkey(\"^!a\", \"TheCallback\")"),
("InputHook", "x := InputHook()"),
("Map", "x := Map()"),
];
#[derive(Debug, Default)]
struct Fake;
impl Desktop for Fake {}
impl Pictures for Fake {}
impl Network for Fake {}
impl Sound for Fake {}
impl Machine for Fake {}
impl Dialogs for Fake {}
impl Files for Fake {
fn open_file(&mut self, _path: &str, _mode: &str) -> Result<i64, String> {
Ok(1)
}
fn read_file_at(&mut self, _handle: i64, _pos: i64, _count: i64) -> Result<Vec<u8>, String> {
Ok(Vec::new())
}
fn write_file_at(&mut self, _handle: i64, _pos: i64, bytes: &[u8]) -> Result<usize, String> {
Ok(bytes.len())
}
fn file_size(&mut self, _handle: i64) -> Result<i64, String> {
Ok(0)
}
fn close_file(&mut self, _handle: i64) -> Result<(), String> {
Ok(())
}
}
impl Clipboard for Fake {}
impl Timers for Fake {}
impl Hotkeys for Fake {}
impl Windows for Fake {}
impl Keyboard for Fake {
fn type_text(&mut self, _text: &str) -> Result<String, String> {
Ok(String::new())
}
fn press_key(&mut self, _name: &str, _modifiers: &[String]) -> Result<String, String> {
Ok(String::new())
}
}
impl Pointer for Fake {
fn click(&mut self, _x: i64, _y: i64, _button: &str, _count: u32) -> Result<String, String> {
Ok(String::new())
}
}
impl Screen for Fake {
fn capture(
&mut self,
_x: i64,
_y: i64,
_width: i64,
_height: i64,
_filename: &str,
) -> Result<String, String> {
Ok(String::new())
}
fn pixel(&mut self, _x: i64, _y: i64) -> Result<String, String> {
Ok("#000000".into())
}
}
impl Processes for Fake {
fn run(&mut self, _command: &str, _window: &str) -> Result<(i32, String, String), String> {
Ok((0, String::new(), String::new()))
}
}
impl Native for Fake {}
impl Com for Fake {}
fn run(source: &str) -> Report {
let mut desktop = Fake;
let statements = parse(source).expect("parses");
Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements)
}
fn names_of(class: &str) -> Vec<String> {
let mut names: Vec<String> = Vec::new();
for line in SURFACE.lines() {
let mut fields = line.trim_end().split('\t');
let kind = fields.next().unwrap_or("");
let owner = fields.next().unwrap_or("");
let name = fields.next().unwrap_or("");
if kind == "method" && owner == class && !names.iter().any(|entry| entry == name) {
names.push(name.to_string());
}
}
names
}
#[test]
fn every_name_of_every_class_answers() {
let mut lost: Vec<String> = Vec::new();
for (class, make) in REACHABLE {
for name in names_of(class) {
let called = run(&format!("{make}\nx.{name}()"));
let error = called.error.unwrap_or_default();
if !error.contains("has no method named") {
continue;
}
let read = run(&format!("{make}\ny := x.{name}"));
let error = read.error.unwrap_or_default();
if error.contains("has no property") {
lost.push(format!("{class}.{name}: {error}"));
}
}
}
assert!(lost.is_empty(), "these names of v2 are lost: {lost:?}");
}
#[test]
fn a_class_does_not_answer_the_names_of_another() {
let report = run("x := Gui()\nx.Focus()");
let error = report.error.unwrap_or_default();
assert!(error.contains("has no method named"), "{error}");
}