LogarithmPlotter/tests/python/test_pyjs.py

59 lines
2.1 KiB
Python
Raw Normal View History

"""
* 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 <https://www.gnu.org/licenses/>.
"""
2024-09-17 22:31:17 +00:00
import pytest
from PySide6.QtQml import QJSEngine, QJSValue
from LogarithmPlotter.util.js import PyJSValue, InvalidAttributeValueException
from globals import app
2024-09-17 22:31:17 +00:00
@pytest.fixture()
def data():
engine = QJSEngine()
obj = PyJSValue(engine.globalObject())
yield engine, obj
2024-09-17 22:31:17 +00:00
class TestPyJS:
def test_set(self, data):
engine, obj = data
2024-09-17 22:31:17 +00:00
obj.num1 = 2
obj.num2 = QJSValue(2)
2024-09-18 16:01:51 +00:00
obj.num3 = PyJSValue(QJSValue(2))
2024-09-17 22:31:17 +00:00
with pytest.raises(InvalidAttributeValueException):
obj.num3 = object()
def test_eq(self, data):
engine, obj = data
2024-09-17 22:31:17 +00:00
obj.num = QJSValue(2)
assert obj.num == 2
assert obj.num == QJSValue(2)
2024-09-18 16:01:51 +00:00
assert obj.num == PyJSValue(QJSValue(2))
2024-09-17 22:31:17 +00:00
assert obj.num != object()
def test_function(self, data):
engine, obj = data
2024-09-17 22:31:17 +00:00
function = PyJSValue(engine.evaluate("(function(argument) {return argument*2})"))
assert function(3) == 6
assert function(10) == 20
function2 = PyJSValue(engine.evaluate("(function(argument) {return argument+3})"), obj.qjs_value)
assert function2(3) == 6
assert function2(10) == 13
function3 = PyJSValue(engine.evaluate("2+2"))
with pytest.raises(InvalidAttributeValueException):
function3()