[Class] expect.ahk (rapid unit testing)

Post your working scripts, libraries and tools for AHK v1.1 and older
DaveT1
Posts: 227
Joined: 07 Oct 2014, 11:23

Re: [Class] expect.ahk (rapid unit testing)

13 Apr 2024, 07:48

Chunjee wrote:
11 Apr 2024, 22:14
v1 does not have a built-in 'contains' function or that would be better.
v1 has if var contains as standard syntax, but this has been removed in v2 - which is a shame imho.
wrote: I believe most examples have been switched to a perhaps clearer expect.true((1 == 1))
Oh yeah, just seen the new docs on github - they are much better. Will you update these on the forum?

You could potentially have expect run an InStr (or similar) check, but I think it would need a new expect function - something along the lines of expect.TestFunction():

Code: Select all

TestFunction("InStr", "string", "w")

TestFunction(psParam1, psParam2, psParam3)
{
    Pos := %psParam1%(psParam2, psParam3)
    if (Pos > 0)
        Answer := "YES: `"" psParam3 "`" is in `"" psParam2 "`""
    else
        Answer := "NO: `"" psParam3 "`" is not in `"" psParam2 "`""
    MsgBox(Answer)

    return
}
Is this general concept useful in anyway? My brain is to addled to work out whether / how many other standard AutoHotkey function there are to add, and how to make a parameter list with a variable number of params in it! I'm sure it's pretty doable with a clear head though!
wrote: Additionally ahk cannot differentiate true from 1
Yep, that's so true!!

----------

Thanks for listening to / helping with all my thoughts :thumbup: :thumbup:
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] expect.ahk (rapid unit testing)

13 Apr 2024, 12:19

DaveT1 wrote:
13 Apr 2024, 07:48
v1 has if var contains as standard syntax, but this has been removed in v2 - which is a shame imho.
Huh did not know that was removed in v2. Obviously since it is not a function in v1 it does not fit comfortably in one line expect.true(UserInput not in yes,no) or whatever it would be.


My current plan is to write the ideal .writeResultsToFile output as a test and write the code to match that
DaveT1
Posts: 227
Joined: 07 Oct 2014, 11:23

Re: [Class] expect.ahk (rapid unit testing)

26 Apr 2024, 07:58

:wave: Hi @Chunjee

I've been working on a version of expect for AHKv2. In doing so I realised that I've made a number of naming and formatting changes. I've also added a public method to access most of AHKv2 Is functions, including InStr (code copied below for your info, comment, use, etc).

Code: Select all

    AHKIsFuncs(pAHKFunc, pParam2, psParam3:="", pNote:="")
    {
        if (pAHKFunc = "InStr")
        {   ;Returns true if the needle(pParam2) is in the haystack(pAHKFunc), else false.

            this.TestType := "InStr"
            iPos := %pAHKFunc%(pParam2, psParam3)
            
            if (iPos > 0)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }
        
        if (pAHKFunc = "IsInteger")
        {   ;Returns true if (pParam2) is an integer, else false.

            this.TestType := "IsInteger"

            if IsInteger(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }

        if (pAHKFunc = "IsFloat")
        {   ;Returns true if (pParam2) is a real number, else false.

            this.TestType := "IsFloat"
            
            if IsFloat(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }
        
        if (pAHKFunc = "IsNumber")
        {   ;Returns true if (pParam2) is an integer or a real number, else false.

            this.TestType := "IsNumber"
            
            if IsNumber(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }

        if (pAHKFunc = "IsObject")
        {   ;Returns true if (pParam2) is an object, else false.

            this.TestType := "IsObject"
            
            if IsObject(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }

        if (pAHKFunc = "IsSet")
        {   ;Returns true if (pParam2) has been assigned a value, else false.
            ;**Im not sure this works...by passing an unset var it seems to become 'set'?**

            this.TestType := "IsSet"

            ;Next line used to test IsSet.
            ; pParam2 := unset
                
            if IsSet(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }
        
        if (pAHKFunc = "IsDigit")
        {   
            /* Returns true if (pParam2) is:
                - a positive integer, 
                - an empty string, 
                - or a string which contains only the characters 0 through 9. 
            Other characters such as the following are not allowed: spaces, tabs, plus signs, 
                minus signs, decimal points, hexadecimal digits, and the 0x prefix.
            */
        
            this.TestType := "IsDigit"
            
            if IsDigit(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }
        
        if (pAHKFunc = "IsAlpha")
        {   
            /*  Returns true if (pParam2) is a string and:
                    - is empty or
                    - contains only alphabetic characters. 
                False if there are any digits, spaces, tabs, punctuation, or other non-alphabetic 
                    characters anywhere in the string. For example, if pParam2 contains a space followed 
                    by a letter, it is not considered to be alpha.
            */
        
            this.TestType := "IsAlpha"
            
            if IsAlpha(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }

        if (pAHKFunc = "IsSpace")
        {   
            /*  Returns true if (pParam2) is a string and:
                    - is empty or 
                    - contains only whitespace consisting of the following characters: 
                        space (A_Space or `s), tab (A_Tab or `t), linefeed (`n), return (`r), vertical tab (`v), and formfeed (`f).
            */
        
            this.TestType := "IsSpace"
            
            if IsSpace(pParam2)
                return this._test(1, true, pNote)
            else
                return this._test(0, true, pNote) 
        }

        return
    }
Ultimately the 'new' expect is now not so similar looking (but is functionally very close) to your script for AHKv1.

I'd like to post it somewhere but don't want to hijack this thread. Or I could start a new thread in the v2 section of the forum and fully reference this thread? Very happy to be guided by what you would like me to do.

Cheers, :thumbup:
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] expect.ahk (rapid unit testing)

02 May 2024, 20:22

DaveT1 wrote:
26 Apr 2024, 07:58
That's fantastic, well done :thumbup:
Would be great to post in the v2 scripts and functions

A short blurb like "Inspired in part by expect.ahk (v1)" would be appreciated but not required.

If you would like a more visual link this could be cool: Image

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: submeg and 122 guests