|
11 | 11 | import pytest |
12 | 12 |
|
13 | 13 | from docx.opc.part import XmlPart |
14 | | -from docx.shared import ElementProxy |
| 14 | +from docx.shared import ElementProxy, Length, Cm, Emu, Inches, Mm, Pt, Twips |
15 | 15 |
|
16 | 16 | from .unitutil.cxml import element |
17 | 17 | from .unitutil.mock import instance_mock |
@@ -70,3 +70,46 @@ def other_proxy_(self, request): |
70 | 70 | @pytest.fixture |
71 | 71 | def part_(self, request): |
72 | 72 | return instance_mock(request, XmlPart) |
| 73 | + |
| 74 | + |
| 75 | +class DescribeLength(object): |
| 76 | + |
| 77 | + def it_can_construct_from_convenient_units(self, construct_fixture): |
| 78 | + UnitCls, units_val, emu = construct_fixture |
| 79 | + length = UnitCls(units_val) |
| 80 | + assert isinstance(length, Length) |
| 81 | + assert length == emu |
| 82 | + |
| 83 | + def it_can_self_convert_to_convenient_units(self, units_fixture): |
| 84 | + emu, units_prop_name, expected_length_in_units, type_ = units_fixture |
| 85 | + length = Length(emu) |
| 86 | + length_in_units = getattr(length, units_prop_name) |
| 87 | + assert length_in_units == expected_length_in_units |
| 88 | + assert isinstance(length_in_units, type_) |
| 89 | + |
| 90 | + # fixtures ------------------------------------------------------- |
| 91 | + |
| 92 | + @pytest.fixture(params=[ |
| 93 | + (Length, 914400, 914400), |
| 94 | + (Inches, 1.1, 1005840), |
| 95 | + (Cm, 2.53, 910799), |
| 96 | + (Emu, 9144.9, 9144), |
| 97 | + (Mm, 13.8, 496800), |
| 98 | + (Pt, 24.5, 311150), |
| 99 | + (Twips, 360, 228600), |
| 100 | + ]) |
| 101 | + def construct_fixture(self, request): |
| 102 | + UnitCls, units_val, emu = request.param |
| 103 | + return UnitCls, units_val, emu |
| 104 | + |
| 105 | + @pytest.fixture(params=[ |
| 106 | + (914400, 'inches', 1.0, float), |
| 107 | + (914400, 'cm', 2.54, float), |
| 108 | + (914400, 'emu', 914400, int), |
| 109 | + (914400, 'mm', 25.4, float), |
| 110 | + (914400, 'pt', 72.0, float), |
| 111 | + (914400, 'twips', 1440, int), |
| 112 | + ]) |
| 113 | + def units_fixture(self, request): |
| 114 | + emu, units_prop_name, expected_length_in_units, type_ = request.param |
| 115 | + return emu, units_prop_name, expected_length_in_units, type_ |
0 commit comments