Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
XML component no longer outputs the generated XML.
Browse files Browse the repository at this point in the history
Fixed a bunch of compiler warnings.
  • Loading branch information
mkauppila committed Jun 23, 2011
1 parent 2b17754 commit 8be7b7b
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 54 deletions.
2 changes: 1 addition & 1 deletion test/test-automation/plain_logger.c
Expand Up @@ -53,7 +53,7 @@ PlainAssert(const char *assertName, int assertResult, const char *assertMessage,
time_t eventTime)
{
const char *result = (assertResult) ? "passed" : "failed";
printf("%s %s: %s\n", assertName, assertResult, assertMessage);
printf("%s %d: %s\n", assertName, assertResult, assertMessage);
}

void
Expand Down
71 changes: 54 additions & 17 deletions test/test-automation/xml.c
Expand Up @@ -118,7 +118,7 @@ const char *EscapeString(const char *string) {
memset(buffer, 0, bufferSize);

// prevents the code doing a 'bus error'
char stringBuffer[bufferSize];
char *stringBuffer = SDL_malloc(bufferSize);
strncpy(stringBuffer, string, bufferSize);

// Ampersand (&) must be first, otherwise it'll mess up the other entities
Expand Down Expand Up @@ -170,40 +170,55 @@ static const char *root;
/*! Buffer for storing the xml element under construction */
static char buffer[bufferSize];

void
XMLOpenDocument(const char *rootTag, LogOutputFp log)
char *
XMLOpenDocument(const char *rootTag)
{
assert(log != NULL);
logger = log;

logger("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
const char *doctype = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";

memset(buffer, 0, bufferSize);
snprintf(buffer, bufferSize, "<%s>", rootTag);
logger(buffer);
//logger(buffer);

AddOpenTag(rootTag);

root = rootTag; // it's fine, as long as rootTag points to static memory?

const int doctypeSize = SDL_strlen(doctype);
const int tagSize = SDL_strlen(buffer);

const int size = doctypeSize + tagSize + 1; // extra byte for '\0'
char *ret = SDL_malloc(size);
// copy doctype
strncpy(ret, doctype, doctypeSize);
// copy tag
strncpy(ret + doctypeSize, buffer, tagSize);
ret[size] = '\0';
return ret;
}

void
char *
XMLCloseDocument() {
XMLCloseElement(root);
return XMLCloseElement(root);
}

void
char *
XMLOpenElement(const char *tag)
{
memset(buffer, 0, bufferSize);
snprintf(buffer, bufferSize, "<%s>", tag);
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;
}


void
char *
XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
{
memset(buffer, 0, bufferSize);
Expand All @@ -212,21 +227,38 @@ XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
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;
}

void
char *
XMLAddContent(const char *content)
{
const char *escapedContent = EscapeString(content);

memset(buffer, 0, bufferSize);
snprintf(buffer, bufferSize, "%s", escapedContent);
logger(buffer);
SDL_free((char *)escapedContent);

const int size = SDL_strlen(buffer);
char *ret = SDL_malloc(size + 1);
strncpy(ret, buffer, size);
ret[size] = '\0';

return ret;
}

void
char *
XMLCloseElement(const char *tag)
{
char *ret = SDL_malloc(bufferSize);
memset(ret, 0, bufferSize);

// Close the open tags with proper nesting. Closes tags until it finds
// the given tag which is the last tag that will be closed
TagList *openTag = openTags;
Expand All @@ -235,7 +267,10 @@ XMLCloseElement(const char *tag)

memset(buffer, 0, bufferSize);
snprintf(buffer, bufferSize, "</%s>", openTag->tag);
logger(buffer);

// \todo use strNcat
strcat(ret, buffer);
//logger(buffer);

const int openTagSize = SDL_strlen(openTag->tag);
const int tagSize = SDL_strlen(tag);
Expand All @@ -254,5 +289,7 @@ XMLCloseElement(const char *tag)
break;
}
}

return ret;
}

13 changes: 7 additions & 6 deletions test/test-automation/xml.h
Expand Up @@ -35,34 +35,35 @@ typedef struct Attribute {
* Creates header and start tag for root element.
*
* \param rootTag Root tag for the XML document
* \return The generated XML output
*/
void XMLOpenDocument(const char *rootTag, LogOutputFp log);
char *XMLOpenDocument(const char *rootTag);

/*!
* Closes the XML-document.
* Creates end tag for root element and closes other open elements
* with correct nesting.
*/
void XMLCloseDocument();
char *XMLCloseDocument();

/*!
* Opens XML-element.
*
* \param tag Element to be opened
*/
void XMLOpenElement(const char *tag);
char *XMLOpenElement(const char *tag);

/*!
* Opens XML-element with given attributes
*/
void XMLOpenElementWithAttribute(const char *tag, Attribute *attribute);
char *XMLOpenElementWithAttribute(const char *tag, Attribute *attribute);

/*!
* Add content to currently open element.
*
* \param content Content for the currently open element
*/
void XMLAddContent(const char *content);
char *XMLAddContent(const char *content);

/*!
* Closes previously opened element until tag given as parameter is met.
Expand All @@ -73,7 +74,7 @@ void XMLAddContent(const char *content);
*
* \param tag Element to close
*/
void XMLCloseElement(const char *tag);
char *XMLCloseElement(const char *tag);

#endif

130 changes: 100 additions & 30 deletions test/test-automation/xml_logger.c
Expand Up @@ -18,95 +18,165 @@
3. This notice may not be removed or altered from any source distribution.
*/

#include <SDL/SDL.h>

#include "xml.h"
#include "logger.h"

#include "xml_logger.h"

LogOutputFp logger;

void
XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
{
//! \todo Giving outputFn to the function is awful, fix it
//! Make the outputting differently
XMLOpenDocument("testlog", outputFn);
logger = outputFn;

char *output = XMLOpenDocument("testlog");
logger(output);
SDL_free(output);

output = XMLOpenElement("parameters");
logger(output);
SDL_free(output);

XMLOpenElement("parameters");
XMLAddContent(runnerParameters);
XMLCloseElement("parameters");
output = XMLAddContent(runnerParameters);
logger(output);
SDL_free(output);

output = XMLCloseElement("parameters");
logger(output);
SDL_free(output);
}

void
XMLRunEnded(int testCount, int suiteCount, int testPassCount, int testFailCount,
time_t endTime, time_t totalRuntime)
{
XMLCloseDocument("testlog");
char *output = XMLCloseDocument("testlog");
logger(output);
SDL_free(output);
}

void
XMLSuiteStarted(const char *suiteName, time_t eventTime)
{
XMLOpenElement("suite");
char *output = XMLOpenElement("suite");
logger(output);
SDL_free(output);

output = XMLOpenElement("eventTime");
logger(output);
SDL_free(output);

XMLOpenElement("eventTime");
//XMLAddContent(evenTime);
XMLCloseElement("eventTime");
output = XMLCloseElement("eventTime");
logger(output);
SDL_free(output);
}

void
XMLSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
double endTime, time_t totalRuntime)
{
XMLCloseElement("suite");
char *output = XMLCloseElement("suite");
logger(output);
SDL_free(output);
}

void
XMLTestStarted(const char *testName, const char *suiteName, const char *testDescription, time_t startTime)
{
XMLOpenElement("test");
char * output = XMLOpenElement("test");
logger(output);
SDL_free(output);


Attribute attribute = {"test", "value"};
//Attribute attribute = {"test", "value"};
//XMLOpenElementWithAttribute("name", &attribute);
output = XMLOpenElement("name");
logger(output);
SDL_free(output);

XMLOpenElementWithAttribute("name", &attribute);
XMLAddContent(testName);
XMLCloseElement("name");
output = XMLAddContent(testName);
logger(output);
SDL_free(output);

XMLOpenElement("description");
XMLAddContent(testDescription);
XMLCloseElement("description");
output = XMLCloseElement("name");
logger(output);
SDL_free(output);


output = XMLOpenElement("description");
logger(output);
SDL_free(output);


output = XMLAddContent(testDescription);
logger(output);
SDL_free(output);

output = XMLCloseElement("description");
logger(output);
SDL_free(output);

output = XMLOpenElement("starttime");
logger(output);
SDL_free(output);

XMLOpenElement("starttime");
//XMLAddContent(startTime);
XMLCloseElement("starttime");
output = XMLCloseElement("starttime");
logger(output);
SDL_free(output);
}

void
XMLTestEnded(const char *testName, const char *suiteName,
int testResult, int numAsserts, time_t endTime, time_t totalRuntime)
{
XMLCloseElement("test");
char *output = XMLCloseElement("test");
logger(output);
SDL_free(output);
}

void
XMLAssert(const char *assertName, int assertResult, const char *assertMessage,
time_t eventTime)
{
XMLOpenElement("assert");
char *output = XMLOpenElement("assert");
logger(output);
SDL_free(output);

output = XMLOpenElement("result");
logger(output);
SDL_free(output);

XMLOpenElement("result");
XMLAddContent((assertResult) ? "pass" : "failure");
XMLOpenElement("result");
output = XMLAddContent((assertResult) ? "pass" : "failure");
logger(output);
SDL_free(output);

output = XMLOpenElement("result");
logger(output);
SDL_free(output);

XMLCloseElement("assert");
output = XMLCloseElement("assert");
logger(output);
SDL_free(output);
}

void
XMLLog(const char *logMessage, time_t eventTime)
{
XMLOpenElement("log");
char *output = XMLOpenElement("log");
logger(output);
SDL_free(output);

XMLAddContent(logMessage);
output = XMLAddContent(logMessage);
logger(output);
SDL_free(output);

XMLCloseElement("log");
output = XMLCloseElement("log");
logger(output);
SDL_free(output);
}

0 comments on commit 8be7b7b

Please sign in to comment.