/
unittest_brain_builtin.py
125 lines (115 loc) · 3.95 KB
/
unittest_brain_builtin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
"""Unit Tests for the builtins brain module."""
import unittest
import pytest
from astroid import nodes, objects, util
from astroid.builder import _extract_single_node
class BuiltinsTest(unittest.TestCase):
def test_infer_property(self):
class_with_property = _extract_single_node(
"""
class Something:
def getter():
return 5
asd = property(getter) #@
"""
)
inferred_property = list(class_with_property.value.infer())[0]
self.assertTrue(isinstance(inferred_property, objects.Property))
self.assertTrue(hasattr(inferred_property, "args"))
class TestStringNodes:
@pytest.mark.parametrize(
"format_string",
[
pytest.param(
""""My name is {}, I'm {}".format("Daniel", 12)""", id="empty-indexes"
),
pytest.param(
""""My name is {0}, I'm {1}".format("Daniel", 12)""",
id="numbered-indexes",
),
pytest.param(
""""My name is {fname}, I'm {age}".format(fname = "Daniel", age = 12)""",
id="named-indexes",
),
pytest.param(
"""
name = "Daniel"
age = 12
"My name is {0}, I'm {1}".format(name, age)
""",
id="numbered-indexes-from-positional",
),
pytest.param(
"""
name = "Daniel"
age = 12
"My name is {fname}, I'm {age}".format(fname = name, age = age)
""",
id="named-indexes-from-keyword",
),
pytest.param(
"""
name = "Daniel"
age = 12
"My name is {0}, I'm {age}".format(name, age = age)
""",
id="mixed-indexes-from-mixed",
),
pytest.param(
"""
string = "My name is {}, I'm {}"
string.format("Daniel", 12)
""",
id="empty-indexes-on-variable",
),
],
)
def test_string_format(self, format_string: str) -> None:
node: nodes.Call = _extract_single_node(format_string)
inferred = next(node.infer())
assert isinstance(inferred, nodes.Const)
assert inferred.value == "My name is Daniel, I'm 12"
@pytest.mark.parametrize(
"format_string",
[
"""
from missing import Unknown
name = Unknown
age = 12
"My name is {fname}, I'm {age}".format(fname = name, age = age)
""",
"""
from missing import Unknown
age = 12
"My name is {fname}, I'm {age}".format(fname = Unknown, age = age)
""",
"""
from missing import Unknown
"My name is {}, I'm {}".format(Unknown, 12)
""",
""""I am {}".format()""",
"""
"My name is {fname}, I'm {age}".format(fsname = "Daniel", age = 12)
""",
"""
"My unicode character is {:c}".format(None)
""",
"""
"My hex format is {:4x}".format('1')
""",
],
)
def test_string_format_uninferable(self, format_string: str) -> None:
node: nodes.Call = _extract_single_node(format_string)
inferred = next(node.infer())
assert inferred is util.Uninferable
def test_string_format_with_specs(self) -> None:
node: nodes.Call = _extract_single_node(
""""My name is {}, I'm {:.2f}".format("Daniel", 12)"""
)
inferred = next(node.infer())
assert isinstance(inferred, nodes.Const)
assert inferred.value == "My name is Daniel, I'm 12.00"