<commit_before>import macros
import nimx.timer
import nimx.app
import nimx.event
import nimx.abstract_window
import nimx.button
import nimx.system_logger

type UITestSuiteStep* = tuple
    code : proc()
    astrepr: string

type UITestSuite* = seq[UITestSuiteStep]

type TestRunnerContext = ref object
    curTest: int
    curTimeout: float

var testRunnerContext : TestRunnerContext
var registeredTests : seq[UITestSuite]

proc registerTest*(ts: UITestSuite) =
    if registeredTests.isNil:
        registeredTests = @[ts]
    else:
        registeredTests.add(ts)

proc testSuiteDefinitionWithNameAndBody(name, body: NimNode): NimNode =
    result = newNimNode(nnkBracket)
    for n in body:
        let procDef = newProc(body = newStmtList().add(n), procType = nnkLambda)
        procDef.pragma = newNimNode(nnkPragma).add(newIdentNode("closure"))

        let step = newNimNode(nnkPar).add(procDef, toStrLit(n))
        result.add(step)
    return newNimNode(nnkLetSection).add(
        newNimNode(nnkIdentDefs).add(name, bindsym "UITestSuite", newCall("@", result)))

macro uiTest*(name: untyped, body: typed): untyped =
    result = testSuiteDefinitionWithNameAndBody(name, body)

macro registeredUiTest*(name: untyped, body: typed): stmt =
    result = newStmtList()
    result.add(testSuiteDefinitionWithNameAndBody(name, body))
    result.add(newCall(bindsym "registerTest", name))

when true:
    proc sendMouseEvent*(wnd: Window, p: Point, bs: ButtonState) =
        var evt = newMouseButtonEvent(p, VirtualKey.MouseButtonPrimary, bs)
        evt.window = wnd
        discard mainApplication().handleEvent(evt)

    proc sendMouseDownEvent*(wnd: Window, p: Point) = sendMouseEvent(wnd, p, bsDown)
    proc sendMouseUpEvent*(wnd: Window, p: Point) = sendMouseEvent(wnd, p, bsUp)

    proc findButtonWithTitle*(v: View, t: string): Button =
        try:
            result = Button(v)
        except:
            discard
        if result.isNil or result.title != t:
            if not v.subviews.isNil:
                for s in v.subviews:
                    result = findButtonWithTitle(s, t)
                    if not result.isNil: break

    proc quitApplication*() =
        when defined(js) or defined(emscripten) or defined(android):<commit_msg>fix button find by title: (#115)<commit_after>import macros
import nimx.timer
import nimx.app
import nimx.event
import nimx.abstract_window
import nimx.button
import nimx.system_logger

type UITestSuiteStep* = tuple
    code : proc()
    astrepr: string

type UITestSuite* = seq[UITestSuiteStep]

type TestRunnerContext = ref object
    curTest: int
    curTimeout: float

var testRunnerContext : TestRunnerContext
var registeredTests : seq[UITestSuite]

proc registerTest*(ts: UITestSuite) =
    if registeredTests.isNil:
        registeredTests = @[ts]
    else:
        registeredTests.add(ts)

proc testSuiteDefinitionWithNameAndBody(name, body: NimNode): NimNode =
    result = newNimNode(nnkBracket)
    for n in body:
        let procDef = newProc(body = newStmtList().add(n), procType = nnkLambda)
        procDef.pragma = newNimNode(nnkPragma).add(newIdentNode("closure"))

        let step = newNimNode(nnkPar).add(procDef, toStrLit(n))
        result.add(step)
    return newNimNode(nnkLetSection).add(
        newNimNode(nnkIdentDefs).add(name, bindsym "UITestSuite", newCall("@", result)))

macro uiTest*(name: untyped, body: typed): untyped =
    result = testSuiteDefinitionWithNameAndBody(name, body)

macro registeredUiTest*(name: untyped, body: typed): stmt =
    result = newStmtList()
    result.add(testSuiteDefinitionWithNameAndBody(name, body))
    result.add(newCall(bindsym "registerTest", name))

when true:
    proc sendMouseEvent*(wnd: Window, p: Point, bs: ButtonState) =
        var evt = newMouseButtonEvent(p, VirtualKey.MouseButtonPrimary, bs)
        evt.window = wnd
        discard mainApplication().handleEvent(evt)

    proc sendMouseDownEvent*(wnd: Window, p: Point) = sendMouseEvent(wnd, p, bsDown)
    proc sendMouseUpEvent*(wnd: Window, p: Point) = sendMouseEvent(wnd, p, bsUp)

    proc findButtonWithTitle*(v: View, t: string): Button =
        if v of Button:
            let btn = Button(v)
            if btn.title == t:
                result = btn
        else:
            for s in v.subviews:
                result = findButtonWithTitle(s, t)
                if not result.isNil: break

    proc quitApplication*() =
        when defined(js) or defined(emscripten) or defined(android):
{'fraction_non_alphanumeric': 0.10111111111111111, 'fraction_numerical': 0.0006666666666666666, 'mean_word_length': 3.4214145383104126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7103864', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1399, 'n_words': 348}