Commits

Andriy Kornatskyy committed 78d0717

Added unit tests for lexer module.

  • Participants
  • Parent commits ab71c2f

Comments (0)

Files changed (4)

 test-cover:
 	$(PYTEST) -q --cov wheezy.html \
 		--cov-report term-missing \
-		src/wheezy/html/tests
+		src/wheezy/html
 
 doc:
 	$(SPHINX) -a -b html doc/ doc/_build/

src/wheezy/html/ext/lexer.py

     """ Generic widget preprocessor.
     """
 
+    CHECKBOX = None
+    ERROR = None
+    ERROR_CLASS0 = None
+    ERROR_CLASS1 = None
+    EXPRESSION = None
+    HIDDEN = '<input type="hidden" name="%(name)s" value="%(value)s" />'
+    INPUT = None
+    LABEL = '<label for="%(id)s"%(attrs)s%(class)s>%(value)s</label>'
+    MESSAGE = None
+    MULTIPLE_CHECKBOX = None
+    MULTIPLE_HIDDEN = None
     PREPEND = None
-    EXPRESSION = '%(expr)s%(expr_filter)s'
-    ERROR_CLASS0 = """\
-"""
-    ERROR_CLASS1 = """\
-"""
-    HIDDEN = """\
-<input type="hidden" name="%(name)s" value="%(value)s" />"""
-    MULTIPLE_HIDDEN = """\
-"""
-    LABEL = """\
-<label for="%(id)s"%(attrs)s%(class)s>%(value)s</label>"""
-    INPUT = """\
-"""
-    TEXTAREA = """\
-"""
-    CHECKBOX = """\
-"""
-    MULTIPLE_CHECKBOX = """\
-"""
-    RADIO = """\
-"""
-    SELECT = """\
-"""
-    ERROR = """\
-"""
-    INFO = """\
-"""
+    RADIO = None
+    SELECT = None
+    TEXTAREA = None
 
     # region: preprocessing
 
     def __init__(self, widgets_pattern):
         self.widgets = {
+            'checkbox': self.checkbox,
+            'dropdown': self.dropdown,
+            'emptybox': self.emptybox,
+            'error': self.error,
             'hidden': self.hidden,
+            'info': self.info,
+            'label': self.label,
+            'listbox': self.listbox,
+            'multiple_checkbox': self.multiple_checkbox,
             'multiple_hidden': self.multiple_hidden,
-            'label': self.label,
-            'emptybox': lambda expr, params, expr_filter: self.input(
-                    expr, params, expr_filter, 'empty'),
-            'textbox': lambda expr, params, expr_filter: self.input(
-                    expr, params, expr_filter, 'text'),
-            'password': lambda expr, params, expr_filter: self.input(
-                    expr, params, expr_filter, 'password'),
+            'multiple_select': self.listbox,
+            'password': self.password,
+            'radio': self.radio,
+            'select': self.dropdown,
             'textarea': self.textarea,
-            'checkbox': self.checkbox,
-            'multiple_checkbox': self.multiple_checkbox,
-            'radio': self.radio,
-            'dropdown': lambda expr, params, expr_filter: self.select(
-                    expr, params, expr_filter, ''),
-            'select': lambda expr, params, expr_filter: self.select(
-                    expr, params, expr_filter, ''),
-            'listbox': lambda expr, params, expr_filter: self.select(
-                    expr, params, expr_filter, ' multiple="multiple"'),
-            'multiple_select': lambda expr, params, expr_filter: self.select(
-                    expr, params, expr_filter, ' multiple="multiple"'),
-            'error': self.error,
-            'info': lambda expr, params, expr_filter: self.info(
-                    expr, params, expr_filter, 'info'),
-            'warning': lambda expr, params, expr_filter: self.info(
-                    expr, params, expr_filter, 'warning')
+            'textbox': self.textbox,
+            'warning': self.warning,
         }
-        self.RE_WIDGETS = re.compile(
-                widgets_pattern % '|'.join(self.widgets))
+        assert '%(widgets)s' in widgets_pattern
+        self.RE_WIDGETS = re.compile(widgets_pattern % {
+            'widgets': '|'.join(sorted(self.widgets.keys()))})
 
     def __call__(self, text, **kwargs):
         """ Preprocess input text.
             'attrs': self.join_attrs(kwargs),
             'class': self.error_class(name, class_)}
 
-    def input(self, expr, params, expr_filter, input_type):
+    def emptybox(self, expr, params, expr_filter):
+        """ HTML element input of type text. Value is rendered
+            only if evaluated to boolean True.
+        """
+        return self.input_helper(expr, params, expr_filter, 'empty')
+
+    def textbox(self, expr, params, expr_filter):
+        """ HTML element input of type text. Value is rendered
+            only if it is not None or ''.
+        """
+        return self.input_helper(expr, params, expr_filter, 'text')
+
+    def password(self, expr, params, expr_filter):
+        """ HTML element input of type password. Value is rendered
+            only if it is not None or ''.
+        """
+        return self.input_helper(expr, params, expr_filter, 'password')
+
+    def input_helper(self, expr, params, expr_filter, input_type):
         """ HTML element input of type input_type.
         """
         name = parse_name(expr)
         return self.RADIO % {
             'id': html_id(name),
             'name': name,
+            'choices': choices,
             'value': expr,
             'expr_filter': expr_filter,
             'attrs': self.join_attrs(kwargs),
-            'class': self.error_class(name, class_),
-            'choices': choices}
+            'class': self.error_class(name, class_)}
 
-    def select(self, expr, params, expr_filter, select_type):
+    def dropdown(self, expr, params, expr_filter):
+        """
+        """
+        return self.select_helper(expr, params, expr_filter, '')
+
+    def listbox(self, expr, params, expr_filter):
+        """
+        """
+        return self.select_helper(
+                expr, params, expr_filter, ' multiple="multiple"')
+
+    def select_helper(self, expr, params, expr_filter, select_type):
         """ HTML element select.
         """
         name = parse_name(expr)
             'id': html_id(name),
             'name': name,
             'select_type': select_type,
+            'choices': choices,
             'value': expr,
             'expr_filter': expr_filter,
             'attrs': self.join_attrs(kwargs),
-            'class': self.error_class(name, class_),
-            'choices': choices}
+            'class': self.error_class(name, class_)}
 
     def error(self, expr, params, expr_filter):
         """ General error message or field error.
             'class': class_,
             'expr_filter': expr_filter}
 
-    def info(self, expr, params, filter, class_):
+    def info(self, expr, params, expr_filter):
+        """ General info message.
+        """
+        return self.message_helper(expr, params, expr_filter, 'info')
+
+    def warning(self, expr, params, expr_filter):
+        """ General warning message.
+        """
+        return self.message_helper(expr, params, expr_filter, 'warning')
+
+    def message_helper(self, expr, params, expr_filter, class_):
         """ General info message.
         """
         name = parse_name(expr)
         args, kwargs = parse_params(params)
         if expr.startswith(name):
             class_ = class_ + '-message'
-        return self.INFO % {
+        return self.MESSAGE % {
             'value': expr,
-            'info': self.expression(expr, filter),
+            'info': self.expression(expr, expr_filter),
             'class': class_}
 
 

src/wheezy/html/ext/mako.py

     def __init__(self, skip_imports=False):
         super(MakoPreprocessor, self).__init__(
             r'(?<!##)\s*\$\{((?P<expr>.+?)\.'
-            r'(?P<widget>%s){1}\((?P<params>.*?)\)\s*'
+            r'(?P<widget>%(widgets)s){1}\((?P<params>.*?)\)\s*'
             r'(?P<expr_filter>(\|\s*[\w,\s]+?|\s*)))\}\s*')
 
     PREPEND = """\
 %% endif
 """
 
-    INFO = """\\
+    MESSAGE = """\\
 %% if %(value)s:
 <span class="%(class)s">%(info)s</span>\\
 %% endif

src/wheezy/html/ext/tenjin.py

     def __init__(self):
         super(TenjinPreprocessor, self).__init__(
             r'\s*(?P<expr_filter>[#\$])\{((?P<expr>.+?)\.'
-            r'(?P<widget>%s){1}'
+            r'(?P<widget>%(widgets)s){1}'
             r'\((?P<params>.*?)\)\s*)\}\s*')
 
     EXPRESSION = '%(expr_filter)s{%(expr)s}'
 <?py #pass ?>
 <?py #endif ?>"""
 
-    INFO = """\
+    MESSAGE = """\
 """