From 0de0e7a5350fefb448f7f5be8d51559d2bd5926b Mon Sep 17 00:00:00 2001 From: Markus Kauppila Date: Thu, 30 Jun 2011 17:11:39 +0300 Subject: [PATCH] Added user-supplied XSL style sheets for XML-based test reports. Added new command line option: --xsl. --- test/test-automation/logger.h | 5 +++-- test/test-automation/plain_logger.c | 3 ++- test/test-automation/plain_logger.h | 5 ++++- test/test-automation/runner.c | 27 +++++++++++++++++++++++++-- test/test-automation/xml.c | 16 ++++++++++++++-- test/test-automation/xml.h | 2 +- test/test-automation/xml_logger.c | 13 +++++++++++-- test/test-automation/xml_logger.h | 3 ++- 8 files changed, 62 insertions(+), 12 deletions(-) diff --git a/test/test-automation/logger.h b/test/test-automation/logger.h index 9ad241aa4..5a93fc9ef 100644 --- a/test/test-automation/logger.h +++ b/test/test-automation/logger.h @@ -25,9 +25,10 @@ /*! * Typedefs for function pointers that implement the generic - * logging interface + * logging interface. See the headers of implementations (plain_logger.h or + * xml_logger.h) for more information. */ -typedef void (*RunStartedFp)(int parameterCount, char *runnerParameters[], time_t eventTime); +typedef void (*RunStartedFp)(int parameterCount, char *runnerParameters[], time_t eventTime, void *data); typedef void (*RunEndedFp)(int testCount, int suiteCount, int testPassCount, int testFailCount, time_t endTime, double totalRuntime); diff --git a/test/test-automation/plain_logger.c b/test/test-automation/plain_logger.c index 991919a97..d7c0f4c9e 100644 --- a/test/test-automation/plain_logger.c +++ b/test/test-automation/plain_logger.c @@ -28,7 +28,8 @@ Output(const char *message, ...) } void -PlainRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime) +PlainRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime, + void *data) { /* Output("Test run started with following parameters\n"); diff --git a/test/test-automation/plain_logger.h b/test/test-automation/plain_logger.h index 64a7b168d..d699f2552 100644 --- a/test/test-automation/plain_logger.h +++ b/test/test-automation/plain_logger.h @@ -9,8 +9,11 @@ * \param parameterCount How many parameters were given * \param runnerParameters What parameters were given to the runner * \param eventTime When the execution started + * \param data Any additional data logger needs + * */ -void PlainRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime); +void PlainRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime, + void *data); /*! * Prints out information about ending the test run. diff --git a/test/test-automation/runner.c b/test/test-automation/runner.c index 4d81fc4d6..894a5b671 100644 --- a/test/test-automation/runner.c +++ b/test/test-automation/runner.c @@ -51,6 +51,8 @@ static int only_selected_suite = 0; static int only_tests_with_string = 0; //!< Flag for enabling XML logging static int xml_enabled = 0; +//! Flag for enabling user-supplied style sheet for XML test report +static int custom_xsl_enabled = 0; //!< Size of the test and suite name buffers @@ -63,6 +65,9 @@ char selected_suite_name[NAME_BUFFER_SIZE]; //!< substring of test case name char testcase_name_substring[NAME_BUFFER_SIZE]; +//! Name for user-supplied XSL style sheet name +char xsl_stylesheet_name[NAME_BUFFER_SIZE]; + //! Default directory of the test suites #define DEFAULT_TEST_DIRECTORY "tests/" @@ -535,6 +540,7 @@ printUsage() { printf(" --in-proc Executes tests in-process\n"); printf(" --show-tests Prints out all the executable tests\n"); printf(" --xml Enables XML logger\n"); + printf(" --xsl FILENAME Use the given file as XSL style sheet for XML\n"); // \todo add to wiki printf(" -t --test TEST Executes only tests with given name\n"); printf(" -ts --name-contains SUBSTR Executes only tests that have given\n"); printf(" substring in test name\n"); @@ -581,6 +587,21 @@ ParseOptions(int argc, char *argv[]) memset(selected_test_name, 0, NAME_BUFFER_SIZE); strcpy(selected_test_name, testName); } + else if(SDL_strcmp(arg, "--xsl") == 0) { + custom_xsl_enabled = 1; + char *stylesheet = NULL; + + if( (i + 1) < argc) { + stylesheet = argv[++i]; + } else { + printf("runner: filename of XSL stylesheet is missing\n"); + printUsage(); + exit(1); + } + + memset(xsl_stylesheet_name, 0, NAME_BUFFER_SIZE); + strncpy(xsl_stylesheet_name, stylesheet, NAME_BUFFER_SIZE); + } else if(SDL_strcmp(arg, "--name-contains") == 0 || SDL_strcmp(arg, "-ts") == 0) { only_tests_with_string = 1; char *substring = NULL; @@ -649,8 +670,12 @@ main(int argc, char *argv[]) #endif if(xml_enabled) { SetupXMLLogger(); + + RunStarted(argc, argv, time(0), xsl_stylesheet_name); } else { SetupPlainLogger(); + + RunStarted(argc, argv, time(0), NULL); } const Uint32 startTicks = SDL_GetTicks(); @@ -671,8 +696,6 @@ main(int argc, char *argv[]) return 0; } - RunStarted(argc, argv, time(0)); - char *currentSuiteName = NULL; int suiteStartTime = SDL_GetTicks(); diff --git a/test/test-automation/xml.c b/test/test-automation/xml.c index b94ef4bc5..5670c119d 100644 --- a/test/test-automation/xml.c +++ b/test/test-automation/xml.c @@ -193,12 +193,22 @@ EscapeString(const char *string) */ char * -XMLOpenDocument(const char *rootTag) +XMLOpenDocument(const char *rootTag, const char *xslStyle) { const char *doctype = "\n"; //! \todo make this optional (and let the user supply the filename?) - const char *style = "\n"; + const char *styleStart = "\n"; + + const int sizeStyleStart = SDL_strlen(styleStart); + const int sizeStyleEnd = SDL_strlen(styleEnd); + const int sizeStyleSheetName = SDL_strlen(xslStyle); + + const int tempSize = sizeStyleStart + sizeStyleEnd + sizeStyleSheetName + 1; + char *style = SDL_malloc(tempSize); + memset(style, 0, tempSize); + snprintf(style, tempSize, "%s%s%s", styleStart, xslStyle, styleEnd); memset(buffer, 0, bufferSize); snprintf(buffer, bufferSize, "<%s>", rootTag); @@ -219,6 +229,8 @@ XMLOpenDocument(const char *rootTag) strcat(retBuf, style); strcat(retBuf, buffer); + SDL_free(style); + return retBuf; } diff --git a/test/test-automation/xml.h b/test/test-automation/xml.h index 05280ac0b..a174d7766 100644 --- a/test/test-automation/xml.h +++ b/test/test-automation/xml.h @@ -39,7 +39,7 @@ typedef struct Attribute { * \param rootTag Root tag for the XML document * \return The generated XML output */ -char *XMLOpenDocument(const char *rootTag); +char *XMLOpenDocument(const char *rootTag, const char *xslStyle); /*! * Closes the XML-document. diff --git a/test/test-automation/xml_logger.c b/test/test-automation/xml_logger.c index 03af02405..7e33c4010 100644 --- a/test/test-automation/xml_logger.c +++ b/test/test-automation/xml_logger.c @@ -105,9 +105,18 @@ XMLOutputter(const int currentIdentLevel, } void -XMLRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime) +XMLRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime, + void *data) { - char *output = XMLOpenDocument(documentRoot); + char *xslStylesheet = "style.xsl"; + if(data != NULL) { + char *tmp = (char *)data; + if(SDL_strlen(tmp) > 0) { + xslStylesheet = tmp; + } + } + + char *output = XMLOpenDocument(documentRoot, xslStylesheet); XMLOutputter(indentLevel++, YES, output); output = XMLOpenElement(parametersElementName); diff --git a/test/test-automation/xml_logger.h b/test/test-automation/xml_logger.h index c9bb1c630..518faa1da 100644 --- a/test/test-automation/xml_logger.h +++ b/test/test-automation/xml_logger.h @@ -9,8 +9,9 @@ * \param parameterCount How many parameters were given * \param runnerParameters What parameters were given to the runner * \param eventTime When the execution started + * \param data Any additional data logger needs */ -void XMLRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime); +void XMLRunStarted(int parameterCount, char *runnerParameters[], time_t eventTime, void *data); /*! * Prints out information about ending the test run in XML