TestValueType   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 75
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A test_list_assignment_with_default_values() 0 8 2
A test_assign_name() 0 8 2
A test_assign_function() 0 8 2
A test_global_list_assignment_with_later_append() 0 10 2
A test_list_assignment_with_append_unknown_value() 0 10 2
A test_direct_assignment() 0 5 2
A test_list_assignment_based_on_later_append() 0 10 2
A test_list_assignment_with_function_call_as_value() 0 8 2
1
from py14.tracer import value_type, value_expr, decltype, is_list, is_recursive
2
from py14.context import add_variable_context, add_list_calls
3
from py14.scope import add_scope_context
4
import ast
5
6
7
def parse(*args):
8
    source = ast.parse("\n".join(args))
9
    add_variable_context(source)
10
    add_scope_context(source)
11
    return source
12
13
14
class TestValueType:
15
    def test_direct_assignment(self):
16
        source = parse("x = 3")
17
        x = source.body[0]
18
        t = value_type(x)
19
        assert t == "3"
20
21
    def test_assign_name(self):
22
        source = parse(
23
            "x = 3",
24
            "y = x",
25
        )
26
        y = source.body[1]
27
        t = value_type(y)
28
        assert t == "x"
29
30
    def test_assign_function(self):
31
        source = parse(
32
            "x = 3",
33
            "y = foo(x)",
34
        )
35
        y = source.body[1]
36
        t = value_type(y)
37
        assert t == "foo(x)"
38
39
    def test_list_assignment_with_default_values(self):
40
        source = parse(
41
            "x = 3",
42
            "results = [x]",
43
        )
44
        results = source.body[1]
45
        t = value_type(results)
46
        assert t == "x"
47
48
    def test_list_assignment_with_function_call_as_value(self):
49
        source = parse(
50
            "x = 3",
51
            "results = [foo(x)]",
52
        )
53
        results = source.body[1]
54
        t = value_type(results)
55
        assert t == "foo(x)"
56
57
    def test_list_assignment_based_on_later_append(self):
58
        source = parse(
59
            "x = 3",
60
            "results = []",
61
            "results.append(x)",
62
        )
63
        add_list_calls(source)
64
        results = source.body[1]
65
        t = value_type(results)
66
        assert t == "3"
67
68
    def test_list_assignment_with_append_unknown_value(self):
69
        source = parse(
70
            "results = []",
71
            "x = 3",
72
            "results.append(x)",
73
        )
74
        add_list_calls(source)
75
        results = source.body[0]
76
        t = value_type(results)
77
        assert t == "3"
78
79
    def test_global_list_assignment_with_later_append(self):
80
        source = parse(
81
            "results = []",
82
            "def add_x():",
83
            "   results.append(2)",
84
        )
85
        add_list_calls(source)
86
        results = source.body[0]
87
        t = value_type(results)
88
        assert t == "2"
89
90
91
class TestValueExpr:
92
    def test_catch_long_expression_chain(self):
93
        source = parse(
94
            "x = 3 * 1",
95
            "y = x - 5",
96
            "z = y + 2",
97
        )
98
        z = source.body[2]
99
        t = value_expr(z)
100
        assert t == "3 * 1 - 5 + 2"
101
102
    def test_catch_expression_chain_with_functions(self):
103
        source = parse(
104
            "x = 3 * 1",
105
            "y = foo(x)",
106
            "z = y + 2",
107
        )
108
        z = source.body[2]
109
        t = value_expr(z)
110
        assert t == "foo(3 * 1) + 2"
111
112
113
def test_decltype_normal_var():
114
    source = parse(
115
        "x = 3",
116
        "y = foo(x)",
117
    )
118
    y = source.body[1]
119
    t = decltype(y)
120
    assert t == "decltype(foo(x))"
121
122
123
def test_decltype_list_var():
124
    source = parse(
125
        "results = []",
126
        "x = 3",
127
        "results.append(x)",
128
    )
129
    add_list_calls(source)
130
    results = source.body[0]
131
    t = decltype(results)
132
    assert t == "std::vector<decltype(3)>"
133
134
135
def test_is_list():
136
    source = parse(
137
        "list1 = []",
138
        "list2 = list1",
139
    )
140
    add_list_calls(source)
141
    list2 = source.body[1]
142
    assert is_list(list2)
143
144
145
def test_is_recursive():
146
    source = parse(
147
        "def rec(n):",
148
        "   return rec(n-1) + rec(n)",
149
    )
150
    fun = source.body[0]
151
    assert is_recursive(fun)
152