sphinx/tests/test_domain_cpp.py

174 lines
6.7 KiB
Python
Raw Normal View History

2010-03-01 18:27:44 -06:00
# -*- coding: utf-8 -*-
"""
2014-08-10 09:47:27 -05:00
test_domain_cpp
2010-03-01 18:27:44 -06:00
~~~~~~~~~~~~~~~
Tests the C++ Domain
2015-01-03 14:41:14 -06:00
:copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
2010-03-01 18:27:44 -06:00
:license: BSD, see LICENSE for details.
"""
from six import text_type
from util import raises
2010-03-01 18:27:44 -06:00
from sphinx.domains.cpp import DefinitionParser, DefinitionError
2010-03-01 18:27:44 -06:00
ids = []
2010-03-01 18:27:44 -06:00
def parse(name, string):
2014-07-23 18:40:48 -05:00
parser = DefinitionParser(string)
res = getattr(parser, "parse_" + name + "_object")()
if not parser.eof:
print("Parsing stopped at", parser.pos)
print(string)
print('-'*parser.pos + '^')
raise DefinitionError("")
return res
def check(name, input, output=None):
# first a simple check of the AST
2014-10-09 09:53:33 -05:00
if output is None:
output = input
2014-07-23 18:40:48 -05:00
ast = parse(name, input)
res = text_type(ast)
if res != output:
2014-08-10 04:59:22 -05:00
print("Input: ", text_type(input))
print("Result: ", res)
print("Expected: ", output)
2014-07-23 18:40:48 -05:00
raise DefinitionError("")
ast.describe_signature([], 'lastIsName', None)
# Artificially set the prefixedName, otherwise the get_id fails.
# It would usually have been set in handle_signarue.
ast.prefixedName = ast.name
ast.get_id_v1()
ast.get_id_v2()
ids.append(ast.get_id_v2())
#print ".. %s:: %s" % (name, input)
2010-03-01 18:27:44 -06:00
def test_type_definitions():
2014-07-23 18:40:48 -05:00
check("type", "public bool b", "bool b")
check("type", "bool A::b")
check("type", "bool *b")
check("type", "bool *const b")
check("type", "bool *volatile const b")
check("type", "bool *volatile const b")
check("type", "bool *volatile const *b")
check("type", "bool &b")
2014-10-09 09:53:33 -05:00
check("type", "bool b[]")
2014-07-23 18:40:48 -05:00
check("type", "std::pair<int, int> coord")
check("type", "long long int foo")
check("type", 'std::vector<std::pair<std::string, long long>> module::blah')
check("type", "std::function<void()> F")
check("type", "std::function<R(A1, A2, A3)> F")
check("type", "std::function<R(A1, A2, A3, As...)> F")
check("type", "MyContainer::const_iterator")
2014-10-09 09:53:33 -05:00
check("type",
"public MyContainer::const_iterator",
"MyContainer::const_iterator")
# test decl specs on right
check("type", "bool const b")
2014-10-09 09:53:33 -05:00
check('member',
' const std::string & name = 42',
'const std::string &name = 42')
2014-07-23 18:40:48 -05:00
check('member', ' const std::string & name', 'const std::string &name')
2014-10-09 09:53:33 -05:00
check('member',
' const std::string & name [ n ]',
'const std::string &name[n]')
check('member',
'const std::vector< unsigned int, long> &name',
'const std::vector<unsigned int, long> &name')
2014-07-23 18:40:48 -05:00
check('member', 'module::myclass foo[n]')
check('function', 'operator bool() const')
check('function', 'A::operator bool() const')
check('function', 'A::operator bool() volatile const &')
check('function', 'A::operator bool() volatile const &&')
check('function', 'bool namespaced::theclass::method(arg1, arg2)')
2014-07-23 18:40:48 -05:00
x = 'std::vector<std::pair<std::string, int>> &module::test(register ' \
'foo, bar, std::string baz = "foobar, blah, bleh") const = 0'
check('function', x)
check('function', 'void f(std::pair<A, B>)')
2014-07-23 18:40:48 -05:00
check('function', 'explicit module::myclass::foo::foo()')
check('function', 'module::myclass::foo::~foo()')
check('function', 'int printf(const char *fmt, ...)')
check('function', 'int foo(const unsigned int j)')
check('function', 'int foo(const int *const ptr)')
check('function', 'module::myclass::operator std::vector<std::string>()')
2014-10-09 09:53:33 -05:00
check('function',
'void operator()(const boost::array<VertexID, 2> &v) const')
check('function',
'void operator()(const boost::array<VertexID, 2, "foo, bar"> &v) const')
2014-07-23 18:40:48 -05:00
check('function', 'MyClass::MyClass(MyClass::MyClass&&)')
check('function', 'constexpr int get_value()')
check('function', 'static constexpr int get_value()')
check('function', 'int get_value() const noexcept')
check('function', 'int get_value() const noexcept = delete')
check('function', 'int get_value() volatile const')
2014-07-23 18:40:48 -05:00
check('function', 'MyClass::MyClass(MyClass::MyClass&&) = default')
check('function', 'virtual MyClass::a_virtual_function() const override')
2014-07-23 18:40:48 -05:00
check('function', 'A B() override')
check('function', 'A B() final')
check('function', 'A B() final override')
check('function', 'A B() override final', 'A B() final override')
check('function', 'MyClass::a_member_function() volatile')
check('function', 'MyClass::a_member_function() volatile const')
check('function', 'MyClass::a_member_function() &&')
check('function', 'MyClass::a_member_function() &')
check('function', 'MyClass::a_member_function() const &')
check('function', 'int main(int argc, char *argv[])')
2014-07-23 18:40:48 -05:00
check('function', 'MyClass &MyClass::operator++()')
check('function', 'MyClass::pointer MyClass::operator->()')
x = 'std::vector<std::pair<std::string, int>> &module::test(register ' \
'foo, bar[n], std::string baz = "foobar, blah, bleh") const = 0'
check('function', x)
2014-10-09 09:53:33 -05:00
check('function',
'int foo(Foo f = Foo(double(), std::make_pair(int(2), double(3.4))))')
2014-07-23 18:40:48 -05:00
check('function', 'int foo(A a = x(a))')
raises(DefinitionError, parse, 'function', 'int foo(B b=x(a)')
raises(DefinitionError, parse, 'function', 'int foo)C c=x(a))')
raises(DefinitionError, parse, 'function', 'int foo(D d=x(a')
check('function', 'int foo(const A&... a)')
check('function', 'virtual void f()')
2015-01-03 09:59:08 -06:00
check('class', 'public A', 'A')
check('class', 'private A')
check('enum', 'A')
check('enum', 'A : std::underlying_type<B>::type')
check('enum', 'struct A')
check('enum', 'struct A : unsigned int')
check('enum', 'class A')
check('enum', 'class A : unsigned int')
check('enum', 'class public A', 'class A')
check('enum', 'class private A')
check('enumerator', 'A')
check('enumerator', 'A = std::numeric_limits<unsigned long>::max()')
def test_bases():
2014-07-23 18:40:48 -05:00
check('class', 'A')
check('class', 'A::B::C')
check('class', 'A : B')
check('class', 'A : private B', 'A : B')
check('class', 'A : public B')
check('class', 'A : B, C')
check('class', 'A : B, protected C, D')
def test_operators():
2014-07-23 18:40:48 -05:00
check('function', 'void operator new [ ] ()', 'void operator new[]()')
check('function', 'void operator delete ()', 'void operator delete()')
check('function', 'void operator bool() const', 'void operator bool() const')
2010-03-30 11:57:56 -05:00
for op in '*-+=/%!':
2014-07-23 18:40:48 -05:00
check('function', 'void operator %s ()' % op, 'void operator%s()' % op)
#def test_print():
# # used for getting all the ids out for checking
# for a in ids:
# print(a)
2015-02-06 17:55:19 -06:00
# raise DefinitionError("")