diff --git a/tests/test_mark_tokens.py b/tests/test_mark_tokens.py index f24a6ca..d0cec81 100644 --- a/tests/test_mark_tokens.py +++ b/tests/test_mark_tokens.py @@ -7,6 +7,7 @@ import textwrap import unittest from . import tools +from .tools import Constant class TestMarkTokens(unittest.TestCase): @@ -181,11 +182,14 @@ def test_deep_recursion(self): m = self.create_mark_checker(source) self.assertEqual(len(m.all_nodes), 2104) self.assertEqual(m.view_node(m.all_nodes[-1]), - "Str:'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7'") + "%s:'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7'" + % Constant('Str')) self.assertEqual(m.view_node(m.all_nodes[-2]), - "Str:'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA'") + "%s:'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA'" + % Constant('Str')) self.assertEqual(m.view_node(m.all_nodes[1053]), - "Str:'R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS'") + "%s:'R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS'" + % Constant('Str')) self.assertEqual(m.view_node(m.all_nodes[1052]), "BinOp:'R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS'\r\n" + " +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp'") @@ -225,7 +229,7 @@ def test_adjacent_strings(self): ) """ m = self.create_mark_checker(source) - node_name = 'Const' if self.is_astroid_test else 'Str' + node_name = 'Const' if self.is_astroid_test else Constant('Str') self.assertEqual(m.view_nodes_at(2, 6), { node_name + ":'x y z' \\\n'''a b c''' \"u v w\"" }) @@ -336,7 +340,8 @@ def test_conditional_expr(self): name_a = 'AssignName:a' if self.is_astroid_test else 'Name:a' const_true = ('Const:True' if self.is_astroid_test else 'Name:True' if six.PY2 else - 'NameConstant:True') + '%s:True' + % Constant('NameConstant')) self.assertEqual(m.view_nodes_at(1, 0), {name_a, "Assign:a = True if True else False", "Module:" + source}) self.assertEqual(m.view_nodes_at(1, 4), @@ -388,7 +393,7 @@ def test_del_dict(self): if self.is_astroid_test: self.assertEqual(m.view_nodes_at(1, 5), {'Const:4'}) else: - self.assertEqual(m.view_nodes_at(1, 5), {'Num:4'}) + self.assertEqual(m.view_nodes_at(1, 5), {'%s:4' % Constant('Num')}) self.assertEqual(m.view_nodes_at(2, 0), {'Delete:del x[4]'}) self.assertEqual(m.view_nodes_at(2, 4), {'Name:x', 'Subscript:x[4]'}) @@ -428,7 +433,7 @@ def test_keyword_arg_only(self): self.assertEqual(m.view_nodes_at(2, 8), {'Keyword:b=[y]'}) else: self.assertEqual(m.view_nodes_at(1, 2), {'keyword:x=1'}) - self.assertEqual(m.view_nodes_at(1, 4), {'Num:1'}) + self.assertEqual(m.view_nodes_at(1, 4), {'%s:1' % Constant('Num')}) self.assertEqual(m.view_nodes_at(2, 2), {'keyword:a=(x)'}) self.assertEqual(m.view_nodes_at(2, 8), {'keyword:b=[y]'}) diff --git a/tests/test_util.py b/tests/test_util.py index 254116c..a73e96f 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -2,8 +2,10 @@ from __future__ import unicode_literals, print_function import ast import astroid +import sys import unittest from .context import asttokens +from .tools import Constant class TestUtil(unittest.TestCase): @@ -48,13 +50,13 @@ def view(node): 'Call:bar(1 + 2)', 'Name:bar', 'BinOp:1 + 2', - 'Num:1', - 'Num:2', + '%s:1' % Constant('Num'), + '%s:2' % Constant('Num'), "BinOp:'hello' + ', ' + 'world'", "BinOp:'hello' + ', '", - "Str:'hello'", - "Str:', '", - "Str:'world'" + "%s:'hello'" % Constant('Str'), + "%s:', '" % Constant('Str'), + "%s:'world'" % Constant('Str'), ]) def test_walk_astroid(self): diff --git a/tests/tools.py b/tests/tools.py index 546879b..66aa5c6 100644 --- a/tests/tools.py +++ b/tests/tools.py @@ -132,3 +132,14 @@ def verify_all_nodes(self, test_case): tested_nodes += 1 return tested_nodes + + +def Constant(name): + """Helper to write tests working on all Python versions. + + Return 'Constant' on Python 3.8 and newer, or name on older Python. + """ + if sys.version_info >= (3, 8): + return 'Constant' + else: + return name diff --git a/tox.ini b/tox.ini index b320505..7616bdb 100644 --- a/tox.ini +++ b/tox.ini @@ -13,4 +13,4 @@ deps = coverage py{27}: astroid py{35,36}: astroid<2 - py{37}: astroid>=2.dev + py{37,38}: astroid>=2.dev