summaryrefslogtreecommitdiff
path: root/tests/test_cpp_domain.py
blob: 1fec2ba29110ee739d3c514263796e886bd6a8b2 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""
    test_cpp_domain
    ~~~~~~~~~~~~~~~

    Tests the C++ Domain

    :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from six import text_type

from util import raises

from sphinx.domains.cpp import DefinitionParser, DefinitionError


def parse(name, string):
    return getattr(DefinitionParser(string), 'parse_' + name)()


def test_type_definitions():
    rv = parse('member_object', '  const  std::string  &  name = 42')
    assert text_type(rv) == 'const std::string& name = 42'

    rv = parse('member_object', '  const  std::string  &  name leftover')
    assert text_type(rv) == 'const std::string& name'

    rv = parse('member_object', '  const  std::string  &  name [n] leftover')
    assert text_type(rv) == 'const std::string& name[n]'

    rv = parse('member_object', 'const std::vector< unsigned int, long> &name')
    assert text_type(rv) == 'const std::vector<unsigned int, long>& name'

    x = 'std::vector<std::pair<std::string, int>>& module::test(register ' \
        'foo, bar, std::string baz="foobar, blah, bleh") const = 0'
    assert text_type(parse('function', x)) == x

    x = 'module::myclass::operator std::vector<std::string>()'
    assert text_type(parse('function', x)) == x
    x = 'explicit module::myclass::foo::foo()'
    assert text_type(parse('function', x)) == x

    x = 'int printf(const char* fmt, ...)'
    assert text_type(parse('function', x)) == x

    x = 'int foo(const unsigned int j)'
    assert text_type(parse('function', x)) == x

    x = 'int foo(const unsigned int const j)'
    assert text_type(parse('function', x)) == x

    x = 'int foo(const int* const ptr)'
    assert text_type(parse('function', x)) == x

    x = 'std::vector<std::pair<std::string, long long>> module::blah'
    assert text_type(parse('type_object', x)) == x

    assert text_type(parse('type_object', 'long long int foo')) == 'long long foo'

    x = 'void operator()(const boost::array<VertexID, 2>& v) const'
    assert text_type(parse('function', x)) == x

    x = 'void operator()(const boost::array<VertexID, 2, "foo,  bar">& v) const'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::MyClass(MyClass::MyClass&&)'
    assert text_type(parse('function', x)) == x

    x = 'constexpr int get_value()'
    assert text_type(parse('function', x)) == x

    x = 'static constexpr int get_value()'
    assert text_type(parse('function', x)) == x

    x = 'int get_value() const noexcept'
    assert text_type(parse('function', x)) == x

    x = 'int get_value() const noexcept = delete'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::MyClass(MyClass::MyClass&&) = default'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::a_virtual_function() const override'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::a_member_function() volatile'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::a_member_function() const volatile'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::a_member_function() &&'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::a_member_function() &'
    assert text_type(parse('function', x)) == x

    x = 'MyClass::a_member_function() const &'
    assert text_type(parse('function', x)) == x

    x = 'int main(int argc, char* argv[][])'
    assert text_type(parse('function', x)) == x

    x = 'std::vector<std::pair<std::string, int>>& module::test(register ' \
        'foo, bar[n], std::string baz="foobar, blah, bleh") const = 0'
    assert text_type(parse('function', x)) == x

    x = 'module::myclass foo[n]'
    assert text_type(parse('member_object', x)) == x

    x = 'int foo(Foo f=Foo(double(), std::make_pair(int(2), double(3.4))))'
    assert text_type(parse('function', x)) == x

    x = 'int foo(A a=x(a))'
    assert text_type(parse('function', x)) == x

    x = 'int foo(B b=x(a)'
    raises(DefinitionError, parse, 'function', x)

    x = 'int foo)C c=x(a))'
    raises(DefinitionError, parse, 'function', x)

    x = 'int foo(D d=x(a'
    raises(DefinitionError, parse, 'function', x)


def test_bases():
    x = 'A'
    assert text_type(parse('class', x)) == x

    x = 'A : B'
    assert text_type(parse('class', x)) == x

    x = 'A : private B'
    assert text_type(parse('class', x)) == 'A : B'

    x = 'A : public B'
    assert text_type(parse('class', x)) == x

    x = 'A : B, C'
    assert text_type(parse('class', x)) == x

    x = 'A : B, protected C, D'
    assert text_type(parse('class', x)) == x


def test_operators():
    x = parse('function', 'void operator new [  ] ()')
    assert text_type(x) == 'void operator new[]()'

    x = parse('function', 'void operator delete ()')
    assert text_type(x) == 'void operator delete()'

    for op in '*-+=/%!':
        x = parse('function', 'void operator %s ()' % op)
        assert text_type(x) == 'void operator%s()' % op