diff --git a/django_pyvows/assertions.py b/django_pyvows/assertions.py index 941f2a8..6dd3502 100644 --- a/django_pyvows/assertions.py +++ b/django_pyvows/assertions.py @@ -8,6 +8,11 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com +from lxml.cssselect import CSSSelector +from lxml.etree import fromstring + +from django.template.loader import render_to_string + from pyvows import Vows class Url(object): @@ -15,6 +20,42 @@ class Url(object): self.context = context self.path = path +class Template(object): + def __init__(self, template_name, context): + self.template_name = template_name + self.context = context + self.doc = None + + def load(self): + if not self.doc: + self.doc = fromstring(render_to_string(self.template_name, self.context)) + return self.doc + + def select_element(self, selector): + sel = CSSSelector(selector) + return sel(self.load()) + + def _to_contain(self, selector): + return len(self.select_element(selector)) > 0 + + def get_text(self, selector): + return "".join((c.text for c in self.select_element(selector))) + + def __unicode__(self): + return self.template_name + +@Vows.assertion +def to_contain(topic, selector): + assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping" + assert topic._to_contain(selector), "Expected template(%s) to have an element(%s), but it don't have" % \ + (unicode(topic), selector) + +@Vows.assertion +def not_to_contain(topic, selector): + assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping" + assert not topic._to_contain(selector), "Expected template(%s) to not have an element(%s), but it have" % \ + (unicode(topic), selector) + @Vows.assertion def to_be_mapped(topic): verify_url_is_mapped_to_method(topic) @@ -35,7 +76,7 @@ def verify_url_is_mapped_to_method(topic, method=None, assert_method_as_well=Fal matches_method = False for urlpattern in urlpatterns: regex = urlpattern.regex - pattern = regex.pattern + pattern = regex.pattern actual_method = urlpattern.callback if topic.path == pattern: diff --git a/django_pyvows/context.py b/django_pyvows/context.py index 001dc06..032ca6d 100644 --- a/django_pyvows/context.py +++ b/django_pyvows/context.py @@ -11,7 +11,7 @@ import os from pyvows import Vows -from django_pyvows.assertions import Url +from django_pyvows.assertions import Url, Template class DjangoContext(Vows.Context): @@ -31,8 +31,14 @@ class DjangoContext(Vows.Context): def _url(self, path): return Url(self, path) + def _template(self, template_name, context): + return Template(template_name, context) + class DjangoSubContext(Vows.Context): def _url(self, path): return self.parent._url(path) + def _template(self, template_name, context={}): + return self.parent._template(template_name, context) + diff --git a/vows/sandbox/settings.py b/vows/sandbox/settings.py index 550f07d..07881f8 100644 --- a/vows/sandbox/settings.py +++ b/vows/sandbox/settings.py @@ -1,8 +1,11 @@ # Django settings for sandbox project. +import os.path DEBUG = True TEMPLATE_DEBUG = DEBUG +PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__)) + ADMINS = ( # ('Your Name', 'your_email@example.com'), ) @@ -106,6 +109,7 @@ TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. + os.path.join(PROJECT_ROOT, "templates"), ) INSTALLED_APPS = ( diff --git a/vows/sandbox/templates/index.html b/vows/sandbox/templates/index.html new file mode 100644 index 0000000..57fc2fd --- /dev/null +++ b/vows/sandbox/templates/index.html @@ -0,0 +1,11 @@ + +
+some text
+Some {{ some }}!
+