def test_size_7_xor(): assert Size(1) ^ "in" == "1in" assert Size("2in") ^ "pt" == "144pt" assert Size("30mm") ^ "cm" == "3cm" assert Size("72pt") ^ "mm" == "25.4mm" assert Size("25.4mm") ^ "px" == "96px"
def test_2_dim(): box = TextBox(x0=Size(1), y0=Size(2), width=Size(4), height=Size(8)) assert box.x1.value == 5 assert box.y1.value == 10 assert box.midx.value == 3 assert box.midy.value == 6
def test_7_xor(self): self.assertEqual(Size(1) ^ "in", "1in") self.assertEqual(Size("2in") ^ "pt", "144pt") self.assertEqual(Size("30mm") ^ "cm", "3cm") self.assertEqual(Size("72pt") ^ "mm", "25.4mm") self.assertEqual(Size("25.4mm") ^ "px", "96px")
def test_3_arith(self): s1 = Size("144pt") s2 = Size("72pt") s3 = s1 + s2 self.assertEqual(s3.value, 3.) s3 = s1 - s2 self.assertEqual(s3.value, 1.) s3 = s1 * 3 self.assertEqual(s3.value, 6.) s3 = 3 * s1 self.assertEqual(s3.value, 6.) s3 = s1 / 4 self.assertEqual(s3.value, 0.5) s3 = s1 + 1 self.assertEqual(s3.value, 3) s3 = 1 + s1 self.assertEqual(s3.value, 3) s3 = "72pt" + s1 self.assertEqual(s3.value, 3) s3 = s1 + "72pt" self.assertEqual(s3.value, 3) s3 = s1 - 1 self.assertEqual(s3.value, 1) s3 = 3 - s1 self.assertEqual(s3.value, 1) s3 = "216pt" - s1 self.assertEqual(s3.value, 1) s3 = s1 - "72pt" self.assertEqual(s3.value, 1)
def test_1_constr(): box = TextBox(x0=Size(1), y0=Size(2), width=Size(4), height=Size(8), text='abc') assert box.x0.value == 1 assert box.y0.value == 2 assert box.width.value == 4 assert box.height.value == 8 assert box.text == 'abc'
def test_size_4_meth(): s1 = Size("144pt") assert s1.pt == 144 assert s1.inches == 2 assert s1.mm == 25.4 * 2 assert s1.px == 96. * 2 s1 = Size("2in", 300) assert s1.pt == 144 assert s1.inches == 2 assert s1.mm == 25.4 * 2 assert s1.px == 300. * 2
def test_text(): emf = EMF(*_size) # it needs a font with emf.use_font(Size("16pt", _dpi)): text = "abcd" emf.text(Size("0pt", _dpi), Size("0pt", _dpi), text) data = emf.data() assert isinstance(data, type(b"")) assert len(data) == _header_bytes + _EOF_bytes + _use_font_bytes + \ _text_bytes(text)
def test_rectangle(): emf = EMF(*_size) # it needs a pen with emf.use_pen("solid", Size("1pt", _dpi), 0): emf.rectangle(Size("0pt", _dpi), Size("0pt", _dpi), Size("1000pt", _dpi), Size("1000pt", _dpi)) data = emf.data() assert isinstance(data, type(b"")) assert len(data) == _header_bytes + _EOF_bytes + _use_pen_bytes + \ _rect_bytes
def test_4_reflow(): box = TextBox(width='36pt', text='abcdefg ABCDEFG', font_size='10pt', line_spacing='3pt', padding='5pt') box.reflow() assert box.x0.value == 0. assert box.y0.value == 0. assert box.width.pt == 36 assert box.height.pt == 10 * 2 + 3 + 2 * 5 assert box.lines == ['abcdefg', 'ABCDEFG'] lines_pos = list(box.lines_pos()) assert lines_pos == [ ("abcdefg", (Size("18pt"), Size("15pt"))), ("ABCDEFG", (Size("18pt"), Size("28pt"))), ]
def test_size_5_copy(): s1 = Size("144pt") s2 = Size(s1 * 2) assert s2.value == 4 assert s2.dpi == 96. s2 = Size(s2 * 2, 150.) assert s2.value == 8 assert s2.dpi == 150. s2 = Size(s2 * 2) assert s2.value == 16 assert s2.dpi == 150. s2 = Size(s2 * 2, 300) assert s2.value == 32 assert s2.dpi == 300.
def test_use_font(): emf = EMF(*_size) with emf.use_font(Size("10pt", _dpi)) as font: assert font == 1 data = emf.data() assert isinstance(data, type(b"")) assert len(data) == _header_bytes + _EOF_bytes + _use_font_bytes
def test_use_pen(): emf = EMF(*_size) with emf.use_pen("solid", Size("1pt", _dpi), 0) as pen: assert pen == 1 data = emf.data() assert isinstance(data, type(b"")) assert len(data) == _header_bytes + _EOF_bytes + _use_pen_bytes
def test_size_1_val(): assert Size().value == 0. assert Size().dpi == 96. assert Size(1).value == 1. assert Size(1).dpi == 96. assert Size(1.).value == 1. assert Size(1., 300.).value == 1. assert Size(1., 300.).dpi == 300.
def test_polyline(): emf = EMF(*_size) # it needs a pen with emf.use_pen("solid", Size("1pt", _dpi), 0): emf.polyline([ (Size("1in", _dpi), Size("1in", _dpi)), (Size("1in", _dpi), Size("2in", _dpi)), (Size("2in", _dpi), Size("2in", _dpi)), (Size("2in", _dpi), Size("1in", _dpi)), ]) data = emf.data() assert isinstance(data, type(b"")) assert len(data) == _header_bytes + _EOF_bytes + _use_pen_bytes + \ _polyline_bytes(4)
def test_2_str(self): self.assertEqual(Size("1").value, 1.) self.assertEqual(Size("0.01").value, 0.01) self.assertEqual(Size("100").value, 100.) self.assertEqual(Size("72pt").value, 1.) self.assertEqual(Size("6.6pt").value, 6.6 / 72) self.assertEqual(Size("2.54cm").value, 1) self.assertEqual(Size("2.54mm").value, 0.1) self.assertEqual(Size("96px").value, 1.) self.assertRaises(TypeError, Size, ([])) self.assertRaises(ValueError, Size, ('12pf'))
def test_4_meth(self): s1 = Size("144pt") self.assertEqual(s1.pt, 144) self.assertEqual(s1.inches, 2)
"""Unit test for dumbemf module """ import logging import pytest from ged2doc.dumbemf import EMF from ged2doc.size import Size logging.basicConfig(level=logging.DEBUG) _dpi = 300. _size = Size("5in", _dpi), Size("3in", _dpi) _header_bytes = 124 _EOF_bytes = 5 * 4 _use_pen_bytes = 56 + 12 + 12 + 12 # EXTCREATEPEN + SELECTOBJECT + SELECTOBJECT + DELETEOBJECT _use_font_bytes = 104 + 12 + 12 + 12 # EXTCREATEFONTINDIRECTW + SELECTOBJECT + SELECTOBJECT + DELETEOBJECT _select_object_bytes = 3 * 4 _rect_bytes = 112 _text_align_bytes = 3 * 4 _text_color_bytes = 3 * 4 def _text_bytes(text): size = 19 * 4 size += len(text) * 2 if size % 4 != 0: size += 2 return size
def test_5_copy(self): s1 = Size("144pt") s2 = Size(s1 * 2) self.assertEqual(s2.value, 4)
def test_6_str(self): self.assertEqual(str(Size()), "0.0in") self.assertEqual(str(Size(2)), "2.0in") self.assertEqual(str(Size("1.5in")), "1.5in")
def test_size_3_arith(): s1 = Size("144pt", 100.) s2 = Size("72pt", 200.) s3 = s1 + s2 assert s3.value == 3. assert s3.dpi == 100. s3 = s2 + s1 assert s3.value == 3. assert s3.dpi == 200. s3 = s1 - s2 assert s3.value == 1. assert s3.dpi == 100. s3 = s2 - s1 assert s3.value == -1. assert s3.dpi == 200. s3 = s1 * 3 assert s3.value == 6. assert s3.dpi == 100. s3 = 3 * s1 assert s3.value == 6. assert s3.dpi == 100. s3 = s1 / 4 assert s3.value == 0.5 assert s3.dpi == 100. s3 = s1 + 1 assert s3.value == 3 assert s3.dpi == 100. s3 = 1 + s1 assert s3.value == 3 assert s3.dpi == 100. s3 = "72pt" + s1 assert s3.value == 3 assert s3.dpi == 100. s3 = s1 + "72pt" assert s3.value == 3 assert s3.dpi == 100. s3 = s1 + "100px" assert s3.value == 3 assert s3.dpi == 100. s3 = "100px" + s1 assert s3.value == 3 assert s3.dpi == 100. s3 = s1 - 1 assert s3.value == 1 assert s3.dpi == 100. s3 = 3 - s1 assert s3.value == 1 assert s3.dpi == 100. s3 = "216pt" - s1 assert s3.value == 1 assert s3.dpi == 100. s3 = s1 - "72pt" assert s3.value == 1 assert s3.dpi == 100. s3 = "300px" - s1 assert s3.value == 1 assert s3.dpi == 100. s3 = s1 - "100px" assert s3.value == 1 assert s3.dpi == 100. s3 = s1 * 5.25 assert s3.value == 10.5 assert s3.dpi == 100. s3 = s1 / 2.5 assert s3.value == 0.8 assert s3.dpi == 100. s3 = (s1 * 5.25) // 3 assert s3.value == 3. assert s3.dpi == 100.
def test_size_2_str(): assert Size("1").value == 1. assert Size("1").dpi == 96. assert Size("1", 300).dpi == 300. assert Size("0.01").value == 0.01 assert Size("100").value == 100. assert Size("72pt").value == 1. assert Size("6.6pt").value == 6.6 / 72 assert Size("2.54cm").value == 1 assert Size("2.54mm").value == 0.1 assert Size("96px").value == 1. assert Size("300px", 300).value == 1. with pytest.raises(TypeError): Size([]) with pytest.raises(ValueError): Size('12pf')
def test_size_8_cmp(): assert Size("1in") < Size("73pt") assert Size("1in") > Size("71pt") assert Size("1in") <= Size("72pt")
def test_8_cmp(self): self.assertLess(Size("1in"), Size("73pt")) self.assertGreater(Size("1in"), Size("71pt")) self.assertLessEqual(Size("1in"), Size("72pt"))
#!/usr/bin/env python """Trivial script to debug content of EMF, just in case some poor schmuck decides to repeat this futile exercise. """ from ged2doc.dumbemf import EMF from ged2doc.size import Size dpi = 300 width = Size("5in", dpi) height = Size("2in", dpi) emf = EMF(width, height) # emf.text_color(0) with emf.use_pen("solid", Size("1pt", dpi), 0x000000): emf.rectangle( Size("1in", dpi), Size(".5in", dpi), Size("4in", dpi), Size("1.5in", dpi), ) emf.text_align("c") fontsize = Size("16pt", dpi) with emf.use_font(fontsize): emf.text(Size("2.5in", dpi), Size("1.25in", dpi), "Test1 Проверка1") with open("test.emf", "wb") as out: out.write(emf.data())
def test_tree_node(): kw = dict(box_width=Size(2), max_box_width=Size(3), font_size="10pt", gen_dist="10pt") oneLineHeightPt = 10. + 2 * 4. # 4pt is default padding twoLineHeightPt = 2 * 10. + 1.5 + 2 * 4. # 4pt is default padding # single person, no parents person = MockIndividual(name=MockName(first="John", surname="Smith", maiden=None), mother=None, father=None, xref_id="@id0@") node = TreeNode(person, 0, motherNode=None, fatherNode=None, **kw) assert node.person is person assert node.mother is None assert node.father is None assert node.name == "John Smith" assert node.textbox.width == Size(2) assert node.textbox.height.pt == oneLineHeightPt assert node.subTreeHeight.pt == oneLineHeightPt assert node.textbox.x0 == Size() assert node.textbox.y0 == Size() # person, one parent mother = MockIndividual(name=MockName(first="Jane", surname="Smith", maiden="Huang"), mother=None, father=None, xref_id="@id1@") person = MockIndividual(name=MockName(first="John", surname="Smith", maiden=None), mother=mother, father=None, xref_id="@id0@") mother_node = TreeNode(mother, 1, motherNode=None, fatherNode=None, **kw) father_node = TreeNode(None, 1, motherNode=None, fatherNode=None, **kw) node = TreeNode(person, 0, motherNode=mother_node, fatherNode=father_node, **kw) assert node.person is person assert node.mother is mother_node assert node.mother.person is mother assert node.father is father_node assert node.father.person is None assert node.name == "John Smith" assert node.mother.name == "Jane Smith" assert node.subTreeHeight.pt == approx(2 * oneLineHeightPt + TreeNode._vpadding.pt) assert node.textbox.x0 == Size() assert node.textbox.midy.pt == approx(node.subTreeHeight.pt / 2) # person, two parents, father's name is very long mother = MockIndividual(name=MockName(first="Jane", surname="Smith", maiden="Huang"), mother=None, father=None, xref_id="@id1@") father = MockIndividual(name=MockName( first="King Huan Carlos TwentySecond", surname="Smith-and-sometimes-Ivanov", maiden=None), mother=None, father=None, xref_id="@id1@") person = MockIndividual(name=MockName(first="John", surname="Smith", maiden=None), mother=mother, father=None, xref_id="@id0@") mother_node = TreeNode(mother, 1, motherNode=None, fatherNode=None, **kw) father_node = TreeNode(father, 1, motherNode=None, fatherNode=None, **kw) node = TreeNode(person, 0, motherNode=mother_node, fatherNode=father_node, **kw) assert node.person is person assert node.mother is mother_node assert node.mother.person is mother assert node.father is father_node assert node.father.person is father assert node.name == "John Smith" assert node.mother.name == "Jane Smith" assert node.father.name == "King Huan Carlos TwentySecond Smith-and-sometimes-Ivanov" assert node.father.textbox.height.pt == approx(twoLineHeightPt) assert node.subTreeHeight.pt == approx(twoLineHeightPt + oneLineHeightPt + TreeNode._vpadding.pt) assert node.textbox.midy.pt == approx( (node.mother.textbox.midy.pt + node.father.textbox.midy.pt) / 2)
def test_size_6_str(): assert str(Size()) == "0.0in" assert str(Size(2)) == "2.0in" assert str(Size("1.5in")) == "1.5in"
def test_1_val(self): self.assertEqual(Size().value, 0.) self.assertEqual(Size(1).value, 1.) self.assertEqual(Size(1.).value, 1.)