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;
#[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 {}
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 the_answer_of(source: &str) -> String {
let report = run(source);
assert!(report.ok, "{:?}", report.error);
report.output.first().cloned().unwrap_or_default()
}
fn the_failure_of(source: &str) -> String {
let report = run(&format!(
"try {{\n {source}\n}} catch Any as e {{\n MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}}\n"
));
assert!(report.ok, "{:?}", report.error);
report
.output
.first()
.cloned()
.unwrap_or_default()
.trim()
.to_string()
}
fn the_load_failure_of(source: &str) -> String {
parse(source).expect_err("does not parse").message
}
#[test]
fn the_base_of_an_object_is_the_prototype_of_its_class() {
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.Base.__Class"),
"Object"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.Prototype.Base.__Class"),
"Object"
);
}
#[test]
fn the_base_of_a_class_is_the_class_that_it_extends() {
assert_eq!(the_answer_of("c := Map\nMsgBox c.Base.__Class"), "Class");
assert_eq!(the_answer_of("c := Object\nMsgBox c.Base.__Class"), "Class");
}
#[test]
fn the_copy_of_an_object_carries_the_state_of_it() {
assert_eq!(
the_answer_of("o := Object()\no.x := 1\nMsgBox o.Clone().x"),
"1"
);
}
#[test]
fn the_copy_of_a_class_is_a_class() {
assert_eq!(the_answer_of("c := Map\nMsgBox c.Clone().__Class"), "Class");
}
#[test]
fn the_call_of_a_class_value_makes_an_object() {
assert_eq!(the_answer_of("c := Map\nMsgBox c.Call().__Class"), "Map");
assert_eq!(
the_answer_of("c := Error\nMsgBox c.Call().__Class"),
"Error"
);
}
#[test]
fn the_static_methods_of_a_class_are_handed_over_as_values() {
assert_eq!(
the_answer_of("c := Map\nf := c.GetMethod(\"HasProp\")\nMsgBox f.Name"),
"HasProp"
);
assert_eq!(
the_answer_of("c := Map\nf := c.GetMethod(\"OwnProps\")\nMsgBox f.Name"),
"Object.Prototype.OwnProps"
);
assert_eq!(
the_answer_of("c := Map\nf := c.GetMethod(\"Call\")\nMsgBox f.Name"),
"Map.Call"
);
assert_eq!(
the_answer_of(
"c := Map\nf := c.GetMethod(\"HasProp\")\nMsgBox f.MinParams \" \" f.MaxParams"
),
"2 2"
);
assert_eq!(
the_answer_of("c := Map\nf := c.GetMethod(\"HasProp\")\nMsgBox f(Map(), \"x\")"),
"0"
);
assert_eq!(
the_answer_of("c := Map\nf := c.GetMethod(\"Call\")\nMsgBox f(c).__Class"),
"Map"
);
assert_eq!(
the_answer_of("c := Map\nf := c.GetMethod(\"Clone\")\nMsgBox f(Object()).__Class"),
"Object"
);
}
#[test]
fn the_method_a_class_does_not_have_is_refused_with_the_words_of_the_dialect() {
assert_eq!(
the_failure_of("c := Map\nf := c.GetMethod(\"Get\")"),
"This value of type \"Class\" has no method named \"Get\". | GetMethod |"
);
}
#[test]
fn the_copy_of_the_base_object_refuses_a_class_that_has_a_copy_of_its_own() {
assert_eq!(
the_failure_of("c := Map\nf := c.GetMethod(\"Clone\")\nf(Map())"),
"Type mismatch. | Object.Prototype.Clone |"
);
}
#[test]
fn a_class_value_has_no_name() {
assert_eq!(
the_failure_of("c := Map\nx := c.Name"),
"This value of type \"Class\" has no property named \"Name\". | |"
);
assert_eq!(
the_failure_of("class C {\n}\nx := C.Name"),
"This value of type \"Class\" has no property named \"Name\". | |"
);
}
#[test]
fn a_name_that_a_script_writes_is_a_property_of_the_class_value() {
assert_eq!(
the_answer_of("c := Map\nc.Name := \"x\"\nMsgBox c.Name"),
"x"
);
assert_eq!(
the_answer_of("c := Map\nc.Name := \"x\"\nMsgBox c.HasOwnProp(\"Name\")"),
"1"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"Name\")"),
"0"
);
assert_eq!(
the_answer_of("c := Map\nc.Name := \"x\"\nMsgBox c.__Class"),
"Class"
);
assert_eq!(
the_answer_of("c := Map\nc.Name := \"x\"\nMsgBox c.Base.__Class"),
"Class"
);
assert_eq!(
the_answer_of("c := Map\nc.Name := \"x\"\nMsgBox c.Prototype.__Class"),
"Map"
);
}
#[test]
fn a_prototype_has_no_name() {
assert_eq!(
the_failure_of("c := Map\nx := c.Prototype.Name"),
"This value of type \"Prototype\" has no property named \"Name\". | |"
);
assert_eq!(
the_answer_of("c := Map\nc.Prototype.Name := \"x\"\nMsgBox c.Prototype.Name"),
"x"
);
assert_eq!(
the_answer_of("c := Map\nc.Prototype.Name := \"x\"\nMsgBox c.Prototype.__Class"),
"Map"
);
}
#[test]
fn the_base_of_a_class_is_written_the_way_objsetbase_writes_it() {
assert_eq!(
the_failure_of("c := Map\nc.Base := 1"),
"Expected an Object but got an Integer. | ObjSetBase | 1"
);
assert_eq!(
the_failure_of("class C {\n}\nC.Base := 1"),
"Expected an Object but got an Integer. | ObjSetBase | 1"
);
assert_eq!(
the_answer_of("c := Map\nc.Base := Object()\nMsgBox c.Base.__Class"),
"Object"
);
assert_eq!(
the_answer_of("c := Map\nc.Base := Object()\nMsgBox c.HasBase(Object)"),
"0"
);
}
#[test]
fn the_prototype_of_a_class_is_written_by_a_script() {
assert_eq!(
the_answer_of("c := Map\nc.Prototype := 1\nMsgBox c.Prototype"),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\nC.Prototype := 1\nMsgBox C.Prototype"),
"1"
);
}
#[test]
fn the_static_methods_of_a_class_are_read_only() {
assert_eq!(
the_failure_of("c := Map\nc.HasProp := 1"),
"Property is read-only. | | HasProp"
);
}
#[test]
fn the_properties_of_a_class_do_not_answer_a_class_value() {
assert_eq!(
the_failure_of("c := Map\nx := c.Count"),
"This value of type \"Class\" has no property named \"Count\". | |"
);
assert_eq!(
the_failure_of("c := Map\nx := c.Clear"),
"This value of type \"Class\" has no property named \"Clear\". | |"
);
}
#[test]
fn the_base_of_a_class_of_the_script_is_the_class_that_it_extends() {
assert_eq!(
the_answer_of("class D {\n}\nclass C extends D {\n}\nx := (C.Base == D)\nMsgBox x"),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\nMsgBox C.Base.__Class"),
"Class"
);
assert_eq!(
the_answer_of("class D {\n}\nclass C extends D {\n}\nMsgBox C.HasBase(D)"),
"1"
);
assert_eq!(the_answer_of("class C {\n}\nMsgBox C.HasBase(Object)"), "1");
assert_eq!(
the_answer_of(
"class D {\n}\nclass C extends D {\n}\nC.Base := Object()\nMsgBox C.HasBase(D)"
),
"0"
);
}
#[test]
fn two_objects_are_the_same_object_and_nothing_else() {
assert_eq!(
the_answer_of("m1 := Map()\nm2 := Map()\nMsgBox m1 == m2"),
"0"
);
assert_eq!(the_answer_of("m1 := Map()\nMsgBox m1 == m1"), "1");
assert_eq!(the_answer_of("m := Map()\nMsgBox m == Map"), "0");
assert_eq!(the_answer_of("c := Map\nMsgBox c == Map"), "1");
assert_eq!(the_answer_of("m := Map()\nMsgBox m == \"map\""), "0");
assert_eq!(
the_answer_of("m1 := Map()\nm2 := Map()\nMsgBox m1 != m2"),
"1"
);
}
#[test]
fn the_static_methods_of_a_class_value_are_handed_over_as_values() {
assert_eq!(
the_answer_of("c := Map\nx := c.Call\nMsgBox x.Name"),
"Map.Call"
);
assert_eq!(
the_answer_of("c := Map\nx := c.HasProp\nMsgBox x.Name"),
"HasProp"
);
assert_eq!(
the_answer_of("class C {\n static Say() {\n }\n}\nx := C.Say\nMsgBox x.Name"),
"C.Say"
);
assert_eq!(
the_failure_of("c := Map\nx := c.Clear"),
"This value of type \"Class\" has no property named \"Clear\". | |"
);
}
#[test]
fn a_class_value_has_the_static_methods_and_not_the_others() {
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasMethod(\"Clear\")"),
"0"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasMethod(\"HasProp\")"),
"1"
);
assert_eq!(the_answer_of("c := Map\nMsgBox c.HasMethod(\"Call\")"), "1");
assert_eq!(
the_answer_of("class C {\n static Say() {\n }\n}\nMsgBox C.HasMethod(\"Say\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n Say() {\n }\n}\nMsgBox C.HasMethod(\"Say\")"),
"0"
);
assert_eq!(the_answer_of("c := Map\nMsgBox c.HasProp(\"Call\")"), "1");
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasProp(\"HasProp\")"),
"1"
);
assert_eq!(the_answer_of("c := Map\nMsgBox c.HasProp(\"Base\")"), "1");
assert_eq!(the_answer_of("c := Map\nMsgBox c.HasProp(\"Clear\")"), "0");
}
#[test]
fn the_own_properties_of_a_class_value_are_the_call_and_the_prototype() {
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"Call\")"),
"1"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"HasProp\")"),
"0"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"Prototype\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n static Say() {\n }\n}\nMsgBox C.HasOwnProp(\"Say\")"),
"1"
);
}
#[test]
fn the_write_of_a_static_method_of_a_class_is_refused() {
assert_eq!(
the_failure_of("class C {\n static Say() {\n }\n}\nC.Say := 1"),
"Property is read-only. | | Say"
);
assert_eq!(
the_answer_of("class C {\n Say() {\n }\n}\nC.Say := 1\nMsgBox C.Say"),
"1"
);
}
#[test]
fn the_methods_of_the_instances_do_not_answer_a_class_value() {
assert_eq!(
the_failure_of("c := Map\nc.Get(\"a\")"),
"This value of type \"Class\" has no method named \"Get\". | |"
);
assert_eq!(
the_failure_of("c := Map\nc.Clear()"),
"This value of type \"Class\" has no method named \"Clear\". | |"
);
assert_eq!(
the_failure_of("class C {\n Say() {\n }\n}\nC.Say()"),
"This value of type \"Class\" has no method named \"Say\". | |"
);
}
#[test]
fn every_class_has_an_initialiser_and_it_is_a_member_of_the_class_value() {
assert_eq!(the_answer_of("x := Map\ny := x.__Init\nMsgBox y.Name"), "");
assert_eq!(
the_answer_of("x := Map\ny := x.__Init\nMsgBox y.MinParams"),
"1"
);
assert_eq!(
the_answer_of("x := Map\ny := x.__Init\nMsgBox y.MaxParams"),
"1"
);
assert_eq!(
the_answer_of("x := Map\ny := x.__Init\nMsgBox y.IsBuiltin"),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\ny := C.__Init\nMsgBox y.Name"),
"C.__Init"
);
assert_eq!(
the_answer_of("class C {\n}\ny := C.__Init\nMsgBox y.IsBuiltin"),
"0"
);
}
#[test]
fn the_own_properties_of_a_class_value_are_the_ones_of_v2() {
assert_eq!(
the_answer_of("c := Map\ns := \"\"\nfor p in c.OwnProps()\n s .= p\nMsgBox s"),
"CallPrototype"
);
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n }\n}\ns := \"\"\nfor p in C.OwnProps()\n s .= p\nMsgBox s"
),
"__InitPrototypeSay"
);
assert_eq!(
the_answer_of("class C {\n}\ns := \"\"\nfor p in C.OwnProps()\n s .= p\nMsgBox s"),
"__InitPrototype"
);
assert_eq!(
the_answer_of(
"class C {\n Say() {\n }\n}\ns := \"\"\nfor p in C.OwnProps()\n s .= p\nMsgBox s"
),
"__InitPrototype"
);
}
#[test]
fn the_methods_of_the_base_object_are_inherited_and_not_own() {
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"Call\")"),
"1"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"__Init\")"),
"0"
);
assert_eq!(
the_answer_of("c := Map\nMsgBox c.HasOwnProp(\"Prototype\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\nMsgBox C.HasOwnProp(\"Call\")"),
"0"
);
assert_eq!(
the_answer_of("class C {\n}\nMsgBox C.HasOwnProp(\"__Init\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n Say() {\n }\n}\nMsgBox C.HasOwnProp(\"Say\")"),
"0"
);
assert_eq!(
the_answer_of("class C {\n static Say() {\n }\n}\nMsgBox C.HasOwnProp(\"Say\")"),
"1"
);
}
#[test]
fn the_properties_of_a_class_are_assigned_after_the_body_of_its_init() {
assert_eq!(
the_answer_of(
"class C {\n __Init() {\n this.value := 2\n }\n value := 1\n}\nobj := C()\nMsgBox obj.value"
),
"1"
);
assert_eq!(
the_answer_of(
"class C {\n __Init() {\n }\n value := 1\n}\nobj := C()\nMsgBox obj.value"
),
"1"
);
assert_eq!(
the_answer_of(
"class D {\n dvalue := 1\n}\nclass C extends D {\n __Init() {\n }\n cvalue := 2\n}\nobj := C()\nMsgBox obj.cvalue"
),
"2"
);
}
#[test]
fn the_names_of_the_own_properties_are_the_ones_that_the_script_wrote() {
assert_eq!(
the_answer_of(
"o := Object()\no.X := 1\ns := \"\"\nfor p in o.OwnProps()\n s .= p\nMsgBox s"
),
"X"
);
assert_eq!(
the_answer_of("o := {X: 1}\ns := \"\"\nfor p in o.OwnProps()\n s .= p\nMsgBox s"),
"X"
);
assert_eq!(
the_answer_of(
"m := Map()\nm.X := 1\ns := \"\"\nfor p in m.OwnProps()\n s .= p\nMsgBox s"
),
"X"
);
assert_eq!(
the_answer_of(
"class C {\n}\nobj := C()\nobj.X := 1\ns := \"\"\nfor p in obj.OwnProps()\n s .= p\nMsgBox s"
),
"X"
);
}
#[test]
fn the_first_write_of_a_property_is_the_one_that_fixes_the_case_of_it() {
assert_eq!(
the_answer_of(
"o := Object()\no.X := 1\no.x := 2\ns := \"\"\nfor p in o.OwnProps()\n s .= p\nMsgBox s"
),
"X"
);
assert_eq!(
the_answer_of(
"o := Object()\no.x := 1\no.X := 2\ns := \"\"\nfor p in o.OwnProps()\n s .= p\nMsgBox s"
),
"x"
);
assert_eq!(
the_answer_of("o := Object()\no.X := 1\no.x := 2\nMsgBox o.X"),
"2"
);
}
#[test]
fn the_names_of_the_own_properties_are_in_the_order_of_v2() {
assert_eq!(
the_answer_of(
"o := Object()\no.Zebra := 1\no.apple := 2\no.Mango := 3\ns := \"\"\nfor p in o.OwnProps()\n s .= p . \" \"\nMsgBox s"
),
"apple Mango Zebra "
);
}
#[test]
fn the_get_method_of_the_prototype_fails_with_the_words_of_the_prototype() {
assert_eq!(
the_failure_of("c := Map\nc.GetMethod(\"Prototype\")"),
"This value of type \"Prototype\" has no method named \"Call\". | GetMethod |"
);
assert_eq!(
the_failure_of("class C {\n}\nC.GetMethod(\"Prototype\")"),
"This value of type \"Prototype\" has no method named \"Call\". | GetMethod |"
);
}
#[test]
fn the_own_properties_of_a_prototype_are_the_members_of_the_class() {
assert_eq!(
the_answer_of("s := \"\"\nfor p in Map.Prototype.OwnProps()\n s .= p\nMsgBox s"),
"__Class__Enum__Item__NewCapacityCaseSenseClearCloneCountDeleteGetHasSet"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in Array.Prototype.OwnProps()\n s .= p\nMsgBox s"),
"__Class__Enum__Item__NewCapacityCloneDeleteGetHasInsertAtLengthPopPushRemoveAt"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in Buffer.Prototype.OwnProps()\n s .= p\nMsgBox s"),
"__Class__NewPtrSize"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in Error.Prototype.OwnProps()\n s .= p\nMsgBox s"),
"__Class__New"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in Object.Prototype.OwnProps()\n s .= p\nMsgBox s"),
"__ClassCloneDefinePropDeletePropGetOwnPropDescHasOwnPropOwnProps"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in Abs.Base.OwnProps()\n s .= p\nMsgBox s"),
"__ClassBindCallIsBuiltInIsByRefIsOptionalIsVariadicMaxParamsMinParamsName"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in Enumerator.Prototype.OwnProps()\n s .= p\nMsgBox s"),
"__Class"
);
assert_eq!(
the_answer_of(
"s := \"\"\nfor p in ClipboardAll.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class__New"
);
}
#[test]
fn the_own_properties_of_the_prototype_of_a_class_of_the_script_are_its_methods() {
assert_eq!(
the_answer_of(
"class C {\n __Init() {\n }\n Say() {\n }\n static Shout() {\n }\n}\ns := \"\"\nfor p in C.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class__InitSay"
);
assert_eq!(
the_answer_of(
"class C {\n Say() {\n }\n}\nclass D extends C {\n Other() {\n }\n}\ns := \"\"\nfor p in D.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__ClassOther"
);
}
#[test]
fn the_prototype_answers_the_questions_about_the_class_of_it() {
assert_eq!(the_answer_of("MsgBox Map.Prototype.__Class"), "Map");
assert_eq!(the_answer_of("MsgBox ObjOwnPropCount(Map.Prototype)"), "13");
assert_eq!(
the_answer_of("MsgBox Map.Prototype.HasOwnProp(\"__Class\")"),
"1"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.HasOwnProp(\"Count\")"),
"1"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.HasOwnProp(\"Nope\")"),
"0"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.HasProp(\"Count\")"),
"1"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.HasMethod(\"Get\")"),
"1"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.HasMethod(\"Count\")"),
"0"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.Get.Name"),
"Map.Prototype.Get"
);
assert_eq!(
the_answer_of("MsgBox Map.Prototype.GetMethod(\"Get\").Name"),
"Map.Prototype.Get"
);
assert_eq!(
the_answer_of("MsgBox Object.Prototype.GetMethod(\"Clone\").Name"),
"Object.Prototype.Clone"
);
}
#[test]
fn the_property_of_a_prototype_is_refused_with_the_kind_of_the_class() {
assert_eq!(
the_failure_of("MsgBox Map.Prototype.Count"),
"Expected a Map but got a Prototype. | Map.Prototype.Count.Get |"
);
assert_eq!(
the_failure_of("MsgBox File.Prototype.Pos"),
"Expected a File but got a Prototype. | File.Prototype.Pos.Get |"
);
assert_eq!(
the_failure_of("MsgBox Abs.Base.Name"),
"Expected a Func but got a Prototype. | Func.Prototype.Name.Get |"
);
}
#[test]
fn the_call_of_a_method_of_a_prototype_is_refused() {
assert_eq!(
the_failure_of("Map.Prototype.Get(\"a\")"),
"Expected a Map but got a Prototype. | Map.Prototype.Get |"
);
assert_eq!(
the_failure_of("Object.Prototype.Clone()"),
"Type mismatch. | Object.Prototype.Clone |"
);
}
#[test]
fn the_members_of_a_prototype_are_read_only() {
assert_eq!(
the_failure_of("Map.Prototype.Count := 5"),
"Property is read-only. | | Count"
);
assert_eq!(
the_failure_of("Map.Prototype.Get := 5"),
"Property is read-only. | | Get"
);
assert_eq!(
the_answer_of("Map.Prototype.Nope := 5\nMsgBox \"ok\""),
"ok"
);
assert_eq!(
the_answer_of("Map.Prototype.Base := Object()\nMsgBox \"ok\""),
"ok"
);
}
#[test]
fn the_methods_are_values() {
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.Clone.Name"),
"Object.Prototype.Clone"
);
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.OwnProps.Name"),
"Object.Prototype.OwnProps"
);
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.HasProp.Name"),
"HasProp"
);
assert_eq!(
the_answer_of("m := Map()\nMsgBox m.Get.Name"),
"Map.Prototype.Get"
);
assert_eq!(
the_answer_of("f := Abs\nMsgBox f.Call.Name"),
"Func.Prototype.Call"
);
}
#[test]
fn the_get_method_of_a_value_hands_the_method_over() {
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.GetMethod(\"Clone\").Name"),
"Object.Prototype.Clone"
);
assert_eq!(
the_answer_of("m := Map()\nMsgBox m.GetMethod(\"Get\").Name"),
"Map.Prototype.Get"
);
assert_eq!(
the_answer_of("a := [1]\nMsgBox a.GetMethod(\"Push\").Name"),
"Array.Prototype.Push"
);
assert_eq!(
the_answer_of("f := Abs\nMsgBox f.GetMethod(\"Call\").Name"),
"Func.Prototype.Call"
);
assert_eq!(
the_answer_of("o := Error(\"x\")\nMsgBox o.GetMethod(\"Clone\").Name"),
"Object.Prototype.Clone"
);
assert_eq!(
the_answer_of(
"class Thing {\n Say() {\n return 1\n }\n}\nobj := Thing()\nMsgBox obj.GetMethod(\"Say\").Name"
),
"Thing.Prototype.Say"
);
assert_eq!(
the_failure_of("m := Map()\nm.GetMethod(\"Nope\")"),
"This value of type \"Map\" has no method named \"Nope\". | GetMethod |"
);
}
#[test]
fn the_number_of_the_arguments_of_a_method_is_the_one_of_the_value() {
assert_eq!(the_answer_of("m := Map()\nMsgBox m.Get.MinParams"), "2");
assert_eq!(the_answer_of("m := Map()\nMsgBox m.Get.MaxParams"), "3");
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.Clone.MinParams"),
"1"
);
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.Clone.MaxParams"),
"1"
);
assert_eq!(
the_answer_of("o := Object()\nMsgBox o.DefineProp.MaxParams"),
"3"
);
}
#[test]
fn the_prototype_of_an_any_has_no_method_at_all() {
assert_eq!(
the_failure_of("Any.Prototype.OwnProps()"),
"This value of type \"Prototype\" has no method named \"OwnProps\". | |"
);
assert_eq!(
the_failure_of("ComValue.Prototype.OwnProps()"),
"This value of type \"Prototype\" has no method named \"OwnProps\". | |"
);
}
#[test]
fn a_static_method_is_not_a_member_of_an_instance() {
assert_eq!(
the_failure_of(
"class C {\n static Say() {\n return 1\n }\n}\nobj := C()\nobj.Say()"
),
"This value of type \"C\" has no method named \"Say\". | |"
);
assert_eq!(
the_failure_of(
"class C {\n static Say() {\n return 1\n }\n}\nobj := C()\nMsgBox obj.Say"
),
"This value of type \"C\" has no property named \"Say\". | |"
);
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n return 1\n }\n}\nobj := C()\nMsgBox obj.HasMethod(\"Say\")"
),
"0"
);
assert_eq!(
the_failure_of(
"class C {\n static Say() {\n return 1\n }\n}\nobj := C()\nobj.GetMethod(\"Say\")"
),
"This value of type \"C\" has no method named \"Say\". | GetMethod |"
);
}
#[test]
fn a_static_method_is_not_a_member_of_the_prototype() {
assert_eq!(
the_failure_of(
"class C {\n static Say() {\n return 1\n }\n}\nMsgBox C.Prototype.Say"
),
"This value of type \"Prototype\" has no property named \"Say\". | |"
);
assert_eq!(
the_failure_of(
"class C {\n static Say() {\n return 1\n }\n}\nC.Prototype.Say()"
),
"This value of type \"Prototype\" has no method named \"Say\". | |"
);
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n return 1\n }\n}\nMsgBox C.Prototype.HasMethod(\"Say\")"
),
"0"
);
assert_eq!(
the_failure_of(
"class C {\n static Say() {\n return 1\n }\n}\nC.Prototype.GetMethod(\"Say\")"
),
"This value of type \"Prototype\" has no method named \"Say\". | GetMethod |"
);
}
#[test]
fn a_static_method_does_not_hide_the_instance_method_of_the_base() {
assert_eq!(
the_answer_of(
"class D {\n Say() {\n return 1\n }\n}\nclass C extends D {\n static Say() {\n return 2\n }\n}\nobj := C()\nMsgBox obj.Say()"
),
"1"
);
assert_eq!(
the_answer_of(
"class D {\n Say() {\n return 1\n }\n}\nclass C extends D {\n static Say() {\n return 2\n }\n}\nobj := C()\nMsgBox obj.Say.Name"
),
"D.Prototype.Say"
);
}
#[test]
fn a_class_value_has_the_static_methods_of_the_class() {
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n return 1\n }\n}\nMsgBox C.Say.Name"
),
"C.Say"
);
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n return 1\n }\n}\nMsgBox C.GetMethod(\"Say\").Name"
),
"C.Say"
);
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n return 1\n }\n}\nMsgBox C.HasMethod(\"Say\")"
),
"1"
);
assert_eq!(
the_answer_of(
"class C {\n static Say() {\n return 1\n }\n}\nMsgBox C.HasProp(\"Say\")"
),
"1"
);
}
#[test]
fn the_prototype_has_the_methods_of_the_class_and_of_the_base() {
assert_eq!(
the_answer_of(
"class D {\n Say() {\n return 1\n }\n}\nclass C extends D {\n}\nMsgBox C.Prototype.HasMethod(\"Say\")"
),
"1"
);
assert_eq!(
the_answer_of(
"class D {\n Say() {\n return 1\n }\n}\nclass C extends D {\n}\nMsgBox C.Prototype.HasProp(\"Say\")"
),
"1"
);
assert_eq!(
the_answer_of(
"class D {\n Say() {\n return 1\n }\n}\nclass C extends D {\n}\nMsgBox C.Prototype.Say.Name"
),
"D.Prototype.Say"
);
assert_eq!(
the_answer_of(
"class D {\n Say() {\n return 1\n }\n}\nclass C extends D {\n}\nMsgBox C.Prototype.GetMethod(\"Say\").Name"
),
"D.Prototype.Say"
);
}
#[test]
fn the_method_of_a_class_runs_on_the_prototype_of_it() {
assert_eq!(
the_answer_of(
"class C {\n Say() {\n return 1\n }\n}\nMsgBox C.Prototype.Say()"
),
"1"
);
assert_eq!(
the_answer_of(
"class C {\n Say() {\n return this\n }\n}\nMsgBox C.Prototype.Say().__Class"
),
"C"
);
}
#[test]
fn the_methods_of_a_class_are_the_properties_of_an_instance() {
assert_eq!(
the_answer_of(
"class C {\n Say() {\n return 1\n }\n}\nobj := C()\nMsgBox obj.HasProp(\"Say\")"
),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\nobj := C()\nMsgBox obj.HasProp(\"__Init\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\nobj := C()\nMsgBox obj.HasProp(\"Base\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n}\nobj := C()\nMsgBox obj.HasProp(\"Nope\")"),
"0"
);
assert_eq!(the_answer_of("m := Map()\nMsgBox m.HasProp(\"Get\")"), "1");
assert_eq!(
the_answer_of("m := Map()\nMsgBox m.HasMethod(\"__Init\")"),
"1"
);
}
#[test]
fn a_static_variable_is_not_a_member_of_an_instance() {
assert_eq!(
the_answer_of(
"class C {\n static value := 1\n}\nobj := C()\nMsgBox obj.HasProp(\"value\")"
),
"0"
);
assert_eq!(
the_failure_of("class C {\n static value := 1\n}\nobj := C()\nMsgBox obj.value"),
"This value of type \"C\" has no property named \"value\". | |"
);
assert_eq!(
the_answer_of("class C {\n static value := 1\n}\nMsgBox C.value"),
"1"
);
}
#[test]
fn the_prototype_owns_the_automatic_init_of_the_class() {
assert_eq!(
the_answer_of(
"class C {\n value := 1\n}\ns := \"\"\nfor p in C.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class__Init"
);
assert_eq!(
the_answer_of(
"class C {\n __Init() {\n }\n}\ns := \"\"\nfor p in C.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class__Init"
);
assert_eq!(
the_answer_of(
"class C {\n}\ns := \"\"\nfor p in C.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class"
);
}
#[test]
fn a_class_inside_a_class_is_an_own_property_of_the_class() {
assert_eq!(
the_answer_of("class Outer {\n class Inner {\n }\n}\nMsgBox Outer.Inner.__Class"),
"Class"
);
assert_eq!(
the_answer_of(
"class Outer {\n class Inner {\n }\n}\ns := \"\"\nfor p in Outer.OwnProps()\n s .= p\nMsgBox s"
),
"__InitInnerPrototype"
);
assert_eq!(
the_answer_of(
"class Outer {\n class Inner {\n }\n}\ns := \"\"\nfor p in Outer.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class"
);
assert_eq!(
the_failure_of(
"class Outer {\n class Inner {\n }\n}\nobj := Outer()\nMsgBox obj.Inner"
),
"This value of type \"Outer\" has no property named \"Inner\". | |"
);
}
#[test]
fn the_name_of_the_class_inside_a_class_is_the_name_of_the_class_that_holds_it() {
assert_eq!(
the_answer_of(
"class Outer {\n class Inner {\n }\n}\nobj := Outer.Inner()\nMsgBox obj.__Class"
),
"Outer.Inner"
);
assert_eq!(
the_answer_of(
"class Outer {\n class Middle {\n class Inner {\n }\n }\n}\nobj := Outer.Middle.Inner()\nMsgBox obj.__Class"
),
"Outer.Middle.Inner"
);
assert_eq!(
the_answer_of(
"class Inner {\n}\nclass Outer {\n class Inner {\n }\n}\nobj := Outer.Inner()\nMsgBox obj.__Class"
),
"Outer.Inner"
);
}
#[test]
fn the_call_of_a_class_inside_a_class_makes_an_object_of_it() {
assert_eq!(
the_answer_of(
"class Outer {\n class Inner {\n Say() {\n return 1\n }\n }\n}\nMsgBox Outer.Inner().Say()"
),
"1"
);
assert_eq!(
the_answer_of(
"class Base {\n Say() {\n return 1\n }\n}\nclass Outer {\n class Inner extends Base {\n Other() {\n return super.Say()\n }\n }\n}\nMsgBox Outer.Inner().Other()"
),
"1"
);
}
#[test]
fn the_duplicate_of_a_class_inside_a_class_is_refused() {
assert_eq!(
the_load_failure_of("class Outer {\n class Inner {\n }\n class Inner {\n }\n}"),
"Duplicate declaration.\n Specifically: Inner"
);
assert_eq!(
the_load_failure_of("class Outer {\n static Inner := 1\n class Inner {\n }\n}"),
"Duplicate declaration.\n Specifically: Inner"
);
}
#[test]
fn a_class_inside_a_class_may_not_be_static() {
assert_eq!(
the_load_failure_of("class Outer {\n static class Inner {\n }\n}"),
"Not a valid method, class or property definition.\n Specifically: static class Inner { }"
);
}
#[test]
fn a_property_of_a_class_may_have_a_getter_and_a_setter() {
assert_eq!(
the_answer_of(
"class C {\n value {\n get => 1\n }\n}\nobj := C()\nMsgBox obj.value"
),
"1"
);
assert_eq!(
the_answer_of(
"class C {\n value {\n set => this.stored := value\n }\n}\nobj := C()\nobj.value := 5\nMsgBox obj.stored"
),
"5"
);
assert_eq!(
the_failure_of(
"class C {\n value {\n set => this.stored := value\n }\n}\nobj := C()\nMsgBox obj.value"
),
"This value of type \"C\" has no property named \"value\". | |"
);
assert_eq!(
the_failure_of(
"class C {\n value {\n get => 1\n }\n}\nobj := C()\nobj.value := 5"
),
"Property is read-only. | | value"
);
assert_eq!(
the_answer_of(
"class C {\n value {\n get => 1\n }\n}\ns := \"\"\nfor p in C.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Classvalue"
);
assert_eq!(
the_answer_of(
"class C {\n value {\n get => 1\n }\n}\nobj := C()\nMsgBox obj.HasProp(\"value\")"
),
"1"
);
assert_eq!(
the_answer_of(
"class C {\n value {\n get => 1\n }\n}\nobj := C()\nMsgBox obj.HasMethod(\"value\")"
),
"0"
);
assert_eq!(
the_answer_of(
"class C {\n value {\n get => 1\n }\n}\nobj := C()\nMsgBox obj.HasOwnProp(\"value\")"
),
"0"
);
assert_eq!(
the_answer_of(
"class C {\n value {\n get => 1\n }\n}\nMsgBox C.Prototype.value"
),
"1"
);
assert_eq!(
the_failure_of(
"class C {\n value {\n get => 1\n }\n}\nobj := C()\nMsgBox obj.value()"
),
"This value of type \"C\" has no method named \"value\". | |"
);
assert_eq!(
the_failure_of("class C {\n value {\n get => 1\n }\n}\nMsgBox C.value"),
"This value of type \"Class\" has no property named \"value\". | |"
);
}
#[test]
fn the_property_of_a_class_is_inherited() {
assert_eq!(
the_answer_of(
"class D {\n value {\n get => 1\n }\n}\nclass C extends D {\n}\nobj := C()\nMsgBox obj.value"
),
"1"
);
assert_eq!(
the_answer_of(
"class D {\n value {\n set => this.stored := value\n }\n}\nclass C extends D {\n}\nobj := C()\nobj.value := 5\nMsgBox obj.stored"
),
"5"
);
assert_eq!(
the_answer_of(
"class D {\n value {\n get => 1\n }\n}\nclass C extends D {\n}\nobj := C()\nMsgBox obj.HasProp(\"value\")"
),
"1"
);
}
#[test]
fn a_static_property_of_a_class_may_have_a_getter() {
assert_eq!(
the_answer_of("class C {\n static value {\n get => 1\n }\n}\nMsgBox C.value"),
"1"
);
assert_eq!(
the_failure_of(
"class C {\n static value {\n get => 1\n }\n}\nobj := C()\nMsgBox obj.value"
),
"This value of type \"C\" has no property named \"value\". | |"
);
assert_eq!(
the_answer_of(
"class C {\n static value {\n get => 1\n }\n}\ns := \"\"\nfor p in C.OwnProps()\n s .= p\nMsgBox s"
),
"__InitPrototypevalue"
);
assert_eq!(
the_answer_of(
"class C {\n static value {\n get => 1\n }\n}\ns := \"\"\nfor p in C.Prototype.OwnProps()\n s .= p\nMsgBox s"
),
"__Class"
);
}
#[test]
fn the_initialiser_of_a_variable_of_an_instance_has_this() {
let report = run(concat!(
"class C {\n",
" x := 123\n",
" y := this.x\n",
"}\n",
"o := C()\n",
"MsgBox o.x \"|\" o.y\n",
));
assert!(report.ok, "{:?}", report.error);
assert_eq!(report.output, vec!["123|123"]);
}
#[test]
fn the_item_of_a_class_is_written_through_its_setter() {
assert_eq!(
the_answer_of(
"class C {\n __New() {\n this.inner := [\"zero\", \"one\"]\n }\n __Item[key] {\n get {\n return this.inner[key]\n }\n set {\n this.inner[key] := value\n }\n }\n}\nobj := C()\nobj[1] := \"ONE\"\nMsgBox obj[1] \"|\" obj[2]"
),
"ONE|one"
);
}
#[test]
fn the_item_of_a_class_is_the_property_that_is_named_item() {
assert_eq!(
the_answer_of(
"class C {\n __Item[key] {\n get => key\n }\n}\nobj := C()\nMsgBox obj[\"a\"]"
),
"a"
);
assert_eq!(
the_failure_of(
"class C {\n __Item {\n get => 7\n }\n}\nobj := C()\nMsgBox obj[\"a\"]"
),
"This value of type \"Integer\" has no property named \"__Item\". | |"
);
assert_eq!(
the_answer_of(
"class C {\n __Item(key) {\n return key\n }\n}\nobj := C()\nx := obj[3]\nMsgBox x.Name"
),
"C.Prototype.__Item"
);
}
#[test]
fn a_variable_of_a_class_that_has_a_getter_is_one_name_and_not_two() {
assert_eq!(
the_failure_of(
"class C {\n value := 1\n value {\n get => 2\n }\n}\nobj := C()"
),
"Property is read-only. | | value"
);
}
#[test]
fn the_failure_of_a_method_of_the_base_object_names_the_method() {
assert_eq!(
the_failure_of("o := Object()\no.GetOwnPropDesc(\"x\")"),
"This value of type \"Object\" has no property named \"x\". | Object.Prototype.GetOwnPropDesc |"
);
assert_eq!(
the_failure_of("o := Object()\no.DefineProp(\"x\", 1)"),
"Parameter #2 of Object.Prototype.DefineProp is invalid. | Object.Prototype.DefineProp | 1"
);
}
#[test]
fn the_name_of_a_property_may_be_a_number() {
assert_eq!(
the_answer_of("class C {\n 123 := 1\n}\no := C()\nMsgBox o.HasProp(\"123\")"),
"1"
);
assert_eq!(
the_answer_of("class C {\n static 123 := 1\n}\nMsgBox C.HasProp(\"123\")"),
"1"
);
}
#[test]
fn a_number_alone_in_a_class_is_refused() {
assert_eq!(
the_load_failure_of("class C {\n 123\n}"),
"This line does not contain a recognized action.\n Specifically: 123"
);
}
#[test]
fn the_name_of_a_method_may_not_begin_with_a_number() {
assert_eq!(
the_load_failure_of("class C {\n 123() {\n }\n}"),
"This method name starts with a number, which is not allowed:\n\"123\""
);
}
#[test]
fn the_name_of_a_class_may_not_begin_with_a_number() {
assert_eq!(
the_load_failure_of("class 123 {\n}"),
"This class name starts with a number, which is not allowed:\n\"123\""
);
assert_eq!(
the_load_failure_of("class C {\n class 123 {\n }\n}"),
"This class name starts with a number, which is not allowed:\n\"123\""
);
assert_eq!(
the_load_failure_of("class 1.5 {\n}"),
"This class name starts with a number, which is not allowed:\n\"1.5\""
);
}
#[test]
fn a_reserved_word_is_not_the_name_of_a_class() {
assert_eq!(
the_load_failure_of("class C {\n class if {\n }\n}"),
"The following reserved word must not be used as a class name:\n\"if\""
);
}
#[test]
fn a_brace_in_a_class_is_refused() {
assert_eq!(
the_load_failure_of("class C {\n {\n}"),
"Unexpected \"{\"\n Specifically: {"
);
}
#[test]
fn the_class_with_no_name_is_read_and_dropped() {
assert_eq!(
the_answer_of(
"class C {\n class {\n }\n}\ns := \"\"\nfor p in ObjOwnProps(C)\n s .= \"[\" p \"]\"\nMsgBox s"
),
"[__Init][Prototype]"
);
}
#[test]
fn the_float_is_not_the_name_of_a_property() {
assert_eq!(
the_load_failure_of("class C {\n 1.5 := 1\n}"),
"Unknown class var.\n Specifically: 1.5 := 1"
);
}
#[test]
fn the_base_of_a_class_that_is_not_there_is_refused() {
assert_eq!(
the_load_failure_of("class C extends Nope {\n}"),
"Unknown class.\n Specifically: Nope"
);
assert_eq!(
the_load_failure_of("class C extends 123 {\n}"),
"Unknown class.\n Specifically: 123"
);
assert_eq!(
the_load_failure_of("class C extends \"Nope\" {\n}"),
"Unknown class.\n Specifically: \"Nope\""
);
assert_eq!(
the_load_failure_of("class C extends Hotkey {\n}"),
"Unknown class.\n Specifically: Hotkey"
);
}
#[test]
fn the_class_value_of_a_class_of_the_runtime_owns_the_call_and_the_prototype() {
assert_eq!(
the_answer_of("s := \"\"\nfor p in ObjOwnProps(Map)\n s .= \"[\" p \"]\"\nMsgBox s"),
"[Call][Prototype]"
);
assert_eq!(
the_answer_of("s := \"\"\nfor p in ObjOwnProps(Object)\n s .= \"[\" p \"]\"\nMsgBox s"),
"[Call][Prototype]"
);
}
#[test]
fn the_class_value_of_an_error_owns_the_prototype_alone() {
assert_eq!(
the_answer_of("s := \"\"\nfor p in ObjOwnProps(Error)\n s .= \"[\" p \"]\"\nMsgBox s"),
"[Prototype]"
);
assert_eq!(
the_answer_of("FileAppend Error.HasOwnProp(\"Call\"), \"*\""),
"0"
);
assert_eq!(
the_answer_of("FileAppend Error.HasProp(\"Call\"), \"*\""),
"1"
);
}
#[test]
fn the_name_of_the_class_is_not_an_own_property_of_it() {
assert_eq!(
the_answer_of("FileAppend Map.HasOwnProp(\"class name\"), \"*\""),
"0"
);
}
#[test]
fn the_base_of_a_class_may_be_declared_after_it() {
assert_eq!(
the_answer_of("class C extends D {\n}\nclass D {\n}\nMsgBox C.Prototype.__Class"),
"C"
);
assert_eq!(
the_answer_of("class C extends Map {\n}\nMsgBox C.Prototype.__Class"),
"C"
);
}