Skip to content

Commit 6833d2c

Browse files
committed
Fixed a failing test case found by hypothesis
1 parent 26b9201 commit 6833d2c

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[flake8]
44
exclude=.tox,build,.eggs
5-
ignore=D100,D101
5+
ignore=D100,D101,W504
66
max-line-length=120
77

88
[isort]

src/fluent_compiler/codegen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ def variable(self, name):
233233

234234

235235
def cleanup_name(name):
236+
"""
237+
Convert name to a allowable identifier
238+
"""
236239
# See https://docs.python.org/2/reference/lexical_analysis.html#grammar-token-identifier
237240
name = _IDENTIFIER_SANITIZER_RE.sub('', name)
238241
if not _IDENTIFIER_START_RE.match(name):

tests/test_codegen.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from __future__ import absolute_import, unicode_literals
44

55
import ast
6+
import builtins
67
import keyword
78
import textwrap
89
import unittest
910

1011
from ast_decompiler.decompiler import Decompiler
11-
from hypothesis import given
12+
from hypothesis import example, given
1213
from hypothesis.strategies import text
1314

1415
from fluent_compiler import codegen
@@ -45,7 +46,7 @@ def as_source_code(codegen_ast):
4546

4647
# Stratgies
4748
non_keyword_text = text().filter(lambda t: t not in keyword.kwlist)
48-
non_builtin_text = non_keyword_text.filter(lambda t: t not in dir(__builtins__))
49+
non_builtin_text = non_keyword_text.filter(lambda t: t not in dir(builtins))
4950

5051

5152
class TestCodeGen(unittest.TestCase):
@@ -532,8 +533,13 @@ def test_cleanup_name_not_empty(self, t):
532533
self.assertTrue(len(codegen.cleanup_name(t)) > 0, " for t = {!r}".format(t))
533534

534535
@given(non_builtin_text)
536+
@example("!$abc<>")
537+
@example(":id")
535538
def test_cleanup_name_allowed_identifier(self, t):
536-
self.assertTrue(allowable_name(codegen.cleanup_name(t)), " for t = {!r}".format(t))
539+
cleaned = codegen.cleanup_name(t)
540+
self.assertTrue(allowable_name(cleaned) or
541+
(cleaned in dir(builtins)) or
542+
keyword.iskeyword(cleaned), " for t = {!r}".format(t))
537543

538544
def test_dict_lookup(self):
539545
scope = codegen.Scope()

0 commit comments

Comments
 (0)