Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Remove dependency on Manim for tests
Disable tests which are not supported
  • Loading branch information
naveen521kk committed May 29, 2021
1 parent 8b12f0b commit 31dec56
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 239 deletions.
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ def delete_media_dir():
delete_media_dir()
CASES_DIR = Path(Path(__file__).parent, "cases").absolute()
FONT_DIR = Path(__file__).parent / "fonts"

# avoid fail due to env vars.
os.environ["PANGOCAIRO_BACKEND"] = ""
os.environ["FONTCONFIG_PATH"] = ""
106 changes: 0 additions & 106 deletions tests/_manim.py

This file was deleted.

20 changes: 20 additions & 0 deletions tests/test_font_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from manimpango import FontProperties


def test_init():
FontProperties()


def test_family_property():
desc = FontProperties()
assert desc.family is None
desc.family = "Roboto"
assert desc.family == "Roboto"


def test_size_property():
desc = FontProperties()
assert desc.size is None
desc.size = 20
assert desc.size == 20
88 changes: 88 additions & 0 deletions tests/test_font_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
import sys
from pathlib import Path

import pytest
from attr.exceptions import FrozenInstanceError

from manimpango import FontProperties, RegisterFont, Style, Variant, Weight, list_fonts

from .test_fonts import font_lists


def test_invalid_size():
with pytest.raises(ValueError):
FontProperties(size=0)


def test_font_properties_attributes():
fp = FontProperties(
family="Hello",
size=10,
style=Style.ITALIC,
variant=Variant.NORMAL,
weight=Weight.BOLD,
)
assert fp.family == "Hello"
assert fp.size == 10
assert fp.style == Style.ITALIC
assert fp.variant == Variant.NORMAL
assert fp.weight == Weight.BOLD


def test_Register_Font_wrapper_frozen():
a = RegisterFont(list(font_lists.keys())[0])
with pytest.raises(FrozenInstanceError):
a.family = ""
a.unregister()


def test_Register_Font():
a = RegisterFont(list(font_lists.keys())[1])
fonts = list_fonts()
assert a.family[0] in fonts
assert isinstance(a.family, list)
a.unregister()
# below one fails due to caching in Pango.
# Maybe we can disable it?
# assert a.family[0] not in fonts


@pytest.mark.skipif(sys.platform.startswith("linux"), reason="uses fc by default.")
def test_fc_in_Register_Font():
a = RegisterFont(list(font_lists.keys())[1], use_fontconfig=True)
fonts = list_fonts()
assert a.family is not None
assert list(font_lists.values())[1] not in fonts
assert a.family[0] not in fonts
a.unregister()


def test_fc_in_Register_Font_with_rendering(setup_fontconfig):
a = RegisterFont(list(font_lists.keys())[1], use_fontconfig=True)
fonts = list_fonts()
assert a.family is not None
assert a.family[0] in fonts
a.unregister()


def test_Register_Font_without_calculating_family():
a = RegisterFont(list(font_lists.keys())[1], calculate_family=False)
assert a.family is None
a.unregister()


@pytest.mark.parametrize("fontconfig", [True, False])
def test_Register_Font_invalid_font_raise(tmpdir, fontconfig):
tmpfile = Path(tmpdir) / "nice.ttf"
with tmpfile.open("w") as f:
f.write("test font")
with pytest.raises(RuntimeError):
RegisterFont(tmpfile, use_fontconfig=fontconfig)


def test_Register_Font_file_not_found(tmpdir):
with pytest.raises(FileNotFoundError):
RegisterFont(Path(tmpdir) / "test")
with pytest.raises(FileNotFoundError):
RegisterFont(Path(tmpdir))
57 changes: 30 additions & 27 deletions tests/test_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from pathlib import Path
from shutil import copyfile

import manim
import pytest

import manimpango
from manimpango.font_manager._register_font import (
fc_register_font,
fc_unregister_font,
register_font,
unregister_font,
)

from . import FONT_DIR
from ._manim import MarkupText

font_lists = {
(FONT_DIR / "AdobeVFPrototype.ttf").absolute(): "Adobe Variable Font Prototype",
Expand All @@ -23,24 +27,25 @@
def test_unicode_font_name(tmpdir):
final_font = str(Path(tmpdir, "庞门正.ttf").absolute())
copyfile(FONT_DIR / "AdobeVFPrototype.ttf", final_font)
assert manimpango.register_font(final_font)
assert manimpango.unregister_font(final_font)
assert register_font(final_font)
assert unregister_font(final_font)


@pytest.mark.parametrize("font_name", font_lists)
def test_register_font(font_name):
intial = manimpango.list_fonts()
assert manimpango.register_font(str(font_name)), "Invalid Font possibly."
assert register_font(str(font_name)), "Invalid Font possibly."
final = manimpango.list_fonts()
assert intial != final


@pytest.mark.parametrize("font_name", font_lists.values())
def test_warning(capfd, font_name):
print(font_name)
manim.Text("Testing", font=font_name)
captured = capfd.readouterr()
assert "Pango-WARNING **" not in captured.err, "Looks like pango raised a warning?"
# @pytest.mark.parametrize("font_name", font_lists.values())
# def test_warning(capfd, font_name):
# print(font_name)
# manim.Text("Testing", font=font_name)
# captured = capfd.readouterr()
# assert "Pango-WARNING **" not in captured.err,
# "Looks like pango raised a warning?"


@pytest.mark.skipif(
Expand All @@ -49,7 +54,7 @@ def test_warning(capfd, font_name):
@pytest.mark.parametrize("font_name", font_lists)
def test_unregister_font(font_name):
intial = manimpango.list_fonts()
assert manimpango.unregister_font(str(font_name)), "Failed to unregister the font"
assert unregister_font(str(font_name)), "Failed to unregister the font"
final = manimpango.list_fonts()
assert intial != final

Expand All @@ -59,8 +64,8 @@ def test_unregister_font(font_name):
)
@pytest.mark.parametrize("font_name", font_lists)
def test_register_and_unregister_font(font_name):
assert manimpango.register_font(str(font_name)), "Invalid Font possibly."
assert manimpango.unregister_font(str(font_name)), "Failed to unregister the font"
assert register_font(str(font_name)), "Invalid Font possibly."
assert unregister_font(str(font_name)), "Failed to unregister the font"


@pytest.mark.skipif(
Expand All @@ -69,17 +74,15 @@ def test_register_and_unregister_font(font_name):
@pytest.mark.parametrize("font_name", font_lists)
@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="always returns true")
def test_fail_just_unregister(font_name):
assert not manimpango.unregister_font(
str(font_name)
), "Failed to unregister the font"
assert not unregister_font(str(font_name)), "Failed to unregister the font"


@pytest.mark.skipif(
sys.platform.startswith("win32"), reason="unsupported api for win32"
)
@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="unsupported api for mac")
def test_unregister_linux():
assert manimpango.unregister_font("random")
assert unregister_font("random")


@pytest.mark.skipif(
Expand All @@ -89,34 +92,34 @@ def test_adding_dummy_font(tmpdir):
dummy = tmpdir / "font.ttf"
with open(dummy, "wb") as f:
f.write(b"dummy")
assert not manimpango.register_font(str(dummy)), "Registered a dummy font?"
assert not register_font(str(dummy)), "Registered a dummy font?"


def test_simple_fonts_render(tmpdir):
filename = str(Path(tmpdir) / "hello.svg")
MarkupText("Hello World", filename=filename)
assert Path(filename).exists()
# def test_simple_fonts_render(tmpdir):
# filename = str(Path(tmpdir) / "hello.svg")
# MarkupText("Hello World", filename=filename)
# assert Path(filename).exists()


@pytest.mark.skipif(
not sys.platform.startswith("linux"), reason="unsupported api other than linux"
)
def test_both_fc_and_register_font_are_same():
assert manimpango.fc_register_font == manimpango.register_font
assert manimpango.fc_unregister_font == manimpango.unregister_font
assert fc_register_font == register_font
assert fc_unregister_font == unregister_font


@pytest.mark.parametrize("font_file", font_lists)
def test_fc_font_register(setup_fontconfig, font_file):
intial = manimpango.list_fonts()
assert manimpango.fc_register_font(str(font_file)), "Invalid Font possibly."
assert fc_register_font(str(font_file)), "Invalid Font possibly."
final = manimpango.list_fonts()
assert intial != final


def test_fc_font_unregister(setup_fontconfig):
# it will remove everything
intial = manimpango.list_fonts()
manimpango.fc_unregister_font("clear")
fc_unregister_font("clear")
final = manimpango.list_fonts()
assert intial != final
5 changes: 3 additions & 2 deletions tests/test_list_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

import manimpango
from manimpango.font_manager._register_font import register_font, unregister_font

from .test_fonts import font_lists

Expand All @@ -20,7 +21,7 @@ def test_whether_list():
)
@pytest.mark.parametrize("font_file", font_lists)
def test_resgister_font_with_list(font_file):
manimpango.register_font(str(font_file))
register_font(str(font_file))
a = manimpango.list_fonts()
assert font_lists[font_file] in a
manimpango.unregister_font(str(font_file))
unregister_font(str(font_file))
Loading

0 comments on commit 31dec56

Please sign in to comment.