Skip to content

Commit

Permalink
Add first test suite and make sure renpy is not needed to run it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Drachenfels committed May 7, 2024
1 parent 1eeaa70 commit c9a917a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Empty file added tests/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import builtins
from unittest.mock import MagicMock, patch

import pytest


@pytest.fixture(autouse=True)
def mock_renpy(monkeypatch):
"""This trick overrides for time of testing need to have renpy in the sys.path.
Rationale is that we want to test NQTR library and and renpy usually is installed
globally somewhere else in the OS.
"""
import_orig = builtins.__import__

def mocked_import(name, *args, **kwargs):
if name in ("renpy.exports", "renpy.character", "renpy"):
return MagicMock()
return import_orig(name, *args, **kwargs)

monkeypatch.setattr(builtins, "__import__", mocked_import)
67 changes: 67 additions & 0 deletions tests/test_nqtr_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest


def test_button():
"""Base tests, to prove that default button state is one we expect"""
from pythonpackages.nqtr.button import Button

some_button = Button()

assert some_button.align == (0, 0)
assert some_button.name == ""
assert some_button.label_name is None
assert some_button.button_icon is None
assert some_button.button_icon_selected is None
assert some_button.picture_in_background is None
assert some_button.picture_in_background_selected is None
assert some_button.xalign == 0
assert some_button.yalign == 0
assert some_button.hidden is False
assert some_button.default_label_name is None


def test_align_not_working_as_expected():
"""Previously this test was showcasing problem with align"""
from pythonpackages.nqtr.button import Button

some_button = Button()

assert type(some_button.align) is tuple
assert some_button.align == (0, 0)

some_button.xalign = 0

assert type(some_button.align) is tuple
assert some_button.align == (0, 0)


@pytest.mark.parametrize(
"xalign,yalign,xexpected,yexpected",
[
(0, 0, 0, 0),
(1, 1, 1, 1),
(-1, -1, -1, -1),
(1.1, 1.2, 1.1, 1.2),
(-1.1, -1.2, -1.1, -1.2),
("", "", 0, 0),
(False, False, 0, 0),
(None, None, 0, 0),
({}, {}, 0, 0),
("0", "0", "0", "0"),
],
)
def test_button_aligns_accept_any_param_except_none(
xalign, yalign, xexpected, yexpected
):
"""Check if various xalign, yalign attributes are properly saved to the class, all
Falsey values should be converted to 0 (special caveat for "0", it's not False-y,
but some may thing it is)
"""
from pythonpackages.nqtr.button import Button

some_button = Button(xalign=xalign, yalign=yalign)

assert some_button.xalign == xexpected
assert some_button.yalign == yexpected
assert type(some_button.xalign) is type(xexpected)
assert type(some_button.yalign) is type(yexpected)

0 comments on commit c9a917a

Please sign in to comment.