def test_round_trip(self): test = twc.CompoundWidget( id='a', children=[ twc.DisplayOnlyWidget( child=twc.RepeatingWidget(id='q', child=twc.Widget)), twc.CompoundWidget(id='cc', children=[ twc.Widget(id='d'), twc.Widget(id='e'), ]) ]) widgets = [ test.children[0].child.rwbc[0], test.children[0].child.rwbc[1], test.children.cc.children.d, test.children.cc.children.e, ] data = dict( (w.compound_id, 'test%d' % i) for i, w in enumerate(widgets)) testapi.request(1) vdata = test.validate(data) test = twc.core.request_local()['validated_widget'] widgets = [ test.children[0].child.children[0], test.children[0].child.children[1], test.children.cc.children.d, test.children.cc.children.e, ] for i, w in enumerate(widgets): eq_(w.value, 'test%d' % i)
def xxtest_wb_dupe(self): try: test = twc.widgets.WidgetBunch( [twc.Widget(id='a'), twc.Widget(id='a')]) assert (False) except twc.WidgetError, e: assert (str(e) == "WidgetBunch contains a duplicate id 'a'")
def test_compound_id(self): test = twc.CompoundWidget(id='x', children=[ twc.Widget(id='a'), twc.Widget(id='b'), ]) assert (test.children.a.compound_id == 'x:a')
def test_child_attr(self): class LayoutContainer(twc.CompoundWidget): label = twc.ChildParam(default='b') test = LayoutContainer(children=[twc.Widget(id='a', label='a')]).req() assert(test.c.a.label == 'a') test = LayoutContainer(children=[twc.Widget(id='a')]).req() assert(test.c.a.label == 'b')
def test_widgetbunch(self): a = twc.Widget(id='a') b = twc.Widget(id='b') test = twc.widgets.WidgetBunch([a, b]) assert (len(test) == 2) assert ([w for w in test] == [a, b]) assert (test[0] is a) assert (test[1] is b) assert (test.a is a) assert (test.b is b) try: test.c assert (False) except AttributeError, e: assert (str(e) == "Widget has no child named 'c'")
def xxtest_required(self): test = twc.Widget(id='test') try: test.req() assert (False) except twc.ParameterError, e: assert (str(e) == 'Widget is missing required parameter template')
def test_auto_unflatten(self): test = twc.CompoundWidget( id='a', children=[ twc.Widget(id='b', validator=twc.Validator(required=True)), ]) testapi.request(1) eq_(test.validate({'a:b': '10'}), {'b': '10'})
def test_backwards_compat_display(self): """ Ticket #4 `.display(value, ...)` """ test = twc.Widget( id='test', template="genshi:tw2.core.test_templates.field_genshi" ) output = test.display("foo") eq_(output, six.u('<p>foo </p>'))
def test_inline_template_with_no_markup(self): """ Ticket #69 """ test = twc.Widget( id='test', template="${w.value}", inline_engine_name="mako", ) output = test.display("foo") eq_(output, u'foo')
def test_inline_template(self): """ Ticket #69 """ test = twc.Widget( id='test', template="<p>${w.value} </p>", inline_engine_name="mako", ) output = test.display("foo") eq_(output, u'<p>foo </p>')
def test_deferred_value_no_subclass(self): test = twc.Widget(id='test', template="<p>${w.value}</p>", inline_engine_name="mako", value=twc.Deferred(lambda: 'test')) assert (test.value != 'test') ins = test.req() ins.prepare() assert (ins.value == 'test') assert (test.display() == "<p>test</p>")
def test_prepare_validate(self): class MyValidator(twc.Validator): def from_python(self, value, state=None): return value.upper() test = twc.Widget(id='a', template='b', validator=MyValidator()).req() testapi.request(1) test.value = 'fred' test.prepare() assert (test.value == 'FRED')
def test_vld_leaf_fail(self): test = twc.Widget(validator=twc.IntValidator()).req() try: test._validate('x') assert (False) except twc.ValidationError: pass assert (test.value == 'x') assert (test.error_msg == 'Must be an integer')
def test_display_only(self): a = twc.Widget(id='a') test = twc.DisplayOnlyWidget(child=a, template='xyz') assert (a.parent is None) assert (test.child.parent.template == test.template) testapi.request(1) test = test.req() test.value = 10 test.prepare() assert (test.child.value == 10)
def test_cw_propagate(self): testb = twc.CompoundWidget(id='a', template='x', children=[ twc.Widget(id='b'), twc.Widget(id='c'), ]) test = testb.req(value={'b': 1, 'c': 2}) test.prepare() assert (test.children.b.value == 1) assert (test.children.c.value == 2) class A: pass a = A() a.b = 10 a.c = 20 test = testb.req(value=a) test.prepare() assert (test.children.b.value == 10) assert (test.children.c.value == 20)
def setUp(self): global _request_local _request_local = {} self.mw = twc.make_middleware(self) self.app = wt.TestApp(self.mw) js = twc.JSLink(link='paj') css = twc.CSSLink(link='joe') TestWidget = twc.Widget( template='genshi:tw2.core.test_templates.inner_genshi', test='test', ) self.inject_widget = TestWidget(id='a', resources=[js, css])
def test_detect_clear(self): widget = twc.Widget( id='a', template='genshi:tw2.core.test_templates.inner_genshi', test='test', resources=[js]) rl = testapi.request(1, mw) eq_(rl.get('resources', []), []) widget.display() rl = twc.core.request_local() assert (len(rl.get('resources', [])) == 1) out = twc.inject_resources(html) eq_(rl.get('resources', []), [])
def test_widget_relative_inheritance(self): twc.core.request_local()['middleware'] = twc.make_middleware(None) # These aren't yet supported in the tests yet. ignored_engines = ['jinja', 'kajiki', 'chameleon'] for engine in engines: if engine in ignored_engines: continue template = "%s:tw2.core.test_templates.child_%s" % (engine, engine) test = twc.Widget(id='x', template=template) expected = """ <html> <head><title>Parent</title></head> <body>Child</body> </html> """ assert_eq_xhtml(test.display(), expected)
def test_child(self): assert (not hasattr(TestContainer, 'test')) test = twc.Widget(id='q') assert (not hasattr(test, 'test')) test2 = TestContainer(id='r', children=[test]).req() assert (test2.c.q.test == 10)
class CompWidget(twc.CompoundWidget): one = twc.Widget(validator=formencode.validators.NotEmpty()) two = twc.Widget(validator=formencode.validators.NotEmpty()) three = twc.Widget(validator=twc.Validator(required=True))
class MyTest(twc.CompoundWidget): a = twc.Widget(id='fred') b = twc.Widget()
def test_vld_leaf_pass(self): test = twc.Widget(validator=twc.IntValidator()) assert (test.req()._validate('1') == 1)
def test_backwards_compat_new(self): """ Ticket #4 `.__init__(id, ...)` """ test = twc.Widget('test') eq_(test.id, 'test')
def test_deferred(self): test = twc.Widget(id='test', template=twc.Deferred(lambda: 'test')) assert (test.template != 'test') ins = test.req() ins.prepare() assert (ins.template == 'test')
class MatchyWidget(twc.CompoundWidget): validator = formencode.validators.FieldsMatch('one', 'two') one = twc.Widget two = twc.Widget three = twc.Widget(validator=twc.Validator(required=True))
import tw2.core.testbase as tb from tw2.core.validation import * import re import datetime import formencode from nose.tools import eq_, raises from webob.multidict import MultiDict from unittest import TestCase import sys HAS_SKIP = sys.version_info[0] == 2 and sys.version_info[1] == 7 compound_widget = twc.CompoundWidget( id='a', children=[ twc.Widget(id='b', validator=twc.Validator(required=True)), twc.Widget(id='c', validator=twc.Validator(required=True)), ]) repeating_widget = twc.RepeatingWidget( id='a', child=twc.Widget(validator=twc.Validator(required=True))) compound_keyed_widget = twc.CompoundWidget( id='a', children=[ twc.Widget(id='b', key='x', validator=twc.Validator(required=True)), twc.Widget(id='c', key='y', validator=formencode.validators.OpenId()), ]) #This is required to make tests pass on non english systems formencode.api.set_stdtranslation(languages=['en'])
def test_genshi_relative_filename_cwd(self): """ Issue #30 take 2 -- http://bit.ly/LT4rBP """ with directory(self._tests_rel_dir): twc.Widget(template='genshi:./test.html').display()
def test_genshi_relative_filename(self): """ Issue #30 take 1 -- http://bit.ly/LT4rBP """ twc.Widget(template='genshi:%s/test.html' % self._tests_rel_dir).display()
def test_genshi_abs(self): test_dir = os.path.sep.join(__file__.split(os.path.sep)[:-1]) fname = os.path.sep.join([test_dir, 'test.html']) twc.Widget(template='genshi_abs:%s' % fname).display()
import webob as wo, webtest as wt, tw2.core as twc, os, testapi, tw2.core.resources as twr, tw2.core.testbase as tb, tw2.core.params as pm import tw2.core.core from nose.tools import eq_, raises from strainer.operators import eq_xhtml from unittest import TestCase js = twc.JSLink(link='paj') css = twc.CSSLink(link='joe') csssrc = twc.CSSSource(src='.bob { font-weight: bold; }') jssrc = twc.JSSource(src='bob') TestWidget = twc.Widget(template='genshi:tw2.core.test_templates.inner_genshi', test='test') html = "<html><head><title>a</title></head><body>hello</body></html>" inject_widget = TestWidget(id='a', resources=[js]) def simple_app(environ, start_response): req = wo.Request(environ) ct = 'text/html' if req.path == '/' else 'test/plain' resp = wo.Response(request=req, content_type="%s; charset=UTF8" % ct) inject_widget.display() resp.body = html return resp(environ, start_response) mw = twc.make_middleware(simple_app) tst_mw = wt.TestApp(mw)