|
| 1 | +""" |
| 2 | +test code for html_render.py |
| 3 | +
|
| 4 | +This is just a start -- you will need more tests! |
| 5 | +""" |
| 6 | + |
| 7 | +import io |
| 8 | +import pytest |
| 9 | + |
| 10 | +# import * is often bad form, but makes it easier to test everything in a module. |
| 11 | +from html_render import * |
| 12 | + |
| 13 | + |
| 14 | +# utility function for testing render methods |
| 15 | +# needs to be used in multiple tests, so we write it once here. |
| 16 | +def render_result(element, ind=""): |
| 17 | + """ |
| 18 | + calls the element's render method, and returns what got rendered as a |
| 19 | + string |
| 20 | + """ |
| 21 | + # the StringIO object is a "file-like" object -- something that |
| 22 | + # provides the methods of a file, but keeps everything in memory |
| 23 | + # so it can be used to test code that writes to a file, without |
| 24 | + # having to actually write to disk. |
| 25 | + outfile = io.StringIO() |
| 26 | + # this so the tests will work before we tackle indentation |
| 27 | + if ind: |
| 28 | + element.render(outfile, ind) |
| 29 | + else: |
| 30 | + element.render(outfile) |
| 31 | + return outfile.getvalue() |
| 32 | + |
| 33 | +######## |
| 34 | +# Step 1 |
| 35 | +######## |
| 36 | + |
| 37 | +def test_init(): |
| 38 | + """ |
| 39 | + This only tests that it can be initialized with and without |
| 40 | + some content -- but it's a start |
| 41 | + """ |
| 42 | + e = Element() |
| 43 | + |
| 44 | + e = Element("this is some text") |
| 45 | + |
| 46 | + |
| 47 | +def test_append(): |
| 48 | + """ |
| 49 | + This tests that you can append text |
| 50 | +
|
| 51 | + It doesn't test if it works -- |
| 52 | + that will be covered by the render test later |
| 53 | + """ |
| 54 | + e = Element("this is some text") |
| 55 | + e.append("some more text") |
| 56 | + |
| 57 | + |
| 58 | +def test_render_element(): |
| 59 | + """ |
| 60 | + Tests whether the Element can render two pieces of text |
| 61 | + So it is also testing that the append method works correctly. |
| 62 | +
|
| 63 | + It is not testing whether indentation or line feeds are correct. |
| 64 | + """ |
| 65 | + e = Element("this is some text") |
| 66 | + e.append("and this is some more text") |
| 67 | + |
| 68 | + # This uses the render_results utility above |
| 69 | + file_contents = render_result(e).strip() |
| 70 | + |
| 71 | + # making sure the content got in there. |
| 72 | + assert("this is some text") in file_contents |
| 73 | + assert("and this is some more text") in file_contents |
| 74 | + |
| 75 | + # make sure it's in the right order |
| 76 | + assert file_contents.index("this is") < file_contents.index("and this") |
| 77 | + |
| 78 | + # making sure the opening and closing tags are right. |
| 79 | + assert file_contents.startswith("<html>") |
| 80 | + assert file_contents.endswith("</html>") |
| 81 | + |
| 82 | +# # Uncomment this one after you get the one above to pass |
| 83 | +# # Does it pass right away? |
| 84 | +# def test_render_element2(): |
| 85 | +# """ |
| 86 | +# Tests whether the Element can render two pieces of text |
| 87 | +# So it is also testing that the append method works correctly. |
| 88 | + |
| 89 | +# It is not testing whether indentation or line feeds are correct. |
| 90 | +# """ |
| 91 | +# e = Element() |
| 92 | +# e.append("this is some text") |
| 93 | +# e.append("and this is some more text") |
| 94 | + |
| 95 | +# # This uses the render_results utility above |
| 96 | +# file_contents = render_result(e).strip() |
| 97 | + |
| 98 | +# # making sure the content got in there. |
| 99 | +# assert("this is some text") in file_contents |
| 100 | +# assert("and this is some more text") in file_contents |
| 101 | + |
| 102 | +# # make sure it's in the right order |
| 103 | +# assert file_contents.index("this is") < file_contents.index("and this") |
| 104 | + |
| 105 | +# # making sure the opening and closing tags are right. |
| 106 | +# assert file_contents.startswith("<html>") |
| 107 | +# assert file_contents.endswith("</html>") |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +# # ######## |
| 112 | +# # # Step 2 |
| 113 | +# # ######## |
| 114 | + |
| 115 | +# # tests for the new tags |
| 116 | +# def test_html(): |
| 117 | +# e = Html("this is some text") |
| 118 | +# e.append("and this is some more text") |
| 119 | + |
| 120 | +# file_contents = render_result(e).strip() |
| 121 | + |
| 122 | +# assert("this is some text") in file_contents |
| 123 | +# assert("and this is some more text") in file_contents |
| 124 | +# print(file_contents) |
| 125 | +# assert file_contents.endswith("</html>") |
| 126 | + |
| 127 | + |
| 128 | +# def test_body(): |
| 129 | +# e = Body("this is some text") |
| 130 | +# e.append("and this is some more text") |
| 131 | + |
| 132 | +# file_contents = render_result(e).strip() |
| 133 | + |
| 134 | +# assert("this is some text") in file_contents |
| 135 | +# assert("and this is some more text") in file_contents |
| 136 | + |
| 137 | +# assert file_contents.startswith("<body>") |
| 138 | +# assert file_contents.endswith("</body>") |
| 139 | + |
| 140 | + |
| 141 | +# def test_p(): |
| 142 | +# e = P("this is some text") |
| 143 | +# e.append("and this is some more text") |
| 144 | + |
| 145 | +# file_contents = render_result(e).strip() |
| 146 | + |
| 147 | +# assert("this is some text") in file_contents |
| 148 | +# assert("and this is some more text") in file_contents |
| 149 | + |
| 150 | +# assert file_contents.startswith("<p>") |
| 151 | +# assert file_contents.endswith("</p>") |
| 152 | + |
| 153 | + |
| 154 | +# def test_sub_element(): |
| 155 | +# """ |
| 156 | +# tests that you can add another element and still render properly |
| 157 | +# """ |
| 158 | +# page = Html() |
| 159 | +# page.append("some plain text.") |
| 160 | +# page.append(P("A simple paragraph of text")) |
| 161 | +# page.append("Some more plain text.") |
| 162 | + |
| 163 | +# file_contents = render_result(page) |
| 164 | +# print(file_contents) # so we can see it if the test fails |
| 165 | + |
| 166 | +# # note: The previous tests should make sure that the tags are getting |
| 167 | +# # properly rendered, so we don't need to test that here. |
| 168 | +# assert "some plain text" in file_contents |
| 169 | +# assert "A simple paragraph of text" in file_contents |
| 170 | +# assert "Some more plain text." in file_contents |
| 171 | +# assert "some plain text" in file_contents |
| 172 | +# # but make sure the embedded element's tags get rendered! |
| 173 | +# assert "<p>" in file_contents |
| 174 | +# assert "</p>" in file_contents |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +######## |
| 180 | +# Step 3 |
| 181 | +######## |
| 182 | + |
| 183 | +# Add your tests here! |
| 184 | + |
| 185 | +# ##################### |
| 186 | +# # indentation testing |
| 187 | +# # Uncomment for Step 9 -- adding indentation |
| 188 | +# ##################### |
| 189 | + |
| 190 | + |
| 191 | +# def test_indent(): |
| 192 | +# """ |
| 193 | +# Tests that the indentation gets passed through to the renderer |
| 194 | +# """ |
| 195 | +# html = Html("some content") |
| 196 | +# file_contents = render_result(html, ind=" ").rstrip() #remove the end newline |
| 197 | + |
| 198 | +# print(file_contents) |
| 199 | +# lines = file_contents.split("\n") |
| 200 | +# assert lines[0].startswith(" <") |
| 201 | +# print(repr(lines[-1])) |
| 202 | +# assert lines[-1].startswith(" <") |
| 203 | + |
| 204 | + |
| 205 | +# def test_indent_contents(): |
| 206 | +# """ |
| 207 | +# The contents in a element should be indented more than the tag |
| 208 | +# by the amount in the indent class attribute |
| 209 | +# """ |
| 210 | +# html = Element("some content") |
| 211 | +# file_contents = render_result(html, ind="") |
| 212 | + |
| 213 | +# print(file_contents) |
| 214 | +# lines = file_contents.split("\n") |
| 215 | +# assert lines[1].startswith(Element.indent) |
| 216 | + |
| 217 | + |
| 218 | +# def test_multiple_indent(): |
| 219 | +# """ |
| 220 | +# make sure multiple levels get indented fully |
| 221 | +# """ |
| 222 | +# body = Body() |
| 223 | +# body.append(P("some text")) |
| 224 | +# html = Html(body) |
| 225 | + |
| 226 | +# file_contents = render_result(html) |
| 227 | + |
| 228 | +# print(file_contents) |
| 229 | +# lines = file_contents.split("\n") |
| 230 | +# for i in range(3): # this needed to be adapted to the <DOCTYPE> tag |
| 231 | +# assert lines[i + 1].startswith(i * Element.indent + "<") |
| 232 | + |
| 233 | +# assert lines[4].startswith(3 * Element.indent + "some") |
| 234 | + |
| 235 | + |
| 236 | +# def test_element_indent1(): |
| 237 | +# """ |
| 238 | +# Tests whether the Element indents at least simple content |
| 239 | + |
| 240 | +# we are expecting to to look like this: |
| 241 | + |
| 242 | +# <html> |
| 243 | +# this is some text |
| 244 | +# <\html> |
| 245 | + |
| 246 | +# More complex indentation should be tested later. |
| 247 | +# """ |
| 248 | +# e = Element("this is some text") |
| 249 | + |
| 250 | +# # This uses the render_results utility above |
| 251 | +# file_contents = render_result(e).strip() |
| 252 | + |
| 253 | +# # making sure the content got in there. |
| 254 | +# assert("this is some text") in file_contents |
| 255 | + |
| 256 | +# # break into lines to check indentation |
| 257 | +# lines = file_contents.split('\n') |
| 258 | +# # making sure the opening and closing tags are right. |
| 259 | +# assert lines[0] == "<html>" |
| 260 | +# # this line should be indented by the amount specified |
| 261 | +# # by the class attribute: "indent" |
| 262 | +# assert lines[1].startswith(Element.indent + "thi") |
| 263 | +# assert lines[2] == "</html>" |
| 264 | +# assert file_contents.endswith("</html>") |
0 commit comments