Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/testlib/qxmltestlogger.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the QtTest module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    4747#include "QtTest/private/qtestresult_p.h"
    4848#include "QtTest/private/qbenchmark_p.h"
     49#include "QtTest/qtestcase.h"
    4950
    5051QT_BEGIN_NAMESPACE
     
    9192
    9293
    93 QXmlTestLogger::QXmlTestLogger(XmlMode mode ):
    94     xmlmode(mode)
     94QXmlTestLogger::QXmlTestLogger(XmlMode mode )
     95    :xmlmode(mode)
    9596{
    9697
     
    99100QXmlTestLogger::~QXmlTestLogger()
    100101{
    101 
    102 }
    103 
     102}
    104103
    105104void QXmlTestLogger::startLogging()
    106105{
    107106    QAbstractTestLogger::startLogging();
    108     char buf[1024];
     107    QTestCharBuffer buf;
    109108
    110109    if (xmlmode == QXmlTestLogger::Complete) {
    111         QTest::qt_snprintf(buf, sizeof(buf),
     110        QTestCharBuffer quotedTc;
     111        xmlQuote(&quotedTc, QTestResult::currentTestObjectName());
     112        QTest::qt_asprintf(&buf,
    112113                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
    113                 "<TestCase name=\"%s\">\n", QTestResult::currentTestObjectName());
    114         outputString(buf);
    115     }
    116 
    117     QTest::qt_snprintf(buf, sizeof(buf),
     114                "<TestCase name=\"%s\">\n", quotedTc.constData());
     115        outputString(buf.constData());
     116    }
     117
     118    QTest::qt_asprintf(&buf,
    118119            "<Environment>\n"
    119120            "    <QtVersion>%s</QtVersion>\n"
    120121            "    <QTestVersion>"QTEST_VERSION_STR"</QTestVersion>\n"
    121122            "</Environment>\n", qVersion());
    122     outputString(buf);
     123    outputString(buf.constData());
    123124}
    124125
     
    134135void QXmlTestLogger::enterTestFunction(const char *function)
    135136{
    136     char buf[1024];
    137     QTest::qt_snprintf(buf, sizeof(buf), "<TestFunction name=\"%s\">\n", function);
    138     outputString(buf);
     137    QTestCharBuffer buf;
     138    QTestCharBuffer quotedFunction;
     139    xmlQuote(&quotedFunction, function);
     140    QTest::qt_asprintf(&buf, "<TestFunction name=\"%s\">\n", quotedFunction.constData());
     141    outputString(buf.constData());
    139142}
    140143
     
    159162        else
    160163            return "<Incident type=\"%s\" file=\"%s\" line=\"%d\">\n"
    161                    "    <DataTag><![CDATA[%s%s%s%s]]></DataTag>\n"
    162                    "</Incident>\n";
     164                "    <DataTag><![CDATA[%s%s%s%s]]></DataTag>\n"
     165                "</Incident>\n";
    163166    } else {
    164167        if (noTag)
    165168            return "<Incident type=\"%s\" file=\"%s\" line=\"%d\">\n"
    166                    "    <Description><![CDATA[%s%s%s%s]]></Description>\n"
    167                    "</Incident>\n";
     169                "    <Description><![CDATA[%s%s%s%s]]></Description>\n"
     170                "</Incident>\n";
    168171        else
    169172            return "<Incident type=\"%s\" file=\"%s\" line=\"%d\">\n"
    170                    "    <DataTag><![CDATA[%s%s%s]]></DataTag>\n"
    171                    "    <Description><![CDATA[%s]]></Description>\n"
    172                    "</Incident>\n";
     173                "    <DataTag><![CDATA[%s%s%s]]></DataTag>\n"
     174                "    <Description><![CDATA[%s]]></Description>\n"
     175                "</Incident>\n";
    173176    }
    174177}
     
    186189        else
    187190            return "<Message type=\"%s\" file=\"%s\" line=\"%d\">\n"
    188                    "    <DataTag><![CDATA[%s%s%s%s]]></DataTag>\n"
    189                    "</Message>\n";
     191                "    <DataTag><![CDATA[%s%s%s%s]]></DataTag>\n"
     192                "</Message>\n";
    190193    } else {
    191194        if (noTag)
    192195            return "<Message type=\"%s\" file=\"%s\" line=\"%d\">\n"
    193                    "    <Description><![CDATA[%s%s%s%s]]></Description>\n"
    194                    "</Message>\n";
     196                "    <Description><![CDATA[%s%s%s%s]]></Description>\n"
     197                "</Message>\n";
    195198        else
    196199            return "<Message type=\"%s\" file=\"%s\" line=\"%d\">\n"
    197                    "    <DataTag><![CDATA[%s%s%s]]></DataTag>\n"
    198                    "    <Description><![CDATA[%s]]></Description>\n"
    199                    "</Message>\n";
     200                "    <DataTag><![CDATA[%s%s%s]]></DataTag>\n"
     201                "    <Description><![CDATA[%s]]></Description>\n"
     202                "</Message>\n";
    200203    }
    201204}
     
    204207
    205208void QXmlTestLogger::addIncident(IncidentTypes type, const char *description,
    206                                  const char *file, int line)
    207 {
    208     char buf[1536];
     209                                const char *file, int line)
     210{
     211    QTestCharBuffer buf;
    209212    const char *tag = QTestResult::currentDataTag();
    210213    const char *gtag = QTestResult::currentGlobalDataTag();
     
    212215    const bool notag = QTest::isEmpty(tag) && QTest::isEmpty(gtag);
    213216
    214     QTest::qt_snprintf(buf, sizeof(buf),
     217    QTestCharBuffer quotedFile;
     218    QTestCharBuffer cdataGtag;
     219    QTestCharBuffer cdataTag;
     220    QTestCharBuffer cdataDescription;
     221
     222    xmlQuote(&quotedFile, file);
     223    xmlCdata(&cdataGtag, gtag);
     224    xmlCdata(&cdataTag, tag);
     225    xmlCdata(&cdataDescription, description);
     226
     227    QTest::qt_asprintf(&buf,
    215228            QTest::incidentFormatString(QTest::isEmpty(description), notag),
    216229            QTest::xmlIncidentType2String(type),
    217             file ? file : "", line,
    218             gtag ? gtag : "",
     230            quotedFile.constData(), line,
     231            cdataGtag.constData(),
    219232            filler,
    220             tag ? tag : "",
    221             description ? description : "");
    222 
    223     outputString(buf);
     233            cdataTag.constData(),
     234            cdataDescription.constData());
     235
     236    outputString(buf.constData());
    224237}
    225238
    226239void QXmlTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
    227240{
    228     char buf[1536];
    229     QTest::qt_snprintf(
    230         buf, sizeof(buf),
     241    QTestCharBuffer buf;
     242    QTestCharBuffer quotedMetric;
     243    QTestCharBuffer quotedTag;
     244
     245    xmlQuote(&quotedMetric,
     246        QBenchmarkGlobalData::current->measurer->metricText().toAscii().constData());
     247    xmlQuote(&quotedTag, result.context.tag.toAscii().constData());
     248
     249    QTest::qt_asprintf(
     250        &buf,
    231251        QTest::benchmarkResultFormatString(),
    232         QBenchmarkGlobalData::current->measurer->metricText().toAscii().data(),
    233         result.context.tag.toAscii().data(),
     252        quotedMetric.constData(),
     253        quotedTag.constData(),
    234254        QByteArray::number(result.value).constData(),  //no 64-bit qt_snprintf support
    235         result.iterations); 
    236     outputString(buf);
     255        result.iterations);
     256    outputString(buf.constData());
    237257}
    238258
     
    240260                                const char *file, int line)
    241261{
    242     char buf[1536];
    243     char msgbuf[1024];
     262    QTestCharBuffer buf;
    244263    const char *tag = QTestResult::currentDataTag();
    245264    const char *gtag = QTestResult::currentGlobalDataTag();
     
    247266    const bool notag = QTest::isEmpty(tag) && QTest::isEmpty(gtag);
    248267
    249     QTest::qt_snprintf(msgbuf, sizeof(msgbuf), "%s",
    250                         message ? message : "");
    251 
    252     QTest::qt_snprintf(buf, sizeof(buf),
     268    QTestCharBuffer quotedFile;
     269    QTestCharBuffer cdataGtag;
     270    QTestCharBuffer cdataTag;
     271    QTestCharBuffer cdataDescription;
     272
     273    xmlQuote(&quotedFile, file);
     274    xmlCdata(&cdataGtag, gtag);
     275    xmlCdata(&cdataTag, tag);
     276    xmlCdata(&cdataDescription, message);
     277
     278    QTest::qt_asprintf(&buf,
    253279            QTest::messageFormatString(QTest::isEmpty(message), notag),
    254280            QTest::xmlMessageType2String(type),
    255             file ? file : "", line,
    256             gtag ? gtag : "",
     281            quotedFile.constData(), line,
     282            cdataGtag.constData(),
    257283            filler,
    258             tag ? tag : "",
    259             msgbuf);
    260 
    261     outputString(buf);
     284            cdataTag.constData(),
     285            cdataDescription.constData());
     286
     287    outputString(buf.constData());
     288}
     289
     290/*
     291    Copy up to n characters from the src string into dest, escaping any special
     292    XML characters as necessary so that dest is suitable for use in an XML
     293    quoted attribute string.
     294*/
     295int QXmlTestLogger::xmlQuote(QTestCharBuffer* destBuf, char const* src, size_t n)
     296{
     297    if (n == 0) return 0;
     298
     299    char *dest = destBuf->data();
     300    *dest = 0;
     301    if (!src) return 0;
     302
     303    char* begin = dest;
     304    char* end = dest + n;
     305
     306    while (dest < end) {
     307        switch (*src) {
     308
     309#define MAP_ENTITY(chr, ent) \
     310            case chr:                                   \
     311                if (dest + sizeof(ent) < end) {         \
     312                    strcpy(dest, ent);                  \
     313                    dest += sizeof(ent) - 1;            \
     314                }                                       \
     315                else {                                  \
     316                    *dest = 0;                          \
     317                    return (dest+sizeof(ent)-begin);    \
     318                }                                       \
     319                ++src;                                  \
     320                break;
     321
     322            MAP_ENTITY('>', "&gt;");
     323            MAP_ENTITY('<', "&lt;");
     324            MAP_ENTITY('\'', "&apos;");
     325            MAP_ENTITY('"', "&quot;");
     326            MAP_ENTITY('&', "&amp;");
     327
     328            // not strictly necessary, but allows handling of comments without
     329            // having to explicitly look for `--'
     330            MAP_ENTITY('-', "&#x002D;");
     331
     332#undef MAP_ENTITY
     333
     334            case 0:
     335                *dest = 0;
     336                return (dest-begin);
     337
     338            default:
     339                *dest = *src;
     340                ++dest;
     341                ++src;
     342                break;
     343        }
     344    }
     345
     346    // If we get here, dest was completely filled (dest == end)
     347    *(dest-1) = 0;
     348    return (dest-begin);
     349}
     350
     351/*
     352    Copy up to n characters from the src string into dest, escaping any
     353    special strings such that dest is suitable for use in an XML CDATA section.
     354*/
     355int QXmlTestLogger::xmlCdata(QTestCharBuffer *destBuf, char const* src, size_t n)
     356{
     357    if (!n) return 0;
     358
     359    char *dest = destBuf->data();
     360
     361    if (!src || n == 1) {
     362        *dest = 0;
     363        return 0;
     364    }
     365
     366    static char const CDATA_END[] = "]]>";
     367    static char const CDATA_END_ESCAPED[] = "]]]><![CDATA[]>";
     368
     369    char* begin = dest;
     370    char* end = dest + n;
     371    while (dest < end) {
     372        if (!*src) {
     373            *dest = 0;
     374            return (dest-begin);
     375        }
     376
     377        if (!strncmp(src, CDATA_END, sizeof(CDATA_END)-1)) {
     378            if (dest + sizeof(CDATA_END_ESCAPED) < end) {
     379                strcpy(dest, CDATA_END_ESCAPED);
     380                src += sizeof(CDATA_END)-1;
     381                dest += sizeof(CDATA_END_ESCAPED) - 1;
     382            }
     383            else {
     384                *dest = 0;
     385                return (dest+sizeof(CDATA_END_ESCAPED)-begin);
     386            }
     387            continue;
     388        }
     389
     390        *dest = *src;
     391        ++src;
     392        ++dest;
     393    }
     394
     395    // If we get here, dest was completely filled (dest == end)
     396    *(dest-1) = 0;
     397    return (dest-begin);
     398}
     399
     400typedef int (*StringFormatFunction)(QTestCharBuffer*,char const*,size_t);
     401
     402/*
     403    A wrapper for string functions written to work with a fixed size buffer so they can be called
     404    with a dynamically allocated buffer.
     405*/
     406int allocateStringFn(QTestCharBuffer* str, char const* src, StringFormatFunction func)
     407{
     408    static const int MAXSIZE = 1024*1024*2;
     409
     410    int size = str->size();
     411
     412    int res = 0;
     413
     414    for (;;) {
     415        res = func(str, src, size);
     416        str->data()[size - 1] = '\0';
     417        if (res < size) {
     418            // We succeeded or fatally failed
     419            break;
     420        }
     421        // buffer wasn't big enough, try again
     422        size *= 2;
     423        if (size > MAXSIZE) {
     424            break;
     425        }
     426        if (!str->reset(size))
     427            break; // ran out of memory - bye
     428    }
     429
     430    return res;
     431}
     432
     433int QXmlTestLogger::xmlQuote(QTestCharBuffer* str, char const* src)
     434{
     435    return allocateStringFn(str, src, QXmlTestLogger::xmlQuote);
     436}
     437
     438int QXmlTestLogger::xmlCdata(QTestCharBuffer* str, char const* src)
     439{
     440    return allocateStringFn(str, src, QXmlTestLogger::xmlCdata);
    262441}
    263442
Note: See TracChangeset for help on using the changeset viewer.