def test_filter_want(): def filter1(tagname, attributes, contents, context, bind): attributes[u'donut'] = u'xyzzy' return contents filter1.tags = [u'div', u'input'] for tag in u'div', u'input': context = Context() context[u'auto_filter'] = True context[u'filters'] = [filter1] given = {} expected = { u'donut': u'xyzzy', } assert_bound_transform(generic.transform_filters, tag, given, expected, context=context) context = Context() context[u'auto_filter'] = True context[u'filters'] = [filter1] given = {} expected = {} assert_bound_transform(generic.transform_filters, u'horse', given, expected, context=context)
def test_update_unknown(): ctx = Context() assert u'xyzzy' not in _default_context.keys() with pytest.raises(KeyError): ctx.update(xyzzy=123) assert u'xyzzy' not in ctx
def test_read_write_unknown(): ctx = Context() needle = 'xyzzy' assert needle not in _default_context.keys() assert needle not in ctx with pytest.raises(KeyError): ctx[needle]() with pytest.raises(KeyError): ctx.__setitem__(needle, Nothing)
def test_filter(): given = {} def filter1(tagname, attributes, contents, context, bind): attributes[u'donut'] = u'xyzzy' return contents context = Context() context[u'auto_filter'] = True context[u'filters'] = [filter1] expected = { u'donut': u'xyzzy', } assert_bound_transform(generic.transform_filters, u'div', given, expected, context=context) def filter2(tagname, attributes, contents, context, bind): return bind.label context = Context() context[u'auto_filter'] = True context[u'filters'] = [filter2, filter1] contents = u'bbq' expected_contents = u'number' expected = { u'donut': u'xyzzy', } assert_bound_transform(generic.transform_filters, u'div', given, expected, context=context, contents=contents, expected_contents=expected_contents) context = Context() context[u'auto_filter'] = True context[u'filters'] = () contents = u'bbq' expected_contents = u'bbq' expected = {} assert_bound_transform(generic.transform_filters, u'div', given, expected, context=context, contents=contents, expected_contents=expected_contents)
def test_push_unknown(): ctx = Context() needle = 'xyzzy' assert needle not in _default_context.keys() with pytest.raises(KeyError): ctx.push(**{needle: Nothing}) with pytest.raises(RuntimeError): ctx.pop()
def test_push_known(): ctx = Context() needle = list(_default_context.keys())[0] assert needle in ctx assert ctx[needle] is not Nothing ctx.push(**{needle: Nothing}) assert ctx[needle] is Nothing ctx.pop() assert ctx[needle] is not Nothing
def __init__(self, markup='xhtml', **options): Context.__init__(self) if markup == 'html': self.xml = False elif markup in ('xhtml', 'xml'): self.xml = True else: raise TypeError("Unknown markup type %r" % markup) self._tags = defaultdict(list) self._frames[-1].update(_default_options) self.push() self.update(options)
def test_push_known(): ctx = Context() needle = _default_context.keys()[0] assert needle in ctx assert ctx[needle] is not Nothing ctx.push(**{needle.encode('ascii'): Nothing}) assert ctx[needle] is Nothing ctx.pop() assert ctx[needle] is not Nothing
def test_stack_plain_push_pop(): ctx = Context() needle, initial_value = _default_context.items()[0] assert ctx[needle] == initial_value ctx.push() assert ctx[needle] == initial_value ctx[needle] = Nothing assert ctx[needle] is Nothing ctx.pop() assert ctx[needle] is not Nothing assert ctx[needle] == initial_value assert_raises(RuntimeError, ctx.pop)
def test_domid_custom_formatting(): context = Context() context[u'domid_format'] = u'--%s--' given = {u'auto_domid': u'on'} expected = {u'id': u'--number--'} assert_bound_transform(generic.transform_domid, u'input', given, expected, context=context)
def test_push_unknown(): ctx = Context() needle = 'xyzzy' assert needle not in _default_context.keys() assert_raises(KeyError, ctx.push, **{needle: Nothing}) assert_raises(RuntimeError, ctx.pop)
def test_read_write_known(): ctx = Context() needle = _default_context.keys()[0] assert needle in ctx assert ctx[needle] is not Nothing ctx[needle] = Nothing assert ctx[needle] is Nothing
def test_read_write_unknown(): ctx = Context() needle = u'xyzzy' assert needle not in _default_context.keys() assert needle not in ctx assert_raises(KeyError, lambda: ctx[needle]) assert_raises(KeyError, ctx.__setitem__, needle, Nothing)
def process(self, stream, directives, ctxt, vars): try: render_context = ctxt['flatland_render_context'] except KeyError: ctxt['flatland_render_context'] = render_context = Context() render_context.update(self.attributes) assert not directives return stream
def test_push_unknown(): ctx = Context() needle = u'xyzzy' needle_attribute = 'xyzzy' # native text type assert needle not in _default_context.keys() assert_raises(KeyError, ctx.push, **{needle_attribute: Nothing}) assert_raises(RuntimeError, ctx.pop)
def test_tabindex_zero(): given = {} context = Context() context[u'auto_tabindex'] = True context[u'tabindex'] = 0 expected = {} assert_unbound_transform(generic.transform_tabindex, u'input', given, expected, context=context)
def _rewrite_stream(stream, directives, ctxt, vars, bind): stream = list(stream) mutable_attrs = {} for control_attribute in directives: control_attribute.inject(mutable_attrs, ctxt, vars) kind, (tagname, attrs), pos = stream[0] if len(stream) == 2: contents = None else: contents = _simplify_stream(stream[1:-1], ctxt, vars) existing_attributes = {} for qname, value in attrs: if qname.namespace is None: if not isinstance(value, unicode): value = _simplify_stream(value, ctxt, vars) attrs |= ((qname, value), ) existing_attributes[qname.localname] = qname mutable_attrs[qname.localname] = value try: render_context = ctxt['flatland_render_context'] except KeyError: ctxt['flatland_render_context'] = render_context = Context() new_contents = transform(tagname.localname, mutable_attrs, contents, render_context, bind) if new_contents is None: new_contents = () elif isinstance(new_contents, unicode): new_contents = [(TEXT, new_contents, (None, -1, -1))] pairs = sorted(mutable_attrs.iteritems(), key=_attribute_sort_key) for attribute_name, value in pairs: if attribute_name in existing_attributes: qname = existing_attributes.pop(attribute_name) else: qname = QName(attribute_name) attrs |= ((qname, value), ) for qname in existing_attributes.values(): attrs -= qname stream[0] = (kind, (tagname, attrs), pos) if new_contents and tagname.localname == u'select' and bind is not None: if tagname.namespace: sub_tag = Namespace(tagname.namespace).option else: # pragma: nocover sub_tag = QName('option') new_contents = _bind_unbound_tags(new_contents, sub_tag, bind) if new_contents: stream[1:-1] = new_contents return iter(stream)
def test_domid_checkable_array(): bind = partial_anon_schema([123, 456]) context = Context() context[u'auto_domid'] = True assert_ = lambda given, expected: assert_transform(generic.transform_domid, u'input', given, expected, context=context, bind=bind) for type in u'radio', u'checkbox': given = { u'type': u'radio', u'value': u'xxx', } expected = { u'type': u'radio', u'value': u'xxx', u'id': u'f_array_xxx', } assert_(given, expected) given = { u'type': u'radio', } expected = { u'type': u'radio', u'id': u'f_array', } assert_(given, expected) given = { u'type': u'radio', u'value': u'Ḑộộḏ!', } expected = { u'type': u'radio', u'value': u'Ḑộộḏ!', u'id': u'f_array', } assert_(given, expected) given = { u'type': u'radio', u'value': u'Ḑộộḏ! yo', } expected = { u'type': u'radio', u'value': u'Ḑộộḏ! yo', u'id': u'f_array_yo', } assert_(given, expected)
def test_tabindex_stop_numbers(): given = {u'auto_tabindex': u'on'} for stop_num in -1, -2: context = Context() context[u'tabindex'] = stop_num expected = {u'tabindex': unicode(stop_num)} assert_bound_transform(generic.transform_tabindex, u'input', given, expected, context=context) assert_unbound_transform(generic.transform_tabindex, u'input', given, expected, context=context) assert context[u'tabindex'] == stop_num
def __init__(self, markup='xhtml', **settings): """Create a generator. Accepts any :ref:`markupsettings`, as well as the following: :param markup: tag output style: 'xml', 'xhtml' or 'html' :param ordered_attributes: if True (default), output markup attributes in a predictable order. Useful for tests and generally a little more pleasant to read. """ Context.__init__(self) if markup == 'html': self.xml = False elif markup in ('xhtml', 'xml'): self.xml = True else: raise TypeError("Unknown markup type %r" % markup) self._tags = defaultdict(list) self._frames[-1].update(_default_settings) self.push() self.update(settings)
def test_update_known(): ctx = Context() known = _default_context.keys() sentinels = [object(), object()] iterable = [(known[0], sentinels[0])] kwargs = {known[1].encode('ascii'): sentinels[1]} ctx.update(iterable, **kwargs) assert ctx[known[0]] is sentinels[0] assert ctx[known[1]] is sentinels[1] ctx = Context() ctx.update(iterable) assert ctx[known[0]] is sentinels[0] ctx = Context() ctx.update(**kwargs) assert ctx[known[1]] is sentinels[1]
def test_tabindex_increment(): given = {} context = Context() context[u'auto_tabindex'] = True context[u'tabindex'] = 1 expected = {u'tabindex': u'1'} assert_unbound_transform(generic.transform_tabindex, u'input', given, expected, context=context) expected = {u'tabindex': u'2'} assert_unbound_transform(generic.transform_tabindex, u'input', given, expected, context=context) assert context[u'tabindex'] != 1
def test_domid_checkable_scalar(): context = Context() context[u'auto_domid'] = True for type in 'radio', 'checkbox': given = { u'type': u'radio', u'value': u'xxx', } expected = { u'type': u'radio', u'value': u'xxx', u'id': u'f_number', } assert_bound_transform(generic.transform_domid, u'input', given, expected, context=context)
def assert_transform(fn, tagname, given, expected, context=Unspecified, bind=Unspecified, contents=Unspecified, expected_contents=Unspecified): if context is Unspecified: context = Context() if bind is Unspecified: bind = schema(123) got = given.copy() got_contents = fn(tagname, got, contents, context, bind) assert got == expected assert got_contents == expected_contents types = zip(sorted(got.keys()), sorted(expected.keys())) for got_key, expected_key in types: assert type(got_key) is type(expected_key)
def process(self, stream, directives, ctxt, vars): try: render_context = ctxt['flatland_render_context'] except KeyError: ctxt['flatland_render_context'] = render_context = Context() if 'filters' not in self.attributes: attrs = self.attributes else: attrs = self.attributes.copy() attrs['filters'] = _eval_expr(Expression(attrs['filters']), ctxt, vars) render_context.push() render_context.update(attrs) assert not directives for event in stream: yield event render_context.pop()
def _context(self, overrides=()): context = Context() context.push() context.update(self.base_context) context.update(overrides) return context
def test_default_minimum_stack(): ctx = Context() with pytest.raises(RuntimeError): ctx.pop()
def test_update_bogus(): ctx = Context() with pytest.raises(TypeError): ctx.update([], [])
def test_update_unknown(): ctx = Context() assert u'xyzzy' not in _default_context.keys() assert_raises(KeyError, ctx.update, xyzzy=123) assert u'xyzzy' not in ctx
def test_default_minimum_stack(): ctx = Context() assert_raises(RuntimeError, ctx.pop)
def test_minimum_repr_sanity(): ctx = Context() assert repr(ctx) # don't blow up assert str(ctx) # string coercion too
def test_update_bogus(): ctx = Context() assert_raises(TypeError, ctx.update, [], [])