File: test_astyle.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (380 lines) | stat: -rw-r--r-- 15,039 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "test_astyle.h"

#include <QTest>
#include <QDebug>
#include <QStandardPaths>

#include "../astyle_formatter.h"
#include <util/formattinghelpers.h>

QTEST_MAIN(TestAstyle)

TestAstyle::~TestAstyle() = default;

void TestAstyle::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);
    m_formatter = std::make_unique<AStyleFormatter>();
    ///TODO: probably all settings should be covered by tests
    ///      or at least set so we can be sure about what we
    ///      actually test...
    m_formatter->setSpaceIndentationNoConversion(4);
}

void TestAstyle::renameVariable()
{
    // think this:
    // int asdf = 1;
    // e.g. asdf was before something different and got renamed
    QString formattedSource = m_formatter->formatSource(
        QStringLiteral("asdf"), QStringLiteral("int "), QStringLiteral(" = 1;")
    );
    qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("asdf"));

    // int main() {
    //     if(asdf){}}
    formattedSource = m_formatter->formatSource(
        QStringLiteral("asdf"), QStringLiteral("int main(){\n     if("), QStringLiteral("){}}")
    );
    qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("asdf"));
}

void TestAstyle::testFuzzyMatching()
{
    // Some formatting styles inserts "{" and "}" parens behind "ifs", or change comment styles
    // The actual text changes, thus it is difficult to match original and formatted text

    QString leftContext = QStringLiteral("void b() {/*some comment*/\nif( ");
    QString center = QStringLiteral("a[   0]");
    QString rightContext =  QStringLiteral(" ) q;\n }\n");
    QString text = leftContext + center + rightContext;
    QString formatted = QStringLiteral("void b() {// some comment\n    if( a[0] ) {\n        q;\n    }\n }\n");
    QString extracted = KDevelop::extractFormattedTextFromContext( formatted, text, QString(), QString() );
    QCOMPARE( extracted, formatted );
    
    extracted = KDevelop::extractFormattedTextFromContext( formatted, center, leftContext, rightContext );
    qDebug() << "extracted" << extracted << "formatted" << formatted;
    QCOMPARE( extracted, QString("a[0]") );

    rightContext = QStringLiteral("\nvoid g() {}");
    extracted = KDevelop::extractFormattedTextFromContext(formatted + rightContext, text, QString(), rightContext);
    QCOMPARE(extracted, formatted);
}

void TestAstyle::testTabMatching()
{
    // Some formatting styles inserts "{" and "}" parens behind "ifs", or change comment styles
    // The actual text changes, thus it is difficult to match original and formatted text

{
	// Mismatch: There is a preceding tab, but the formatter replaces the tab with spaces
	// The tab is matched with 2 spaces, since we set tab-width 2
    QString extracted = KDevelop::extractFormattedTextFromContext(
		QStringLiteral("class C {\n  class A;\n}\n"),
		QStringLiteral("class A;"), QStringLiteral("class C {\n	"), QStringLiteral("\n}\n"), 2 );
    QCOMPARE( extracted, QString("class A;") );
	
	// Two tabs are inserted insead of 1
	extracted = KDevelop::extractFormattedTextFromContext(
		QStringLiteral("class C {\n		class A;\n}\n"),
		QStringLiteral("class A;"), QStringLiteral("class C {\n	"), QStringLiteral("\n}\n"), 2 );
    QCOMPARE( extracted, QString("	class A;") );
	
	// One space is inserted behind the tab
	extracted = KDevelop::extractFormattedTextFromContext(
		QStringLiteral("class C {\n	 class A;\n}\n"),
		QStringLiteral("class A;"), QStringLiteral("class C {\n	"), QStringLiteral("\n}\n"), 2 );
    QCOMPARE( extracted, QString(" class A;") );

	// Two tabs are inserted, with 2 preceding whitespaces
	// Add only 1 tab
	extracted = KDevelop::extractFormattedTextFromContext(
		QStringLiteral("class C {\n		class A;\n}\n"),
		QStringLiteral("class A;"), QStringLiteral("class C {\n  "), QStringLiteral("\n}\n"), 2 );
    QCOMPARE( extracted, QString("	class A;") );

	extracted = KDevelop::extractFormattedTextFromContext(
		QStringLiteral("class C {\n          class A;\n}\n"),
		QStringLiteral("class A;"), QStringLiteral("class C {\n		"), QStringLiteral("\n}\n"), 4 );
    QCOMPARE( extracted, QString("  class A;") );
}
{
	// Already correctly formatted
    QString leftContext = QStringLiteral("void b() {\n c = 4;\n	");
    QString center = QStringLiteral("a = 3;");
    QString rightContext =  QStringLiteral("\n b = 5;\n }\n");
    QString text = leftContext + center + rightContext;
    QString formatted = QStringLiteral("void b() {\n	c = 4;\n	a = 3;\n	b = 5;\n }\n");
    QString extracted = KDevelop::extractFormattedTextFromContext( formatted, text, QString(), QString() );
    QCOMPARE( extracted, formatted );
    
    extracted = KDevelop::extractFormattedTextFromContext( formatted, center, leftContext, rightContext );
    QCOMPARE( extracted, QString("a = 3;") );
}
{
    QString leftContext = QStringLiteral("void b() {\n c = 4;\n");
    QString center = QStringLiteral("a = 3;\n");
    QString rightContext =  QStringLiteral("b = 5;\n }\n");
    QString text = leftContext + center + rightContext;
    QString formatted = QStringLiteral("void b() {\n	c = 4;\n	a = 3;\n	b = 5;\n }\n");
    QString extracted = KDevelop::extractFormattedTextFromContext( formatted, text, QString(), QString() );
    QCOMPARE( extracted, formatted );
    
    extracted = KDevelop::extractFormattedTextFromContext( formatted, center, leftContext, rightContext );
    QCOMPARE( extracted, QString("	a = 3;\n	") );
}
}

void TestAstyle::overrideHelper()
{
    // think this:
    // virtual void asdf();
    // gets included into a class

    // test1: not indented
    QString formattedSource = m_formatter->formatSource(
        QStringLiteral("virtual void asdf();"), QStringLiteral("class asdf {\n    int bar();\n"), QStringLiteral("\n};")
    );
    qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("    virtual void asdf();"));

    // test2: already indented
    formattedSource = m_formatter->formatSource(
        QStringLiteral("virtual void asdf();"), QStringLiteral("class asdf {\n    int bar();\n    "), QStringLiteral("\n};")
    );
    qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("virtual void asdf();"));
}

void TestAstyle::varTypeAssistant()
{
    // think this:
    // asdf = 1;
    // and you execute the assistant to get:
    // int asdf = 1;

    // test1: already indented
    QString formattedSource = m_formatter->formatSource(
        QStringLiteral("int "), QStringLiteral("int main() {\n    "), QStringLiteral("asdf = 1;\n}\n")
    );
    qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("int "));

    // test2: not yet indented
    formattedSource = m_formatter->formatSource(
        QStringLiteral("int "), QStringLiteral("int main() {\n"), QStringLiteral("asdf = 1;\n}\n")
    );
    qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("    int "));

}

void TestAstyle::testMultipleFormatters()
{
    // just test that multiple formatters can exist at the same time
    auto* formatter1 = new AStyleFormatter;
    auto* formatter2 = new AStyleFormatter;
    delete formatter1;
    delete formatter2;
}

void TestAstyle::testMacroFormatting()
{
    AStyleFormatter fmt;
    fmt.setSpaceIndentationNoConversion(2);
    fmt.setPreprocessorIndent(true);
    QString formatted = fmt.formatSource(QStringLiteral("#define asdf\\\nfoobar\n"));
    QCOMPARE(formatted, QString("#define asdf\\\n  foobar\n"));
}

void TestAstyle::testContext()
{
    auto* formatter = new AStyleFormatter;
    formatter->setBracketFormatMode(astyle::LINUX_MODE);
    formatter->setParensInsidePaddingMode(true);
    formatter->setBlockIndent(true);
    // We enable break-blocks mode, so that we can test the newline matching
    formatter->setBreakBlocksMode(true);
    formatter->setBreakClosingHeaderBlocksMode(true);
    formatter->setParensUnPaddingMode(true);
    
    QString leftContext = QStringLiteral("int main() {\n");
    QString rightContext = QStringLiteral(";\n}\n");
    
    /// Newline tests
    
    QString formattedSource = formatter->formatSource(
        QStringLiteral(" int a;\n"), leftContext, "int b;" + rightContext );
    
//     qDebug() << formattedSource;
    // Adjust indentation
    QCOMPARE(formattedSource, QString("    int a;\n    "));
    
    formattedSource = formatter->formatSource(
        QStringLiteral(" int a;\n"), leftContext + " ", "   int b;" + rightContext );
    
//     qDebug() << formattedSource;
    QCOMPARE(formattedSource, QString("   int a;\n "));
    
    /// "if(a);" is interpreted as own block, so due to the "break blocks" option,
    /// astyle breaks these blocks with a newline in between.
    formattedSource = formatter->formatSource(
        QStringLiteral("  if(a); "), leftContext + " if(a); ", " if(a);" + rightContext );
    
//     qDebug() << formattedSource;
    QCOMPARE(formattedSource, QString("\n\n    if( a );\n\n   "));

    formattedSource = formatter->formatSource(
        QStringLiteral("  if(a); "), leftContext + " if(a);\n", " \n if(a);" + rightContext );
    
//     qDebug() << formattedSource;
    QCOMPARE(formattedSource, QString("\n    if( a );\n"));

    formattedSource = formatter->formatSource(
        QStringLiteral("  if(a)\na; "), leftContext + " if(a);\n", " \n\n if(a);" + rightContext );
    
//     qDebug() << formattedSource;
    // Adjust indentation, successor already partially indentend
    QCOMPARE(formattedSource, QString("\n    if( a )\n        a;"));
    
    /// Whitespace tests
    
    formattedSource = formatter->formatSource(
        QStringLiteral("int "), leftContext + "  ", rightContext );
    
    // 2 whitespaces are already in the context, so add only 2
//     qDebug() << "formatted source:" << formattedSource;
    QCOMPARE(formattedSource, QString("  int "));

    formattedSource = formatter->formatSource(
        QStringLiteral("q"), leftContext + "  if(", ")" + rightContext );
    
    // Padding was added around both parens
    QCOMPARE(formattedSource, QString(" q "));

    formattedSource = formatter->formatSource(
        QStringLiteral("q"), leftContext + "  if( ", " )" + rightContext );
    
    // Padding already existed around both parens
    QCOMPARE(formattedSource, QString("q"));

    formattedSource = formatter->formatSource(
        QStringLiteral(" q "), leftContext + "  if(", "   )" + rightContext );
    
//     qDebug() << formattedSource;
    // No padding on left, too much padding on right
    QCOMPARE(formattedSource, QString(" q"));

    formattedSource = formatter->formatSource(
        QStringLiteral("   "), leftContext + "  if(q", ")" + rightContext );
    
//     qDebug() << formattedSource;
    // Normalize padding: from 3 to 1
    QCOMPARE(formattedSource, QString(" "));
    
    formattedSource = formatter->formatSource(
        QString(), leftContext + "  if(", "q )" + rightContext );
    
//     qDebug() << formattedSource;
    // Normalize padding: from 0 to 1
    QCOMPARE(formattedSource, QString(" "));
    
    formattedSource = formatter->formatSource(
        QStringLiteral(" "), leftContext + "  if(   ", "q )" + rightContext );
    
//     qDebug() << formattedSource;
    // Reduce padding as much as possible
    QCOMPARE(formattedSource, QString());
    
    delete formatter;
}

void TestAstyle::testTabIndentation()
{
    AStyleFormatter formatter;
    formatter.setTabIndentation(2, false);

    const QString initial(QStringLiteral("int a() {\n  return 0;\n}\n"));
    const QString expected(QStringLiteral("int a() {\n\treturn 0;\n}\n"));
    const QString formatted = formatter.formatSource(initial);
    QCOMPARE(formatted, expected);
}

void TestAstyle::testForeach()
{
    AStyleFormatter formatter;
    QVERIFY(formatter.predefinedStyle("KDELibs"));

    const QString initial(QStringLiteral("int a(){QList<int> v;\n    foreach(int i,v){\nreturn i;}}\n"));
    const QString expected(QStringLiteral("int a()\n{\n    QList<int> v;\n    foreach (int i, v) {\n        return i;\n    }\n}\n"));
    const QString formatted = formatter.formatSource(initial);
    QCOMPARE(formatted, expected);
}

void TestAstyle::testPointerAlignment()
{
    AStyleFormatter formatter;
    formatter.setPointerAlignment(astyle::PTR_ALIGN_NAME);

    const QString initial(QStringLiteral("int* a;\nint * b;\nint *c;\nint & d;\nconst double * const e;\n"));
    const QString expected(QStringLiteral("int *a;\nint *b;\nint *c;\nint &d;\nconst double *const e;\n"));
    const QString formatted = formatter.formatSource(initial);
    QCOMPARE(formatted, expected);
}

void TestAstyle::testKdeFrameworks()
{
    AStyleFormatter formatter;
    QVERIFY(formatter.predefinedStyle("KDELibs"));

    QFETCH(QString, initial);
    QFETCH(QString, leftContext);
    QFETCH(QString, rightContext);
    QFETCH(QString, expected);

    const QString formatted = formatter.formatSource(initial, leftContext, rightContext);
    QCOMPARE(formatted, expected);
}

void TestAstyle::testKdeFrameworks_data()
{
    QTest::addColumn<QString>("initial");
    QTest::addColumn<QString>("leftContext");
    QTest::addColumn<QString>("rightContext");
    QTest::addColumn<QString>("expected");

    const QString leftContext = QStringLiteral("int main()\n{\n");
    const QString rightContext = QStringLiteral("\n}\n");

    QString initial = QStringLiteral("\t int a;");
    QString expected = QStringLiteral("    int a;");
    QTest::newRow("indentation") << initial << leftContext << rightContext << expected;

    initial = QStringLiteral("if(1);\n while (false);\nfor(int i=0; i<1; i++);\n");
    expected = QStringLiteral("    if (1);\n    while (false);\n    for (int i = 0; i < 1; i++);\n");
    QTest::newRow("space-after-keyword") << initial << leftContext << rightContext << expected;

    initial = QStringLiteral("int* a;\nint * b;\nint *c;\nint & d;\nconst double * const e;\n");
    expected = QStringLiteral("int *a;\nint *b;\nint *c;\nint &d;\nconst double *const e;\n");
    QTest::newRow("pointer-alignment") << initial << QString() << QString() << expected;

    initial = QStringLiteral("if (true)\n{\n}\n");
    expected = QStringLiteral("    if (true) {\n    }\n");
    QTest::newRow("brace-on-same-line") << initial << leftContext << rightContext << expected;

    initial = QStringLiteral("void foo(void) {\n}\nclass a {\n};\nnamespace c\n{\n}\n");
    expected = QStringLiteral("void foo(void)\n{\n}\nclass a\n{\n};\nnamespace c\n{\n}\n");
    QTest::newRow("brace-on-new-line") << initial << QString() << QString() << expected;

    initial = QStringLiteral("switch (myEnum)\n{\ncase Value1:\ndoSomething();}\n");
    expected = QStringLiteral("    switch (myEnum) {\n    case Value1:\n        doSomething();\n    }\n");
    QTest::newRow("switch-statement") << initial << leftContext << rightContext << expected;
}

#include "moc_test_astyle.cpp"