from __future__ import absolute_import
# Copyright (c) 2010-2017 openpyxl
import pytest
[docs]class DummyWorkbook:
encoding = "utf-8"
def __init__(self):
self.sheetnames = ["Sheet 1"]
@pytest.fixture
[docs]def WorkbookChild():
from .. child import _WorkbookChild
return _WorkbookChild
s = r'[\\*?:/\[\]]'
@pytest.mark.parametrize("value",
[
"Title:",
"title?",
"title/",
"title[",
"title]",
r"title\\",
"title*",
]
)
[docs]def test_invalid_chars(value):
from ..child import INVALID_TITLE_REGEX
assert INVALID_TITLE_REGEX.search(value)
@pytest.mark.parametrize("names, value, result",
[
([], "Sheet", "Sheet"),
(["Sheet2"], "Sheet2", "Sheet21"), # suggestions are stupid
([u"R\xf3g"], u"R\xf3g", u"R\xf3g1"),
(["Sheet", "Sheet1"], 'Sheet', 'Sheet2'),
(["Regex Test ("], "Regex Test (", "Regex Test (1"),
(["Foo", "Baz", "Sheet2", "Sheet3", "Bar", "Sheet4", "Sheet6"], "Sheet", "Sheet"),
(["Foo"], "FOO", "FOO1"),
]
)
[docs]def test_duplicate_title(names, value, result):
from ..child import avoid_duplicate_name
title = avoid_duplicate_name(names, value)
assert title == result
[docs]class TestWorkbookChild:
[docs] def test_ctor(self, WorkbookChild):
wb = DummyWorkbook()
child = WorkbookChild(wb)
assert child.parent == wb
assert child.encoding == "utf-8"
assert child.title == "Sheet"
[docs] def test_repr(self, WorkbookChild):
wb = DummyWorkbook()
child = WorkbookChild(wb)
assert repr(child) == '<_WorkbookChild "Sheet">'
[docs] def test_invalid_title(self, WorkbookChild):
wb = DummyWorkbook()
child = WorkbookChild(wb)
with pytest.raises(ValueError):
child.title = "title?"
[docs] def test_reassign_title(self, WorkbookChild):
wb = DummyWorkbook()
child = WorkbookChild(wb, "Sheet")
assert child.title == "Sheet"
[docs] def test_title_too_long(self, WorkbookChild, recwarn):
WorkbookChild(DummyWorkbook(), 'X' * 50)
w = recwarn.pop()
assert w.category == UserWarning
[docs] def test_set_encoded_title(self, WorkbookChild):
with pytest.raises(ValueError):
WorkbookChild(DummyWorkbook(), b'B\xc3\xbcro')
[docs] def test_empty_title(self, WorkbookChild):
child = WorkbookChild(DummyWorkbook())
with pytest.raises(ValueError):
child.title = ""