use ahk::desktop::{
Clipboard, 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;
#[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 {}
impl Clipboard for Fake {}
impl Timers for Fake {}
impl Hotkeys 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 Windows for Fake {}
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 {}
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)
}
#[test]
fn a_function_has_no_address() {
let report = run("f := Abs\nx := f.Ptr");
assert!(!report.ok);
assert_eq!(
report.error.as_deref(),
Some("This value of type \"Func\" has no property named \"Ptr\".")
);
}
#[test]
fn a_function_has_no_address_to_call() {
let report = run("f := Abs\nx := f.Ptr()");
assert!(!report.ok);
assert_eq!(
report.error.as_deref(),
Some("This value of type \"Func\" has no method named \"Ptr\".")
);
}
#[test]
fn the_flags_of_the_parameters_are_the_ones_of_v2() {
let report = run(concat!(
"f(a, b?) {\n",
" return 1\n",
"}\n",
"g(&x, y := 2) {\n",
" return 1\n",
"}\n",
"class C {\n",
" m(a, b?) {\n",
" return 1\n",
" }\n",
"}\n",
"t := C()\n",
"MsgBox f.IsOptional(1) \"|\" f.IsOptional(2) \"|\" g.IsByRef(1) \"|\" g.IsOptional(2) \"|\" t.m.IsOptional(3)",
));
assert!(report.ok, "{:?}", report.error);
assert_eq!(report.output, vec!["0|1|1|1|1"]);
let report = run(
"f(a) {\n return 1\n}\ntry {\n x := f.IsOptional(2)\n} catch Any as e {\n MsgBox e.Message\n}",
);
assert!(report.ok, "{:?}", report.error);
assert_eq!(
report.output,
vec!["Parameter #1 of Func.Prototype.IsOptional is invalid."]
);
}
#[test]
fn a_function_is_not_made_again() {
let report = run("f := Abs\nx := f.__New()");
assert!(!report.ok);
assert_eq!(
report.error.as_deref(),
Some("This value of type \"Func\" has no method named \"__New\".")
);
}
#[test]
fn a_function_is_not_made() {
let report = run(
"try {\n x := Func()\n} catch Any as e {\n MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
);
assert!(report.ok, "{:?}", report.error);
assert_eq!(report.output, vec!["Invalid base. | Object.Call | "]);
}
#[test]
fn the_name_of_a_function_may_not_be_written() {
let report = run(
"try {\n f := (a, b) => a\n f.Name := \"x\"\n} catch Any as e {\n MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
);
assert!(report.ok, "{:?}", report.error);
assert_eq!(report.output, vec!["Property is read-only. | | Name"]);
}
#[test]
fn the_bind_of_a_method_calls_it_with_the_object() {
let report = run(
"class Thing {\n __New() {\n this.v := 42\n }\n Method(x) {\n return this.v + x\n }\n}\nt := Thing()\nf := t.Method.Bind(t)\nMsgBox f(8)",
);
assert!(report.ok, "{:?}", report.error);
assert_eq!(report.output, vec!["50"]);
}
#[test]
fn the_bind_of_a_builtin_is_made_and_its_call_fails() {
let report = run("f := Abs.Bind(5)\nMsgBox f(3)");
assert!(!report.ok);
assert_eq!(
report.error.as_deref(),
Some("Too many parameters passed to function.")
);
}