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
|
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "astyletest.h"
#include <QtTest/QTest>
#include <QDebug>
#include "../astyle_formatter.h"
#include <util/formattinghelpers.h>
QTEST_MAIN(AstyleTest)
void AstyleTest::initTestCase()
{
m_formatter = new 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->setSpaceIndentation(4);
}
void AstyleTest::renameVariable()
{
// think this:
// int asdf = 1;
// e.g. asdf was before something different and got renamed
QString formattedSource = m_formatter->formatSource(
"asdf", "int ", " = 1;"
);
qDebug() << "formatted source:" << formattedSource;
QCOMPARE(formattedSource, QString("asdf"));
// int main() {
// if(asdf){}}
formattedSource = m_formatter->formatSource(
"asdf", "int main(){\n if(", "){}}"
);
qDebug() << "formatted source:" << formattedSource;
QCOMPARE(formattedSource, QString("asdf"));
}
void AstyleTest::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 = "void b() {/*some comment*/\nif( ";
QString center = "a[ 0]";
QString rightContext = " ) q;\n }\n";
QString text = leftContext + center + rightContext;
QString formatted = "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]") );
}
void AstyleTest::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(
"class C {\n class A;\n}\n",
"class A;", "class C {\n ", "\n}\n", 2 );
QCOMPARE( extracted, QString("class A;") );
// Two tabs are inserted insead of 1
extracted = KDevelop::extractFormattedTextFromContext(
"class C {\n class A;\n}\n",
"class A;", "class C {\n ", "\n}\n", 2 );
QCOMPARE( extracted, QString(" class A;") );
// One space is inserted behind the tab
extracted = KDevelop::extractFormattedTextFromContext(
"class C {\n class A;\n}\n",
"class A;", "class C {\n ", "\n}\n", 2 );
QCOMPARE( extracted, QString(" class A;") );
// Two tabs are inserted, with 2 preceding whitespaces
// Add only 1 tab
extracted = KDevelop::extractFormattedTextFromContext(
"class C {\n class A;\n}\n",
"class A;", "class C {\n ", "\n}\n", 2 );
QCOMPARE( extracted, QString(" class A;") );
extracted = KDevelop::extractFormattedTextFromContext(
"class C {\n class A;\n}\n",
"class A;", "class C {\n ", "\n}\n", 4 );
QCOMPARE( extracted, QString(" class A;") );
}
{
// Already correctly formatted
QString leftContext = "void b() {\n c = 4;\n ";
QString center = "a = 3;";
QString rightContext = "\n b = 5;\n }\n";
QString text = leftContext + center + rightContext;
QString formatted = "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 = "void b() {\n c = 4;\n";
QString center = "a = 3;\n";
QString rightContext = "b = 5;\n }\n";
QString text = leftContext + center + rightContext;
QString formatted = "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 AstyleTest::overrideHelper()
{
// think this:
// virtual void asdf();
// gets included into a class
// test1: not indented
QString formattedSource = m_formatter->formatSource(
"virtual void asdf();", "class asdf {\n int bar();\n", "\n};"
);
qDebug() << "formatted source:" << formattedSource;
QCOMPARE(formattedSource, QString(" virtual void asdf();"));
// test2: already indented
formattedSource = m_formatter->formatSource(
"virtual void asdf();", "class asdf {\n int bar();\n ", "\n};"
);
qDebug() << "formatted source:" << formattedSource;
QCOMPARE(formattedSource, QString("virtual void asdf();"));
}
void AstyleTest::varTypeAssistant()
{
// think this:
// asdf = 1;
// and you execute the assitant to get:
// int asdf = 1;
// test1: already indented
QString formattedSource = m_formatter->formatSource(
"int ", "int main() {\n ", "asdf = 1;\n}\n"
);
qDebug() << "formatted source:" << formattedSource;
QCOMPARE(formattedSource, QString("int "));
// test2: not yet indented
formattedSource = m_formatter->formatSource(
"int ", "int main() {\n", "asdf = 1;\n}\n"
);
qDebug() << "formatted source:" << formattedSource;
QCOMPARE(formattedSource, QString(" int "));
}
void AstyleTest::testMultipleFormatters()
{
// just test that multiple formatters can exist at the same time
AStyleFormatter* formatter1 = new AStyleFormatter;
AStyleFormatter* formatter2 = new AStyleFormatter;
delete formatter1;
delete formatter2;
}
void AstyleTest::testMacroFormatting()
{
AStyleFormatter fmt;
fmt.setSpaceIndentation(2);
fmt.setPreprocessorIndent(true);
QString formatted = fmt.formatSource("#define asdf\\\nfoobar\n");
QCOMPARE(formatted, QString("#define asdf\\\n foobar\n"));
}
void AstyleTest::testContext()
{
AStyleFormatter* 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 = "int main() {\n";
QString rightContext = ";\n}\n";
/// Newline tests
QString formattedSource = formatter->formatSource(
" int a;\n", leftContext, "int b;" + rightContext );
// qDebug() << formattedSource;
// Adjust indentation
QCOMPARE(formattedSource, QString(" int a;\n "));
formattedSource = formatter->formatSource(
" 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(
" if(a); ", leftContext + " if(a); ", " if(a);" + rightContext );
// qDebug() << formattedSource;
QCOMPARE(formattedSource, QString("\n\n if( a );\n\n "));
formattedSource = formatter->formatSource(
" if(a); ", leftContext + " if(a);\n", " \n if(a);" + rightContext );
// qDebug() << formattedSource;
QCOMPARE(formattedSource, QString("\n if( a );\n"));
formattedSource = formatter->formatSource(
" 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(
"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(
"q", leftContext + " if(", ")" + rightContext );
// Padding was added around both parens
QCOMPARE(formattedSource, QString(" q "));
formattedSource = formatter->formatSource(
"q", leftContext + " if( ", " )" + rightContext );
// Padding already existed around both parens
QCOMPARE(formattedSource, QString("q"));
formattedSource = formatter->formatSource(
" q ", leftContext + " if(", " )" + rightContext );
// qDebug() << formattedSource;
// No padding on left, too much padding on right
QCOMPARE(formattedSource, QString(" q"));
formattedSource = formatter->formatSource(
" ", leftContext + " if(q", ")" + rightContext );
// qDebug() << formattedSource;
// Normalize padding: from 3 to 1
QCOMPARE(formattedSource, QString(" "));
formattedSource = formatter->formatSource(
"", leftContext + " if(", "q )" + rightContext );
// qDebug() << formattedSource;
// Normalize padding: from 0 to 1
QCOMPARE(formattedSource, QString(" "));
formattedSource = formatter->formatSource(
" ", leftContext + " if( ", "q )" + rightContext );
// qDebug() << formattedSource;
// Reduce padding as much as possible
QCOMPARE(formattedSource, QString(""));
delete formatter;
}
void AstyleTest::testTabIndentation()
{
AStyleFormatter formatter;
formatter.setTabSpaceConversionMode(false);
formatter.setTabIndentation(2, false);
const QString initial("int a() {\n return 0;\n}\n");
const QString expected("int a() {\n\treturn 0;\n}\n");
const QString formatted = formatter.formatSource(initial);
QCOMPARE(formatted, expected);
}
#include "astyletest.moc"
|