def test_after_idle(): stuff = [] for i in range(5): teek.after_idle(stuff.append, [i]) # test positional args teek.after_idle(teek.quit) teek.run() assert stuff == list(range(5))
def test_weird_error(capfd): # make sure that previous tests don't mess up teek.after_idle(teek.quit) teek.run() # ignore anything that ended up in the stderr because previous tests # TODO: why is stderr SOMETIMES non-empty?? capfd.readouterr() teek.create_command(print) # the command is intentionally not deleted teek.quit() teek.update() assert capfd.readouterr() == ('', '')
def deinit_threads(): """Make sure that init_threads() has not been called when test completes. If you have a test like this... def test_tootie(): teek.init_threads() ...the test will cause problems for any other tests that also call init_threads(), because it can't be called twice. Using this fixture in test_tootie() would fix that problem. """ yield teek.after_idle(teek.quit) teek.run()
def func(monkeypatch, handy_callback): @handy_callback def fake_run(): pass with monkeypatch.context() as monkey: monkey.setattr(teek, 'run', fake_run) exec(code, {'__file__': os.path.join(EXAMPLES_DIR, filename)}) assert fake_run.ran_once() # make sure that nothing breaks if the real .run() is called teek.update() teek.after_idle(teek.quit) teek.run()
def test_errors(capsys): def thingy(**kwargs): # test kwargs assert kwargs == {'lol': 'wut'} raise RuntimeError("\N{pile of poo}") timeout = teek.after_idle(thingy, kwargs={'lol': 'wut'}) assert repr(timeout).startswith("<pending 'thingy' timeout") teek.after_idle(teek.quit) teek.run() assert repr(timeout).startswith("<failed 'thingy' timeout") output, errors = capsys.readouterr() assert not output assert "timeout = teek.after_idle(thingy, kwargs={'lol': 'wut'})" in errors assert "\N{pile of poo}" in errors
def test_init_threads_errors(deinit_threads, handy_callback): @handy_callback def thread1_target(): # the Tcl interpreter isn't started yet, so this runs an error that is # not covered by the code below with pytest.raises(RuntimeError) as error: teek.tcl_eval(None, '') assert str(error.value) == "init_threads() wasn't called" thread1 = threading.Thread(target=thread1_target) thread1.start() thread1.join() assert thread1_target.ran_once() # this starts the Tcl interpreter teek.tcl_eval(None, '') @handy_callback def thread2_target(): with pytest.raises(RuntimeError) as error: teek.init_threads() assert (str( error.value) == "init_threads() must be called from main thread") for cb in [ functools.partial(teek.tcl_call, None, 'puts', 'hello'), functools.partial(teek.tcl_eval, None, 'puts hello') ]: with pytest.raises(RuntimeError) as error: cb() assert str(error.value) == "init_threads() wasn't called" thread2 = threading.Thread(target=thread2_target) thread2.start() thread2.join() assert thread2_target.ran_once() teek.init_threads() with pytest.raises(RuntimeError) as error: teek.init_threads() assert str(error.value) == "init_threads() was called twice" teek.after_idle(teek.quit) teek.run()
def test_cancel(): timeout = teek.after(1000, print, args=["it didn't work"]) timeout.cancel() assert repr(timeout).startswith("<cancelled 'print' timeout") with pytest.raises(RuntimeError) as error: timeout.cancel() assert str(error.value) == "cannot cancel a cancelled timeout" def try_to_cancel_the_completed_timeout(): with pytest.raises(RuntimeError) as error: timeout.cancel() assert str(error.value) == ("cannot cancel a successfully " + "completed timeout") timeout = teek.after_idle(lambda: None) teek.after(50, try_to_cancel_the_completed_timeout) teek.after(100, teek.quit) teek.run()