From 1c127fd643e212e7f4860cfc1735e510f5cc72a3 Mon Sep 17 00:00:00 2001 From: ericmenezesn@gmail.com Date: Thu, 17 Jul 2014 16:14:03 -0300 Subject: [PATCH] Inclusão do teste e dos métodos: -getValueFromTags -getValueFromFile -setValueToFile -createFolder -deleteFolder -deleteFile --- cacic-teste/testcacic.cpp | 39 +++++++++++++++++++++++++++------------ cacic-teste/testcacic.h | 7 ++++++- src/ccacic.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- src/ccacic.h | 7 ++++++- 4 files changed, 108 insertions(+), 24 deletions(-) diff --git a/cacic-teste/testcacic.cpp b/cacic-teste/testcacic.cpp index e293a01..c31a4d5 100644 --- a/cacic-teste/testcacic.cpp +++ b/cacic-teste/testcacic.cpp @@ -14,8 +14,8 @@ CTestCacic::CTestCacic(QObject *parent) : "1", "0", "0", - "YwpgjzZ86/eCsjvOki1KkQ==__CRYPTED__", - "8PT6U445MPNr3S7WHFa20ge/8NJSspSYE/UL3zf[[MAIS]]wkEfNZ7p1B/2o6i89gTZ44Ok__CRYPTED__", + /*QDir::currentPath(), */"YwpgjzZ86/eCsjvOki1KkQ==__CRYPTED__", + /*QTest::currentAppName(), */"8PT6U445MPNr3S7WHFa20ge/8NJSspSYE/UL3zf[[MAIS]]wkEfNZ7p1B/2o6i89gTZ44Ok__CRYPTED__", "wshPlZWvLPhbUEQFQ1aQmA==__CRYPTED__", "E9aKtVz/OSmGv7v1xW4FwaFukOob3qQ/HFDIz916K3ys82qfX9vBDz[[MAIS]]M2cdoYedhNkXt[[MAIS]]UVb10nD[[MAIS]]tOfbn7mlOlTDoGzUl9CgdPCsb[[MAIS]]Qa4wzuCrMw0BcCuaKfr62E3b8vh6Ug4JJbOgBo3ohG3x5O7Ap0D94GKtAWjMuTXRBbhGxPrRS4M6AhzvaN5P8pWd9FikrCLWuMs4ebnRlftef3RmVKScBDQ5zmK6sKxYNmX[[MAIS]]kKn/Gi2[[MAIS]]HftZ7HD9JLvZBpYhp4yjqShblQ67d6PlWgrRWXxwDseIiJe3NlF70zrCPBBelkeAhVDXMXnsYtLms3ElsLzxa3c5vpWzTfgyE[[MAIS]]hvALy4ivew7zaxEwrg2KY1T7GOSWZ7vljIvbhVZJgtzbnYiulFjJTtm5W4uJMKLuUJNzB0rxMkzToIE9/kDmeO2OKiwFbIfbe2Wdq0VpllOkjKtj9WM676iBSpYAXG0MsmLMj9CZ/sNi06hGF[[MAIS]]rXxiOSk66M0Hq[[MAIS]]OeUBh4z3Ra6f9yPJn0K9hIQyT[[MAIS]]Dr/QW/QdJIOkLTW8ZyyAF4DW9ojiTlUdySvB5EB1jyZ5nNTpzyE38qIdg7uM1tdWdm9FZGYxMdHwi1tS2dBeIWkDfWEjih01xiVt2dmdRgo0zIZvmmYDJ9LhCU3XPDlwdeQ8[[MAIS]]F7fnxDfxGqZQjZJ[[MAIS]]Y/Q8bdLpgbhcd3gt3SzjMLE0M[[MAIS]]hTmHG1KP2CzCibZ7grFAN3Y3jSPjmEj5Fzs26tdhdjNzSIt1FCueea8P2c4NmxPYkJCg0D1mgNrkH3L5svsO[[MAIS]]6YYrl3df4s[[MAIS]]fcK/to9s/pnkKfBKrAPF6Z4s2WPsALUPBDS5ty7RC2vqXRvH6hPR4o7xhXzQOQ/1xytr/RoEWEVeZ2wgHVVbbtFsALul/IDc7B45wmwxBx9iZHVwpQJVlNZZUBtwKqKhnXz5pM[[MAIS]]7yUAkfhMcO09pPd5kB2MBdRJs[[MAIS]]2FIk0GRUw05IHkc1rZEX5Ow==__CRYPTED__", "Yot8BeM9lOh431SB7dYQXw==__CRYPTED__", @@ -23,8 +23,26 @@ CTestCacic::CTestCacic(QObject *parent) : "2.5.1.1.256.32", "2.8.1.7", "2.8.1.6"); + testPath = QDir::currentPath() + "/teste"; + testIniPath = testPath + "/teste.ini"; } +void CTestCacic::testCreateFolder() +{ + QVERIFY(OCacic.createFolder(testPath)); +} + +void CTestCacic::testGetAndSetValueFromFile() +{ + OCacic.setValueToFile("Teste", "teste", "Valor de teste", testIniPath); + QVERIFY( OCacic.getValueFromFile("Teste", "teste", testIniPath) == "Valor de teste"); +} + +void CTestCacic::testGetValueFromTags() +{ + QString value = "blablab[teste]Valor de teste[/teste]feihgj"; + QVERIFY(OCacic.getValueFromTags(value, "teste") == "Valor de teste"); +} void CTestCacic::testCommStatus() { @@ -34,21 +52,18 @@ void CTestCacic::testCommStatus() void CTestCacic::testComm() { - if (OCacicComm.commStatus()) - QVERIFY(OCacicComm.comm("/ws/get/test") != QString("")); - else + if (OCacicComm.commStatus()){ + QVERIFY(OCacic.getValueFromTags(OCacicComm.comm("/ws/get/test"), "Comm_Status", "<>") == QString("OK")); + } else QSKIP("Teste de comunicação negativo!"); } -void CTestCacic::testGetValueFromFile() +void CTestCacic::testDeleteFile() { - QVERIFY( OCacic.getValueFromFile("Teste", "teste","E:/teste.txt") == "Valor de teste"); + QVERIFY(OCacic.deleteFile(testIniPath)); } -void CTestCacic::testGetValueFromTags() +void CTestCacic::testDeleteFolder() { - QString value = "blablab[teste]Valor de teste[/teste]feihgj"; - QString retorno = OCacic.getValueFromTags(value, "teste"); - qDebug(retorno.toLocal8Bit()); - QVERIFY(retorno == "Valor de teste"); + QVERIFY(OCacic.deleteFolder(testPath)); } diff --git a/cacic-teste/testcacic.h b/cacic-teste/testcacic.h index 463cf40..247c158 100644 --- a/cacic-teste/testcacic.h +++ b/cacic-teste/testcacic.h @@ -14,6 +14,8 @@ public: private: CacicComm OCacicComm; CCacic OCacic; + QString testPath; + QString testIniPath; signals: @@ -22,8 +24,11 @@ public slots: private slots: void testCommStatus(); void testComm(); - void testGetValueFromFile(); + void testGetAndSetValueFromFile(); void testGetValueFromTags(); + void testCreateFolder(); + void testDeleteFolder(); + void testDeleteFile(); }; #endif // TESTINSTALLCACIC_H diff --git a/src/ccacic.cpp b/src/ccacic.cpp index 82b0654..2ade4e4 100644 --- a/src/ccacic.cpp +++ b/src/ccacic.cpp @@ -4,35 +4,61 @@ CCacic::CCacic(QObject *parent) : QObject(parent) { } - +/* getValueFromFile + * Pega valor específico dentro de um arquivo + * @parameter QString sectionName: nome da seção onde estará a key) + * @parameter QString keyName: nome da key que está o valor) + * @parameter QString filePath: caminho do arquivo que será aberto; + * + * @return QString: "" (vazio) Caso não encontre, + * "0" caso não seja possível abrir o arquivo; + */ QString CCacic::getValueFromFile(QString sectionName, QString keyName, QString filePath) { QFile file(filePath); - sectionName = "<" + sectionName + ">"; + QString line = ""; + sectionName = "[" + sectionName + "]"; keyName = keyName + "="; int sizeKeyName = keyName.size(); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return "0"; - - QString line = file.readLine(); - while (!file.atEnd()) { + while (!file.atEnd()){ + line = file.readLine(); if (line.contains(sectionName, Qt::CaseInsensitive)) { - line = file.readLine(); - while (!file.atEnd()){ + do { + line = file.readLine(); if (line.contains(keyName, Qt::CaseInsensitive)) { file.close(); return line.mid(sizeKeyName).trimmed(); } - } - } else { - line = file.readLine(); + } while (!file.atEnd()); } } file.close(); return ""; } +/*getValueFromTags + * @parameter QString sectionName: Grupo onde está a chave; + * @parameter QString keyName: chave que queremos para colocar o valor; + * @parameter QString value: valor para colocar dentro da chave; + * @parameter QString filePath: Local do arquivo; + */ +void CCacic::setValueToFile(QString sectionName, QString keyName, QString value, QString filePath) +{ + QSettings settings(filePath, QSettings::IniFormat); + settings.beginGroup(sectionName); + settings.setValue(keyName, value); + settings.endGroup(); +} +/*getValueFromTags + * @parameter QString fullString: string onde será pesquisado o valor desejado; + * @parameter QString tag: nome da tag onde estará o valor; + * @parameter QString tagType: padrão "[]", tipo da tag; + * + * @return: QString: valor entre as tags. + */ QString CCacic::getValueFromTags(QString fullString, QString tag, QString tagType) { QString tagFim = tagType.mid(0,1) + "/" + tag + tagType.mid(1); int tagSize; @@ -41,6 +67,39 @@ QString CCacic::getValueFromTags(QString fullString, QString tag, QString tagTyp return fullString.mid(fullString.indexOf(tag) + tagSize, fullString.indexOf(tagFim) - (fullString.indexOf(tag) + tagSize)).trimmed(); } +/*createFolder + * @parameter QString path: caminho onde será criado o diretório, sendo criado toda a árvore se necessário. + * @return bool: true se conseguir ou já existir, false se não. + */ +bool CCacic::createFolder(QString path) +{ + QDir folder(path); + return folder.mkpath(path); +} +/*deleteFolder + * @parameter QString path: caminho do diretório; + * @return bool: true se conseguir ou já não existir, false se não conseguir. + */ +bool CCacic::deleteFolder(QString path) +{ + QDir folder(path); + if (folder.exists()) + return folder.removeRecursively(); + else + return true; +} +/*deleteFile + * @parameter QString path: caminho do arquivo; + * @return bool: true se conseguir ou já não existir, false se não conseguir. + */ +bool CCacic::deleteFile(QString path) +{ + QDir folder(path); + if (folder.exists()) + return folder.remove(path); + else + return true; +} /*Getters/Setters * Begin: diff --git a/src/ccacic.h b/src/ccacic.h index f8552c5..6fed412 100644 --- a/src/ccacic.h +++ b/src/ccacic.h @@ -5,7 +5,8 @@ #include #include #include -#include +#include +#include class CCacic : public QObject { @@ -13,7 +14,11 @@ class CCacic : public QObject public: explicit CCacic(QObject *parent = 0); QString getValueFromFile(QString sectionName, QString keyName, QString filePath); + void setValueToFile(QString sectionName, QString keyName, QString value, QString filePath); QString getValueFromTags(QString fullString, QString tag, QString tagType = "[]"); + bool createFolder(QString path); + bool deleteFolder(QString path); + bool deleteFile(QString path); //Geters/seters: -- libgit2 0.21.2