การพัฒนาที่ขับเคลื่อนด้วยการทดสอบสำหรับ Fontlab Studio 5 Python Macros และโมดูล
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 API ที่ไม่เป็นทางการอันล้ำค่าและจากการเรียกใช้สคริปต์ใน Fontlab Studio และตรวจสอบสิ่งที่พวกเขาทำนอกเหนือจากการกระแทกแอปพลิเคชัน
เมื่อคุณเปิดใช้งานสภาพแวดล้อมเสมือนจริงของคุณ:
$ pip install -e .ฟลอริด้าสามารถนำเข้าด้านนอกของ 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 และโมดูล 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
ไฟล์ 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 ================================