• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pcolby / doxlee / 9041885859

11 May 2024 06:52AM UTC coverage: 74.501% (+0.2%) from 74.286%
9041885859

push

github

pcolby
Apply categories to all log output

11 of 46 new or added lines in 3 files covered. (23.91%)

6 existing lines in 3 files now uncovered.

336 of 451 relevant lines covered (74.5%)

1188.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.36
/src/variant.cpp
1
// SPDX-FileCopyrightText: 2021-2024 Paul Colby <git@colby.id.au>
2
// SPDX-License-Identifier: LGPL-3.0-or-later
3

4
#include "variant.h"
5

6
#include <QCoreApplication>
7
#include <QDebug>
8
#include <QLoggingCategory>
9
#include <QXmlStreamReader>
10

11
/// Shorten QCoreApplication::translate calls for readability.
12
#define QTR(str) QCoreApplication::translate("toVariant", str)
13

14
namespace doxlee {
15

NEW
16
static Q_LOGGING_CATEGORY(lc, "doxlee.variant", QtInfoMsg);
×
17

18
void sortBy(QVariantList &list, const QString &key)
144✔
19
{
20
    std::sort(list.begin(), list.end(),
108✔
21
        [&key](const QVariant &a, const QVariant &b) {
3,024✔
22
            return a.toMap().value(key).toString() < b.toMap().value(key).toString();
1,008✔
23
        });
24
}
144✔
25

26
QVariantMap toVariant(const QHash<QString,QVariantList> &hash)
48✔
27
{
28
    QVariantMap map;
36✔
29
    for (auto iter = hash.begin(); iter != hash.end(); ++iter) {
84✔
30
        map.insert(iter.key(), iter.value());
144✔
31
    }
32
    return map;
48✔
33
}
34

35
/// Borrowed from (my own old) gist: https://gist.github.com/pcolby/6558910
36
QVariantMap toVariant(QXmlStreamReader &xml, const QString &prefix, const int maxDepth)
8,832✔
37
{
38
    if (maxDepth < 0) {
8,832✔
NEW
39
        qCWarning(lc).noquote() << QTR("max depth exceeded");
×
40
        return QVariantMap();
41
    }
42

43
    if (xml.hasError()) {
8,832✔
NEW
44
        qCWarning(lc).noquote() << xml.errorString();
×
45
        return QVariantMap();
46
    }
47

48
    if (xml.tokenType() == QXmlStreamReader::NoToken)
8,832✔
49
        xml.readNext();
72✔
50

51
    if ((xml.tokenType() != QXmlStreamReader::StartDocument) &&
17,592✔
52
        (xml.tokenType() != QXmlStreamReader::StartElement)) {
8,760✔
NEW
53
        qCWarning(lc).noquote() << QTR("unexpected XML tokenType %1 (%2)").arg(xml.tokenString()).arg(xml.tokenType());
×
54
        return QVariantMap();
55
    }
56

57
    QMap<QString, QVariant> map;
6,624✔
58
    if (xml.tokenType() == QXmlStreamReader::StartDocument) {
8,832✔
59
        map.insert(prefix + QLatin1String("DocumentEncoding"), xml.documentEncoding().toString());
198✔
60
        map.insert(prefix + QLatin1String("DocumentVersion"), xml.documentVersion().toString());
198✔
61
        map.insert(prefix + QLatin1String("StandaloneDocument"), xml.isStandaloneDocument());
144✔
62
    } else {
63
        if (!xml.namespaceUri().isEmpty())
8,760✔
64
            map.insert(prefix + QLatin1String("NamespaceUri"), xml.namespaceUri().toString());
×
65
        const auto attributes = xml.attributes();
8,760✔
66
        for (const QXmlStreamAttribute &attribute: attributes) {
18,408✔
67
            QVariantMap attributeMap;
7,236✔
68
            attributeMap.insert(QLatin1String("Value"), attribute.value().toString());
19,296✔
69
            if (!attribute.namespaceUri().isEmpty())
9,648✔
70
                attributeMap.insert(QLatin1String("NamespaceUri"), attribute.namespaceUri().toString());
288✔
71
            if (!attribute.prefix().isEmpty())
9,648✔
72
                attributeMap.insert(QLatin1String("Prefix"), attribute.prefix().toString());
288✔
73
            attributeMap.insert(QLatin1String("QualifiedName"), attribute.qualifiedName().toString());
19,296✔
74
            map.insert(prefix + attribute.name().toString(), attributeMap);
24,120✔
75
        }
2,412✔
76
    }
77

78
    for (xml.readNext(); (!xml.atEnd()) && (xml.tokenType() != QXmlStreamReader::EndElement)
68,040✔
79
          && (xml.tokenType() != QXmlStreamReader::EndDocument); xml.readNext()) {
77,880✔
80
        switch (xml.tokenType()) {
23,016✔
81
        case QXmlStreamReader::Characters:
14,256✔
82
        case QXmlStreamReader::Comment:
83
        case QXmlStreamReader::DTD:
84
        case QXmlStreamReader::EntityReference:
85
            map.insert(prefix + xml.tokenString(), xml.text().toString());
35,640✔
86
            break;
14,256✔
87
        case QXmlStreamReader::ProcessingInstruction:
×
88
            map.insert(prefix + xml.processingInstructionTarget().toString(),
×
89
                            xml.processingInstructionData().toString());
×
90
            break;
×
91
        case QXmlStreamReader::StartElement:
8,760✔
92
            map.insert(xml.name().toString(), toVariant(xml, prefix, maxDepth-1));
21,900✔
93
            break;
8,760✔
UNCOV
94
        default:
×
NEW
95
            qCWarning(lc) << QTR("unexpected XML tokenType %1 (%2)").arg(xml.tokenString()).arg(xml.tokenType());
×
96
        }
97
    }
98
    return QVariantMap(map);
2,208✔
99
}
2,208✔
100

101
} // namespace doxlee
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc