summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2017-09-22 05:59:08 +0200
committerUlf Magnusson <ulfalizer@gmail.com>2017-09-22 07:02:05 +0200
commit8f81cbdcc1d35eeb419c96968d4eedbfeee42e55 (patch)
treea0cf13795808eff611383295ddf66ac3e3ca91c4
parent2de42031515a88d9847889e2d742b7066e33c26e (diff)
Simplify _expr_to_str()
- Get rid of _sym_str_string(), which was only used here. - Remove 'if expr is None' case that could never trigger - Add a test for printing string symbols, as they are a bit tricky: Default values should not be evaluated to tristate values.
-rw-r--r--kconfiglib.py18
-rw-r--r--tests/Ktext10
-rw-r--r--testsuite.py45
3 files changed, 54 insertions, 19 deletions
diff --git a/kconfiglib.py b/kconfiglib.py
index 1e7056d..8ae3360 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -3326,11 +3326,6 @@ def _make_block_conf(block, append_fn):
for item in block:
item._make_conf(append_fn)
-def _sym_str_string(sym_or_str):
- if isinstance(sym_or_str, str):
- return '"' + sym_or_str + '"'
- return sym_or_str.name
-
def _format_and_op(expr):
"""_expr_to_str() helper. Returns the string representation of 'expr',
which is assumed to be an operand to AND, with parentheses added if
@@ -3340,17 +3335,16 @@ def _format_and_op(expr):
return _expr_to_str(expr)
def _expr_to_str(expr):
- if expr is None:
- return ""
+ if isinstance(expr, str):
+ return '"{}"'.format(expr)
- if isinstance(expr, (Symbol, str)):
- return _sym_str_string(expr)
+ if isinstance(expr, Symbol):
+ return expr.name
if expr[0] == NOT:
- op1_str = _expr_to_str(expr[1])
if isinstance(expr[1], (str, Symbol)):
- return "!" + op1_str
- return "!({})".format(op1_str)
+ return "!" + _expr_to_str(expr[1])
+ return "!({})".format(_expr_to_str(expr[1]))
if expr[0] == AND:
return "{} && {}".format(_format_and_op(expr[1]),
diff --git a/tests/Ktext b/tests/Ktext
index 958ee18..599cd36 100644
--- a/tests/Ktext
+++ b/tests/Ktext
@@ -15,6 +15,16 @@ config ADVANCED
config ADVANCED
tristate "advanced prompt 2"
+config STRING
+ string
+ default "foo"
+ default "bar" if BAR
+ default STRING2 if BAZ
+
+config STRING2
+ string
+ default "baz"
+
endif
config SELECTED_1
diff --git a/testsuite.py b/testsuite.py
index fb5d677..f582fb0 100644
--- a/testsuite.py
+++ b/testsuite.py
@@ -628,6 +628,37 @@ def run_selftests():
!BASIC && !BASIC (value: "y")
Locations: Kconfiglib/tests/Ktext:6 Kconfiglib/tests/Ktext:15""")
+ verify_print(c["STRING"], """
+ Symbol STRING
+ Type : string
+ Value : "foo"
+ User value : (no user value)
+ Visibility : "n"
+ Is choice item : false
+ Is defined : true
+ Is from env. : false
+ Is special : false
+ Prompts:
+ (no prompts)
+ Default values:
+ "foo"
+ Condition: (none)
+ "bar"
+ Condition: BAR (value: "n")
+ STRING2 (value: "baz")
+ Condition: BAZ (value: "n")
+ Selects:
+ (no selects)
+ Implies:
+ (no implies)
+ Reverse (select-related) dependencies:
+ (no reverse dependencies)
+ Weak reverse (imply-related) dependencies:
+ (no weak reverse dependencies)
+ Additional dependencies from enclosing menus and ifs:
+ !BASIC && !BASIC (value: "y")
+ Locations: Kconfiglib/tests/Ktext:18""")
+
verify_print(c["HAS_RANGES"], """
Symbol HAS_RANGES
Type : int
@@ -656,7 +687,7 @@ def run_selftests():
(no weak reverse dependencies)
Additional dependencies from enclosing menus and ifs:
(no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:35""")
+ Locations: Kconfiglib/tests/Ktext:45""")
# Printing of Choice
@@ -677,7 +708,7 @@ def run_selftests():
CHOICE_ITEM_1 CHOICE_ITEM_2 CHOICE_ITEM_3
Additional dependencies from enclosing menus and ifs:
(no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:41""")
+ Locations: Kconfiglib/tests/Ktext:51""")
c["CHOICE_ITEM_2"].set_user_value("y")
@@ -698,7 +729,7 @@ def run_selftests():
CHOICE_ITEM_1 CHOICE_ITEM_2 CHOICE_ITEM_3
Additional dependencies from enclosing menus and ifs:
(no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:41""")
+ Locations: Kconfiglib/tests/Ktext:51""")
# Printing of Menu
@@ -709,7 +740,7 @@ def run_selftests():
'visible if' dependencies : (no dependencies)
Additional dependencies from enclosing menus and ifs:
(no additional dependencies)
- Location: Kconfiglib/tests/Ktext:53""")
+ Location: Kconfiglib/tests/Ktext:63""")
verify_print(c.get_menus()[1], """
Menu
@@ -718,7 +749,7 @@ def run_selftests():
'visible if' dependencies : !DUMMY (value: "y")
Additional dependencies from enclosing menus and ifs:
!DUMMY (value: "y")
- Location: Kconfiglib/tests/Ktext:57""")
+ Location: Kconfiglib/tests/Ktext:67""")
# Printing of Comment
@@ -728,7 +759,7 @@ def run_selftests():
Dependencies: (no dependencies)
Additional dependencies from enclosing menus and ifs:
(no additional dependencies)
- Location: Kconfiglib/tests/Ktext:63""")
+ Location: Kconfiglib/tests/Ktext:73""")
verify_print(c.get_comments()[1], """
Comment
@@ -736,7 +767,7 @@ def run_selftests():
Dependencies: !BASIC (value: "y")
Additional dependencies from enclosing menus and ifs:
!DUMMY (value: "y")
- Location: Kconfiglib/tests/Ktext:66""")
+ Location: Kconfiglib/tests/Ktext:76""")
verify_equals(c["NO_HELP"].get_help(), None)
verify_equals(c["EMPTY_HELP"].get_help(), "")