コード例 #1
0
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)
コード例 #2
0
ファイル: test_context.py プロジェクト: mbr/flatland0
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
コード例 #3
0
ファイル: test_context.py プロジェクト: mbr/flatland0
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)
コード例 #4
0
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)
コード例 #5
0
ファイル: test_context.py プロジェクト: mbr/flatland0
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()
コード例 #6
0
ファイル: test_context.py プロジェクト: mbr/flatland0
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
コード例 #7
0
ファイル: markup.py プロジェクト: gaconnet/flatland
 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)
コード例 #8
0
 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)
コード例 #9
0
ファイル: test_context.py プロジェクト: dag/flatland
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
コード例 #10
0
ファイル: test_context.py プロジェクト: dag/flatland
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)
コード例 #11
0
ファイル: test_transforms.py プロジェクト: gaconnet/flatland
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)
コード例 #12
0
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)
コード例 #13
0
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
コード例 #14
0
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)
コード例 #15
0
ファイル: genshi_06.py プロジェクト: gaconnet/flatland
 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
コード例 #16
0
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)
コード例 #17
0
ファイル: test_transforms.py プロジェクト: gaconnet/flatland
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)
コード例 #18
0
ファイル: genshi_06.py プロジェクト: gaconnet/flatland
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)
コード例 #19
0
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)
コード例 #20
0
ファイル: test_transforms.py プロジェクト: gaconnet/flatland
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
コード例 #21
0
ファイル: markup.py プロジェクト: mbr/flatland0
    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)
コード例 #22
0
ファイル: markup.py プロジェクト: dag/flatland
    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)
コード例 #23
0
ファイル: test_context.py プロジェクト: dag/flatland
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]
コード例 #24
0
ファイル: test_transforms.py プロジェクト: gaconnet/flatland
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
コード例 #25
0
ファイル: test_transforms.py プロジェクト: gaconnet/flatland
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)
コード例 #26
0
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
コード例 #27
0
ファイル: test_context.py プロジェクト: orutherfurd/flatland
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
コード例 #28
0
ファイル: test_transforms.py プロジェクト: gaconnet/flatland
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)
コード例 #29
0
ファイル: genshi_06.py プロジェクト: gaconnet/flatland
    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()
コード例 #30
0
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)
コード例 #31
0
ファイル: test_transforms.py プロジェクト: dag/flatland
 def _context(self, overrides=()):
     context = Context()
     context.push()
     context.update(self.base_context)
     context.update(overrides)
     return context
コード例 #32
0
ファイル: test_context.py プロジェクト: mbr/flatland0
def test_default_minimum_stack():
    ctx = Context()
    with pytest.raises(RuntimeError):
        ctx.pop()
コード例 #33
0
ファイル: test_context.py プロジェクト: mbr/flatland0
def test_update_bogus():
    ctx = Context()
    with pytest.raises(TypeError):
        ctx.update([], [])
コード例 #34
0
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]
コード例 #35
0
 def _context(self, overrides=()):
     context = Context()
     context.push()
     context.update(self.base_context)
     context.update(overrides)
     return context
コード例 #36
0
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
コード例 #37
0
def test_default_minimum_stack():
    ctx = Context()
    assert_raises(RuntimeError, ctx.pop)
コード例 #38
0
def test_minimum_repr_sanity():
    ctx = Context()
    assert repr(ctx)  # don't blow up
    assert str(ctx)  # string coercion too
コード例 #39
0
def test_update_bogus():
    ctx = Context()
    assert_raises(TypeError, ctx.update, [], [])