[Class] expect.ahk (rapid unit testing)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

[Class] expect.ahk (rapid unit testing)

Post by Chunjee » 26 Sep 2021, 00:09

expect.ahk
A unit test package for AutoHotkey


Installation

In a terminal or command line navigated to your project folder:

Code: Select all

npm install expect.ahk
In your code only export.ahk needs to be included:

Code: Select all

#Include %A_ScriptDir%\node_modules
#Include expect\export.ahk
expect := new expect()

testVar := 2 + 2
expect.equal(testVar, 4)
expect.fullReport()
You may also review or copy the library from ./export.ahk on GitHub; #Include as you would normally when manually downloading.


Usage

Grants access to a class named expect with the following methods: .equal, .notEqual, .true, .false, .label, .group, .report, .fullReport, and .writeTestResultsToFile

Code: Select all

expect := new expect()

; .equal checks and logs whether or not both arguments are the same
expect.label("string comparison")
expect.equal("StringExample", "StringExample")

expect.label("value testing")
expect.equal((1 > 0 ), true)

expect.label("true/false testing")
expect.true((1 == 1))
expect.false((1 != 1))
expect.notEqual(true,false)

expect.report()
expect.fullReport()
expect.writeTestResultsToFile()

API

.equal(actual, expected)

Alias: .test

checks if actual and expected are the same or equal. The comparison is case-insensitive when ahk is inStringCaseSense, Off (default ahk behavior)

Arguments
  1. actual (*): The actual value computed
  2. expected (*): The expected value
Returns (boolean): returns true if the values were the same, else false

Example

Code: Select all

expect.equal("string", "tsring")
; => false

expect.equal((1 > 0 ), true)
; => true
.true(actual) checks if actual value is true.

Arguments
  1. actual (*): The actual value computed
Returns (boolean): returns true if the value is true, else false

Example

Code: Select all

expect.true((1 == 1))
; => true

expect.true(InStr("String", "S"))
; => true
.false(actual) checks if actual value is false.

Arguments
  1. actual (*): The actual value computed
Returns (boolean): returns true if the value is false, else false

Example

Code: Select all

expect.false((1 != 1))
; => true

expect.false(InStr("String", "X"))
; => true
.notEqual(actual, expected) checks if actual and expected are NOT the same or equal. The comparison is case-insensitive when ahk is inStringCaseSense, Off (default ahk behavior)

Arguments
  1. actual (*): The actual value computed
  2. expected (*): The expected value
Returns (boolean): returns true if the value is false, else false

Example

Code: Select all

expect.notEqual((1 != 1))
; => true

expect.notEqual(InStr("String", "X"))
; => true
.undefined(actual) checks if actual is undefined ("").

Arguments
  1. actual (*): The actual value computed
Returns (boolean): returns true if the value is "", else false

Example

Code: Select all

expect.false((1 != 1))
; => true

expect.false(InStr("String", "X"))
; => true
.label(label) labels the following tests for logs and readability

Arguments
  1. label (string): A human readable label for the next test(s) in sequence
Example

Code: Select all

expect.label("string comparisons")

expect.equal("String", "s")
expect.fullReport()
/*---------------------------
1 tests completed with 0% success (1 failure)
=================================
== string comparisons ==
Test Number: 1
Expected: s
Actual: String
---------------------------*/
.group(label) appends the label to a group of following tests for logs and readability

This may be useful when one has a lot of tests; and doesn't want to type out a repeatative label

Arguments
  1. label (string): A human readable label prepend for the next test(s) in sequence
Example

Code: Select all

expect.group("strings")
expect.label("comparison")
expect.equal("String", "s")

expect.label("length")
expect.equal(strLen("String"), 9)

expect.fullReport()
/*---------------------------
2 tests completed with 0% success (2 failures)
=================================
== strings - comparisons ==
Test Number: 1
Expected: s
Actual: String

== strings - length ==
Test Number: 2
Expected: 99
Actual: 6
---------------------------*/
.report() Uses msgbox to display the results of all tests

Example

Code: Select all

expect.true(InStr("String", "S"))

expect.report()
/*---------------------------
1 test completed with 100% success
---------------------------*/
.fullReport() Uses msgbox to display the results of all tests with details of any failures

Example

Code: Select all

expect.true(InStr("String", "X"))

expect.fullReport()
/*---------------------------
1 tests completed with 0% success (1 failure)
=================================
Test Number: 1
Expected: true
Actual: false
---------------------------*/
.writeResultsToFile([filepath, fileopen]) writes test results to a file

Arguments
  1. filepath (string): Optional, The file path to write all tests results to, the default is A_ScriptDir "\result.tests.log"
  1. fileopen (bool): Optional, Open the file if true. Default: false
Example

Code: Select all

expect.true(InStr("String", "X"))

expect.writeTestResultsToFile()
/*
1 test completed with 0% success (1 failure)

Test Number: 1
Expected: true
Actual: false*/
Last edited by Chunjee on 27 Sep 2021, 23:37, edited 1 time in total.


User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

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

Post by Chunjee » 26 Sep 2021, 00:16

When I decided to start writing biga.ahk; I knew there were going to be a lot of methods and many that feed off each others output. So I needed somekind of way to ensure updates and small changes didn't cause things to break elsewhere. Therefore I wrote this this unit testing class and made a lot of small improvements whenever I encountered a new need. As of this writing that project has 650 tests and most didn't take longer than a few seconds to write.

There are some other unit testing projects already out there that are more conventional; but I was going to have hundreds of tests, the normal way to write tests is quite verbose where you extend the class a lot. The scale of the project I was starting would probably make writing tests that way basically impossible for one person I felt.
This exists to make it easier to write a lot of small tests. But other testing libraries are probably more powerful.

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

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

Post by kczx3 » 27 Sep 2021, 18:54

Your second code box uses “new unit()”. Did you mean “new expect()”?


User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

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

Post by Chunjee » 29 Jul 2022, 19:39

I found what I would call a big bug.

fixed in v0.1.1
expect.ahk will always test with case sensitivity on now

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

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

Post by DaveT1 » 12 Dec 2023, 04:25

Hi @Chunjee, great class, thanks for providing.

I've a few Qs, but am new to classes and units testing and only an average amateur AutoHotkey user - so hopefully these Qs are not foolish!:
(I'm using exportv0.1.1.ahk)

1) The example given in the documentation for .writeResultsToFile([filepath, fileopen]) calls for the method .writeTestResultsToFile, but the method name is .writeResultsToFile?

2) In exportv0.1.1.ahk there's an empty method writeTAP(...)?

3) The example given in the documentation for .undefined(actual) uses expect.false(...). Should probably be expect.undefined(...)?

4) There is no undefined method expect.undefined("")?

5) In the method test(...) the last parameter is param_note:="", but this is not used in the method?

6) param_note is a parameter for the following methods: true, false, equal, notEqual, but is only used in the last method.

7) In the method test(...), should the line this._logTestFail(param_actual, param_expected) read this._logTestFail(param_actual, param_expected, param_note)? This allows a 'per-test' message to be added to each test failure.

Cheers, and many thanks again :thumbup:


DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

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

Post by DaveT1 » 02 Jan 2024, 04:47

Thanks @Chunjee, looking forward to your feedback when you have time :thumbup:

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

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

Post by DaveT1 » 22 Feb 2024, 07:50

Hi Chunjee, respectfully wondering if you had a moment to look through the Qs I raised above?

Very many thanks :thumbup:

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

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

Post by Chunjee » 22 Feb 2024, 11:25

Haven't forgotten. I did confirm some of the issues however. Thank you

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

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

Post by DaveT1 » 23 Feb 2024, 00:02

Chunjee wrote:
22 Feb 2024, 11:25
Haven't forgotten. I did confirm some of the issues however. Thank you
:thumbup: :thumbup:

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

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

Post by Chunjee » 07 Mar 2024, 14:40

Github repo contains most fixes. Documentation has not been fixed yet.

I noticed a TAP 14 spec which this may not be following completely. I would like to get that looked at as well

Post Reply

Return to “Scripts and Functions (v1)”