1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
# Copyright (C) 2011-2012, Manfred Moitzi
# License: MIT License
import pytest
import ezdxf
from ezdxf.document import Drawing
from ezdxf.addons.tablepainter import (
TablePainter,
CustomCell,
Grid,
CellStyle,
DEFAULT_BORDER_COLOR,
)
@pytest.fixture(scope="module")
def doc() -> Drawing:
return ezdxf.new("R12")
class MockCell(CustomCell):
counter = 0
def render(self, layout, coords, layer):
MockCell.counter += 1
@staticmethod
def reset():
MockCell.counter = 0
def test_init():
table = TablePainter((0, 0), 10, 10, default_grid=False)
assert bool(table) is True
style = table.get_cell_style("default")
assert style.left.status is False
assert style.right.status is False
assert style.top.status is False
assert style.bottom.status is False
table = TablePainter((0, 0), 10, 10, default_grid=True)
style = table.get_cell_style("default")
assert style.left.status is True
assert style.right.status is True
assert style.top.status is True
assert style.bottom.status is True
def test_setter_methods():
table = TablePainter((0, 0), 10, 10)
table.set_col_width(0, 3.0)
assert table.col_widths[0] == 3.0
table.set_row_height(0, 4.0)
assert table.row_heights[0] == 4.0
def test_cell_index():
table = TablePainter((0, 0), 10, 10)
with pytest.raises(IndexError):
table.get_cell(10, 10)
with pytest.raises(IndexError):
table.get_cell(-1, 10)
def test_default_text_cell():
table = TablePainter((0, 0), 10, 10)
text_cell = table.text_cell(0, 0, "test")
assert text_cell.text == "test"
cell = table.get_cell(0, 0)
assert cell.span == (1, 1)
assert cell.stylename == "default"
def test_text_cell():
table = TablePainter((0, 0), 10, 10)
text_cell = table.text_cell(8, 8, "test88", span=(2, 2), style="extrastyle")
assert text_cell.text == "test88"
cell = table.get_cell(8, 8)
assert cell.span == (2, 2)
assert cell.stylename == "extrastyle"
def test_block_cell(doc):
block = doc.blocks.new("EMPTY_BLOCK")
table = TablePainter((0, 0), 10, 10)
block_cell = table.block_cell(1, 1, block, span=(3, 3))
assert block_cell.block_name == "EMPTY_BLOCK"
cell = table.get_cell(1, 1)
assert cell.span == (3, 3)
assert cell.stylename == "default"
def test_frame():
table = TablePainter((0, 0), 10, 10)
frame = table.frame(0, 0, width=10, height=2)
assert frame.pos == (0, 0)
assert frame.span == (2, 10)
def test_cell_style():
table = TablePainter((0, 0), 10, 10)
table.new_cell_style("extra", text_color=199)
style = table.get_cell_style("extra")
assert style.text_color == 199
with pytest.raises(KeyError):
table.get_cell_style("extraextra")
def test_border_style():
table = TablePainter((0, 0), 10, 10)
border_style = table.new_border_style(
color=1, status=True, linetype="DOT", priority=99
)
assert border_style.color == 1
assert border_style.status is True
assert border_style.linetype == "DOT"
assert border_style.priority == 99
def test_visibility_map():
from ezdxf.addons.tablepainter import VisibilityMap
table = TablePainter((0, 0), 3, 3)
text_cell = table.text_cell(0, 0, "text", span=(2, 2))
vmap = VisibilityMap(table)
empty = table.empty_cell
expected = [
(0, 0, text_cell),
(0, 2, empty), # cell (0, 1) is covered by (0,0)
(1, 2, empty), # cells (1, 0), (1, 2) are covered by cell (0, 0)
(2, 0, empty),
(2, 1, empty),
(2, 2, empty),
] # row 2
for got, should in zip(table.iter_visible_cells(vmap), expected):
assert got[0] == should[0] # row
assert got[1] == should[1] # col
assert got[2] == should[2] # cell
def test_rendering(doc):
MockCell.reset()
layout = doc.blocks.new("test_rendering")
table = TablePainter((0, 0), 3, 3)
indices = [
(0, 0),
(0, 1),
(0, 2),
(1, 0),
(1, 1),
(1, 2),
(2, 0),
(2, 1),
(2, 2),
]
cell = MockCell(table, "default", (1, 1))
for row, col in indices:
table.set_cell(row, col, cell)
table.render(layout)
assert cell.counter == 9 # count get_dxf_entity calls
def test_dxf_creation_span(doc):
MockCell.reset()
layout = doc.blocks.new("test_dxf_creation_span")
table = TablePainter((0, 0), 3, 3)
indices = [
(0, 0),
(0, 1),
(0, 2),
(1, 0),
(1, 1),
(1, 2),
(2, 0),
(2, 1),
(2, 2),
]
cell = MockCell(table, "default", (1, 1))
for row, col in indices:
table.set_cell(row, col, cell)
spancell = MockCell(table, "default", span=(2, 2)) # hides 3 cells
table.set_cell(0, 0, spancell)
table.render(layout)
assert cell.counter == 6 # count get_dxf_entity calls
def test_span_beyond_table_borders(doc):
layout = doc.blocks.new("test_span_beyond_table_borders")
table = TablePainter((0, 0), 3, 3)
table.text_cell(0, 2, "ERROR", span=(1, 2))
with pytest.raises(IndexError):
table.render(layout)
table.text_cell(2, 0, "ERROR", span=(2, 1))
with pytest.raises(IndexError):
table.render(layout)
@pytest.fixture
def table():
table = TablePainter((0, 0), 3, 3)
for x in range(3):
table.set_col_width(x, 3.0)
table.set_row_height(x, 3.0)
return table
def test_grid_coords(table):
grid = Grid(table)
left, right, top, bottom = grid.cell_coords(1, 1, span=(1, 1))
assert left == 3.0 # , places=4)
assert right == 6.0
assert top == -3.0
assert bottom == -6.0
def test_grid_coords_span(table):
grid = Grid(table)
left, right, top, bottom = grid.cell_coords(0, 0, span=(2, 2))
assert left == 0.0
assert right == 6.0
assert top == 0.0
assert bottom == -6.0
def test_draw_cell_background(doc, table):
grid = Grid(table)
layout = doc.blocks.new("test_draw_cell_background")
table.new_cell_style("fill", bg_color=17)
cell = table.get_cell(0, 0)
cell.stylename = "fill"
grid.render_cell_background(layout, 0, 0, cell)
solid = list(layout)[0]
assert solid.dxftype() == "SOLID"
def test_set_border_status():
style = CellStyle()
style.set_border_status(False, True, False, True)
assert style.left.status is False
assert style.right.status is True
assert style.top.status is False
assert style.bottom.status is True
def test_set_border_style():
style = CellStyle()
border_style = CellStyle.get_default_border_style()
border_style.color = 17
style.set_border_style(border_style, False, True, False, True)
assert style.left.color == DEFAULT_BORDER_COLOR
assert style.right.color == 17
assert style.top.color == DEFAULT_BORDER_COLOR
assert style.bottom.color == 17
class TestStyle:
def test_init_default_style(self):
style = CellStyle()
assert style.text_style == "STANDARD"
def test_init_custom_style(self):
style = CellStyle({"text_style": "Arial"})
assert style.text_style == "Arial"
def test_get_attribute_index_operator(self):
style = CellStyle()
assert style["text_style"] == style.text_style
def test_invalid_attribute_name_raises_key_error(self):
style = CellStyle()
with pytest.raises(KeyError): # get
_ = style["xxx"]
with pytest.raises(KeyError): # set
style["xxx"] = "xxx"
|