1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
37 #include <FullySpecifiedType.h>
39 #include <CoreTypes.h>
41 #include <TranslationUnit.h>
47 #include <TypePrettyPrinter.h>
49 //TESTED_COMPONENT=src/libs/cplusplus
50 using namespace CPlusPlus;
52 class tst_TypePrettyPrinter: public QObject
61 Q_DECLARE_METATYPE(CPlusPlus::FullySpecifiedType);
63 TranslationUnit *unit;
65 const Identifier *nameId(const QString &name)
66 { return new Identifier(name.toLatin1().constData(), name.toLatin1().size()); }
68 Argument *arg(const QString &name, const FullySpecifiedType &ty)
70 Argument *a = new Argument(unit, 0, nameId(name));
75 FullySpecifiedType voidTy()
76 { return FullySpecifiedType(new VoidType); }
78 FullySpecifiedType intTy()
79 { return FullySpecifiedType(new IntegerType(IntegerType::Int)); }
81 FullySpecifiedType fnTy(const QString &name, const FullySpecifiedType &ret)
83 Function *fn = new Function(unit, 0, nameId(name));
84 fn->setReturnType(ret);
85 return FullySpecifiedType(fn);
88 FullySpecifiedType fnTy(const QString &name, const FullySpecifiedType &ret, const FullySpecifiedType &a0)
90 Function *fn = new Function(unit, 0, nameId(name));
91 fn->setReturnType(ret);
92 fn->addMember(arg("a0", a0));
93 return FullySpecifiedType(fn);
96 FullySpecifiedType ptr(const FullySpecifiedType &el)
97 { return FullySpecifiedType(new PointerType(el)); }
99 FullySpecifiedType ref(const FullySpecifiedType &el)
100 { return FullySpecifiedType(new ReferenceType(el, false)); }
102 FullySpecifiedType rref(const FullySpecifiedType &el)
103 { return FullySpecifiedType(new ReferenceType(el, true)); }
105 FullySpecifiedType arr(const FullySpecifiedType &el)
106 { return FullySpecifiedType(new ArrayType(el, 4)); }
108 FullySpecifiedType cnst(const FullySpecifiedType &el)
109 { FullySpecifiedType r(el); r.setConst(true); return r; }
113 void addRow(const FullySpecifiedType &f, QString result, QString name = QString())
115 QTest::newRow(result.toLatin1().constData()) << f << name << result;
118 void tst_TypePrettyPrinter::basic_data()
120 // seems it now works without a translation unit
122 // TranslationUnit t(&c, 0);
125 QTest::addColumn<FullySpecifiedType>("type");
126 QTest::addColumn<QString>("name");
127 QTest::addColumn<QString>("result");
129 addRow(voidTy(), "void");
130 addRow(cnst(voidTy()), "const void");
131 addRow(ptr(fnTy("foo", voidTy())), "void (*)()");
132 addRow(ptr(fnTy("foo", voidTy())), "void (*foo)()", "foo");
135 addRow(voidTy(), "void foo", "foo");
136 addRow(ptr(voidTy()), "void *foo", "foo");
137 addRow(cnst(ptr(voidTy())), "void *const foo", "foo");
138 addRow(arr(voidTy()), "void foo[]", "foo");
139 addRow(ptr(arr(voidTy())), "void (*foo)[]", "foo");
142 addRow(ptr(voidTy()), "void *");
143 addRow(ptr(ptr(voidTy())), "void **");
145 addRow(ptr(cnst(voidTy())), "const void *");
146 addRow(cnst(ptr(cnst(voidTy()))), "const void *const");
147 addRow(cnst(ptr(voidTy())), "void *const");
149 addRow(ptr(ptr(cnst(voidTy()))), "const void **");
150 addRow(ptr(cnst(ptr(cnst(voidTy())))), "const void *const*");
151 addRow(cnst(ptr(ptr(cnst(voidTy())))), "const void **const");
152 addRow(cnst(ptr(cnst(ptr(cnst(voidTy()))))), "const void *const*const");
153 addRow(ptr(cnst(ptr(voidTy()))), "void *const*");
154 addRow(cnst(ptr(ptr(voidTy()))), "void **const");
155 addRow(cnst(ptr(cnst(ptr(voidTy())))), "void *const*const");
157 addRow(arr(voidTy()), "void[]");
158 addRow(arr(ptr(voidTy())), "void *[]");
159 addRow(ptr(arr(voidTy())), "void (*)[]");
160 addRow(ptr(arr(arr(voidTy()))), "void (*)[][]");
161 addRow(ptr(arr(ptr(voidTy()))), "void *(*)[]");
162 addRow(arr(ptr(arr(voidTy()))), "void (*[])[]");
165 addRow(ref(voidTy()), "void &");
166 addRow(ref(ref(voidTy())), "void & &");
168 addRow(ref(cnst(voidTy())), "const void &");
169 addRow(cnst(ref(cnst(voidTy()))), "const void &const");
170 addRow(cnst(ref(voidTy())), "void &const");
172 addRow(ref(ref(cnst(voidTy()))), "const void & &");
173 addRow(ref(cnst(ref(cnst(voidTy())))), "const void &const&");
174 addRow(cnst(ref(ref(cnst(voidTy())))), "const void & &const");
175 addRow(cnst(ref(cnst(ref(cnst(voidTy()))))), "const void &const&const");
176 addRow(ref(cnst(ref(voidTy()))), "void &const&");
177 addRow(cnst(ref(ref(voidTy()))), "void & &const");
178 addRow(cnst(ref(cnst(ref(voidTy())))), "void &const&const");
180 addRow(arr(voidTy()), "void[]");
181 addRow(arr(ref(voidTy())), "void &[]");
182 addRow(ref(arr(voidTy())), "void (&)[]");
183 addRow(ref(arr(arr(voidTy()))), "void (&)[][]");
184 addRow(ref(arr(ref(voidTy()))), "void &(&)[]");
185 addRow(arr(ref(arr(voidTy()))), "void (&[])[]");
188 addRow(rref(voidTy()), "void &&");
189 addRow(rref(cnst(voidTy())), "const void &&");
191 addRow(rref(arr(voidTy())), "void (&&)[]");
192 addRow(rref(arr(arr(voidTy()))), "void (&&)[][]");
195 addRow(ptr(fnTy("foo", voidTy(), intTy())), "void (*foo)(int)", "foo");
196 addRow(ptr(fnTy("foo", voidTy(), ptr(voidTy()))), "void (*foo)(void *)", "foo");
199 void tst_TypePrettyPrinter::basic()
201 QFETCH(FullySpecifiedType, type);
202 QFETCH(QString, name);
203 QFETCH(QString, result);
206 o.setShowReturnTypes(true);
207 TypePrettyPrinter pp(&o);
208 QCOMPARE(pp(type, name), result);
211 QTEST_APPLESS_MAIN(tst_TypePrettyPrinter)
212 #include "tst_typeprettyprinter.moc"