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

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

type UITestSuite* = seq[UITestSuiteStep]

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, kcMouseButtonPrimary, 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 quitApplication*() =
        when defined(js):
            # Hopefully we're using nimx automated testing in Firefox
            {.emit: """window.dump("---AUTO-TEST-QUIT---\n");""".}
        else:
            quit()

when false:
    macro dump(b: typed): stmt =
        echo treeRepr(b)

    dump:
        let ts : UITestSuite = @[
            (
                (proc() {.closure.} = echo "hi"),
                "hello"
            )
        ]

    uiTest myTest:
        echo "hi"
        echo "bye"

    registerTest(myTest)

proc startTest*(t: UITestSuite) =
    var curTest = 0
    var tim : Timer
    tim = setInterval(0.5, proc() =
        logi "RUNNING"
        logi t[curTest].astrepr<commit_msg>check for dump<commit_after>import macros
import nimx.timer
import nimx.app
import nimx.event
import nimx.abstract_window
import nimx.system_logger

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

type UITestSuite* = seq[UITestSuiteStep]

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, kcMouseButtonPrimary, 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 quitApplication*() =
        when defined(js):
            # Hopefully we're using nimx automated testing in Firefox
            {.emit: """if ('dump' in window) window.dump("---AUTO-TEST-QUIT---\n");""".}
        else:
            quit()

when false:
    macro dump(b: typed): stmt =
        echo treeRepr(b)

    dump:
        let ts : UITestSuite = @[
            (
                (proc() {.closure.} = echo "hi"),
                "hello"
            )
        ]

    uiTest myTest:
        echo "hi"
        echo "bye"

    registerTest(myTest)

proc startTest*(t: UITestSuite) =
    var curTest = 0
    var tim : Timer
    tim = setInterval(0.5, proc() =
        logi "RUNNING"
        logi t[curTest].astrepr
{'fraction_non_alphanumeric': 0.1169023000633045, 'fraction_numerical': 0.0012660898923823592, 'mean_word_length': 2.9598997493734336, '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': '4394158', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1527, 'n_words': 359}