diff options
author | German M. Bravo <german.mb@deipi.com> | 2013-08-17 18:31:09 -0500 |
---|---|---|
committer | German M. Bravo <german.mb@deipi.com> | 2013-08-17 18:31:09 -0500 |
commit | 239203dc57f032cab125953fc4d434df22aa6a68 (patch) | |
tree | 07d644b6a558bb108c95dcd2ddbdf9e57991bf7c | |
parent | e340ad421b7e2bdc391acb9ddca5a5ae6309c7bc (diff) | |
download | pyscss-239203dc57f032cab125953fc4d434df22aa6a68.tar.gz |
Using css built-in functions vs. css & scss functions vs. non-existent functions
-rw-r--r-- | scss/expression.py | 49 | ||||
-rw-r--r-- | scss/tests/test_expression.py | 11 |
2 files changed, 36 insertions, 24 deletions
diff --git a/scss/expression.py b/scss/expression.py index 7dc1f71..fa2157f 100644 --- a/scss/expression.py +++ b/scss/expression.py @@ -144,14 +144,7 @@ class Calculator(object): else: ast_cache[expr] = ast - try: - return ast.evaluate(self, divide=divide) - except Exception: - # TODO hoist me up since the rule is gone - #log.exception("Exception raised: %s in `%s' (%s)", e, expr, rule.file_and_line) - if config.DEBUG: - raise - return None + return ast.evaluate(self, divide=divide) def parse_expression(self, expr, target='goal'): ast_cache = self.get_ast_cache(target) @@ -287,36 +280,44 @@ class CallOp(Expression): kwargs[var.lstrip('$').replace('-', '_')] = value # TODO merge this with the library + funct = None try: - func = calculator.namespace.function(func_name, argspec_len) + funct = calculator.namespace.function(func_name, argspec_len) # @functions take a ns as first arg. TODO: Python functions possibly # should too - if getattr(func, '__name__', None) == '__call': - func = partial(func, calculator.namespace) + if getattr(funct, '__name__', None) == '__call': + funct = partial(funct, calculator.namespace) except KeyError: try: if kwargs: raise KeyError # Fallback to single parameter: - func = calculator.namespace.function(func_name, 1) + funct = calculator.namespace.function(func_name, 1) args = [args] except KeyError: if not is_builtin_css_function(func_name): log.error("Function not found: %s:%s", func_name, argspec_len, extra={'stack': True}) + raise + if funct: + try: + return funct(*args, **kwargs) + except Exception: + # TODO hoist me up since the rule is gone + #log.exception("Exception raised: %s in `%s' (%s)", e, expr, rule.file_and_line) + if not is_builtin_css_function(func_name): + raise - rendered_args = [] - for var, value in evald_argpairs: - rendered_value = value.render() - if var is None: - rendered_args.append(rendered_value) - else: - rendered_args.append("%s: %s" % (var, rendered_value)) - - return String( - u"%s(%s)" % (func_name, u", ".join(rendered_args)), - quotes=None) + rendered_args = [] + for var, value in evald_argpairs: + rendered_value = value.render() + if var is None: + rendered_args.append(rendered_value) + else: + rendered_args.append("%s: %s" % (var, rendered_value)) - return func(*args, **kwargs) + return String( + u"%s(%s)" % (func_name, u", ".join(rendered_args)), + quotes=None) class Literal(Expression): diff --git a/scss/tests/test_expression.py b/scss/tests/test_expression.py index 7acbff8..a10f21b 100644 --- a/scss/tests/test_expression.py +++ b/scss/tests/test_expression.py @@ -163,4 +163,15 @@ def test_parse(calc): ]) +def test_functions(calc): + ns = Namespace(functions=CORE_LIBRARY) + calc = Calculator(ns).calculate + + assert calc('grayscale(red)') == Color((127.5, 127.5, 127.5, 1)) + assert calc('grayscale(1)') == String('grayscale(1)', quotes=None) # Misusing css built-in functions (with scss counterpart) + assert calc('skew(1)') == String('skew(1)', quotes=None) # Missing css-only built-in functions + with pytest.raises(TypeError): + calc('unitless("X")') # Misusing non-css built-in scss funtions + + # TODO write more! i'm lazy. |