Skip to content

Tutorial

SATO Kentaro edited this page Mar 11, 2012 · 5 revisions

Here we create TitleMatchMode class that provides setting and reverting A_TitleMatchMode state functionality to see how to use AhkUnit.

The specification of the class is:

  • Sets A_TitleMatchMode and A_TitleMatchModeSpeed on construction, and remembers the previous state.
  • Restores the state on destruction.
  • Set() to change the state again.
  • Reset() to revert the state temporarily.
  • Set()'s parameter, either mode or mode-speed can be blank to leave it unchanged.

Write the test class

So write the test class first. Open the new document and save as TitleMatchModeTest.ahk.

On head of the test file don't forget to include the AhkUnit/GuiRunner.ahk file.

#NoEnv
#include %A_AppData%\AhkUnit\GuiRunner.ahk
#include %A_ScriptDir%\TitleMatchMode.ahk

And define the test class. This class should extend AhkUnit_Framework class, that provides assertion methods to call. Remember that Test methods' name should end with "Test", and the word is case sensitive.

class TitleMatchModeTest extends AhkUnit_Framework {
	NewTest() {
		SetTitleMatchMode,1
		SetTitleMatchMode,Fast
		tmm := new TitleMatchMode(2, "Slow")
		this.AssertEqual(2, A_TitleMatchMode, "change mode")
		this.AssertEqual("Slow", A_TitleMatchModeSpeed, "change speed")
		tmm2 := new TitleMatchMode(3)
		this.AssertEqual(3, A_TitleMatchMode, "change mode")
		this.AssertEqual("Slow", A_TitleMatchModeSpeed, "retain speed")
		tmm3 := new TitleMatchMode("", "Fast")
		this.AssertEqual(3, A_TitleMatchMode, "retain mode")
		this.AssertEqual("Fast", A_TitleMatchModeSpeed, "change speed")
	}
	
	DeleteTest() {
		SetTitleMatchMode,1
		SetTitleMatchMode,Fast
		this.DeleteTestSub(2, "Slow")
		this.AssertEqual(1, A_TitleMatchMode, "revert mode")
		this.AssertEqual("Fast", A_TitleMatchModeSpeed, "revert speed")
	}
	
	DeleteTestSub(mode, modeSpeed) {
		tmm := new TitleMatchMode(mode, modeSpeed)
		this.AssertEqual(mode, A_TitleMatchMode)
		this.AssertEqual(modeSpeed, A_TitleMatchModeSpeed)
	}
	
	SetTest() {
		SetTitleMatchMode,1
		SetTitleMatchMode,Fast
		tmm := new TitleMatchMode(2, "Slow")
		tmm.Set(1)
		this.AssertEqual(1, A_TitleMatchMode, "")
		this.AssertEqual("Slow", A_TitleMatchModeSpeed, "")
		tmm.Set("", "Fast")
		this.AssertEqual(1, A_TitleMatchMode, "")
		this.AssertEqual("Fast", A_TitleMatchModeSpeed, "")
		tmm.Set(3, "Slow")
		this.AssertEqual(3, A_TitleMatchMode, "")
		this.AssertEqual("Slow", A_TitleMatchModeSpeed, "")
	}
	
	ResetTest() {
		SetTitleMatchMode,1
		SetTitleMatchMode,Fast
		tmm := new TitleMatchMode(2, "Slow")
		tmm.Reset()
		this.AssertEqual(1, A_TitleMatchMode, "")
		this.AssertEqual("Fast", A_TitleMatchModeSpeed, "")
		tmm.Set("2", "Slow")
		tmm.Reset()
		this.AssertEqual(1, A_TitleMatchMode, "")
		this.AssertEqual("Fast", A_TitleMatchModeSpeed, "")
	}
}

Then at the bottom write the code to run the test.

AhkUnit.AddTestClass(TitleMatchModeTest)
AhkUnit.Run()

Write the implementation

For the implementation, there's nothing new. Open the new document and save the code below as TitleMatchMode.ahk.

class TitleMatchMode {
	__New(newMode = "", newSpeed = "") {
		this.previousMode := A_TitleMatchMode
		this.previousSpeed := A_TitleMatchModeSpeed
		this.Set(newMode, newSpeed)
	}
	
	__Delete() {
		this.Reset()
	}
	
	Set(newMode = "", newSpeed = "") {
		if (newMode != "") {
			SetTitleMatchMode,% newMode
		}
		if (newSped != "") { ; typo
			SetTitleMatchMode,% newSpeed
		}
	}
	
	Reset() {
		SetTitleMatchMode,% this.previousMode
		SetTitleMatchMode,% this.previousSpeed
	}
}

Run the test

Launch TitleMatchModeTest.ahk. The test runs and the result is shown in the window.

As the above implementation has a typo, a few test failures are shown.

Fix the code and click Reload.

You should see no failures and now everything is fine.

As a side note, this kind of typo can be eliminated by using #Warn All

Clone this wiki locally