FontLab Studio 5 Pythonマクロとモジュールのテスト駆動型開発。
Fakelabは、PythonコードをテストするためのFontLab Studio 5の代替品です。
すべてがFontLab Studio 5以外でインポートできるようにし、テストを実行するためにのみ実装されています。
FAKELABを仮想環境にインストールすることをお勧めします。これにより、FontLabは実際のFontLab Studio 5でスクリプトを実行するときに偽のモジュールを誤ってインポートしません。Python2.7と3の間の非互換性のある人と一緒に暮らすことができれば、Python 3でFakelabを実行できます。
VFB形式は公開されていないため、VFBの保存はサポートされていませんが、JSONとしてFontオブジェクトを保存できます。
Fakelabの実装は、貴重な非公式のFontLab/Python APIリファレンスに基づいており、FontLab Studioでスクリプトを実行し、アプリケーションのクラッシュとは別に、それらが何をするかを確認します。
仮想環境をアクティブにしたとき:
$ pip install -e .FLは、FontLab Studioの外でインポートできます。
from FL import fl , Font
# Make an empty font
f = Font ()
# Add the font to the mock app
fl . Add ( f )
# Close the font
fl . Close ()仮想環境で実行されており、fontLabモジュールをインポートできるようにする必要がある場合は、仮想環境のサイトパッケージディレクトリに.pthファイルを追加します。
# fl5modules.pth
/Users/<yourname>/Code/FLMacros/System/Modules
Fakelabは進行中の作業であり、自分のスクリプトで必要な限り実装されています。私はそれらの大部分にまだテストがないことを認めなければなりません。
テストを書いているときにNotImplementedErrorに遭遇した場合、ここでこのプロジェクトの改善に役立つことができます。)プルリクエストを喜んで受け入れます。または、問題を開いて、私にコーヒーを買って、私の子供たちが私を放っておいて、あなたの問題に取り組む時間を見つけることができることを願っています。
自動テストなしでスクリプトを開発することは、実際には非常に小さなプロジェクトのみです。モジュールまたはスクリプトの結果を確認するには、常にテストを作成する必要があります。これは通常、pytestを使用して行われます。
コンポーネントを含むグリフを選択するためのfontlabスクリプトがあると仮定しましょう。 FontLab用の独自のツールコレクションがある場合、このスクリプトは、FontLabのマクロツールバーにリストされている1つのスクリプトと、ツールバースクリプトで呼び出されるロジックを実装する1つのPythonモジュールの2つの部分で構成されている場合があります。
Studio 5
+- Macros
+- Selection
+- Select Composites.py
+- System
+- Modules
+- fakeLabDemo
+- selection
+- __init__.py
+- composites.py
Select Composites.pyスクリプトは次のようになります:
#FLM: Select composites
# Studio 5/Macros/Selection/Select Composites.py
from fakeLabDemo . selection . composites import selectComposites
selectComposites ( fl . font )およびモジュール:
# Studio 5/Macros/System/Modules/fakeLabDemo/selection/composites.py
from __future__ import absolute_import , division , print_function
from FL import fl
def getFontIndex ( font ):
"""
Get the index of the supplied font.
We must iterate through the open fonts and compare file names,
because the == operator can not compare the font objects directly.
(FL font objects get a different id() each time they are called)
:param font: A title for the dialog.
:type font: :py:class:`FL.Font`
"""
for i in range ( len ( fl )):
cf = fl [ i ]
if cf . file_name == font . file_name :
if font . file_name is None :
if (
cf . family_name == font . family_name
and cf . style_name == font . style_name
):
return ( cf , i )
else :
return ( cf , i )
# Font was not found, probably there are no open fonts
return ( None , - 1 )
def setSelection ( font , glyph_names ):
"""
Set glyphs from the glyph_names list as selected in the font window.
"""
f , i = getFontIndex ( font )
if i > - 1 :
fl . ifont = i
fl . Unselect ()
for n in glyph_names :
fl . Select ( n )
def selectComposites ( font ):
"""
Select composites in font.
"""
setSelection (
font ,
[
glyph . name
for glyph in font . glyphs
if glyph . components
]
)このスクリプトがそれがすべきことをすることをどのように確認できますか? Pytestの場合、既存の構造に別の並列フォルダー構造を追加します。
Studio 5
+- Macros
+- Selection
+- Select Composites.py
+- System
+- Modules
+- fakeLabDemo
+- selection
+- __init__.py
+- composites.py
+- tests
+- fakeLabDemo
+- selection
+- composites_test.py
ファイルcomposites_test.pyは、それが関連するモジュールファイルに類似しているという名前で、テストを実装する場所です。
# Studio 5/Macros/System/Modules/tests/fakeLabDemo/selection/composites_test.py
import pytest
from FL import fl , Component , Font , Glyph , Point
from fakeLabDemo . selection . composites import selectComposites
def test_selectComposites ():
# Construct a fake FontLab font object
font = Font ()
g = Glyph ( 1 )
g . name = "A"
g . width = 500
g . unicode = 0x41
font . glyphs . append ( g )
g = Glyph ( 1 )
g . name = "dieresis"
g . width = 500
g . unicode = 0xA8
font . glyphs . append ( g )
g = Glyph ( 1 )
g . name = "Adieresis"
g . width = 500
g . unicode = 0xC4
g . components . append ( Component ( 0 ))
g . components . append ( Component ( 1 , Point ( 0 , 300 )))
font . glyphs . append ( g )
# Add the font to the FL object
fl . Add ( font )
fl . UpdateFont ()
# Run our script to be tested on the font
selectComposites ( fl . font )
# You could save the fake font to JSON instead of VFB.
# fl.font.Save("test_composites.vfb.json")
# Test if the correct glyphs have been selected
assert fl . Selected ( 0 ) == 0
assert fl . Selected ( 1 ) == 0
assert fl . Selected ( 2 ) == 1
# Close the fake font
fl . Close ()ご覧のとおり、fontlab内と同じようにオブジェクトを使用できます。既存のVFBからフォントを開くことはできません。これははるかに簡単です。ただし、VFBファイル形式は公開されていません。
代わりに、FL Python APIを使用してテストフォントを作成する必要があります。
仮想環境がアクティブになっている間に、端末ウィンドウでテストスクリプトを呼び出します。
cd " Studio 5/Macros/System/Modules "
python -m pytest tests/fakeLabDemo/selection/composites_test.pyすべてがうまくいくと、このような出力が表示されます。
============================ test session starts ===============================
platform darwin -- Python 3.8.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /Users/jens/Code/fakelab
collected 1 item
tests/fakeLabDemo/selection/composites_test.py . [100%]
============================= 1 passed in 0.02s ================================