Adding a test around the whitespace trimming logic

This commit is contained in:
Darren Burns
2022-02-03 10:27:01 +00:00
parent 04181dc0b1
commit d21bd82406
2 changed files with 19 additions and 1 deletions

View File

@@ -242,7 +242,8 @@ def substitute_references(tokens: Iterator[Token]) -> Iterable[Token]:
variable_name = token.value[1:-1] # Trim the $ and the :, i.e. "$x:" -> "x" variable_name = token.value[1:-1] # Trim the $ and the :, i.e. "$x:" -> "x"
yield token yield token
# Lookahead for the variable value tokens # Store the tokens for any variable definitions, and substitute
# any variable references we encounter with them.
leading_whitespace = True leading_whitespace = True
while True: while True:
token = next(tokens, None) token = next(tokens, None)

View File

@@ -30,6 +30,23 @@ class TestVariableReferenceSubstitution:
Token(name='declaration_set_end', value='}', path='', code=css, location=(0, 31)) Token(name='declaration_set_end', value='}', path='', code=css, location=(0, 31))
] ]
def test_simple_reference_no_whitespace(self):
css = "$x:1; #some-widget{border: $x;}"
variables = substitute_references(tokenize(css, ""))
assert list(variables) == [
Token(name='variable_name', value='$x:', path='', code=css, location=(0, 0)),
Token(name='number', value='1', path='', code=css, location=(0, 3)),
Token(name='variable_value_end', value=';', path='', code=css, location=(0, 4)),
Token(name='whitespace', value=' ', path='', code=css, location=(0, 5)),
Token(name='selector_start_id', value='#some-widget', path='', code=css, location=(0, 6)),
Token(name='declaration_set_start', value='{', path='', code=css, location=(0, 18)),
Token(name='declaration_name', value='border:', path='', code=css, location=(0, 19)),
Token(name='whitespace', value=' ', path='', code=css, location=(0, 26)),
Token(name='number', value='1', path='', code=css, location=(0, 3)),
Token(name='declaration_end', value=';', path='', code=css, location=(0, 29)),
Token(name='declaration_set_end', value='}', path='', code=css, location=(0, 30))
]
def test_undefined_variable(self): def test_undefined_variable(self):
css = ".thing { border: $not-defined; }" css = ".thing { border: $not-defined; }"
with pytest.raises(UnresolvedVariableError): with pytest.raises(UnresolvedVariableError):