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
|
import mozunit
import pytest
LINTER = "perfdocs"
testdata = [
{
"table_specifications": {
"title": ["not a string"],
"widths": [10, 10, 10, 10],
"header_rows": 1,
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": 2,
},
"error_msg": "TableBuilder attribute title must be a string.",
},
{
"table_specifications": {
"title": "I've got a lovely bunch of coconuts",
"widths": ("not", "a", "list"),
"header_rows": 1,
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": 2,
},
"error_msg": "TableBuilder attribute widths must be a list of integers.",
},
{
"table_specifications": {
"title": "There they are all standing in a row",
"widths": ["not an integer"],
"header_rows": 1,
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": 2,
},
"error_msg": "TableBuilder attribute widths must be a list of integers.",
},
{
"table_specifications": {
"title": "Big ones, small ones",
"widths": [10, 10, 10, 10],
"header_rows": "not an integer",
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": 2,
},
"error_msg": "TableBuilder attribute header_rows must be an integer.",
},
{
"table_specifications": {
"title": "Some as big as your head!",
"widths": [10, 10, 10, 10],
"header_rows": 1,
"headers": ("not", "a", "list"),
"indent": 2,
},
"error_msg": "TableBuilder attribute headers must be a two-dimensional list of strings.",
},
{
"table_specifications": {
"title": "(And bigger)",
"widths": [10, 10, 10, 10],
"header_rows": 1,
"headers": ["not", "two", "dimensional"],
"indent": 2,
},
"error_msg": "TableBuilder attribute headers must be a two-dimensional list of strings.",
},
{
"table_specifications": {
"title": "Give 'em a twist, a flick of the wrist'",
"widths": [10, 10, 10, 10],
"header_rows": 1,
"headers": [[1, 2, 3]],
"indent": 2,
},
"error_msg": "TableBuilder attribute headers must be a two-dimensional list of strings.",
},
{
"table_specifications": {
"title": "That's what the showman said!",
"widths": [10, 10, 10, 10],
"header_rows": 1,
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": "not an integer",
},
"error_msg": "TableBuilder attribute indent must be an integer.",
},
]
table_specifications = {
"title": "I've got a lovely bunch of coconuts",
"widths": [10, 10, 10],
"header_rows": 1,
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": 2,
}
@pytest.mark.parametrize("testdata", testdata)
def test_table_builder_invalid_attributes(testdata):
from perfdocs.doc_helpers import TableBuilder
table_specifications = testdata["table_specifications"]
error_msg = testdata["error_msg"]
with pytest.raises(TypeError) as error:
TableBuilder(
table_specifications["title"],
table_specifications["widths"],
table_specifications["header_rows"],
table_specifications["headers"],
table_specifications["indent"],
)
assert str(error.value) == error_msg
def test_table_builder_mismatched_columns():
from perfdocs.doc_helpers import MismatchedRowLengthsException, TableBuilder
table_specifications = {
"title": "I've got a lovely bunch of coconuts",
"widths": [10, 10, 10, 42],
"header_rows": 1,
"headers": [["Coconut 1", "Coconut 2", "Coconut 3"]],
"indent": 2,
}
with pytest.raises(MismatchedRowLengthsException) as error:
TableBuilder(
table_specifications["title"],
table_specifications["widths"],
table_specifications["header_rows"],
table_specifications["headers"],
table_specifications["indent"],
)
assert (
str(error.value)
== "Number of table headers must match number of column widths."
)
def test_table_builder_add_row_too_long():
from perfdocs.doc_helpers import MismatchedRowLengthsException, TableBuilder
table = TableBuilder(
table_specifications["title"],
table_specifications["widths"],
table_specifications["header_rows"],
table_specifications["headers"],
table_specifications["indent"],
)
with pytest.raises(MismatchedRowLengthsException) as error:
table.add_row([
"big ones",
"small ones",
"some as big as your head!",
"(and bigger)",
])
assert (
str(error.value)
== "Number of items in a row must must number of columns defined."
)
def test_table_builder_add_rows_type_error():
from perfdocs.doc_helpers import TableBuilder
table = TableBuilder(
table_specifications["title"],
table_specifications["widths"],
table_specifications["header_rows"],
table_specifications["headers"],
table_specifications["indent"],
)
with pytest.raises(TypeError) as error:
table.add_rows([
"big ones",
"small ones",
"some as big as your head!",
"(and bigger)",
])
assert str(error.value) == "add_rows() requires a two-dimensional list of strings."
def test_table_builder_validate():
from perfdocs.doc_helpers import TableBuilder
table = TableBuilder(
table_specifications["title"],
table_specifications["widths"],
table_specifications["header_rows"],
table_specifications["headers"],
table_specifications["indent"],
)
table.add_row(["big ones", "small ones", "some as big as your head!"])
table.add_row([
"Give 'em a twist",
"A flick of the wrist",
"That's what the showman said!",
])
table = table.finish_table()
print(table)
assert (
table == " .. list-table:: **I've got a lovely bunch of coconuts**\n"
" :widths: 10 10 10\n :header-rows: 1\n\n"
" * - **Coconut 1**\n - Coconut 2\n - Coconut 3\n"
" * - **big ones**\n - small ones\n - some as big as your head!\n"
" * - **Give 'em a twist**\n - A flick of the wrist\n"
" - That's what the showman said!\n\n"
)
if __name__ == "__main__":
mozunit.main()
|