From e2799bbbb359cabf99cf20ca6d9fd463d11da4c2 Mon Sep 17 00:00:00 2001 From: Markus Kauppila Date: Fri, 24 Jun 2011 14:35:14 +0300 Subject: [PATCH] Added case insensitivity to XML elements. --- test/test-automation/logger.c | 12 +++- test/test-automation/logger.h | 2 +- test/test-automation/plain_logger.c | 10 +-- test/test-automation/xml.c | 97 ++++++++++++++++++----------- test/test-automation/xml.h | 5 -- test/test-automation/xml_logger.c | 14 ++--- 6 files changed, 83 insertions(+), 57 deletions(-) diff --git a/test/test-automation/logger.c b/test/test-automation/logger.c index 5a6d548b8..a7969eb6f 100644 --- a/test/test-automation/logger.c +++ b/test/test-automation/logger.c @@ -4,6 +4,8 @@ #include #include +#include + #include "logger.h" #include "xml_logger.h" #include "plain_logger.h" @@ -25,9 +27,15 @@ LogFp Log = 0; * \return Possible error value (\todo) */ int -LogGenericOutput(const char *message) +LogGenericOutput(const char *message, ...) { - fprintf(stderr, "%s\n", message); + va_list list; + va_start(list, message); + + char buffer[1024]; + SDL_vsnprintf(buffer, sizeof(buffer), message, list); + + fprintf(stderr, "%s\n", buffer); fflush(stderr); } diff --git a/test/test-automation/logger.h b/test/test-automation/logger.h index 917c1a757..9a570143a 100644 --- a/test/test-automation/logger.h +++ b/test/test-automation/logger.h @@ -24,7 +24,7 @@ #include // Function pointer to function which handles to output -typedef int (*LogOutputFp)(const char *); +typedef int (*LogOutputFp)(const char *, ...); /*! diff --git a/test/test-automation/plain_logger.c b/test/test-automation/plain_logger.c index 853b38919..0f5902f1b 100644 --- a/test/test-automation/plain_logger.c +++ b/test/test-automation/plain_logger.c @@ -25,14 +25,14 @@ PlainRunEnded(int testCount, int suiteCount, int testPassCount, int testFailCoun void PlainSuiteStarted(const char *suiteName, time_t eventTime) { - printf("Executing tests in %s\n", suiteName); + logger("Executing tests in %s\n", suiteName); } void PlainSuiteEnded(int testsPassed, int testsFailed, int testsSkipped, double endTime, time_t totalRuntime) { - printf("Suite executed. %d passed, %d failed and %d skipped\n", testsPassed, testsFailed, testsSkipped); + logger("Suite executed. %d passed, %d failed and %d skipped\n", testsPassed, testsFailed, testsSkipped); } void @@ -44,8 +44,8 @@ void PlainTestEnded(const char *testName, const char *suiteName, int testResult, int numAsserts, time_t endTime, time_t totalRuntime) { - printf("Asserts:%d\n", numAsserts); - printf("%s: ok\n", testName); + logger("Asserts:%d\n", numAsserts); + logger("%s: ok\n", testName); } void @@ -53,7 +53,7 @@ PlainAssert(const char *assertName, int assertResult, const char *assertMessage, time_t eventTime) { const char *result = (assertResult) ? "passed" : "failed"; - printf("%s %d: %s\n", assertName, assertResult, assertMessage); + logger("%s %d: %s\n", assertName, assertResult, assertMessage); } void diff --git a/test/test-automation/xml.c b/test/test-automation/xml.c index 410e25c46..59f45b942 100644 --- a/test/test-automation/xml.c +++ b/test/test-automation/xml.c @@ -53,7 +53,10 @@ AddOpenTag(const char *tag) } memset(openTag, 0, sizeof(TagList)); - openTag->tag = tag; // Should be fine without malloc? + const int tagSize = SDL_strlen(tag) + 1; + openTag->tag = SDL_malloc(tagSize); + strncpy(openTag->tag, tag, tagSize); + openTag->next = openTags; openTags = openTag; @@ -75,15 +78,27 @@ RemoveOpenTag(const char *tag) int retVal = 0; + const int size = SDL_strlen(tag); + char *tempTag = SDL_malloc(size); + strncpy(tempTag, tag, size); + // Tag should always be the same as previously opened tag // It prevents opening and ending tag mismatch - if(SDL_strcmp(openTags->tag, tag) == 0) { + if(SDL_strcmp(tempTag, tag) == 0) { TagList *openTag = openTags; - openTags = openTags->next; + SDL_free(openTag->tag); - free(openTag); + /* + int counter = 0; + for(; counter < strlen(buffer); ++counter) { + buffer[counter] = tolower(buffer[counter]); + } + */ + + openTags = openTags->next; + SDL_free(openTag); } else { - printf("Debug | RemoveOpenTag(): open/end tag mismatch"); + //printf("Debug | xml.c:RemoveOpenTag(): open/end tag mismatch"); retVal = 1; } @@ -153,7 +168,30 @@ const char *EscapeString(const char *string) { return stringBuffer; } +/*! Turns all the characters of the given + * string to lowercase and returns the resulting string. + * + * \param string String to be converted + * \return Lower-case version of the given string + */ +char * +ToLowerCase(char *string) +{ + const int size = SDL_strlen(string); + char *ret = SDL_malloc(size + 1); + strncpy(ret, string, size); + ret[size] = '\0'; + // turn the tag to lower case for case-insensitive comparation + int counter = 0; + for(; counter < size; ++counter) { + ret[counter] = tolower(ret[counter]); + } + + // printf("Debug: %s == %s\n", string, ret); + + return ret; +} /* =================== @@ -177,7 +215,6 @@ XMLOpenDocument(const char *rootTag) memset(buffer, 0, bufferSize); snprintf(buffer, bufferSize, "<%s>", rootTag); - //logger(buffer); AddOpenTag(rootTag); @@ -217,25 +254,6 @@ XMLOpenElement(const char *tag) return ret; } - -char * -XMLOpenElementWithAttribute(const char *tag, Attribute *attribute) -{ - memset(buffer, 0, bufferSize); - snprintf(buffer, bufferSize, "<%s %s='%s'>", tag, - attribute->attribute, attribute->value); - logger(buffer); - - AddOpenTag(tag); - - const int size = SDL_strlen(buffer); - char *ret = SDL_malloc(size + 1); - strncpy(ret, buffer, size); - ret[size] = '\0'; - - return ret; -} - char * XMLAddContent(const char *content) { @@ -265,21 +283,28 @@ XMLCloseElement(const char *tag) while(openTag) { TagList *temp = openTag->next; + char *lowOpenTag = ToLowerCase(openTag->tag); + char *lowTag = ToLowerCase(tag); + + const int openTagSize = SDL_strlen(lowOpenTag); + const int tagSize = SDL_strlen(lowTag); + const int compSize = (openTagSize > tagSize) ? openTagSize : tagSize; + memset(buffer, 0, bufferSize); - snprintf(buffer, bufferSize, "", openTag->tag); + + int breakOut = 0; + if(SDL_strncmp(lowOpenTag, lowTag, compSize) == 0) { + breakOut = 1; + snprintf(buffer, bufferSize, "", tag); + } else { + snprintf(buffer, bufferSize, "", openTag->tag); + } + + SDL_free(lowOpenTag); + SDL_free(lowTag); // \todo use strNcat strcat(ret, buffer); - //logger(buffer); - - const int openTagSize = SDL_strlen(openTag->tag); - const int tagSize = SDL_strlen(tag); - const int compSize = (openTagSize > tagSize) ? openTagSize : tagSize; - - int breakOut = 0; - if(SDL_strncmp(openTag->tag, tag, compSize) == 0) { - breakOut = 1; - } RemoveOpenTag(openTag->tag); diff --git a/test/test-automation/xml.h b/test/test-automation/xml.h index c14aea066..2f925062b 100644 --- a/test/test-automation/xml.h +++ b/test/test-automation/xml.h @@ -53,11 +53,6 @@ char *XMLCloseDocument(); */ char *XMLOpenElement(const char *tag); -/*! - * Opens XML-element with given attributes - */ -char *XMLOpenElementWithAttribute(const char *tag, Attribute *attribute); - /*! * Add content to currently open element. * diff --git a/test/test-automation/xml_logger.c b/test/test-automation/xml_logger.c index 608d151e0..52ed9cb2f 100644 --- a/test/test-automation/xml_logger.c +++ b/test/test-automation/xml_logger.c @@ -32,11 +32,11 @@ XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTi { logger = outputFn; - char *output = XMLOpenDocument("testlog"); + char *output = XMLOpenDocument("teSTtlog"); logger(output); SDL_free(output); - output = XMLOpenElement("parameters"); + output = XMLOpenElement("paRameters"); logger(output); SDL_free(output); @@ -44,7 +44,7 @@ XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTi logger(output); SDL_free(output); - output = XMLCloseElement("parameters"); + output = XMLCloseElement("Parameters"); logger(output); SDL_free(output); } @@ -53,7 +53,7 @@ void XMLRunEnded(int testCount, int suiteCount, int testPassCount, int testFailCount, time_t endTime, time_t totalRuntime) { - char *output = XMLCloseDocument("testlog"); + char *output = XMLCloseDocument("testlOg"); logger(output); SDL_free(output); } @@ -65,12 +65,12 @@ XMLSuiteStarted(const char *suiteName, time_t eventTime) logger(output); SDL_free(output); - output = XMLOpenElement("eventTime"); + output = XMLOpenElement("EVENTTime"); logger(output); SDL_free(output); //XMLAddContent(evenTime); - output = XMLCloseElement("eventTime"); + output = XMLCloseElement("eventTIME"); logger(output); SDL_free(output); } @@ -91,7 +91,6 @@ XMLTestStarted(const char *testName, const char *suiteName, const char *testDesc logger(output); SDL_free(output); - //Attribute attribute = {"test", "value"}; //XMLOpenElementWithAttribute("name", &attribute); output = XMLOpenElement("name"); @@ -111,7 +110,6 @@ XMLTestStarted(const char *testName, const char *suiteName, const char *testDesc logger(output); SDL_free(output); - output = XMLAddContent(testDescription); logger(output); SDL_free(output);