| 68 | } |
| 69 | |
| 70 | void TestAstyle::testTabMatching() |
| 71 | { |
| 72 | // Some formatting styles inserts "{" and "}" parens behind "ifs", or change comment styles |
| 73 | // The actual text changes, thus it is difficult to match original and formatted text |
| 74 | |
| 75 | { |
| 76 | // Mismatch: There is a preceding tab, but the formatter replaces the tab with spaces |
| 77 | // The tab is matched with 2 spaces, since we set tab-width 2 |
| 78 | QString extracted = KDevelop::extractFormattedTextFromContext( |
| 79 | QStringLiteral("class C {\n class A;\n}\n"), |
| 80 | QStringLiteral("class A;"), QStringLiteral("class C {\n "), QStringLiteral("\n}\n"), 2 ); |
| 81 | QCOMPARE( extracted, QString("class A;") ); |
| 82 | |
| 83 | // Two tabs are inserted instead of 1 |
| 84 | extracted = KDevelop::extractFormattedTextFromContext( |
| 85 | QStringLiteral("class C {\n class A;\n}\n"), |
| 86 | QStringLiteral("class A;"), QStringLiteral("class C {\n "), QStringLiteral("\n}\n"), 2 ); |
| 87 | QCOMPARE( extracted, QString(" class A;") ); |
| 88 | |
| 89 | // One space is inserted behind the tab |
| 90 | extracted = KDevelop::extractFormattedTextFromContext( |
| 91 | QStringLiteral("class C {\n class A;\n}\n"), |
| 92 | QStringLiteral("class A;"), QStringLiteral("class C {\n "), QStringLiteral("\n}\n"), 2 ); |
| 93 | QCOMPARE( extracted, QString(" class A;") ); |
| 94 | |
| 95 | // Two tabs are inserted, with 2 preceding whitespaces |
| 96 | // Add only 1 tab |
| 97 | extracted = KDevelop::extractFormattedTextFromContext( |
| 98 | QStringLiteral("class C {\n class A;\n}\n"), |
| 99 | QStringLiteral("class A;"), QStringLiteral("class C {\n "), QStringLiteral("\n}\n"), 2 ); |
| 100 | QCOMPARE( extracted, QString(" class A;") ); |
| 101 | |
| 102 | extracted = KDevelop::extractFormattedTextFromContext( |
| 103 | QStringLiteral("class C {\n class A;\n}\n"), |
| 104 | QStringLiteral("class A;"), QStringLiteral("class C {\n "), QStringLiteral("\n}\n"), 4 ); |
| 105 | QCOMPARE( extracted, QString(" class A;") ); |
| 106 | } |
| 107 | { |
| 108 | // Already correctly formatted |
| 109 | QString leftContext = QStringLiteral("void b() {\n c = 4;\n "); |
| 110 | QString center = QStringLiteral("a = 3;"); |
| 111 | QString rightContext = QStringLiteral("\n b = 5;\n }\n"); |
| 112 | QString text = leftContext + center + rightContext; |
| 113 | QString formatted = QStringLiteral("void b() {\n c = 4;\n a = 3;\n b = 5;\n }\n"); |
| 114 | QString extracted = KDevelop::extractFormattedTextFromContext( formatted, text, QString(), QString() ); |
| 115 | QCOMPARE( extracted, formatted ); |
| 116 | |
| 117 | extracted = KDevelop::extractFormattedTextFromContext( formatted, center, leftContext, rightContext ); |
| 118 | QCOMPARE( extracted, QString("a = 3;") ); |
| 119 | } |
| 120 | { |
| 121 | QString leftContext = QStringLiteral("void b() {\n c = 4;\n"); |
| 122 | QString center = QStringLiteral("a = 3;\n"); |
| 123 | QString rightContext = QStringLiteral("b = 5;\n }\n"); |
| 124 | QString text = leftContext + center + rightContext; |
| 125 | QString formatted = QStringLiteral("void b() {\n c = 4;\n a = 3;\n b = 5;\n }\n"); |
| 126 | QString extracted = KDevelop::extractFormattedTextFromContext( formatted, text, QString(), QString() ); |
| 127 | QCOMPARE( extracted, formatted ); |