From 34caf20593859dcd4d2cd111487d51927d2eda0e Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Wed, 16 Oct 2024 05:38:49 +0200 Subject: [PATCH 1/4] Fixing LaTeX tests, adding new sexy natural language method spy, started testing Promises. --- common/src/module/canvas.mjs | 4 +- common/test/mock/latex.mjs | 18 +- runtime-pyside6/LogarithmPlotter/util/js.py | 6 +- .../LogarithmPlotter/util/promise.py | 41 ++- runtime-pyside6/tests/spy.py | 254 ++++++++++++++++++ runtime-pyside6/tests/test_config.py | 18 ++ runtime-pyside6/tests/test_helper.py | 16 +- runtime-pyside6/tests/test_latex.py | 57 +++- runtime-pyside6/tests/test_native.py | 1 - runtime-pyside6/tests/test_promise.py | 115 ++++++++ runtime-pyside6/tests/test_update.py | 15 +- scripts/run-tests.sh | 1 + 12 files changed, 511 insertions(+), 35 deletions(-) create mode 100644 runtime-pyside6/tests/spy.py create mode 100644 runtime-pyside6/tests/test_promise.py diff --git a/common/src/module/canvas.mjs b/common/src/module/canvas.mjs index 913442f..ba6d5ca 100644 --- a/common/src/module/canvas.mjs +++ b/common/src/module/canvas.mjs @@ -530,14 +530,14 @@ class CanvasAPI extends Module { if(this.#redrawCount === currentRedrawCount) callback(imgData) else - console.log("1Discard render of", imgData.source, this.#redrawCount, currentRedrawCount) + console.log("1. Discard render of", imgData.source, this.#redrawCount, currentRedrawCount) }) } else { // Callback directly if(this.#redrawCount === currentRedrawCount) callback(imgData) else - console.log("2Discard render of", imgData.source, this.#redrawCount, currentRedrawCount) + console.log("2. Discard render of", imgData.source, this.#redrawCount, currentRedrawCount) } } const prerendered = Latex.findPrerendered(ltxText, this.textsize, color) diff --git a/common/test/mock/latex.mjs b/common/test/mock/latex.mjs index ab56999..c0a029c 100644 --- a/common/test/mock/latex.mjs +++ b/common/test/mock/latex.mjs @@ -23,6 +23,10 @@ const PIXEL = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVR4AWNgAAAAA export class MockLatex { constructor() { } + + get supportsAsyncRender() { + return true + } /** * Creates a simple string hash. @@ -55,13 +59,23 @@ export class MockLatex { return `${TMP}/${name}.png` } + /** + * @param {string} markup - LaTeX markup to render + * @param {number} fontSize - Font size (in pt) to render + * @param {string} color - Color of the text to render + * @returns {Promise} - Comma separated data of the image (source, width, height) + */ + async renderAsync(markup, fontSize, color) { + return this.renderSync(markup, fontSize, color) + } + /** * @param {string} markup - LaTeX markup to render * @param {number} fontSize - Font size (in pt) to render * @param {string} color - Color of the text to render * @returns {string} - Comma separated data of the image (source, width, height) */ - render(markup, fontSize, color) { + renderSync(markup, fontSize, color) { const file = this.__getFileName(markup, fontSize, color) writeFileSync(file, PIXEL, "base64") return `${file},1,1` @@ -87,4 +101,4 @@ export class MockLatex { checkLatexInstallation() { return true // We're not *actually* doing any latex. } -} \ No newline at end of file +} diff --git a/runtime-pyside6/LogarithmPlotter/util/js.py b/runtime-pyside6/LogarithmPlotter/util/js.py index 05f30d5..944c2b0 100644 --- a/runtime-pyside6/LogarithmPlotter/util/js.py +++ b/runtime-pyside6/LogarithmPlotter/util/js.py @@ -75,6 +75,7 @@ class PyJSValue: return value def type(self) -> any: + ret = None matcher = [ (lambda: self.qjs_value.isArray(), list), (lambda: self.qjs_value.isBool(), bool), @@ -93,8 +94,9 @@ class PyJSValue: ] for (test, value) in matcher: if test(): - return value - return None + ret = value + break + return ret def primitive(self): """ diff --git a/runtime-pyside6/LogarithmPlotter/util/promise.py b/runtime-pyside6/LogarithmPlotter/util/promise.py index e475b36..3c2d223 100644 --- a/runtime-pyside6/LogarithmPlotter/util/promise.py +++ b/runtime-pyside6/LogarithmPlotter/util/promise.py @@ -31,7 +31,7 @@ def check_callable(function: Callable|QJSValue) -> Callable|None: """ if isinstance(function, QJSValue) and function.isCallable(): return PyJSValue(function) - elif isinstance(function, Callable): + elif callable(function): return function return None @@ -51,9 +51,7 @@ class PyPromiseRunner(QRunnable): def run(self): try: data = self.runner(*self.args) - if isinstance(data, QObject): - data = data - elif type(data) in [int, str, float, bool, bytes]: + if type(data) in [int, str, float, bool]: data = QJSValue(data) elif data is None: data = QJSValue.SpecialValue.UndefinedValue @@ -62,7 +60,7 @@ class PyPromiseRunner(QRunnable): elif isinstance(data, PyJSValue): data = data.qjs_value else: - raise InvalidReturnValue("Must return either a primitive, a valid QObject, JS Value, or None.") + raise InvalidReturnValue("Must return either a primitive, a JS Value, or None.") self.promise.fulfilled.emit(data) except Exception as e: try: @@ -79,7 +77,7 @@ class PyPromise(QObject): Runs to_run in another thread, and calls fulfilled (populated by then) with its return value. """ fulfilled = Signal((QJSValue,), (QObject,)) - rejected = Signal(Exception) + rejected = Signal(str) def __init__(self, to_run: Callable|QJSValue, args=[], start_automatically=True): QObject.__init__(self) @@ -94,7 +92,7 @@ class PyPromise(QObject): raise ValueError("New PyPromise created with invalid function") self._runner = PyPromiseRunner(to_run, self, args) if start_automatically: - self._start() + self.start() @Slot() def start(self, *args, **kwargs): @@ -122,6 +120,35 @@ class PyPromise(QObject): if on_reject is not None: self._rejects.append(on_reject) return self + + def calls_upon_fulfillment(self, function: Callable | QJSValue) -> bool: + """ + Returns True if the given function will be callback upon the promise fulfillment. + False otherwise. + """ + return self._calls_in(function, self._fulfills) + + def calls_upon_rejection(self, function: Callable | QJSValue) -> bool: + """ + Returns True if the given function will be callback upon the promise rejection. + False otherwise. + """ + return self._calls_in(function, self._rejects) + + def _calls_in(self, function: Callable | QJSValue, within: list) -> bool: + """ + Returns True if the given function resides in the given within list, False otherwise. + Internal method of calls_upon_fulfill + """ + function = check_callable(function) + ret = False + if isinstance(function, PyJSValue): + found = next((f for f in within if f.qjs_value == function.qjs_value), None) + ret = found is not None + elif callable(function): + found = next((f for f in within if f == function), None) + ret = found is not None + return ret @Slot(QJSValue) @Slot(QObject) diff --git a/runtime-pyside6/tests/spy.py b/runtime-pyside6/tests/spy.py new file mode 100644 index 0000000..01f9aa0 --- /dev/null +++ b/runtime-pyside6/tests/spy.py @@ -0,0 +1,254 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +from typing import Callable, Self + +from PySide6.QtQml import QJSValue + +PRINT_PREFIX = (" " * 24) + +class SpyAssertionFailed(Exception): + def __init__(self, message, calls): + self.message = message + "\n" + if len(calls) > 0: + self.message += self.render_calls(calls) + else: + self.message += f"{PRINT_PREFIX}0 registered calls." + + def repr(self, data): + if isinstance(data, QJSValue): + variant = data.toVariant() + return f"QJSValue<{type(variant).__name__}>({repr(variant)})" + else: + return repr(data) + + def render_calls(self, calls): + lines = [f"{PRINT_PREFIX}{len(calls)} registered call(s):"] + for call in calls: + repr_args = [self.repr(arg) for arg in call[0]] + repr_kwargs =[f"{key}={self.repr(arg)}" for key, arg in call[1].items()] + lines.append(f" - {', '.join([*repr_args, *repr_kwargs])}") + return ("\n" + PRINT_PREFIX).join(lines) + + def __str__(self): + return self.message + +class Methods: + AT_LEAST_ONCE = "AT_LEAST_ONCE" + EXACTLY = "EXACTLY" + AT_LEAST = "AT_LEAST" + AT_MOST = "AT_MOST" + MORE_THAN = "AT_LEAST" + LESS_THAN = "AT_MOST" + +class CalledInterface: + """ + Internal class generated by Spy.was_called. + """ + + def __init__(self, calls: list[tuple[list, dict]]): + self.__calls = calls + self.__method = Methods.AT_LEAST_ONCE + self.__times = None + + def __apply_method(self, calls): + required = self.__times + calls_count = len(calls) + match self.__method: + case Methods.AT_LEAST_ONCE: + compare = len(calls) >= 1 + error = "Method was not called" + case Methods.EXACTLY: + compare = len(calls) == required + error = f"Method was not called {required} times ({required} != {calls_count})" + case Methods.AT_LEAST: + compare = len(calls) >= required + error = f"Method was not called at least {required} times ({required} > {calls_count})" + case Methods.AT_MOST: + compare = len(calls) <= required + error = f"Method was not called at most {required} times ({required} < {calls_count})" + case Methods.MORE_THAN: + compare = len(calls) > required + error = f"Method was not called more than {required} times ({required} >= {calls_count})" + case Methods.LESS_THAN: + compare = len(calls) < required + error = f"Method was not called less than {required} times ({required} <= {calls_count})" + case _: + raise RuntimeError(f"Unknown method {self.__method}.") + return compare, error + + def __bool__(self): + """ + Converts to boolean on assertion. + """ + compare, error = self.__apply_method(self.__calls) + if not compare: + raise SpyAssertionFailed(error+".") + return compare + + + """ + Chaining methods + """ + def __call__(self, *args, **kwargs) -> Self: + if len(args) != 1: + raise RuntimeError("Cannot call called interface with more than one argument.") + self.__times = int(args[0]) + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.EXACTLY + return self + + @property + def never(self) -> Self: + return self(0) + + @property + def once(self) -> Self: + return self(1) + + @property + def twice(self) -> Self: + return self(2) + + @property + def thrice(self) -> Self: + return self(3) + + @property + def at_least(self) -> Self: + self.__method = Methods.AT_LEAST + return self + + @property + def at_most(self) -> Self: + self.__method = Methods.AT_MOST + return self + + @property + def more_than(self) -> Self: + self.__method = Methods.MORE_THAN + return self + + @property + def less_than(self) -> Self: + self.__method = Methods.LESS_THAN + return self + + @property + def times(self) -> Self: + return self + + """ + Class properties. + """ + def __match_calls_for_condition(self, condition: Callable[[list, dict], bool]) -> tuple[bool, str]: + calls = [] + for call in self.__calls: + if condition(call[0], call[1]): + calls.append(call) + compare, error = self.__apply_method(calls) + return compare, error + + + def with_arguments(self, *args, **kwargs): + """ + Checks if the Spy has been called the given number of times + with at least the given arguments. + """ + def some_args_matched(a, kw): + args_matched = all(( + arg in a + for arg in args + )) + kwargs_matched = all(( + key in kw and kw[key] == arg + for key, arg in kwargs.items() + )) + return args_matched and kwargs_matched + compare, error = self.__match_calls_for_condition(some_args_matched) + if not compare: + repr_args = ', '.join([repr(arg) for arg in args]) + repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) + raise SpyAssertionFailed(f"{error} with arguments ({repr_args}) and keyword arguments ({repr_kwargs}).", self.__calls) + return compare + + + def with_arguments_matching(self, test_condition: Callable[[list, dict], bool]): + """ + Checks if the Spy has been called the given number of times + with arguments matching the given conditions. + """ + compare, error = self.__match_calls_for_condition(test_condition) + if not compare: + raise SpyAssertionFailed(f"{error} with arguments matching given conditions.", self.__calls) + return compare + + + def with_exact_arguments(self, *args, **kwargs): + """ + Checks if the Spy has been called the given number of times + with all the given arguments. + """ + compare, error = self.__match_calls_for_condition(lambda a, kw: a == args and kw == kwargs) + if not compare: + repr_args = ', '.join([repr(arg) for arg in args]) + repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) + raise SpyAssertionFailed(f"{error} with exact arguments ({repr_args}) and keyword arguments ({repr_kwargs}).", self.__calls) + return compare + + def with_no_argument(self): + """ + Checks if the Spy has been called the given number of times + with all the given arguments. + """ + compare, error = self.__match_calls_for_condition(lambda a, kw: len(a) == 0 and len(kw) == 0) + if not compare: + raise SpyAssertionFailed(f"{error} with no arguments.", self.__calls) + return compare + + + +class Spy: + """ + Class to spy into method calls with natural language expressions. + """ + + def __init__(self, function: Callable = None): + self.function = function + self.calls = [] + + def __call__(self, *args, **kwargs): + self.calls.append((args, kwargs)) + if self.function is not None: + self.function(*args, **kwargs) + + @property + def was_called(self) -> CalledInterface: + """ + Returns a boolean-able interface to check conditions for a given number of + time the spy was called. + """ + return CalledInterface(self.calls) + + @property + def was_not_called(self) -> CalledInterface: + """ + Returns a boolean-able interface to check that conditions were never fulfilled + in the times the spy was called. + """ + ret = CalledInterface(self.calls) + return ret(0) diff --git a/runtime-pyside6/tests/test_config.py b/runtime-pyside6/tests/test_config.py index c919c6f..cef3673 100644 --- a/runtime-pyside6/tests/test_config.py +++ b/runtime-pyside6/tests/test_config.py @@ -1,3 +1,21 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + import pytest from LogarithmPlotter.util import config from tempfile import TemporaryDirectory diff --git a/runtime-pyside6/tests/test_helper.py b/runtime-pyside6/tests/test_helper.py index 5cfb239..6bc1f0d 100644 --- a/runtime-pyside6/tests/test_helper.py +++ b/runtime-pyside6/tests/test_helper.py @@ -30,7 +30,7 @@ from PySide6.QtWidgets import QApplication from LogarithmPlotter import __VERSION__ as version from LogarithmPlotter.util import config, helper -from LogarithmPlotter.util.helper import Helper, InvalidFileException +from LogarithmPlotter.util.helper import Helper, fetch_changelog, read_changelog, InvalidFileException pwd = getcwd() helper.SHOW_GUI_MESSAGES = False @@ -60,19 +60,23 @@ CHANGELOG_BASE_PATH = path.realpath(path.join(path.dirname(path.realpath(__file_ class TestHelper: def test_changelog(self, temporary, qtbot): + # Exists helper.CHANGELOG_VERSION = '0.5.0' tmpfile, directory = temporary obj = Helper(pwd, tmpfile) promise = obj.fetchChangelog() create_changelog_callback_asserter(promise, expect_404=False) - qtbot.waitSignal(promise.fulfilled, timeout=10000) - # No exist + with qtbot.waitSignal(promise.fulfilled, timeout=10000): + pass + assert type(fetch_changelog()) == str + # Does not exist helper.CHANGELOG_VERSION = '2.0.0' tmpfile, directory = temporary obj = Helper(pwd, tmpfile) promise = obj.fetchChangelog() create_changelog_callback_asserter(promise, expect_404=True) - qtbot.waitSignal(promise.fulfilled, timeout=10000) + with qtbot.waitSignal(promise.fulfilled, timeout=10000): + pass # Local tmpfile, directory = temporary obj = Helper(pwd, tmpfile) @@ -81,7 +85,9 @@ class TestHelper: assert path.exists(helper.CHANGELOG_CACHE_PATH) promise = obj.fetchChangelog() create_changelog_callback_asserter(promise, expect_404=False) - qtbot.waitSignal(promise.fulfilled, timeout=10000) # Local + with qtbot.waitSignal(promise.fulfilled, timeout=100): # Local + pass + assert type(read_changelog()) == str def test_read(self, temporary): # Test file reading and information loading. diff --git a/runtime-pyside6/tests/test_latex.py b/runtime-pyside6/tests/test_latex.py index 6fd5c97..124186b 100644 --- a/runtime-pyside6/tests/test_latex.py +++ b/runtime-pyside6/tests/test_latex.py @@ -22,6 +22,7 @@ from shutil import which from os.path import exists from re import match from PySide6.QtGui import QColor +from PySide6.QtQml import QJSValue from LogarithmPlotter.util import latex @@ -38,10 +39,24 @@ def latex_obj(): directory.cleanup() +BLACK = QColor(0, 0, 0, 255) +BLUE = QColor(128, 128, 255, 255) + +def check_render_results(result): + if isinstance(result, QJSValue): + result = result.toVariant() + assert type(result) == str + [path, width, height] = result.split(",") + assert exists(path) + assert match(r"\d+", width) + assert match(r"\d+", height) + return True + class TestLatex: def test_check_install(self, latex_obj: latex.Latex) -> None: assert latex_obj.latexSupported == True assert latex_obj.checkLatexInstallation() == True + assert type(latex_obj.supportsAsyncRender) is bool bkp = [latex.DVIPNG_PATH, latex.LATEX_PATH] # Check what happens when one is missing. latex.DVIPNG_PATH = None @@ -55,25 +70,25 @@ class TestLatex: [latex.DVIPNG_PATH, latex.LATEX_PATH] = bkp def test_render_sync(self, latex_obj: latex.Latex) -> None: - result = latex_obj.renderSync(r"\frac{d\sqrt{\mathrm{f}(x \times 2.3)}}{dx}", 14, QColor(0, 0, 0, 255)) + result = latex_obj.renderSync("\\frac{d \\sqrt{\\mathrm{f}(x \\times 2.3)}}{dx}", 14, BLACK) # Ensure result format - assert type(result) == str - [path, width, height] = result.split(",") - assert exists(path) - assert match(r"\d+", width) - assert match(r"\d+", height) + check_render_results(result) # Ensure it returns errors on invalid latex. with pytest.raises(latex.RenderError): - latex_obj.renderSync(r"\nonexistant", 14, QColor(0, 0, 0, 255)) + latex_obj.renderSync("\\nonexistant", 14, BLACK) # Replace latex bin with one that returns errors bkp = latex.LATEX_PATH latex.LATEX_PATH = which("false") with pytest.raises(latex.RenderError): - latex_obj.renderSync(r"\mathrm{f}(x)", 14, QColor(0, 0, 0, 255)) + latex_obj.renderSync("\\mathrm{f}(x)", 14, BLACK) + # Replace latex bin with one goes indefinitely + latex.LATEX_PATH = which("import") + with pytest.raises(latex.RenderError): + latex_obj.renderSync("\\mathrm{f}(x)", 14, BLACK) latex.LATEX_PATH = bkp def test_prerendered(self, latex_obj: latex.Latex) -> None: - args = [r"\frac{d\sqrt{\mathrm{f}(x \times 2.3)}}{dx}", 14, QColor(0, 0, 0, 255)] + args = ["\\frac{d \\sqrt{\\mathrm{f}(x \\times 2.3)}}{dx}", 14, BLACK] latex_obj.renderSync(*args) prerendered = latex_obj.findPrerendered(*args) assert type(prerendered) == str @@ -84,3 +99,27 @@ class TestLatex: prerendered2 = latex_obj.findPrerendered(args[0], args[1]+2, args[2]) assert prerendered2 == "" + def test_render_async(self, latex_obj: latex.Latex, qtbot) -> None: + formula = "\\int\\limits^{3x}_{-\\infty}9\\mathrm{f}(x)^3+t dx" + og_promise = latex_obj.renderAsync(formula, 14, BLACK) + # Ensure we get the same locked one if we try to render it again. + assert og_promise == latex_obj.renderAsync(formula, 14, BLACK) + # Ensure queued renders. + promises = [ + latex_obj.renderAsync(formula, 14, BLUE), + latex_obj.renderAsync(formula, 10, BLACK), + latex_obj.renderAsync(formula, 10, BLUE), + ] + for prom in promises: + assert og_promise.calls_upon_fulfillment(prom.start) + # Ensure other renders get done in parallel. + other_promise = latex_obj.renderAsync(formula+" dt", 10, BLACK) + assert not og_promise.calls_upon_fulfillment(other_promise.start) + # Ensure all of them render. + proms = [og_promise, *promises, other_promise] + with qtbot.waitSignals( + [p.fulfilled for p in proms], + raising=True, timeout=10000, + check_params_cbs=[check_render_results]*len(proms) + ): + pass diff --git a/runtime-pyside6/tests/test_native.py b/runtime-pyside6/tests/test_native.py index 6fc2d65..3ca4834 100644 --- a/runtime-pyside6/tests/test_native.py +++ b/runtime-pyside6/tests/test_native.py @@ -17,7 +17,6 @@ """ import pytest -from os.path import exists from PySide6.QtCore import QEvent, QObject, QUrl from PySide6.QtGui import QActionEvent, QFileOpenEvent diff --git a/runtime-pyside6/tests/test_promise.py b/runtime-pyside6/tests/test_promise.py new file mode 100644 index 0000000..06c0b8a --- /dev/null +++ b/runtime-pyside6/tests/test_promise.py @@ -0,0 +1,115 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +from time import sleep + +import pytest +from PySide6.QtCore import QObject +from PySide6.QtQml import QJSValue + +from spy import Spy +from LogarithmPlotter.util.js import PyJSValue +from LogarithmPlotter.util.promise import PyPromise + + +def check_promise_result(value): + def got_result(args, kwargs, val=value): + valid = len(args) == 1 and len(kwargs) == 0 + if valid: + got_value = args[0].toVariant() if isinstance(args[0], QJSValue) else args[0] + valid = got_value == val + return valid + return got_result + +def create_async_func(value): + def async_function(val=value): + sleep(1) + return val + + return async_function + + +def qjs_eq(origin): + def compare(result): + res = result.toVariant() == origin + print("Unknown res!", res, repr(result.toVariant()), repr(origin)) + return res + return compare + + +def async_throw(): + sleep(1) + raise Exception("aaaa") + + +class TestPyPromise: + + def test_fulfill_values(self, qtbot): + qjsv = QJSValue(3) + values = [ + [True, qjs_eq(True)], + [3, qjs_eq(3)], + [2.2, qjs_eq(2.2)], + ["String", qjs_eq("String")], + [qjsv, qjs_eq(3)], + [None, qjs_eq(None)], + [PyJSValue(QJSValue("aaa")), qjs_eq("aaa")] + ] + for [value, test] in values: + promise = PyPromise(create_async_func(value)) + with qtbot.assertNotEmitted(promise.rejected, wait=1000): + print("Testing", value) + with qtbot.waitSignal(promise.fulfilled, check_params_cb=test, timeout=2000): + assert promise.state == "pending" + assert promise.state == "fulfilled" + + def test_reject(self, qtbot): + promise = PyPromise(async_throw) + with qtbot.assertNotEmitted(promise.fulfilled, wait=1000): + with qtbot.waitSignal(promise.rejected, timeout=10000, + check_params_cb=lambda t: t == "Exception('aaaa')"): + assert promise.state == "pending" + assert promise.state == "rejected" + + def test_fulfill(self, qtbot): + spy_fulfilled = Spy() + spy_rejected = Spy() + promise = PyPromise(create_async_func(3)) + then_res = promise.then(spy_fulfilled, spy_rejected) + # Check if the return value is the same promise (so we can chain then) + assert then_res == promise + # Check on our spy. + with qtbot.waitSignal(promise.fulfilled, timeout=10000): + pass + assert spy_fulfilled.was_called.once + assert spy_fulfilled.was_not_called.with_arguments(3) + assert spy_fulfilled.was_called.with_arguments_matching(check_promise_result(3)) + assert spy_rejected.was_not_called + + def test_rejected(self, qtbot): + spy_fulfilled = Spy() + spy_rejected = Spy() + promise = PyPromise(async_throw) + then_res = promise.then(spy_fulfilled, spy_rejected) + # Check if the return value is the same promise (so we can chain then) + assert then_res == promise + # Check on our spies. + with qtbot.waitSignal(promise.rejected, timeout=10000): + pass + assert spy_rejected.was_called.once + assert spy_rejected.was_called.with_arguments("Exception('aaaa')") + assert spy_fulfilled.was_not_called diff --git a/runtime-pyside6/tests/test_update.py b/runtime-pyside6/tests/test_update.py index 5e1a705..ff71e61 100644 --- a/runtime-pyside6/tests/test_update.py +++ b/runtime-pyside6/tests/test_update.py @@ -49,19 +49,20 @@ def test_update(qtbot): update_info_newer = UpdateInformation() update_info_newer.got_update_info.connect(check_newer) runnable = UpdateCheckerRunnable('1.0.0', update_info_newer) - runnable.run() - qtbot.waitSignal(update_info_newer.got_update_info, timeout=10000) + with qtbot.waitSignal(update_info_newer.got_update_info, timeout=10000): + runnable.run() runnable = UpdateCheckerRunnable('0.1.0', update_info_older) - runnable.run() - qtbot.waitSignal(update_info_older.got_update_info, timeout=10000) + with qtbot.waitSignal(update_info_older.got_update_info, timeout=10000): + runnable.run() runnable = UpdateCheckerRunnable('0.5.0+dev0+git20240101', update_info_older) - runnable.run() - qtbot.waitSignal(update_info_older.got_update_info, timeout=10000) + with qtbot.waitSignal(update_info_older.got_update_info, timeout=10000): + runnable.run() def test_update_checker(qtbot): update_info = check_for_updates('0.6.0', MockWindow()) assert QThreadPool.globalInstance().activeThreadCount() >= 1 - qtbot.waitSignal(update_info.got_update_info, timeout=10000) + with qtbot.waitSignal(update_info.got_update_info, timeout=10000): + pass argv.append("--no-check-for-updates") update_info = check_for_updates('0.6.0', MockWindow()) assert QThreadPool.globalInstance().activeThreadCount() < 2 # No new update checks where added diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh index 1410244..ac1fdc5 100644 --- a/scripts/run-tests.sh +++ b/scripts/run-tests.sh @@ -23,6 +23,7 @@ fi # Run python tests +rm -rf build/runtime-pyside6/tests cp -r runtime-pyside6/tests build/runtime-pyside6 cp -r ci CHANGELOG.md build/runtime-pyside6 cd build/runtime-pyside6 || exit 1 From 8fab9d8e529772a1dfcde801996d03002d3e829f Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Wed, 16 Oct 2024 22:18:53 +0200 Subject: [PATCH 2/4] Starting natural language plugin. --- runtime-pyside6/tests/__init__.py | 0 runtime-pyside6/tests/plugins/__init__.py | 0 .../tests/plugins/natural/__init__.py | 21 ++ .../plugins/natural/interfaces/__init__.py | 0 .../plugins/natural/interfaces/assertion.py | 30 +++ .../tests/plugins/natural/interfaces/base.py | 101 ++++++++ .../tests/plugins/natural/interfaces/basic.py | 61 +++++ .../tests/plugins/natural/interfaces/int.py | 229 ++++++++++++++++++ .../tests/plugins/natural/interfaces/utils.py | 27 +++ .../tests/{ => plugins/natural}/spy.py | 128 +++++----- runtime-pyside6/tests/plugins/natural/that.py | 57 +++++ runtime-pyside6/tests/test_promise.py | 20 +- 12 files changed, 593 insertions(+), 81 deletions(-) create mode 100644 runtime-pyside6/tests/__init__.py create mode 100644 runtime-pyside6/tests/plugins/__init__.py create mode 100644 runtime-pyside6/tests/plugins/natural/__init__.py create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/__init__.py create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/assertion.py create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/base.py create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/basic.py create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/int.py create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/utils.py rename runtime-pyside6/tests/{ => plugins/natural}/spy.py (69%) create mode 100644 runtime-pyside6/tests/plugins/natural/that.py diff --git a/runtime-pyside6/tests/__init__.py b/runtime-pyside6/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/runtime-pyside6/tests/plugins/__init__.py b/runtime-pyside6/tests/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/runtime-pyside6/tests/plugins/natural/__init__.py b/runtime-pyside6/tests/plugins/natural/__init__.py new file mode 100644 index 0000000..7a67869 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/__init__.py @@ -0,0 +1,21 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +from .spy import Spy +from .that import that + diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/__init__.py b/runtime-pyside6/tests/plugins/natural/interfaces/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py b/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py new file mode 100644 index 0000000..f2504a2 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py @@ -0,0 +1,30 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +class Assertion(Exception): + def __init__(self, assertion: bool, message: str): + self.assertion = assertion + self.message = message + + def __str__(self): + return self.message + + def __bool__(self): + if not self.assertion: + raise self + return self.assertion \ No newline at end of file diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/base.py b/runtime-pyside6/tests/plugins/natural/interfaces/base.py new file mode 100644 index 0000000..88b9e06 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/interfaces/base.py @@ -0,0 +1,101 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +from typing import Self + +from tests.plugins.natural.interfaces.assertion import Assertion +from tests.plugins.natural.interfaces.utils import repr_ + + +class AssertionInterface: + """ + Most basic assertion interface. + You probably want to use BaseAssertionInterface + """ + def __init__(self, value): + self._value = value + + @property + def was(self) -> Self: + return self + + @property + def be(self) -> Self: + return self + + @property + def been(self) -> Self: + return self + + @property + def have(self) -> Self: + return self + + @property + def has(self) -> Self: + return self + + @property + def a(self) -> Self: + return self + + @property + def an(self) -> Self: + return self + + def is_a(self, type_) -> Assertion: + """ + Checks if the current value is equal to the provided value + """ + value_type_name = type(self._value).__name__ + return Assertion(isinstance(type_, type_), f"The value ({value_type_name} {repr_(self._value)}) is not a {type_.__name__}.") + + + +class EqualAssertionInterface(AssertionInterface): + """ + Interface created for when its value should be checked for equality + """ + def __init__(self, value): + super().__init__(value) + + def __call__(self, value) -> Assertion: + return Assertion(value == self._value, f"The value ({repr_(self._value)}) is not equal to {repr(value)}.") + + def to(self, value) -> Self: + return self(value) + + def of(self, value) -> Self: + return self(value) + + + +class BaseAssertionInterface(AssertionInterface): + + @property + def equals(self) -> EqualAssertionInterface: + """ + Checks if the current value is equal to the provided value + """ + return EqualAssertionInterface(self._value) + + @property + def is_equal(self) -> EqualAssertionInterface: + """ + Checks if the current value is equal to the provided value + """ + return self.equals \ No newline at end of file diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/basic.py b/runtime-pyside6/tests/plugins/natural/interfaces/basic.py new file mode 100644 index 0000000..1d5a878 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/interfaces/basic.py @@ -0,0 +1,61 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +from tests.plugins.natural.interfaces.assertion import Assertion +from tests.plugins.natural.interfaces.base import BaseAssertionInterface +from tests.plugins.natural.interfaces.int import IntInterface +from tests.plugins.natural.interfaces.utils import repr_ + + +class FixedIteratorInterface(BaseAssertionInterface): + @property + def length(self) -> IntInterface: + return IntInterface(len(self._value)) + + def elements(self, *elements) -> Assertion: + tests = [elem for elem in elements if elem in self._value] + return Assertion( + len(tests) == 0, + f"This value ({repr_(self._value)}) does not have elements ({repr_(tests)})" + ) + + def element(self, element) -> Assertion: + return Assertion( + element in self._value, + f"This value ({repr_(self._value)}) does not have element ({repr_(element)})" + ) + + def contains(self, *elements) -> Assertion: + return self.elements(*elements) + +class BoolInterface(BaseAssertionInterface): + @property + def is_true(self): + return Assertion( + self._value == True, + f"The value ({repr_(self._value)}) is not True." + ) + + @property + def is_false(self): + return Assertion( + self._value == False, + f"The value ({repr_(self._value)}) is not False." + ) + +class StringInterface(LengthInterface): + pass \ No newline at end of file diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/int.py b/runtime-pyside6/tests/plugins/natural/interfaces/int.py new file mode 100644 index 0000000..aaf0511 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/interfaces/int.py @@ -0,0 +1,229 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +from typing import Self + +from tests.plugins.natural.interfaces.assertion import Assertion +from tests.plugins.natural.interfaces.base import BaseAssertionInterface, EqualAssertionInterface, AssertionInterface +from tests.plugins.natural.interfaces.utils import repr_ + + +class IntComparisonAssertionInterface(AssertionInterface): + def __init__(self, value): + super().__init__(value) + self._compare_to = None + + def _compare(self) -> Assertion: + raise RuntimeError(f"No comparison method defined in {type(self).__name__}.") + + def __bool__(self) -> bool: + return bool(self._compare()) + + def __call__(self, compare_to: int) -> Self: + if self._compare_to is None: + self._compare_to = int(compare_to) + else: + self._compare_to *= int(compare_to) + return self + + @property + def time(self) -> Self: + return self + + @property + def times(self) -> Self: + return self + + @property + def never(self) -> Self: + return self(0) + + @property + def once(self) -> Self: + return self(1) + + @property + def twice(self) -> Self: + return self(2) + + @property + def thrice(self) -> Self: + return self(3) + + @property + def zero(self) -> Self: + return self(0) + + @property + def one(self) -> Self: + return self(1) + + @property + def two(self) -> Self: + return self(2) + + @property + def three(self) -> Self: + return self(3) + + @property + def four(self) -> Self: + return self(4) + + @property + def five(self) -> Self: + return self(5) + + @property + def six(self) -> Self: + return self(6) + + @property + def seven(self) -> Self: + return self(7) + + @property + def eight(self) -> Self: + return self(8) + + @property + def nine(self) -> Self: + return self(9) + + @property + def ten(self) -> Self: + return self(10) + + @property + def twenty(self) -> Self: + return self(20) + + @property + def thirty(self) -> Self: + return self(30) + + @property + def forty(self) -> Self: + return self(40) + + @property + def fifty(self) -> Self: + return self(50) + + @property + def sixty(self) -> Self: + return self(60) + + @property + def seventy(self) -> Self: + return self(70) + + @property + def eighty(self) -> Self: + return self(70) + + @property + def ninety(self) -> Self: + return self(70) + + @property + def hundred(self) -> Self: + return self(100) + + @property + def thousand(self) -> Self: + return self(1_000) + + @property + def million(self) -> Self: + return self(1_000_000) + + @property + def billion(self) -> Self: + return self(1_000_000_000) + + +class LessThanComparisonInterface(IntComparisonAssertionInterface): + def _compare(self) -> Assertion: + return Assertion( + self._value < self._compare_to, + f"The value ({repr_(self._value)}) is not less than to {repr_(self._compare_to)}." + ) + +class MoreThanComparisonInterface(IntComparisonAssertionInterface): + def _compare(self) -> Assertion: + return Assertion( + self._value > self._compare_to, + f"The value ({repr_(self._value)}) is not more than to {repr_(self._compare_to)}." + ) + +class AtLeastComparisonInterface(IntComparisonAssertionInterface): + def _compare(self) -> Assertion: + return Assertion( + self._value >= self._compare_to, + f"The value ({repr_(self._value)}) is not at least to {repr_(self._compare_to)}." + ) + +class AtMostComparisonInterface(IntComparisonAssertionInterface): + def _compare(self) -> Assertion: + return Assertion( + self._value <= self._compare_to, + f"The value ({repr_(self._value)}) is not at least to {repr_(self._compare_to)}." + ) + +class EqualComparisonInterface(IntComparisonAssertionInterface): + def _compare(self) -> Assertion: + return Assertion( + self._value == self._compare_to, + f"The value ({repr_(self._value)}) is not equal to {repr_(self._compare_to)}." + ) + + def to(self) -> Self: + return self + + def of(self) -> Self: + return self + + +class IntInterface(AssertionInterface): + def less_than(self) -> LessThanComparisonInterface: + return LessThanComparisonInterface(self._value) + + @property + def more_than(self) -> MoreThanComparisonInterface: + return MoreThanComparisonInterface(self._value) + + @property + def at_least(self) -> AtLeastComparisonInterface: + return AtLeastComparisonInterface(self._value) + + @property + def at_most(self) -> AtMostComparisonInterface: + return AtMostComparisonInterface(self._value) + + @property + def equals(self) -> EqualComparisonInterface: + return EqualComparisonInterface(self._value) + + @property + def is_equal(self) -> EqualComparisonInterface: + return EqualComparisonInterface(self._value) + + @property + def exactly(self) -> EqualComparisonInterface: + return EqualComparisonInterface(self._value) + diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/utils.py b/runtime-pyside6/tests/plugins/natural/interfaces/utils.py new file mode 100644 index 0000000..50e3777 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/interfaces/utils.py @@ -0,0 +1,27 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +from PySide6.QtQml import QJSValue + + +def repr_(data): + if isinstance(data, QJSValue): + variant = data.toVariant() + return f"QJSValue<{type(variant).__name__}>({repr(variant)})" + else: + return repr(data) \ No newline at end of file diff --git a/runtime-pyside6/tests/spy.py b/runtime-pyside6/tests/plugins/natural/spy.py similarity index 69% rename from runtime-pyside6/tests/spy.py rename to runtime-pyside6/tests/plugins/natural/spy.py index 01f9aa0..1d77629 100644 --- a/runtime-pyside6/tests/spy.py +++ b/runtime-pyside6/tests/plugins/natural/spy.py @@ -17,35 +17,29 @@ """ from typing import Callable, Self -from PySide6.QtQml import QJSValue +from tests.plugins.natural.interfaces.base import Assertion, repr_, AssertionInterface +from tests.plugins.natural.interfaces.int import IntComparisonAssertionInterface PRINT_PREFIX = (" " * 24) -class SpyAssertionFailed(Exception): - def __init__(self, message, calls): - self.message = message + "\n" + +class SpyAssertion(Assertion): + def __init__(self, assertion: bool, message: str, calls: list): + super().__init__(assertion, message + "\n") if len(calls) > 0: self.message += self.render_calls(calls) else: self.message += f"{PRINT_PREFIX}0 registered calls." - def repr(self, data): - if isinstance(data, QJSValue): - variant = data.toVariant() - return f"QJSValue<{type(variant).__name__}>({repr(variant)})" - else: - return repr(data) def render_calls(self, calls): lines = [f"{PRINT_PREFIX}{len(calls)} registered call(s):"] for call in calls: - repr_args = [self.repr(arg) for arg in call[0]] - repr_kwargs =[f"{key}={self.repr(arg)}" for key, arg in call[1].items()] + repr_args = [repr_(arg) for arg in call[0]] + repr_kwargs = [f"{key}={repr_(arg)}" for key, arg in call[1].items()] lines.append(f" - {', '.join([*repr_args, *repr_kwargs])}") return ("\n" + PRINT_PREFIX).join(lines) - def __str__(self): - return self.message class Methods: AT_LEAST_ONCE = "AT_LEAST_ONCE" @@ -55,18 +49,19 @@ class Methods: MORE_THAN = "AT_LEAST" LESS_THAN = "AT_MOST" -class CalledInterface: + +class CalledInterface(IntComparisonAssertionInterface): """ Internal class generated by Spy.was_called. """ def __init__(self, calls: list[tuple[list, dict]]): + super().__init__(len(calls)) self.__calls = calls self.__method = Methods.AT_LEAST_ONCE - self.__times = None def __apply_method(self, calls): - required = self.__times + required = self._compare_to calls_count = len(calls) match self.__method: case Methods.AT_LEAST_ONCE: @@ -91,61 +86,59 @@ class CalledInterface: raise RuntimeError(f"Unknown method {self.__method}.") return compare, error - def __bool__(self): + def __bool__(self) -> bool: """ Converts to boolean on assertion. """ compare, error = self.__apply_method(self.__calls) - if not compare: - raise SpyAssertionFailed(error+".") - return compare - + return bool(SpyAssertion(compare, error + ".", self.__calls)) """ Chaining methods """ + def __call__(self, *args, **kwargs) -> Self: if len(args) != 1: raise RuntimeError("Cannot call called interface with more than one argument.") - self.__times = int(args[0]) + self._compare_to = int(args[0]) if self.__method == Methods.AT_LEAST_ONCE: self.__method = Methods.EXACTLY return self - @property - def never(self) -> Self: - return self(0) - - @property - def once(self) -> Self: - return self(1) - - @property - def twice(self) -> Self: - return self(2) - - @property - def thrice(self) -> Self: - return self(3) - @property def at_least(self) -> Self: - self.__method = Methods.AT_LEAST + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.AT_LEAST + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.AT_MOST}") return self @property def at_most(self) -> Self: - self.__method = Methods.AT_MOST + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.AT_MOST + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.AT_MOST}") return self @property def more_than(self) -> Self: - self.__method = Methods.MORE_THAN + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.MORE_THAN + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.MORE_THAN}") return self @property def less_than(self) -> Self: - self.__method = Methods.LESS_THAN + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.LESS_THAN + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.LESS_THAN}") + return self + + @property + def time(self) -> Self: return self @property @@ -155,6 +148,7 @@ class CalledInterface: """ Class properties. """ + def __match_calls_for_condition(self, condition: Callable[[list, dict], bool]) -> tuple[bool, str]: calls = [] for call in self.__calls: @@ -163,12 +157,12 @@ class CalledInterface: compare, error = self.__apply_method(calls) return compare, error - - def with_arguments(self, *args, **kwargs): + def with_arguments(self, *args, **kwargs) -> SpyAssertion: """ Checks if the Spy has been called the given number of times with at least the given arguments. """ + def some_args_matched(a, kw): args_matched = all(( arg in a @@ -179,55 +173,49 @@ class CalledInterface: for key, arg in kwargs.items() )) return args_matched and kwargs_matched + compare, error = self.__match_calls_for_condition(some_args_matched) - if not compare: - repr_args = ', '.join([repr(arg) for arg in args]) - repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) - raise SpyAssertionFailed(f"{error} with arguments ({repr_args}) and keyword arguments ({repr_kwargs}).", self.__calls) - return compare + repr_args = ', '.join([repr(arg) for arg in args]) + repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) + msg = f"{error} with arguments ({repr_args}) and keyword arguments ({repr_kwargs})." + return SpyAssertion(compare, msg, self.__calls) - - def with_arguments_matching(self, test_condition: Callable[[list, dict], bool]): + def with_arguments_matching(self, test_condition: Callable[[list, dict], bool]) -> SpyAssertion: """ Checks if the Spy has been called the given number of times with arguments matching the given conditions. """ compare, error = self.__match_calls_for_condition(test_condition) - if not compare: - raise SpyAssertionFailed(f"{error} with arguments matching given conditions.", self.__calls) - return compare + msg = f"{error} with arguments matching given conditions." + return SpyAssertion(compare, msg, self.__calls) - - def with_exact_arguments(self, *args, **kwargs): + def with_exact_arguments(self, *args, **kwargs) -> SpyAssertion: """ Checks if the Spy has been called the given number of times with all the given arguments. """ compare, error = self.__match_calls_for_condition(lambda a, kw: a == args and kw == kwargs) - if not compare: - repr_args = ', '.join([repr(arg) for arg in args]) - repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) - raise SpyAssertionFailed(f"{error} with exact arguments ({repr_args}) and keyword arguments ({repr_kwargs}).", self.__calls) - return compare + repr_args = ', '.join([repr(arg) for arg in args]) + repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) + msg = f"{error} with exact arguments ({repr_args}) and keyword arguments ({repr_kwargs})." + return SpyAssertion(compare, msg, self.__calls) - def with_no_argument(self): + def with_no_argument(self) -> SpyAssertion: """ Checks if the Spy has been called the given number of times with all the given arguments. """ compare, error = self.__match_calls_for_condition(lambda a, kw: len(a) == 0 and len(kw) == 0) - if not compare: - raise SpyAssertionFailed(f"{error} with no arguments.", self.__calls) - return compare + return SpyAssertion(compare, f"{error} with no arguments.", self.__calls) - -class Spy: +class Spy(AssertionInterface): """ Class to spy into method calls with natural language expressions. """ def __init__(self, function: Callable = None): + super().__init__(function) self.function = function self.calls = [] @@ -237,7 +225,7 @@ class Spy: self.function(*args, **kwargs) @property - def was_called(self) -> CalledInterface: + def called(self) -> CalledInterface: """ Returns a boolean-able interface to check conditions for a given number of time the spy was called. @@ -245,7 +233,7 @@ class Spy: return CalledInterface(self.calls) @property - def was_not_called(self) -> CalledInterface: + def not_called(self) -> CalledInterface: """ Returns a boolean-able interface to check that conditions were never fulfilled in the times the spy was called. diff --git a/runtime-pyside6/tests/plugins/natural/that.py b/runtime-pyside6/tests/plugins/natural/that.py new file mode 100644 index 0000000..38ee54e --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/that.py @@ -0,0 +1,57 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +from typing import overload, Generic, TypeVar + +from .spy import Spy +from .interfaces.base import AssertionInterface, BaseAssertionInterface +from .interfaces.basic import StringInterface, BoolInterface +from .interfaces.int import IntInterface + +Interface = TypeVar("Interface", bound=AssertionInterface) + +MATCHES = [ + (str, StringInterface), + (int, IntInterface), + (bool, BoolInterface), +] + + +@overload +def that(value: str) -> StringInterface: ... + + +@overload +def that(value: int) -> IntInterface: ... + + +@overload +def that(value: bool) -> BoolInterface: ... + + +@overload +def that[Interface](value: Interface) -> Interface: ... + + +def that(value: any) -> AssertionInterface: + if not isinstance(value, AssertionInterface): + interface = next((i for t, i in MATCHES if isinstance(value, t)), None) + if interface is not None: + value = interface(value) + else: + value = BaseAssertionInterface(value) + return value diff --git a/runtime-pyside6/tests/test_promise.py b/runtime-pyside6/tests/test_promise.py index 06c0b8a..a97f864 100644 --- a/runtime-pyside6/tests/test_promise.py +++ b/runtime-pyside6/tests/test_promise.py @@ -17,11 +17,9 @@ """ from time import sleep -import pytest -from PySide6.QtCore import QObject from PySide6.QtQml import QJSValue -from spy import Spy +from tests.plugins.natural import that, Spy from LogarithmPlotter.util.js import PyJSValue from LogarithmPlotter.util.promise import PyPromise @@ -95,10 +93,10 @@ class TestPyPromise: # Check on our spy. with qtbot.waitSignal(promise.fulfilled, timeout=10000): pass - assert spy_fulfilled.was_called.once - assert spy_fulfilled.was_not_called.with_arguments(3) - assert spy_fulfilled.was_called.with_arguments_matching(check_promise_result(3)) - assert spy_rejected.was_not_called + assert that(spy_fulfilled).was.called.once + assert that(spy_fulfilled).was.not_called.with_arguments(3) + assert that(spy_fulfilled).was.called.with_arguments_matching(check_promise_result(3)) + assert spy_rejected.was.not_called def test_rejected(self, qtbot): spy_fulfilled = Spy() @@ -106,10 +104,10 @@ class TestPyPromise: promise = PyPromise(async_throw) then_res = promise.then(spy_fulfilled, spy_rejected) # Check if the return value is the same promise (so we can chain then) - assert then_res == promise + assert that(then_res).is_equal.to(promise) # Check on our spies. with qtbot.waitSignal(promise.rejected, timeout=10000): pass - assert spy_rejected.was_called.once - assert spy_rejected.was_called.with_arguments("Exception('aaaa')") - assert spy_fulfilled.was_not_called + assert that(spy_rejected).was.called.once + assert that(spy_rejected).was.called.with_arguments("Exception('aaaa')") + assert that(spy_fulfilled).was.not_called From ef465b34e7f9cf9d78d4dd9aa25a14af5b7050a5 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Thu, 17 Oct 2024 02:08:24 +0200 Subject: [PATCH 3/4] Finishing natural language plugin. --- .../tests/plugins/natural/__init__.py | 1 + .../plugins/natural/interfaces/assertion.py | 15 +- .../tests/plugins/natural/interfaces/base.py | 122 +++++++--- .../tests/plugins/natural/interfaces/basic.py | 54 +++-- .../tests/plugins/natural/interfaces/int.py | 208 ++++++++++++----- .../tests/plugins/natural/interfaces/spy.py | 218 ++++++++++++++++++ runtime-pyside6/tests/plugins/natural/spy.py | 213 +---------------- runtime-pyside6/tests/plugins/natural/that.py | 23 +- .../tests/plugins/tests/__init__.py | 0 .../tests/plugins/tests/test_natural.py | 217 +++++++++++++++++ 10 files changed, 751 insertions(+), 320 deletions(-) create mode 100644 runtime-pyside6/tests/plugins/natural/interfaces/spy.py create mode 100644 runtime-pyside6/tests/plugins/tests/__init__.py create mode 100644 runtime-pyside6/tests/plugins/tests/test_natural.py diff --git a/runtime-pyside6/tests/plugins/natural/__init__.py b/runtime-pyside6/tests/plugins/natural/__init__.py index 7a67869..4cfe179 100644 --- a/runtime-pyside6/tests/plugins/natural/__init__.py +++ b/runtime-pyside6/tests/plugins/natural/__init__.py @@ -18,4 +18,5 @@ from .spy import Spy from .that import that +from .interfaces.base import Assertion diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py b/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py index f2504a2..e1e43c4 100644 --- a/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py +++ b/runtime-pyside6/tests/plugins/natural/interfaces/assertion.py @@ -17,14 +17,23 @@ """ class Assertion(Exception): - def __init__(self, assertion: bool, message: str): + def __init__(self, assertion: bool, message: str, invert: bool): self.assertion = assertion self.message = message + self.invert = invert + + def _invert_message(self): + for verb in ('is', 'was', 'has', 'have'): + for negative in ("n't", ' not', ' never', ' no'): + self.message = self.message.replace(f"{verb}{negative}", verb.upper()) def __str__(self): return self.message def __bool__(self): - if not self.assertion: + if not self.invert and not self.assertion: raise self - return self.assertion \ No newline at end of file + if self.invert and self.assertion: + self._invert_message() + raise self + return True # Raises otherwise. \ No newline at end of file diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/base.py b/runtime-pyside6/tests/plugins/natural/interfaces/base.py index 88b9e06..0786285 100644 --- a/runtime-pyside6/tests/plugins/natural/interfaces/base.py +++ b/runtime-pyside6/tests/plugins/natural/interfaces/base.py @@ -15,10 +15,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . """ -from typing import Self +from typing import Self, Callable, Any -from tests.plugins.natural.interfaces.assertion import Assertion -from tests.plugins.natural.interfaces.utils import repr_ +from .assertion import Assertion +from .utils import repr_ class AssertionInterface: @@ -26,15 +26,91 @@ class AssertionInterface: Most basic assertion interface. You probably want to use BaseAssertionInterface """ - def __init__(self, value): + + def __init__(self, value, parent: Self = None): self._value = value + self._parent = parent + if parent is None: + self.__not = False @property - def was(self) -> Self: + def _not(self) -> bool: + """ + Internal state of whether the expression was negated. + Use "not_" to set it. + :return: + """ + return self.__not if self._parent is None else self._parent._not + + @_not.setter + def _not(self, value: bool): + if self._not is True: + raise RuntimeError("Cannot call is_not or was_not twice in the same statement.") + if self._parent is None: + self.__not = True + else: + self._parent._not = True + + def instance_of(self, type_: type) -> Assertion: + """ + Checks if the current value is equal to the provided value + """ + value_type_name = type(self._value).__name__ + if not isinstance(type_, type): + raise RuntimeError("Provided 'type' provided is not a class.") + return Assertion( + isinstance(self._value, type_), + f"The value ({value_type_name} {repr_(self._value)}) is not a {type_.__name__}.", + self._not + ) + + def __call__(self, condition: Callable[[Any], bool]) -> Assertion: + """ + Apply condition to value that returns whether or not the value is valid. + """ + return Assertion( + condition(self._value), + f"The value ({repr_(self._value)}) did not match given conditions.", + self._not + ) + + """ + NOT Properties. + """ + + @property + def NOT(self) -> Self: + self._not = True return self @property - def be(self) -> Self: + def not_(self) -> Self: + self._not = True + return self + + @property + def never(self) -> Self: + self._not = True + return self + + """ + Chain self properties to sound natural + """ + + @property + def that(self) -> Self: + return self + + @property + def is_(self) -> Self: + return self + + @property + def does(self) -> Self: + return self + + @property + def was(self) -> Self: return self @property @@ -57,31 +133,25 @@ class AssertionInterface: def an(self) -> Self: return self - def is_a(self, type_) -> Assertion: - """ - Checks if the current value is equal to the provided value - """ - value_type_name = type(self._value).__name__ - return Assertion(isinstance(type_, type_), f"The value ({value_type_name} {repr_(self._value)}) is not a {type_.__name__}.") - - class EqualAssertionInterface(AssertionInterface): """ Interface created for when its value should be checked for equality """ - def __init__(self, value): - super().__init__(value) + + def __init__(self, value, parent: AssertionInterface = None): + super().__init__(value, parent) def __call__(self, value) -> Assertion: - return Assertion(value == self._value, f"The value ({repr_(self._value)}) is not equal to {repr(value)}.") - - def to(self, value) -> Self: - return self(value) - - def of(self, value) -> Self: - return self(value) + return Assertion( + value == self._value, + f"The value {repr_(self._value)} is different from {repr(value)}.", + self._not + ) + @property + def to(self) -> Self: + return self class BaseAssertionInterface(AssertionInterface): @@ -91,11 +161,11 @@ class BaseAssertionInterface(AssertionInterface): """ Checks if the current value is equal to the provided value """ - return EqualAssertionInterface(self._value) + return EqualAssertionInterface(self._value, self) @property - def is_equal(self) -> EqualAssertionInterface: + def equal(self) -> EqualAssertionInterface: """ Checks if the current value is equal to the provided value """ - return self.equals \ No newline at end of file + return self.equals diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/basic.py b/runtime-pyside6/tests/plugins/natural/interfaces/basic.py index 1d5a878..25d45cb 100644 --- a/runtime-pyside6/tests/plugins/natural/interfaces/basic.py +++ b/runtime-pyside6/tests/plugins/natural/interfaces/basic.py @@ -15,47 +15,69 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . """ -from tests.plugins.natural.interfaces.assertion import Assertion -from tests.plugins.natural.interfaces.base import BaseAssertionInterface -from tests.plugins.natural.interfaces.int import IntInterface -from tests.plugins.natural.interfaces.utils import repr_ +from .assertion import Assertion +from .base import BaseAssertionInterface +from .int import NumberInterface +from .utils import repr_ class FixedIteratorInterface(BaseAssertionInterface): @property - def length(self) -> IntInterface: - return IntInterface(len(self._value)) + def length(self) -> NumberInterface: + return NumberInterface(len(self._value), self) def elements(self, *elements) -> Assertion: - tests = [elem for elem in elements if elem in self._value] + tests = [repr_(elem) for elem in elements if elem not in self._value] return Assertion( len(tests) == 0, - f"This value ({repr_(self._value)}) does not have elements ({repr_(tests)})" + f"This value ({repr_(self._value)}) does not have elements {', '.join(tests)}.", + self._not ) def element(self, element) -> Assertion: return Assertion( element in self._value, - f"This value ({repr_(self._value)}) does not have element ({repr_(element)})" + f"This value ({repr_(self._value)}) does not have element {repr_(element)}.", + self._not ) def contains(self, *elements) -> Assertion: - return self.elements(*elements) + """ + Check if the element(s) are contained in the iterator. + """ + if len(elements) == 1: + return self.element(elements[0]) + else: + return self.elements(*elements) + + def contain(self, *elements): + """ + Check if the element(s) are contained in the iterator. + """ + return self.contains(*elements) + class BoolInterface(BaseAssertionInterface): @property - def is_true(self): + def true(self): return Assertion( self._value == True, - f"The value ({repr_(self._value)}) is not True." + f"The value ({repr_(self._value)}) is not True.", + self._not ) @property - def is_false(self): + def false(self): return Assertion( self._value == False, - f"The value ({repr_(self._value)}) is not False." + f"The value ({repr_(self._value)}) is not False.", + self._not ) -class StringInterface(LengthInterface): - pass \ No newline at end of file + +class StringInterface(FixedIteratorInterface): + pass + + +class ListInterface(FixedIteratorInterface): + pass diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/int.py b/runtime-pyside6/tests/plugins/natural/interfaces/int.py index aaf0511..7ac8871 100644 --- a/runtime-pyside6/tests/plugins/natural/interfaces/int.py +++ b/runtime-pyside6/tests/plugins/natural/interfaces/int.py @@ -15,17 +15,47 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . """ +from idlelib.configdialog import is_int +from math import log10, floor, ceil from typing import Self -from tests.plugins.natural.interfaces.assertion import Assertion -from tests.plugins.natural.interfaces.base import BaseAssertionInterface, EqualAssertionInterface, AssertionInterface -from tests.plugins.natural.interfaces.utils import repr_ +from .assertion import Assertion +from .base import AssertionInterface +from .utils import repr_ -class IntComparisonAssertionInterface(AssertionInterface): - def __init__(self, value): - super().__init__(value) - self._compare_to = None +class NumberComparisonAssertionInterface(AssertionInterface): + def __init__(self, value, parent: AssertionInterface = None): + super().__init__(value, parent) + self._compare_stack = [] + + def _generate_compare_to(self) -> int: + """ + The number generated by the comparison stack. + E.g. can parse one.hundred.million.and.thirty.three.thousand.and.twelve.hundred.and.seven + as ['one', 'hundred', 'million', 'thirty', 'three', 'thousand', 'twelve', 'hundred', 'seven'] + which results 100,034,207 + """ + minus = len(self._compare_stack) > 0 and self._compare_stack[0] == -1 + if len(self._compare_stack) < (2 if minus else 1): + raise RuntimeError("No number to compare the value to provided.") + if minus: + self._compare_stack.pop(0) + # Compute the number + add_stack = [self._compare_stack.pop(0)] + for element in self._compare_stack: + last_power = floor(log10(abs(add_stack[-1]))) + current_power = floor(log10(abs(element))) + if last_power < current_power: # E.g. one hundred + add_stack[-1] *= element + elif last_power == 1 and current_power == 0: # E.g thirty four + add_stack[-1] += element + elif last_power > current_power: # E.g a hundred and five + add_stack.append(element) + else: + raise RuntimeError(f"Cannot chain two numbers with the same power ({add_stack[-1]} => {element}.") + total = sum(add_stack) + return -total if minus else total def _compare(self) -> Assertion: raise RuntimeError(f"No comparison method defined in {type(self).__name__}.") @@ -34,23 +64,26 @@ class IntComparisonAssertionInterface(AssertionInterface): return bool(self._compare()) def __call__(self, compare_to: int) -> Self: - if self._compare_to is None: - self._compare_to = int(compare_to) - else: - self._compare_to *= int(compare_to) + if type(compare_to) not in (float, int): + raise RuntimeError(f"Cannot compare number ({self._value}) to non number ({repr_(compare_to)}).") + self._compare_stack.append(compare_to) + return self + + """ + Chain self properties + """ + + @property + def and_(self) -> Self: return self @property - def time(self) -> Self: + def AND(self) -> Self: return self - @property - def times(self) -> Self: - return self - - @property - def never(self) -> Self: - return self(0) + """ + Number shorthands + """ @property def once(self) -> Self: @@ -64,6 +97,10 @@ class IntComparisonAssertionInterface(AssertionInterface): def thrice(self) -> Self: return self(3) + @property + def minus(self) -> Self: + return self(-1) + @property def zero(self) -> Self: return self(0) @@ -108,6 +145,42 @@ class IntComparisonAssertionInterface(AssertionInterface): def ten(self) -> Self: return self(10) + @property + def eleven(self) -> Self: + return self(11) + + @property + def twelve(self) -> Self: + return self(12) + + @property + def thirteen(self) -> Self: + return self(13) + + @property + def fourteen(self) -> Self: + return self(14) + + @property + def fifteen(self) -> Self: + return self(15) + + @property + def sixteen(self) -> Self: + return self(16) + + @property + def seventeen(self) -> Self: + return self(17) + + @property + def eighteen(self) -> Self: + return self(18) + + @property + def nineteen(self) -> Self: + return self(19) + @property def twenty(self) -> Self: return self(20) @@ -134,11 +207,11 @@ class IntComparisonAssertionInterface(AssertionInterface): @property def eighty(self) -> Self: - return self(70) + return self(80) @property def ninety(self) -> Self: - return self(70) + return self(90) @property def hundred(self) -> Self: @@ -157,73 +230,92 @@ class IntComparisonAssertionInterface(AssertionInterface): return self(1_000_000_000) -class LessThanComparisonInterface(IntComparisonAssertionInterface): +class LessThanComparisonInterface(NumberComparisonAssertionInterface): def _compare(self) -> Assertion: + compare = self._generate_compare_to() return Assertion( - self._value < self._compare_to, - f"The value ({repr_(self._value)}) is not less than to {repr_(self._compare_to)}." + self._value < compare, + f"The value ({repr_(self._value)}) is not less than to {repr_(compare)}.", + self._not ) -class MoreThanComparisonInterface(IntComparisonAssertionInterface): + +class MoreThanComparisonInterface(NumberComparisonAssertionInterface): def _compare(self) -> Assertion: + compare = self._generate_compare_to() return Assertion( - self._value > self._compare_to, - f"The value ({repr_(self._value)}) is not more than to {repr_(self._compare_to)}." + self._value > compare, + f"The value ({repr_(self._value)}) is not more than to {repr_(compare)}.", + self._not ) -class AtLeastComparisonInterface(IntComparisonAssertionInterface): + +class AtLeastComparisonInterface(NumberComparisonAssertionInterface): def _compare(self) -> Assertion: + compare = self._generate_compare_to() return Assertion( - self._value >= self._compare_to, - f"The value ({repr_(self._value)}) is not at least to {repr_(self._compare_to)}." + self._value >= compare, + f"The value ({repr_(self._value)}) is not at least to {repr_(compare)}.", + self._not ) -class AtMostComparisonInterface(IntComparisonAssertionInterface): + +class AtMostComparisonInterface(NumberComparisonAssertionInterface): def _compare(self) -> Assertion: + compare = self._generate_compare_to() return Assertion( - self._value <= self._compare_to, - f"The value ({repr_(self._value)}) is not at least to {repr_(self._compare_to)}." + self._value <= compare, + f"The value ({repr_(self._value)}) is not at least to {repr_(compare)}.", + self._not ) -class EqualComparisonInterface(IntComparisonAssertionInterface): + +class EqualComparisonInterface(NumberComparisonAssertionInterface): def _compare(self) -> Assertion: + compare = self._generate_compare_to() return Assertion( - self._value == self._compare_to, - f"The value ({repr_(self._value)}) is not equal to {repr_(self._compare_to)}." + self._value == compare, + f"The value ({repr_(self._value)}) is not equal to {repr_(compare)}.", + self._not ) + @property def to(self) -> Self: return self - def of(self) -> Self: - return self - -class IntInterface(AssertionInterface): - def less_than(self) -> LessThanComparisonInterface: - return LessThanComparisonInterface(self._value) - - @property - def more_than(self) -> MoreThanComparisonInterface: - return MoreThanComparisonInterface(self._value) - - @property - def at_least(self) -> AtLeastComparisonInterface: - return AtLeastComparisonInterface(self._value) - - @property - def at_most(self) -> AtMostComparisonInterface: - return AtMostComparisonInterface(self._value) +class NumberInterface(AssertionInterface): + def __call__(self, value): + return EqualComparisonInterface(self._value, self)(value) @property def equals(self) -> EqualComparisonInterface: - return EqualComparisonInterface(self._value) + return EqualComparisonInterface(self._value, self) @property - def is_equal(self) -> EqualComparisonInterface: - return EqualComparisonInterface(self._value) + def equal(self) -> EqualComparisonInterface: + return EqualComparisonInterface(self._value, self) @property def exactly(self) -> EqualComparisonInterface: - return EqualComparisonInterface(self._value) + return EqualComparisonInterface(self._value, self) + @property + def of(self) -> EqualComparisonInterface: + return EqualComparisonInterface(self._value, self) + + @property + def less_than(self) -> LessThanComparisonInterface: + return LessThanComparisonInterface(self._value, self) + + @property + def more_than(self) -> MoreThanComparisonInterface: + return MoreThanComparisonInterface(self._value, self) + + @property + def at_least(self) -> AtLeastComparisonInterface: + return AtLeastComparisonInterface(self._value, self) + + @property + def at_most(self) -> AtMostComparisonInterface: + return AtMostComparisonInterface(self._value, self) diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/spy.py b/runtime-pyside6/tests/plugins/natural/interfaces/spy.py new file mode 100644 index 0000000..59b09d3 --- /dev/null +++ b/runtime-pyside6/tests/plugins/natural/interfaces/spy.py @@ -0,0 +1,218 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +from typing import Callable, Self + +from .base import Assertion, repr_, AssertionInterface +from .int import NumberComparisonAssertionInterface + +PRINT_PREFIX = (" " * 24) + + +class SpyAssertion(Assertion): + def __init__(self, assertion: bool, message: str, calls: list, invert: bool): + super().__init__(assertion, message + "\n", invert) + if len(calls) > 0: + self.message += self.render_calls(calls) + else: + self.message += f"{PRINT_PREFIX}0 registered calls." + + def render_calls(self, calls): + lines = [f"{PRINT_PREFIX}{len(calls)} registered call(s):"] + for call in calls: + repr_args = [repr_(arg) for arg in call[0]] + repr_kwargs = [f"{key}={repr_(arg)}" for key, arg in call[1].items()] + lines.append(f" - {', '.join([*repr_args, *repr_kwargs])}") + return ("\n" + PRINT_PREFIX).join(lines) + + +class Methods: + AT_LEAST_ONCE = "AT_LEAST_ONCE" + EXACTLY = "EXACTLY" + AT_LEAST = "AT_LEAST" + AT_MOST = "AT_MOST" + MORE_THAN = "MORE_THAN" + LESS_THAN = "LESS_THAN" + + +class CalledInterface(NumberComparisonAssertionInterface): + """ + Internal class generated by Spy.called. + """ + + def __init__(self, calls: list[tuple[list, dict]], parent: AssertionInterface): + super().__init__(len(calls), parent) + self.__calls = calls + self.__method = Methods.AT_LEAST_ONCE + + def __apply_method(self, calls): + required = None if self._compare_stack == [] else self._generate_compare_to() + calls_count = len(calls) + match self.__method: + case Methods.AT_LEAST_ONCE: + compare = len(calls) >= 1 + error = f"Method was not called" + case Methods.EXACTLY: + compare = len(calls) == required + error = f"Method was not called {required} times ({required} != {calls_count})" + case Methods.AT_LEAST: + compare = len(calls) >= required + error = f"Method was not called at least {required} times ({required} >= {calls_count})" + case Methods.AT_MOST: + compare = len(calls) <= required + error = f"Method was not called at most {required} times ({required} <= {calls_count})" + case Methods.MORE_THAN: + compare = len(calls) > required + error = f"Method was not called more than {required} times ({required} > {calls_count})" + case Methods.LESS_THAN: + compare = len(calls) < required + error = f"Method was not called less than {required} times ({required} < {calls_count})" + case _: + raise RuntimeError(f"Unknown method {self.__method}.") + return compare, error + + def __bool__(self) -> bool: + """ + Converts to boolean on assertion. + """ + compare, error = self.__apply_method(self.__calls) + return bool(SpyAssertion(compare, error + ".", self.__calls, self._not)) + + """ + Chaining methods + """ + + def __call__(self, compare_to: int) -> Self: + super().__call__(compare_to) + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.EXACTLY + return self + + @property + def at_least(self) -> Self: + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.AT_LEAST + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.AT_MOST}") + return self + + @property + def at_most(self) -> Self: + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.AT_MOST + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.AT_MOST}") + return self + + @property + def more_than(self) -> Self: + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.MORE_THAN + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.MORE_THAN}") + return self + + @property + def less_than(self) -> Self: + if self.__method == Methods.AT_LEAST_ONCE: + self.__method = Methods.LESS_THAN + else: + raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.LESS_THAN}") + return self + + @property + def time(self) -> Self: + return self + + @property + def times(self) -> Self: + return self + + """ + Class properties. + """ + + def __match_calls_for_condition(self, condition: Callable[[list, dict], bool]) -> tuple[bool, str]: + calls = [] + for call in self.__calls: + if condition(call[0], call[1]): + calls.append(call) + compare, error = self.__apply_method(calls) + return compare, error + + def with_arguments(self, *args, **kwargs) -> SpyAssertion: + """ + Checks if the Spy has been called the given number of times + with at least the given arguments. + """ + + def some_args_matched(a, kw): + args_matched = all(( + arg in a + for arg in args + )) + kwargs_matched = all(( + key in kw and kw[key] == arg + for key, arg in kwargs.items() + )) + return args_matched and kwargs_matched + + compare, error = self.__match_calls_for_condition(some_args_matched) + repr_args = ', '.join([repr(arg) for arg in args]) + repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) + msg = f"{error} with arguments ({repr_args}) and keyword arguments ({repr_kwargs})." + return SpyAssertion(compare, msg, self.__calls, self._not) + + def with_arguments_matching(self, test_condition: Callable[[list, dict], bool]) -> SpyAssertion: + """ + Checks if the Spy has been called the given number of times + with arguments matching the given conditions. + """ + compare, error = self.__match_calls_for_condition(test_condition) + msg = f"{error} with arguments matching given conditions." + return SpyAssertion(compare, msg, self.__calls, self._not) + + def with_exact_arguments(self, *args, **kwargs) -> SpyAssertion: + """ + Checks if the Spy has been called the given number of times + with all the given arguments. + """ + compare, error = self.__match_calls_for_condition(lambda a, kw: a == args and kw == kwargs) + repr_args = ', '.join([repr(arg) for arg in args]) + repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) + msg = f"{error} with exact arguments ({repr_args}) and keyword arguments ({repr_kwargs})." + return SpyAssertion(compare, msg, self.__calls, self._not) + + def with_no_argument(self) -> SpyAssertion: + """ + Checks if the Spy has been called the given number of times + with all the given arguments. + """ + compare, error = self.__match_calls_for_condition(lambda a, kw: len(a) == 0 and len(kw) == 0) + return SpyAssertion(compare, f"{error} with no arguments.", self.__calls, self._not) + + +class SpyAssertionInterface(AssertionInterface): + + @property + def called(self) -> CalledInterface: + """ + Returns a boolean-able interface to check conditions for a given number of + time the spy was called. + """ + return CalledInterface(self._value.calls, self) diff --git a/runtime-pyside6/tests/plugins/natural/spy.py b/runtime-pyside6/tests/plugins/natural/spy.py index 1d77629..fbd8e17 100644 --- a/runtime-pyside6/tests/plugins/natural/spy.py +++ b/runtime-pyside6/tests/plugins/natural/spy.py @@ -15,207 +15,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . """ -from typing import Callable, Self - -from tests.plugins.natural.interfaces.base import Assertion, repr_, AssertionInterface -from tests.plugins.natural.interfaces.int import IntComparisonAssertionInterface - -PRINT_PREFIX = (" " * 24) +from typing import Callable -class SpyAssertion(Assertion): - def __init__(self, assertion: bool, message: str, calls: list): - super().__init__(assertion, message + "\n") - if len(calls) > 0: - self.message += self.render_calls(calls) - else: - self.message += f"{PRINT_PREFIX}0 registered calls." - - - def render_calls(self, calls): - lines = [f"{PRINT_PREFIX}{len(calls)} registered call(s):"] - for call in calls: - repr_args = [repr_(arg) for arg in call[0]] - repr_kwargs = [f"{key}={repr_(arg)}" for key, arg in call[1].items()] - lines.append(f" - {', '.join([*repr_args, *repr_kwargs])}") - return ("\n" + PRINT_PREFIX).join(lines) - - -class Methods: - AT_LEAST_ONCE = "AT_LEAST_ONCE" - EXACTLY = "EXACTLY" - AT_LEAST = "AT_LEAST" - AT_MOST = "AT_MOST" - MORE_THAN = "AT_LEAST" - LESS_THAN = "AT_MOST" - - -class CalledInterface(IntComparisonAssertionInterface): - """ - Internal class generated by Spy.was_called. - """ - - def __init__(self, calls: list[tuple[list, dict]]): - super().__init__(len(calls)) - self.__calls = calls - self.__method = Methods.AT_LEAST_ONCE - - def __apply_method(self, calls): - required = self._compare_to - calls_count = len(calls) - match self.__method: - case Methods.AT_LEAST_ONCE: - compare = len(calls) >= 1 - error = "Method was not called" - case Methods.EXACTLY: - compare = len(calls) == required - error = f"Method was not called {required} times ({required} != {calls_count})" - case Methods.AT_LEAST: - compare = len(calls) >= required - error = f"Method was not called at least {required} times ({required} > {calls_count})" - case Methods.AT_MOST: - compare = len(calls) <= required - error = f"Method was not called at most {required} times ({required} < {calls_count})" - case Methods.MORE_THAN: - compare = len(calls) > required - error = f"Method was not called more than {required} times ({required} >= {calls_count})" - case Methods.LESS_THAN: - compare = len(calls) < required - error = f"Method was not called less than {required} times ({required} <= {calls_count})" - case _: - raise RuntimeError(f"Unknown method {self.__method}.") - return compare, error - - def __bool__(self) -> bool: - """ - Converts to boolean on assertion. - """ - compare, error = self.__apply_method(self.__calls) - return bool(SpyAssertion(compare, error + ".", self.__calls)) - - """ - Chaining methods - """ - - def __call__(self, *args, **kwargs) -> Self: - if len(args) != 1: - raise RuntimeError("Cannot call called interface with more than one argument.") - self._compare_to = int(args[0]) - if self.__method == Methods.AT_LEAST_ONCE: - self.__method = Methods.EXACTLY - return self - - @property - def at_least(self) -> Self: - if self.__method == Methods.AT_LEAST_ONCE: - self.__method = Methods.AT_LEAST - else: - raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.AT_MOST}") - return self - - @property - def at_most(self) -> Self: - if self.__method == Methods.AT_LEAST_ONCE: - self.__method = Methods.AT_MOST - else: - raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.AT_MOST}") - return self - - @property - def more_than(self) -> Self: - if self.__method == Methods.AT_LEAST_ONCE: - self.__method = Methods.MORE_THAN - else: - raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.MORE_THAN}") - return self - - @property - def less_than(self) -> Self: - if self.__method == Methods.AT_LEAST_ONCE: - self.__method = Methods.LESS_THAN - else: - raise RuntimeError(f"Cannot redefine method from {self.__method} to {Methods.LESS_THAN}") - return self - - @property - def time(self) -> Self: - return self - - @property - def times(self) -> Self: - return self - - """ - Class properties. - """ - - def __match_calls_for_condition(self, condition: Callable[[list, dict], bool]) -> tuple[bool, str]: - calls = [] - for call in self.__calls: - if condition(call[0], call[1]): - calls.append(call) - compare, error = self.__apply_method(calls) - return compare, error - - def with_arguments(self, *args, **kwargs) -> SpyAssertion: - """ - Checks if the Spy has been called the given number of times - with at least the given arguments. - """ - - def some_args_matched(a, kw): - args_matched = all(( - arg in a - for arg in args - )) - kwargs_matched = all(( - key in kw and kw[key] == arg - for key, arg in kwargs.items() - )) - return args_matched and kwargs_matched - - compare, error = self.__match_calls_for_condition(some_args_matched) - repr_args = ', '.join([repr(arg) for arg in args]) - repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) - msg = f"{error} with arguments ({repr_args}) and keyword arguments ({repr_kwargs})." - return SpyAssertion(compare, msg, self.__calls) - - def with_arguments_matching(self, test_condition: Callable[[list, dict], bool]) -> SpyAssertion: - """ - Checks if the Spy has been called the given number of times - with arguments matching the given conditions. - """ - compare, error = self.__match_calls_for_condition(test_condition) - msg = f"{error} with arguments matching given conditions." - return SpyAssertion(compare, msg, self.__calls) - - def with_exact_arguments(self, *args, **kwargs) -> SpyAssertion: - """ - Checks if the Spy has been called the given number of times - with all the given arguments. - """ - compare, error = self.__match_calls_for_condition(lambda a, kw: a == args and kw == kwargs) - repr_args = ', '.join([repr(arg) for arg in args]) - repr_kwargs = ', '.join([f"{key}={repr(arg)}" for key, arg in kwargs.items()]) - msg = f"{error} with exact arguments ({repr_args}) and keyword arguments ({repr_kwargs})." - return SpyAssertion(compare, msg, self.__calls) - - def with_no_argument(self) -> SpyAssertion: - """ - Checks if the Spy has been called the given number of times - with all the given arguments. - """ - compare, error = self.__match_calls_for_condition(lambda a, kw: len(a) == 0 and len(kw) == 0) - return SpyAssertion(compare, f"{error} with no arguments.", self.__calls) - - -class Spy(AssertionInterface): +class Spy: """ Class to spy into method calls with natural language expressions. """ def __init__(self, function: Callable = None): - super().__init__(function) self.function = function self.calls = [] @@ -223,20 +31,3 @@ class Spy(AssertionInterface): self.calls.append((args, kwargs)) if self.function is not None: self.function(*args, **kwargs) - - @property - def called(self) -> CalledInterface: - """ - Returns a boolean-able interface to check conditions for a given number of - time the spy was called. - """ - return CalledInterface(self.calls) - - @property - def not_called(self) -> CalledInterface: - """ - Returns a boolean-able interface to check that conditions were never fulfilled - in the times the spy was called. - """ - ret = CalledInterface(self.calls) - return ret(0) diff --git a/runtime-pyside6/tests/plugins/natural/that.py b/runtime-pyside6/tests/plugins/natural/that.py index 38ee54e..05a3e31 100644 --- a/runtime-pyside6/tests/plugins/natural/that.py +++ b/runtime-pyside6/tests/plugins/natural/that.py @@ -17,17 +17,21 @@ """ from typing import overload, Generic, TypeVar -from .spy import Spy +from . import Spy from .interfaces.base import AssertionInterface, BaseAssertionInterface -from .interfaces.basic import StringInterface, BoolInterface -from .interfaces.int import IntInterface +from .interfaces.basic import StringInterface, BoolInterface, ListInterface +from .interfaces.int import NumberInterface +from .interfaces.spy import SpyAssertionInterface Interface = TypeVar("Interface", bound=AssertionInterface) MATCHES = [ (str, StringInterface), - (int, IntInterface), (bool, BoolInterface), + (int, NumberInterface), + (float, NumberInterface), + (list, ListInterface), + (Spy, SpyAssertionInterface) ] @@ -36,11 +40,18 @@ def that(value: str) -> StringInterface: ... @overload -def that(value: int) -> IntInterface: ... +def that(value: bool) -> BoolInterface: ... @overload -def that(value: bool) -> BoolInterface: ... +def that(value: int) -> NumberInterface: ... + + +@overload +def that(value: float) -> NumberInterface: ... + +@overload +def that(value: Spy) -> SpyAssertionInterface: ... @overload diff --git a/runtime-pyside6/tests/plugins/tests/__init__.py b/runtime-pyside6/tests/plugins/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/runtime-pyside6/tests/plugins/tests/test_natural.py b/runtime-pyside6/tests/plugins/tests/test_natural.py new file mode 100644 index 0000000..d7fe26c --- /dev/null +++ b/runtime-pyside6/tests/plugins/tests/test_natural.py @@ -0,0 +1,217 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2021-2024 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" +import pytest + +from ..natural import that, Assertion, Spy + + +def test_string(): + assert that("QWERTY").is_.an.instance_of(str) + assert that("QWERTY").is_.not_.an.instance_of(int) + assert that("QWERTY").is_.equal.to("QWERTY") + assert that("QWERTY").is_.NOT.equal.to("QWERTYUIOP") + assert that("QWERTY").is_.NOT.equal.to(3) + assert that("QWERTY").has.a.length.of(6) + assert that("QWERTY").does.NOT.have.a.length.of(7) + assert that("QWERTY").has.a.length.that.is_.NOT(5) + assert that("QWERTY").contains("WER") + assert that("QWERTY").contains("WER", "TY") + assert that("QWERTY").does.not_.contain("AZERTY") + with pytest.raises(Assertion): + assert that("QWERTY").is_.an.instance_of(int) + with pytest.raises(Assertion): + assert that("QWERTY").is_.equal.to(False) + with pytest.raises(Assertion): + assert that("QWERTY").has.a.length.of(1) + with pytest.raises(Assertion): + assert that("QWERTY").contains("AZERTY") + with pytest.raises(Assertion): + assert that("QWERTY").does.NOT.contain("QWE") + +def test_bool(): + assert that(True).is_.an.instance_of(bool) + assert that(True).is_.an.instance_of(int) + assert that(True).is_.NOT.an.instance_of(str) + assert that(True).equals(True) + assert that(True).is_.true + assert that(True).is_.NOT.false + assert that(False).is_.equal.to(False) + assert that(False).is_.false + assert that(False).is_.NOT.true + with pytest.raises(Assertion): + assert that(True).is_.false + with pytest.raises(Assertion): + assert that(True).is_.NOT.true + +def test_int(): + assert that(2).is_.an.instance_of(int) + assert that(2).is_.NOT.an.instance_of(bool) + assert that(2).is_.NOT.an.instance_of(str) + assert that(2).is_.more_than(1) + assert that(2).is_.NOT.less_than(1) + assert that(2).is_.less_than(3) + assert that(2).is_.NOT.more_than(3) + assert that(2).is_.at_least(1) + assert that(2).is_.NOT.at_most(1) + assert that(2).is_.at_most(3) + assert that(2).is_.NOT.at_least(3) + assert that(2).is_.at_most(2) + assert that(2).is_.at_least(2) + # Equality + assert that(2).is_(2) + assert that(2).was(2) + assert that(2).is_.exactly(2) + assert that(2).is_.equal.to(2) + assert that(2).equals(2) + assert that(2).is_.NOT(3) + assert that(2).does.NOT.equal(3) + +def test_int_shorthands(): + # Direct numbers + assert that(0).equals.zero + assert that(1).equals.one + assert that(2).equals.two + assert that(3).equals.three + assert that(4).equals.four + assert that(5).equals.five + assert that(6).equals.six + assert that(7).equals.seven + assert that(8).equals.eight + assert that(9).equals.nine + assert that(10).equals.ten + assert that(11).equals.eleven + assert that(12).equals.twelve + assert that(13).equals.thirteen + assert that(14).equals.fourteen + assert that(15).equals.fifteen + assert that(16).equals.sixteen + assert that(17).equals.seventeen + assert that(18).equals.eighteen + assert that(19).equals.nineteen + assert that(20).equals.twenty + assert that(30).equals.thirty + assert that(40).equals.forty + assert that(50).equals.fifty + assert that(60).equals.sixty + assert that(70).equals.seventy + assert that(80).equals.eighty + assert that(90).equals.ninety + assert that(100).equals.a.hundred + assert that(1000).equals.a.thousand + assert that(1_000_000).equals.a.million + +def test_add_natural_complex(): + # Test composed + assert that(34).equals.thirty.four + assert that(-34).equals.minus.thirty.four + assert that(100_033_207).equals.one.hundred.million.AND.thirty.three.thousand.AND.two.hundred.AND.seven + assert that(-1_200_033_207).equals.minus.one.billion.AND.two.hundred.million.AND.thirty.three.thousand.AND.two.hundred.AND.seven + assert that(7890).equals.seven.thousand.eight.hundred.and_.ninety + assert that(7890).equals.seventy.eight.hundred.and_.ninety + assert that(7890).equals(78)(100)(90) + with pytest.raises(RuntimeError): + assert that(1_000_000).equals.a.thousand.thousand + with pytest.raises(RuntimeError): + assert that(600).equals.one.twenty.thirty + with pytest.raises(RuntimeError): + assert that(2).equals + with pytest.raises(RuntimeError): + assert that(2).equals.one.minus.two + +def test_spy(): + spy = Spy() + assert that(spy).is_.an.instance_of(Spy) + assert that(spy).is_(callable) + # Check calls + assert that(spy).was.never.called + assert that(spy).was.called.zero.times + spy(30, arg="string") + assert that(spy).was.called + assert that(spy).was.called.once + assert that(spy).was.called.one.time + assert that(spy).was.NOT.called.more_than.once + assert that(spy).was.called.with_arguments(30) + assert that(spy).was.called.with_arguments_matching(lambda args, kwargs: len(args) == 1 and len(kwargs) == 1) + assert that(spy).was.NOT.called.with_arguments(50) + assert that(spy).was.NOT.called.with_exact_arguments(30) + assert that(spy).was.NOT.called.with_no_argument() + assert that(spy).was.called.with_exact_arguments(30, arg="string") + with pytest.raises(Assertion): + assert that(spy).was.called.with_arguments(50) + with pytest.raises(Assertion): + assert that(spy).was.called.with_exact_arguments(30) + with pytest.raises(Assertion): + assert that(spy).was.called.with_no_argument() + +def test_spy_seral_calls(): + spy = Spy(lambda *args, **kw: None) + obj = object() + spy() + spy(30, arg="string") + spy(obj, 30, example=obj, none=None) + assert that(spy).was.called + assert that(spy).was.called.more_than.once + assert that(spy).was.called.more_than.twice + assert that(spy).was.NOT.called.more_than.thrice + assert that(spy).was.called.at_most.thrice + assert that(spy).was.called.at_least.thrice + assert that(spy).was.called.three.times + assert that(spy).was.called.less_than(4).times + # Check arguments + assert that(spy).was.called.once.with_no_argument() + assert that(spy).was.called.at_most.once.with_no_argument() + assert that(spy).was.called.twice.with_arguments(30) + assert that(spy).was.NOT.called.less_than.twice.with_arguments(30) + assert that(spy).was.called.once.with_arguments(obj) + assert that(spy).was.called.once.with_arguments(arg="string") + assert that(spy).was.called.once.with_arguments(30, obj) + assert that(spy).was.called.once.with_arguments(none=None) + assert that(spy).was.NOT.called.with_arguments(None) + assert that(spy).was.NOT.called.with_arguments(obj, 30, arg="string") + with pytest.raises(Assertion): + assert that(spy).was.called.with_arguments(obj, 30, arg="string") + # Checking with exact arguments + assert that(spy).was.called.once.with_exact_arguments(30, arg="string") + assert that(spy).was.called.once.with_exact_arguments(obj, 30, example=obj, none=None) + assert that(spy).was.NOT.called.with_exact_arguments(obj, 30, arg="string") + with pytest.raises(Assertion): + assert that(spy).was.called.with_exact_arguments(obj, 30, arg="string") + # Check arguments matching + assert that(spy).has.NOT.been.called.with_arguments_matching(lambda a, kw: len(a) + len(kw) == 3) + assert that(spy).was.called.once.with_arguments_matching(lambda a, kw: len(a) + len(kw) == 2) + assert that(spy).was.called.once.with_arguments_matching(lambda a, kw: len(a) + len(kw) == 4) + with pytest.raises(Assertion): + assert that(spy).was.called.with_arguments_matching(lambda a, kw: len(a) + len(kw) == 3) + + +def test_wrongful_expressions(): + spy = Spy() + with pytest.raises(RuntimeError): + assert that(3).is_.less_than("str") + with pytest.raises(RuntimeError): + assert that(3).does.NOT.NOT.equal(3) + with pytest.raises(RuntimeError): + assert that(3).is_.an.instance_of("non type") + with pytest.raises(RuntimeError): + assert that(spy).was.called.more_than.at_least.once + with pytest.raises(RuntimeError): + assert that(spy).was.called.more_than.at_most.once + with pytest.raises(RuntimeError): + assert that(spy).was.called.more_than.less_than.once + with pytest.raises(RuntimeError): + assert that(spy).was.called.more_than.more_than.once \ No newline at end of file From a182c703f43a9542bb55662b0dd68bc12872b48a Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Thu, 17 Oct 2024 03:38:36 +0200 Subject: [PATCH 4/4] Finishing testing promises. --- .../LogarithmPlotter/util/promise.py | 24 ++--- .../tests/plugins/natural/interfaces/spy.py | 2 +- runtime-pyside6/tests/plugins/natural/spy.py | 2 +- .../tests/plugins/tests/test_natural.py | 6 +- runtime-pyside6/tests/test_main.py | 3 +- runtime-pyside6/tests/test_promise.py | 98 +++++++++++++++---- runtime-pyside6/tests/test_pyjs.py | 1 - scripts/run-tests.sh | 14 +++ 8 files changed, 114 insertions(+), 36 deletions(-) diff --git a/runtime-pyside6/LogarithmPlotter/util/promise.py b/runtime-pyside6/LogarithmPlotter/util/promise.py index 3c2d223..f43e85b 100644 --- a/runtime-pyside6/LogarithmPlotter/util/promise.py +++ b/runtime-pyside6/LogarithmPlotter/util/promise.py @@ -73,10 +73,10 @@ class PyPromiseRunner(QRunnable): class PyPromise(QObject): """ - Asynchronous Promise-like object meant to interface between Python and Javascript easily. + Threaded A+/Promise implementation meant to interface between Python and Javascript easily. Runs to_run in another thread, and calls fulfilled (populated by then) with its return value. """ - fulfilled = Signal((QJSValue,), (QObject,)) + fulfilled = Signal(QJSValue) rejected = Signal(str) def __init__(self, to_run: Callable|QJSValue, args=[], start_automatically=True): @@ -115,10 +115,8 @@ class PyPromise(QObject): """ on_fulfill = check_callable(on_fulfill) on_reject = check_callable(on_reject) - if on_fulfill is not None: - self._fulfills.append(on_fulfill) - if on_reject is not None: - self._rejects.append(on_reject) + self._fulfills.append(on_fulfill) + self._rejects.append(on_reject) return self def calls_upon_fulfillment(self, function: Callable | QJSValue) -> bool: @@ -155,20 +153,22 @@ class PyPromise(QObject): def _fulfill(self, data): self._state = "fulfilled" no_return = [None, QJSValue.SpecialValue.UndefinedValue] - for on_fulfill in self._fulfills: + print("Fulfill") + for i in range(len(self._fulfills)): try: - result = on_fulfill(data) + result = self._fulfills[i](data) result = result.qjs_value if isinstance(result, PyJSValue) else result data = result if result not in no_return else data # Forward data. except Exception as e: - self._reject(repr(e)) + self._reject(repr(e), start_at=i) break @Slot(QJSValue) @Slot(str) - def _reject(self, error): + def _reject(self, error, start_at=0): self._state = "rejected" no_return = [None, QJSValue.SpecialValue.UndefinedValue] - for on_reject in self._rejects: - result = on_reject(error) + for i in range(start_at, len(self._rejects)): + result = self._rejects[i](error) + result = result.qjs_value if isinstance(result, PyJSValue) else result error = result if result not in no_return else error # Forward data. diff --git a/runtime-pyside6/tests/plugins/natural/interfaces/spy.py b/runtime-pyside6/tests/plugins/natural/interfaces/spy.py index 59b09d3..524fd71 100644 --- a/runtime-pyside6/tests/plugins/natural/interfaces/spy.py +++ b/runtime-pyside6/tests/plugins/natural/interfaces/spy.py @@ -21,7 +21,7 @@ from typing import Callable, Self from .base import Assertion, repr_, AssertionInterface from .int import NumberComparisonAssertionInterface -PRINT_PREFIX = (" " * 24) +PRINT_PREFIX = (" " * 3) class SpyAssertion(Assertion): diff --git a/runtime-pyside6/tests/plugins/natural/spy.py b/runtime-pyside6/tests/plugins/natural/spy.py index fbd8e17..2f9211a 100644 --- a/runtime-pyside6/tests/plugins/natural/spy.py +++ b/runtime-pyside6/tests/plugins/natural/spy.py @@ -30,4 +30,4 @@ class Spy: def __call__(self, *args, **kwargs): self.calls.append((args, kwargs)) if self.function is not None: - self.function(*args, **kwargs) + return self.function(*args, **kwargs) diff --git a/runtime-pyside6/tests/plugins/tests/test_natural.py b/runtime-pyside6/tests/plugins/tests/test_natural.py index d7fe26c..f381639 100644 --- a/runtime-pyside6/tests/plugins/tests/test_natural.py +++ b/runtime-pyside6/tests/plugins/tests/test_natural.py @@ -134,13 +134,13 @@ def test_add_natural_complex(): assert that(2).equals.one.minus.two def test_spy(): - spy = Spy() + spy = Spy(lambda *args, **kw: 10) assert that(spy).is_.an.instance_of(Spy) assert that(spy).is_(callable) # Check calls assert that(spy).was.never.called assert that(spy).was.called.zero.times - spy(30, arg="string") + assert spy(30, arg="string") == 10 assert that(spy).was.called assert that(spy).was.called.once assert that(spy).was.called.one.time @@ -159,7 +159,7 @@ def test_spy(): assert that(spy).was.called.with_no_argument() def test_spy_seral_calls(): - spy = Spy(lambda *args, **kw: None) + spy = Spy() obj = object() spy() spy(30, arg="string") diff --git a/runtime-pyside6/tests/test_main.py b/runtime-pyside6/tests/test_main.py index 047e21f..6b161ad 100644 --- a/runtime-pyside6/tests/test_main.py +++ b/runtime-pyside6/tests/test_main.py @@ -22,12 +22,13 @@ from os.path import exists, join from PySide6.QtGui import QIcon from tempfile import TemporaryDirectory +from .globals import app + from LogarithmPlotter.logarithmplotter import get_linux_theme, LINUX_THEMES, get_platform_qt_style, \ register_icon_directories, install_translation, create_engine from LogarithmPlotter.util import config from LogarithmPlotter.util.helper import Helper from LogarithmPlotter.util.latex import Latex -from globals import app THEMES = [ "Basic", diff --git a/runtime-pyside6/tests/test_promise.py b/runtime-pyside6/tests/test_promise.py index a97f864..30f0a1e 100644 --- a/runtime-pyside6/tests/test_promise.py +++ b/runtime-pyside6/tests/test_promise.py @@ -17,9 +17,10 @@ """ from time import sleep +import pytest from PySide6.QtQml import QJSValue -from tests.plugins.natural import that, Spy +from .plugins.natural import that, Spy from LogarithmPlotter.util.js import PyJSValue from LogarithmPlotter.util.promise import PyPromise @@ -56,6 +57,10 @@ def async_throw(): class TestPyPromise: + def test_invalid_function(self): + with pytest.raises(ValueError): + promise = PyPromise("not a function") + def test_fulfill_values(self, qtbot): qjsv = QJSValue(3) values = [ @@ -70,7 +75,6 @@ class TestPyPromise: for [value, test] in values: promise = PyPromise(create_async_func(value)) with qtbot.assertNotEmitted(promise.rejected, wait=1000): - print("Testing", value) with qtbot.waitSignal(promise.fulfilled, check_params_cb=test, timeout=2000): assert promise.state == "pending" assert promise.state == "fulfilled" @@ -84,30 +88,90 @@ class TestPyPromise: assert promise.state == "rejected" def test_fulfill(self, qtbot): - spy_fulfilled = Spy() - spy_rejected = Spy() + fulfilled = Spy() + rejected = Spy() promise = PyPromise(create_async_func(3)) - then_res = promise.then(spy_fulfilled, spy_rejected) + then_res = promise.then(fulfilled, rejected) # Check if the return value is the same promise (so we can chain then) - assert then_res == promise + assert that(then_res).does.equal(promise) # Check on our spy. with qtbot.waitSignal(promise.fulfilled, timeout=10000): pass - assert that(spy_fulfilled).was.called.once - assert that(spy_fulfilled).was.not_called.with_arguments(3) - assert that(spy_fulfilled).was.called.with_arguments_matching(check_promise_result(3)) - assert spy_rejected.was.not_called + assert that(fulfilled).was.called.once + assert that(fulfilled).was.NOT.called.with_arguments(3) + assert that(fulfilled).was.called.with_arguments_matching(check_promise_result(3)) + assert that(rejected).was.never.called def test_rejected(self, qtbot): - spy_fulfilled = Spy() - spy_rejected = Spy() + fulfilled = Spy() + rejected = Spy() promise = PyPromise(async_throw) - then_res = promise.then(spy_fulfilled, spy_rejected) + then_res = promise.then(fulfilled, rejected) # Check if the return value is the same promise (so we can chain then) - assert that(then_res).is_equal.to(promise) + assert that(then_res).does.equal(promise) # Check on our spies. with qtbot.waitSignal(promise.rejected, timeout=10000): pass - assert that(spy_rejected).was.called.once - assert that(spy_rejected).was.called.with_arguments("Exception('aaaa')") - assert that(spy_fulfilled).was.not_called + assert that(rejected).was.called.once + assert that(rejected).was.called.with_arguments("Exception('aaaa')") + assert that(fulfilled).has.never.been.called + + def test_chain_fulfill(self, qtbot): + convert = Spy(lambda v: v.toVariant()) + plus = Spy(lambda v: v + 1) + rejected = Spy() + promise = PyPromise(create_async_func(5)) + then_res = promise.then(convert, rejected).then(plus, rejected).then(plus, rejected).then(plus, rejected) + # Check if the return value is the same promise (so we can chain then) + assert that(then_res).does.equal(promise) + with qtbot.waitSignal(promise.fulfilled, timeout=10000): + pass + assert that(convert).was.called.once.with_arguments_matching(check_promise_result(5)) + assert that(rejected).was.never.called + assert that(plus).was.called.three.times + assert that(plus).was.called.once.with_exact_arguments(5) + assert that(plus).was.called.once.with_exact_arguments(6) + assert that(plus).was.called.once.with_exact_arguments(7) + + def test_chain_reject(self, qtbot): + fulfilled = Spy() + convert = Spy(lambda v: len(v)) + minus = Spy(lambda v: v - 1) + promise = PyPromise(async_throw) + then_res = promise.then(fulfilled, convert).then(fulfilled, minus).then(fulfilled, minus).then(fulfilled, minus) + # Check if the return value is the same promise (so we can chain then) + assert that(then_res).does.equal(promise) + with qtbot.waitSignal(promise.rejected, timeout=10000): + pass + assert that(fulfilled).was.never.called + assert that(convert).was.called.once.with_arguments_matching(check_promise_result("Exception('aaaa')")) + assert that(minus).was.called.three.times + assert that(minus).was.called.once.with_exact_arguments(17) + assert that(minus).was.called.once.with_exact_arguments(16) + assert that(minus).was.called.once.with_exact_arguments(15) + + def test_check_calls_upon(self): + promise = PyPromise(async_throw) + fulfilled = Spy() + rejected = Spy() + promise.then(fulfilled, rejected) + assert promise.calls_upon_fulfillment(fulfilled) + assert promise.calls_upon_rejection(rejected) + assert not promise.calls_upon_fulfillment(rejected) + assert not promise.calls_upon_rejection(fulfilled) + + def test_reject_in_fulfill(self, qtbot): + def fulfilled_throw(x): + raise Exception('noooo') + promise = PyPromise(create_async_func("3")) + fulfilled_throw = Spy(fulfilled_throw) + fulfilled = Spy() + rejected = Spy() + then_res = promise.then(fulfilled, rejected).then(fulfilled_throw, rejected).then(fulfilled, rejected).then(fulfilled, rejected) + # Check if the return value is the same promise (so we can chain then) + assert that(then_res).does.equal(promise) + with qtbot.waitSignal(promise.fulfilled, timeout=10000): + pass + assert that(fulfilled_throw).has.been.called.once + assert that(rejected).has.been.called.three.times + assert that(rejected).has.been.called.three.times.with_arguments("Exception('noooo')") \ No newline at end of file diff --git a/runtime-pyside6/tests/test_pyjs.py b/runtime-pyside6/tests/test_pyjs.py index 5742cfc..95c3cf6 100644 --- a/runtime-pyside6/tests/test_pyjs.py +++ b/runtime-pyside6/tests/test_pyjs.py @@ -21,7 +21,6 @@ from re import Pattern from PySide6.QtQml import QJSEngine, QJSValue from LogarithmPlotter.util.js import PyJSValue, InvalidAttributeValueException, NotAPrimitiveException -from globals import app @pytest.fixture() def data(): diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh index ac1fdc5..8c09c54 100644 --- a/scripts/run-tests.sh +++ b/scripts/run-tests.sh @@ -1,8 +1,20 @@ #!/bin/bash cd "$(dirname "$(readlink -f "$0" || realpath "$0")")/.." || exit 1 +box() { + len=${#1} + echo "┌─$(printf '─%.0s' $(seq 1 "$len"))─┐" + echo "│ $1 │" + echo "└─$(printf '─%.0s' $(seq 1 "$len"))─┘" +} + rebuild=true +cd runtime-pyside6/tests/plugins || exit 1 +box "Testing pytest natural plugins..." +PYTHONPATH="$PYTHONPATH:." pytest --cov=natural --cov-report term-missing . +cd ../../../ + while [ $# -gt 0 ]; do case "$1" in --no-rebuild) @@ -27,10 +39,12 @@ rm -rf build/runtime-pyside6/tests cp -r runtime-pyside6/tests build/runtime-pyside6 cp -r ci CHANGELOG.md build/runtime-pyside6 cd build/runtime-pyside6 || exit 1 +box "Testing runtime-pyside6..." PYTHONPATH="$PYTHONPATH:." pytest --cov=LogarithmPlotter --cov-report term-missing . cd ../../ # Run js tests cd common || exit 1 +box "Testing common..." npm test