1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "TextObjectTests.h"
23 
24 #include <QDomElement>
25 
26 #include <U2Core/TextObject.h>
27 
28 namespace U2 {
29 
30 #define OBJ_ATTR "obj"
31 #define STRING_ATTR "string"
32 #define WITH_LINE_BREAK "whole_line"
33 #define MUST_EXIST "must_exist"
34 #define NEWLINES "newlines"
35 
init(XMLTestFormat *,const QDomElement & el)36 void GTest_CheckStringExists::init(XMLTestFormat *, const QDomElement &el) {
37     objContextName = el.attribute(OBJ_ATTR);
38     if (objContextName.isEmpty()) {
39         failMissingValue(OBJ_ATTR);
40         return;
41     }
42 
43     stringToCheck = el.attribute(STRING_ATTR);
44     if (stringToCheck.isNull()) {
45         failMissingValue(STRING_ATTR);
46         return;
47     }
48 
49     wholeLine = (el.attribute(WITH_LINE_BREAK) == "true");
50     mustExist = (el.attribute(MUST_EXIST) == "true");
51     newlines = (el.attribute(NEWLINES) == "true");
52 
53     while (newlines && stringToCheck.contains("\\n")) {
54         stringToCheck.replace("\\n", "\n");
55     }
56 }
57 
report()58 Task::ReportResult GTest_CheckStringExists::report() {
59     TextObject *obj = getContext<TextObject>(this, objContextName);
60     if (obj == nullptr) {
61         stateInfo.setError(QString("invalid object context"));
62         return ReportResult_Finished;
63     }
64 
65     QString stringToFind = QRegExp::escape(stringToCheck);
66     if (wholeLine) {
67         stringToFind = "^(.*\\n)?" + QRegExp::escape(stringToCheck) + "(\\n.*)?$";
68     }
69 
70     const QString text = obj->getText();
71     int index = text.indexOf(QRegExp(stringToFind));
72 
73     if (mustExist) {
74         if (-1 == index) {
75             stateInfo.setError(QString("String doesn't exist: '%1'").arg(stringToCheck));
76         }
77     } else {
78         if (-1 != index) {
79             stateInfo.setError(QString("String unexpectedly exists: '%1' at position %2").arg(stringToCheck).arg(index));
80         }
81     }
82 
83     return ReportResult_Finished;
84 }
85 
createTestFactories()86 QList<XMLTestFactory *> TextObjectTests::createTestFactories() {
87     QList<XMLTestFactory *> res;
88     res.append(GTest_CheckStringExists::createFactory());
89     return res;
90 }
91 
92 }  // namespace U2
93