mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Ignore leading whitespace in formulas, and strip out leading '=' sign users might add
Summary:
This addresses two issues, differently:
- For a formula with leading whitespace, like " 1+1", it is stored as is, but
is fixed to work (it should be valid Python, and whitespace is only stripped out
at parsing time to avoid intentation errors caused by the way it gets parsed)
- For a formula with a leading equals-sign ("="), it is stripped out on the
client side before the formula is stored. Grist documentation uses leading
"=" to indicate formulas (because UI shows an "=" icon), and Excel formulas
actually contain the leading "=", so it is a common mistake to include it.
Test Plan: Added new test cases
Reviewers: jarek
Reviewed By: jarek
Differential Revision: https://phab.getgrist.com/D3873
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import unittest
|
||||
|
||||
import codebuilder
|
||||
import six
|
||||
import test_engine
|
||||
|
||||
unicode_prefix = 'u' if six.PY2 else ''
|
||||
|
||||
def make_body(formula, default=None):
|
||||
return codebuilder.make_formula_body(formula, default).get_text()
|
||||
|
||||
class TestCodeBuilder(unittest.TestCase):
|
||||
class TestCodeBuilder(test_engine.EngineTestCase):
|
||||
def test_make_formula_body(self):
|
||||
# Test simple usage.
|
||||
self.assertEqual(make_body(""), "return None")
|
||||
@@ -239,3 +238,19 @@ return x or y
|
||||
|
||||
# Check that missing arguments is OK
|
||||
self.assertEqual(make_body("ISERR()"), "return ISERR()")
|
||||
|
||||
|
||||
def test_leading_whitespace(self):
|
||||
self.assertEqual(make_body(" $A + 1"), "return rec.A + 1")
|
||||
|
||||
self.assertEqual(make_body("""
|
||||
if $A:
|
||||
return $A
|
||||
|
||||
$B
|
||||
"""), """
|
||||
if rec.A:
|
||||
return rec.A
|
||||
|
||||
return rec.B
|
||||
""")
|
||||
|
||||
Reference in New Issue
Block a user