OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / tests / auto / cplusplus / typeprettyprinter / tst_typeprettyprinter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include <QtTest>
34 #include <QObject>
35 #include <QList>
36
37 #include <FullySpecifiedType.h>
38 #include <Type.h>
39 #include <CoreTypes.h>
40 #include <Symbols.h>
41 #include <TranslationUnit.h>
42 #include <Control.h>
43 #include <Names.h>
44 #include <Literals.h>
45 #include <Overview.h>
46 #include <Scope.h>
47 #include <TypePrettyPrinter.h>
48
49 //TESTED_COMPONENT=src/libs/cplusplus
50 using namespace CPlusPlus;
51
52 class tst_TypePrettyPrinter: public QObject
53 {
54     Q_OBJECT
55
56 private Q_SLOTS:
57     void basic();
58     void basic_data();
59 };
60
61 Q_DECLARE_METATYPE(CPlusPlus::FullySpecifiedType);
62
63 TranslationUnit *unit;
64
65 const Identifier *nameId(const QString &name)
66 { return new Identifier(name.toLatin1().constData(), name.toLatin1().size()); }
67
68 Argument *arg(const QString &name, const FullySpecifiedType &ty)
69 {
70     Argument *a = new Argument(unit, 0, nameId(name));
71     a->setType(ty);
72     return a;
73 }
74
75 FullySpecifiedType voidTy()
76 { return FullySpecifiedType(new VoidType); }
77
78 FullySpecifiedType intTy()
79 { return FullySpecifiedType(new IntegerType(IntegerType::Int)); }
80
81 FullySpecifiedType fnTy(const QString &name, const FullySpecifiedType &ret)
82 {
83     Function *fn = new Function(unit, 0, nameId(name));
84     fn->setReturnType(ret);
85     return FullySpecifiedType(fn);
86 }
87
88 FullySpecifiedType fnTy(const QString &name, const FullySpecifiedType &ret, const FullySpecifiedType &a0)
89 {
90     Function *fn = new Function(unit, 0, nameId(name));
91     fn->setReturnType(ret);
92     fn->addMember(arg("a0", a0));
93     return FullySpecifiedType(fn);
94 }
95
96 FullySpecifiedType ptr(const FullySpecifiedType &el)
97 { return FullySpecifiedType(new PointerType(el)); }
98
99 FullySpecifiedType ref(const FullySpecifiedType &el)
100 { return FullySpecifiedType(new ReferenceType(el, false)); }
101
102 FullySpecifiedType rref(const FullySpecifiedType &el)
103 { return FullySpecifiedType(new ReferenceType(el, true)); }
104
105 FullySpecifiedType arr(const FullySpecifiedType &el)
106 { return FullySpecifiedType(new ArrayType(el, 4)); }
107
108 FullySpecifiedType cnst(const FullySpecifiedType &el)
109 { FullySpecifiedType r(el); r.setConst(true); return r; }
110
111
112
113 void addRow(const FullySpecifiedType &f, QString result, QString name = QString())
114 {
115     QTest::newRow(result.toLatin1().constData()) << f << name << result;
116 }
117
118 void tst_TypePrettyPrinter::basic_data()
119 {
120     // seems it now works without a translation unit
121 //    Control c;
122 //    TranslationUnit t(&c, 0);
123 //    unit = 0;
124
125     QTest::addColumn<FullySpecifiedType>("type");
126     QTest::addColumn<QString>("name");
127     QTest::addColumn<QString>("result");
128
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");
133
134     // named types
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");
140
141     // pointers
142     addRow(ptr(voidTy()), "void *");
143     addRow(ptr(ptr(voidTy())), "void **");
144
145     addRow(ptr(cnst(voidTy())), "const void *");
146     addRow(cnst(ptr(cnst(voidTy()))), "const void *const");
147     addRow(cnst(ptr(voidTy())), "void *const");
148
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");
156
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 (*[])[]");
163
164     // references
165     addRow(ref(voidTy()), "void &");
166     addRow(ref(ref(voidTy())), "void & &");
167
168     addRow(ref(cnst(voidTy())), "const void &");
169     addRow(cnst(ref(cnst(voidTy()))), "const void &const");
170     addRow(cnst(ref(voidTy())), "void &const");
171
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");
179
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 (&[])[]");
186
187     // rvalue references
188     addRow(rref(voidTy()), "void &&");
189     addRow(rref(cnst(voidTy())), "const void &&");
190
191     addRow(rref(arr(voidTy())), "void (&&)[]");
192     addRow(rref(arr(arr(voidTy()))), "void (&&)[][]");
193
194     // simple functions
195     addRow(ptr(fnTy("foo", voidTy(), intTy())), "void (*foo)(int)", "foo");
196     addRow(ptr(fnTy("foo", voidTy(), ptr(voidTy()))), "void (*foo)(void *)", "foo");
197 }
198
199 void tst_TypePrettyPrinter::basic()
200 {
201     QFETCH(FullySpecifiedType, type);
202     QFETCH(QString, name);
203     QFETCH(QString, result);
204
205     Overview o;
206     o.setShowReturnTypes(true);
207     TypePrettyPrinter pp(&o);
208     QCOMPARE(pp(type, name), result);
209 }
210
211 QTEST_APPLESS_MAIN(tst_TypePrettyPrinter)
212 #include "tst_typeprettyprinter.moc"