التطوير الذي يحركه الاختبار لـ Fontlab Studio 5 وحدات وحدات Python.
Fakelab هو استبدال Fontlab Studio 5 لاختبار رمز Python.
يتم تنفيذ كل شيء فقط إلى حد جعل كائنات fontlab قابلة للاستيراد خارج Fontlab Studio 5 ، واختبارات التشغيل.
يُقترح تثبيت Fakelab في بيئة افتراضية حتى لا تستورد Fontlab عن طريق الخطأ الوحدة المزيفة عند تشغيل البرامج النصية في استوديو Fontlab الفعلي 5. إذا كنت تستطيع العيش مع غير التوافق بين Python 2.7 و 3 ، فيمكنك أيضًا تشغيل Fakelab في Python 3.
لا يتم دعم حفظ VFBS ، لأن تنسيق VFB ليس عامًا ، ولكن يمكنك حفظ كائنات Font مثل JSON.
يعتمد تنفيذ Fakelab على مرجع واجهة برمجة تطبيقات Fontlab/Python غير الرسمية التي لا تقدر بثمن ، ومن تشغيل البرامج النصية في Fontlab Studio والتحقق من ما يفعلونه ، بصرف النظر عن تعطل التطبيق.
عندما تنشط بيئتك الافتراضية:
$ pip install -e .ثم يكون FL قابلاً للاستيراد خارج استوديو Fontlab:
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 ، فقد يتكون هذا البرنامج النصي من جزأين: برنامج نصي واحد مدرج في شريط أدوات Macro Fontlab ، ووحدة Python واحدة تنفذ المنطق ، والذي يطلق عليه البرنامج النصي شريط الأدوات.
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
File 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 ================================