Debian Codegraph @

test/jpypetest/test_keywords.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import keyword # From the standard library.
19
20 import pytest
21
22 import jpype
23 import common
24
25
26 @pytest.mark.parametrize(
27 "identifier",
28 list(keyword.kwlist) + [
29 '__del__',
30 # Print is no longer a keyword in Python 3, but still protected to
31 # avoid API breaking in JPype v1.
32 'print',
33 '____', # Not allowed.
34 ]
35 )
36 def testPySafe__Keywords(identifier):
37 safe = jpype._pykeywords.pysafe(identifier)
38 if identifier.startswith("__"):
39 assert safe is None
40 else:
41 assert isinstance(safe, str), f"Fail on keyword {identifier}"
42 assert safe.endswith("_")
43
44
45 @pytest.mark.parametrize(
46 "identifier",
47 [
48 'notSpecial',
49 '__notSpecial',
50 'notSpecial__',
51 '_notSpecial_',
52 '_not__special_',
53 '__', '___', # Technically these are fine.
54 ])
55 def testPySafe__NotKeywords(identifier):
56 safe = jpype._pykeywords.pysafe(identifier)
57 assert safe == identifier
58
59
60 class AttributeTestCase(common.JPypeTestCase):
61 def testPySafe(self):
62 cls = jpype.JPackage("jpype").attr.TestKeywords
63 self.assertTrue(hasattr(cls, "__leading_double_underscore"))
64 self.assertFalse(hasattr(cls, "__dunder_name__"))