Uberi / Yunit

Super simple testing framework for AutoHotkey.
GNU Affero General Public License v3.0
53 stars 21 forks source link

Update for v2.0-a090+ #23

Closed Lexikos closed 6 years ago

Lexikos commented 6 years ago

IsFunc requires a function name in v2.0-a090+ and throws an exception if given an object. This was done to avoid the issue of Func vs. other callable objects. If you specifically require a Func object (i.e. with all its expected properties and methods), it is more appropriate to use an explicit type check. In general, my recommendation is to perform a type check only if absolutely necessary - so for instance, I might omit the type checks for Begin and End.

Type(x) = "Func" is the direct replacement for IsObject(x) && IsFunc(x), but for more general values of x, the correct replacement would be IsObject(x) ? Type(x) = "Func" : IsFunc(x). I chose to use the shorter, more restrictive (object-only) form for Begin/End since it seems unlikely one would be storing function names in the test suite object vs. defining methods. The check could be omitted since the test suite object will presumably only ever contain methods and classes intended for consumption by Yunit, but I suppose it's safer to keep the check.

Type(x) = "Func" (or previously IsFunc(x)) won't fully protect you against an invalid x: if x defines more mandatory parameters than you pass, your attempt to call will still throw an exception. What it will do is prevent the use of BoundFunc or user-defined objects.

mmikeww commented 6 years ago

Thank you sir. You must have seen my thread here: https://autohotkey.com/boards/viewtopic.php?f=37&t=51333

I see you don't like this method:

for k,v in MyClass
{
   if IsObject(v) && IsFunc("MyClass." k)
mmikeww commented 6 years ago

See that commit above^ If neither way matters that much, then we'll just use yours

Lexikos commented 6 years ago

Is there any reason to use your method?

It is longer and slower. There is no guarantee that the Begin method will be named ClassName.Begin, nor that IsFunc can detect it, nor that cls.Begin will (still) contain a reference to that method even if IsFunc succeeds.

mmikeww commented 6 years ago

No reason to use the other method, I was just curious. Merging yours now