def test_generate_with_children(self): element = Element("div") element.extend(["<foo>", "&"]) element.append_raw("<bar>") assert_equal( [b"<div>", b"<foo>", b"&", b"<bar>", b"</div>"], list(iter(element)))
def test_scripts(self): head = _TestingHead() doc = Document() doc.root.head = head doc.add_script("script.js") doc.add_scripts("script1.js", "script2.js") assert_equal(["script.js", "script1.js", "script2.js"], head.scripts)
def test_default_language(self): root = HTMLRoot() root.head = _TestingHead() assert_equal([b'<html lang="en" xml:lang="en" ' b'xmlns="http://www.w3.org/1999/xhtml">', b'<body>', b'</body>', b'</html>'], list(iter(root)))
def test_append(self): doc = Document() old_child_count = len(doc.root.head) doc.append_head("Test Head") assert_equal(old_child_count + 1, len(doc.root.head)) doc.append_body("Test Body") assert_equal(1, len(doc.root.body))
def test_remove_attribute(self): element = Element("div") element.remove_attribute("foo") element.set_attribute("foo", "bar") element.remove_attribute("foo") assert_is_none(element.get_attribute("foo")) assert_equal([b'<div>', b"</div>"], list(iter(element)))
def test_create_option__option_object(self): select = Select() option = select.create_option("Option Label", "test-value", selected=True) assert_equal("option", option.element_name) assert_equal("test-value", option.value) assert_true(option.selected)
def test_create_column(self): group = ColumnGroup() col = group.create_column() col.add_css_classes("col-cls") group.create_column() assert_equal('<colgroup><col class=\"col-cls\"></col><col></col>' '</colgroup>', str(group))
def test_time_with_fraction(self): class MyElement(Element): attr = time_html_attribute("data-time") element = MyElement("div") element.attr = datetime.time(14, 13, 9, 123456) assert_equal(datetime.time(14, 13, 9, 123456), element.attr) assert_equal('<div data-time="14:13:09.123456"></div>', str(element))
def test_attributes(self): number = NumberInput() number.minimum = 4.1 number.maximum = 10.5 number.step = 0.8 assert_equal('<input max="10.5" min="4.1" step="0.8" type="number"/>', str(number))
def test_defaults(self): time = TimeInput() assert_equal("", time.name) assert_is_none(time.time) assert_is_none(time.minimum) assert_is_none(time.maximum) assert_is_none(time.step) assert_equal('<input type="time"/>', str(time))
def test_generate_children(self): class TestingElement(NonVoidElement): def generate_children(self): yield "Hello World!" yield VoidElement("br") element = TestingElement("div") assert_equal([b"<div>", b"Hello World!", b"<br/>", b"</div>"], list(iter(element)))
def test_boolean_attributes(self): input_ = Input() input_.disabled = True input_.focus = True input_.readonly = True assert_equal([b'<input autofocus="autofocus" disabled="disabled" ' b'readonly="readonly" type="text"/>'], list(iter(input_)))
def test_attributes(self): input_ = Input() input_.placeholder = "Foo" input_.size = 5 input_.value = "My Value" assert_equal([b'<input placeholder="Foo" size="5" type="text" ' b'value="My Value"/>'], list(iter(input_)))
def test_stylesheets(self): head = _TestingHead() doc = Document() doc.root.head = head doc.add_stylesheet("style.css") doc.add_stylesheets("style1.css", "style2.css") assert_equal(["style.css", "style1.css", "style2.css"], head.stylesheets)
def test_append_extend(self): element = Element("div") element.append("Hello") element.extend([", ", "World", "!"]) assert_equal(4, len(element)) element.append_raw("Hello") element.extend_raw([", ", "World", "!"]) assert_equal(8, len(element))
def test_append(self): head = _TestingHead() doc = Document() doc.root.head = head doc.append_head("Test Head") assert_equal(1, len(head)) doc.append_body("Test Body") assert_equal(1, len(doc.root.body))
def test_add_multiple_css_classes(self): element = Element("div") element.add_css_classes("foo", "bar", "baz") element.add_css_classes("bar") matches = re.search(r'class="(.*)"', str(element)) css_classes = matches.group(1).split(" ") css_classes.sort() assert_equal(["bar", "baz", "foo"], css_classes)
def test_data(self): class MyElement(Element): attr = data_attribute("attr") element = MyElement("div") assert_is_none(element.get_attribute("data-attr")) element.attr = "foo" assert_equal("foo", element.get_attribute("data-attr")) element.set_attribute("data-attr", "bar") assert_equal("bar", element.attr)
def test_minimum_maximum(self): time = TimeInput() time.minimum = datetime.time(12, 14) time.maximum = datetime.time(19, 45) assert_equal('<input max="19:45:00" min="12:14:00" type="time"/>', str(time)) time.minimum = None time.maximum = None assert_equal('<input type="time"/>', str(time))
def test_create_option__selected(self): select = Select() option = select.create_option("Option Label", "test-value", selected=True) assert_is(option, select.selected_option) assert_equal( '<select><option selected="selected" value="test-value">' 'Option Label</option></select>', str(select))
def test_custom_title_element(self): head = Head() old_title = head.title old_child_count = len(head) new_title = Title() head.title = new_title assert_equal(old_child_count, len(head)) assert_is_not(old_title, head.title) assert_is(new_title, head.title)
def test_set_selected_value(self): select = Select() select.create_option("L1", "v1") option = select.create_option("L2", "v2") select.create_option("L3", "v3") select.selected_value = "v2" assert_equal("v2", select.selected_value) assert_is(option, select.selected_option) assert_true(option.selected)
def test_remove_css_classes(self): element = Element("div") element.add_css_classes("foo", "bar", "baz") element.remove_css_classes("bar", "xxx") matches = re.search(r'class="(.*)"', str(element)) assert matches is not None css_classes = matches.group(1).split(" ") css_classes.sort() assert_equal(["baz", "foo"], css_classes)
def test_children_order(self): table = Table() table.append_raw("<tr>Bare line</tr>") table.create_row() table.create_header_row() assert_equal([b"<table>", b"<thead>", b"<tr>", b"</tr>", b"</thead>", b"<tbody>", b"<tr>", b"</tr>", b"</tbody>", b"<tr>Bare line</tr>", b"</table>"], list(iter(table)))
def test_create_items(self): list_ = UnorderedList() items = list_.create_items("foo", "bar", "baz") assert_equal([b"<ul>", b"<li>", b"foo", b"</li>", b"<li>", b"bar", b"</li>", b"<li>", b"baz", b"</li>", b"</ul>"], list(iter(list_))) assert_equal(3, len(items))
def test_set_multiple_styles(self): element = Element("div") element.set_style("color", "black") element.set_style("background-color", "rgb(255, 0, 0)") element.set_style("display", "block") matches = re.search(r'style="(.*)"', str(element)) css_classes = matches.group(1).split("; ") css_classes.sort() assert_equal(["background-color: rgb(255, 0, 0)", "color: black", "display: block"], css_classes)
def test_data_external(self): element = Element("div") element.set_attribute("data-foo", "bar") assert_equal("bar", element.data["foo"]) element.data["xyz"] = "abc" assert_equal("abc", element.get_attribute("data-xyz")) element.data.clear() assert_is_none(element.get_attribute("data-foo")) element.set_attribute("data-old", "") element.data = {} assert_is_none(element.get_attribute("data-old"))
def test_create_head_and_body(self): table = Table() table.create_body() table.create_head() table.create_row() table.create_header_row() assert_equal([b"<table>", b"<thead>", b"<tr>", b"</tr>", b"</thead>", b"<tbody>", b"<tr>", b"</tr>", b"</tbody>", b"<tbody>", b"</tbody>", b"<thead>", b"</thead>", b"</table>"], list(iter(table)))
def test_attribute_order(self): """Test attribute order. The attributes are ordered alphabetically so that unit and doctests can rely on this order. """ element = Element("div") element.set_attribute("def", "") element.set_attribute("abc", "") element.set_attribute("ghi", "") assert_equal([b'<div abc="" def="" ghi="">', b"</div>"], list(iter(element)))
def test_title(self): doc = Document(title="Test Title") assert_equal("Test Title", doc.title) assert_equal("Test Title", doc.root.head.title.title) doc.title = "New Title" assert_equal("New Title", doc.title) assert_equal("New Title", doc.root.head.title.title)
def test_append_raw(self): generator = HTMLChildGenerator() generator.append_raw(u"c1&c2") generator.append_raw(_TestingGenerator([u"c3", u"<c4>"])) assert_equal([b"c1&c2", b"c3", b"<c4>"], list(iter(generator)))
def app(env: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]: assert_equal(expected_path, env["PATH_INFO"]) sr("200 OK", []) return []
def test_children(self): generator = ChildGenerator() generator.append("Foo") assert_equal(["Foo"], generator.children) generator.children.append("Bar") assert_equal(["Foo"], generator.children)
def test_wrap_string(self): generator = generate_html_string("Test") assert_is_instance(generator, HTMLChildGenerator) result = list(iter(generator)) assert_equal([b"Test"], result)
def handle_path(_: Request, paths: Sequence[Any], __: str) -> int: assert_equal(("xyz", ), paths) return 123
def test_len(self): generator = HTMLChildGenerator() generator.append(u"c1") generator.extend([u"c2", u"c3", NullGenerator()]) assert_equal(4, len(generator))
def test_children(self): generator = HTMLChildGenerator() generator.append("Foo") generator.append("<tag>") assert_equal(["Foo", "<tag>"], generator.children)
def check_changelog(context, item=None): """Check that the changelog contains the text.""" item_path = f"{item}/{context.uuid[item]}/" if item else "" response = context.get(f"changelog/{item_path}10") for index, line in enumerate(context.text.split("\n")): assert_equal(line, response["changelog"][index]["delta"])
from asserts import assert_true, assert_equal from unittest.mock import patch from io import StringIO with patch('sys.stdout', new=StringIO()) as fake_output: import index actual = fake_output.getvalue().strip() assert_equal(actual, 'What Is Dead May Never Die') print(actual)
def sub(environ: WSGIEnvironment, sr: StartResponse) -> WSGIResponse: assert_equal("/sub", environ["PATH_INFO"]) sr("204 No Content", []) return []
from asserts import assert_true, assert_equal from unittest.mock import patch from io import StringIO with patch("sys.stdout", new=StringIO()) as fake_output: import index actual = fake_output.getvalue().strip() assert_equal(actual, "49.0") print(actual)
def handle(environ: WSGIEnvironment, sr: StartResponse) -> Sequence[bytes]: assert_equal(expected_path, environ["PATH_INFO"]) sr("200 OK", []) return []
def tmpl(_: Request, path: Sequence[str], v: str) -> str: assert_equal((), path) return v * 2
def handle(environ: WSGIEnvironment, start_response: StartResponse) -> Iterable[bytes]: assert_equal(["xyzxyz"], environ["rouver.path_args"]) start_response("200 OK", []) return []
def test_extend_raw(self): generator = HTMLChildGenerator() generator.append(u"c1") generator.extend_raw([_TestingGenerator([u"c&2", u"c3"]), u"<c4>"]) assert_equal([b"c1", b"c&2", b"c3", b"<c4>"], list(iter(generator)))
def validate_price_dict(dict): assert_equal(_instrument, dict["instrument"]) assert_equal(_status, dict["status"]) assert_equal(__time, dict["time"]) assert_equal(_tradeable, dict["tradeable"]) assert_equal(_ask, dict["ask"]) assert_equal(_mid, dict["mid"]) assert_equal(_spread, dict["spread"]) assert_equal(_ask_liquidity, dict["ask_liquidity"]) assert_equal(_bid_liquidity, dict["bid_liquidity"]) assert_equal(_ask_closeout, dict["ask_closeout"]) assert_equal(_bid_closeout, dict["bid_closeout"])
def test_remove_raw(self): generator = HTMLChildGenerator() generator.extend_raw(["foo", "bar", "lower < than"]) generator.remove_raw("foo") generator.remove_raw("lower < than") assert_equal([b"bar"], list(iter(generator)))
def handle(environ: WSGIEnvironment, start_response: StartResponse) -> Iterable[bytes]: assert_equal(["value"], environ["rouver.path_args"]) assert_equal("/abc/def", environ["rouver.wildcard_path"]) start_response("200 OK", []) return [b""]
def test_empty(self): generator = HTMLChildGenerator() generator.append(u"c1") generator.extend([u"c2", u"c3", NullGenerator()]) generator.empty() assert_equal([], list(iter(generator)))
def handle_path(_: Request, __: Any, v: str) -> None: assert_equal("foo/bar", v)
def test_children_readonly(self): generator = HTMLChildGenerator() generator.append("Foo") generator.children.append("Bar") assert_equal(["Foo"], generator.children)
def handle_path(request: Request, paths: Sequence[Any], path: str) -> str: assert_is_instance(request, Request) assert_equal((), paths) return path * 2
def test_escape_string(self): generator = generate_html_string("<br>") assert_is_instance(generator, HTMLChildGenerator) result = list(iter(generator)) assert_equal([b"<br>"], result)
def check_json_error(context): """Check the json error.""" assert_equal(400, context.response.status_code) assert_equal("application/json", context.response.headers["Content-Type"])
def validate_price(price: Price): assert_equal(_instrument, price.instrument) assert_equal(_status, price.status) assert_equal(__time, price.time) assert_equal(_tradeable, price.tradeable) assert_equal(_ask, price.ask) assert_equal(_mid, price.mid) assert_equal(_spread, price.spread) assert_equal(_ask_liquidity, price.ask_liquidity) assert_equal(_bid_liquidity, price.bid_liquidity) assert_equal(_ask_closeout, price.ask_closeout) assert_equal(_bid_closeout, price.bid_closeout)
def handle(environ: WSGIEnvironment, start_response: StartResponse) -> Iterable[bytes]: assert_equal("test.example.com", environ["SERVER_NAME"]) start_response("200 OK", []) return [b""]
from asserts import assert_true, assert_equal from unittest.mock import patch from io import StringIO with patch("sys.stdout", new=StringIO()) as fake_output: import index actual = fake_output.getvalue().strip() assert_equal(actual, "4") print(actual)
def check_pdf(context): """Check the pdf.""" assert_equal("application/pdf", context.response.headers["Content-Type"])
def app(env: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]: assert_equal("/foo", env["PATH_INFO"]) assert_equal("/sub/foo", env["rouver.original_path_info"]) sr("200 OK", []) return []
def handle(environ: WSGIEnvironment, start_response: StartResponse) -> Iterable[bytes]: assert_equal("/", environ["rouver.wildcard_path"]) start_response("200 OK", []) return []