diff options
author | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2016-06-21 11:16:39 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2016-06-22 12:22:57 +0000 |
commit | 1ea640433736d599a2ec0cb716924db1560f59a3 (patch) | |
tree | c19e3d55c5d4200b0aa15a64e0342969fb7d2528 /src/plugins/cppeditor/cppquickfix_test.cpp | |
parent | b928a1ce46a810941a54c42b20f4692f2db2ebb0 (diff) | |
download | qt-creator-1ea640433736d599a2ec0cb716924db1560f59a3.tar.gz |
CppTools: Allow prefering getter names with "get" prefix
We default to "foo()" for e.g. a member variable "m_foo", but other
coding styles require "getFoo()".
Task-number: QTCREATORBUG-16452
Change-Id: I9ccfdf88e4c469bc1c06fde855ad754faf2bd238
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Diffstat (limited to 'src/plugins/cppeditor/cppquickfix_test.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppquickfix_test.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index ddd2b5097d..742106252a 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -1890,6 +1890,85 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlySetter() QuickFixOperationTest(testDocuments, &factory, ProjectPartHeaderPaths(), 2); } +class CppCodeStyleSettingsChanger { +public: + CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings); + ~CppCodeStyleSettingsChanger(); // Restore original + + static CppCodeStyleSettings currentSettings(); + +private: + void setSettings(const CppCodeStyleSettings &settings); + + CppCodeStyleSettings m_originalSettings; +}; + +CppCodeStyleSettingsChanger::CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings) +{ + m_originalSettings = currentSettings(); + setSettings(settings); +} + +CppCodeStyleSettingsChanger::~CppCodeStyleSettingsChanger() +{ + setSettings(m_originalSettings); +} + +void CppCodeStyleSettingsChanger::setSettings(const CppCodeStyleSettings &settings) +{ + QVariant variant; + variant.setValue(settings); + + CppCodeStylePreferences *preferences = CppToolsSettings::instance()->cppCodeStyle(); + preferences->currentDelegate()->setValue(variant); +} + +CppCodeStyleSettings CppCodeStyleSettingsChanger::currentSettings() +{ + CppCodeStylePreferences *preferences = CppToolsSettings::instance()->cppCodeStyle(); + return preferences->currentDelegate()->value().value<CppCodeStyleSettings>(); +} + +void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlyGetter_DontPreferGetterWithGet() +{ + CppCodeStyleSettings modifiedSettings = CppCodeStyleSettingsChanger::currentSettings(); + modifiedSettings.preferGetterNameWithoutGetPrefix = false; + CppCodeStyleSettingsChanger changer(modifiedSettings); + + QList<QuickFixTestDocument::Ptr> testDocuments; + QByteArray original; + QByteArray expected; + + // Header File + original = + "class Foo\n" + "{\n" + "public:\n" + " int m_bar@;\n" + "};\n"; + expected = + "class Foo\n" + "{\n" + "public:\n" + " int m_bar@;\n" + " int getBar() const;\n" + "};\n"; + testDocuments << QuickFixTestDocument::create("file.h", original, expected); + + // Source File + original.resize(0); + expected = + "\n" + "int Foo::getBar() const\n" + "{\n" + " return m_bar;\n" + "}\n"; + testDocuments << QuickFixTestDocument::create("file.cpp", original, expected); + + GenerateGetterSetter factory; + QuickFixOperationTest(testDocuments, &factory, ProjectPartHeaderPaths(), 1); +} + /// Checks: Offer a "generate getter" quick fix if there is a setter void CppEditorPlugin::test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent() { |