Added user-supplied XSL style sheets for XML-based test reports.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Thu, 30 Jun 2011 17:11:39 +0300
changeset 5728 7cf47cdaf4ab
parent 5727 390fdae0a318
child 5729 27be98c43bbc
Added user-supplied XSL style sheets for XML-based test reports. Added new command line option: --xsl.
test/test-automation/logger.h
test/test-automation/plain_logger.c
test/test-automation/plain_logger.h
test/test-automation/runner.c
test/test-automation/xml.c
test/test-automation/xml.h
test/test-automation/xml_logger.c
test/test-automation/xml_logger.h
--- a/test/test-automation/logger.h	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/logger.h	Thu Jun 30 17:11:39 2011 +0300
@@ -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);
 
--- a/test/test-automation/plain_logger.c	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/plain_logger.c	Thu Jun 30 17:11:39 2011 +0300
@@ -28,7 +28,8 @@
 }
 
 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");
--- a/test/test-automation/plain_logger.h	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/plain_logger.h	Thu Jun 30 17:11:39 2011 +0300
@@ -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.
--- a/test/test-automation/runner.c	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/runner.c	Thu Jun 30 17:11:39 2011 +0300
@@ -51,6 +51,8 @@
 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 @@
 //!< 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 @@
 	  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 @@
     	  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 @@
 #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 @@
 		return 0;
 	}
 
-	RunStarted(argc, argv, time(0));
-
 	char *currentSuiteName = NULL;
 
 	int suiteStartTime = SDL_GetTicks();
--- a/test/test-automation/xml.c	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/xml.c	Thu Jun 30 17:11:39 2011 +0300
@@ -193,12 +193,22 @@
 */
 
 char *
-XMLOpenDocument(const char *rootTag)
+XMLOpenDocument(const char *rootTag, const char *xslStyle)
 {
 	const char *doctype = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
 
 	//! \todo make this optional (and let the user supply the filename?)
-	const char *style = "<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>\n";
+	const char *styleStart = "<?xml-stylesheet type=\"text/xsl\" href=\"";
+	const char *styleEnd = "\"?>\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 @@
 	strcat(retBuf, style);
 	strcat(retBuf, buffer);
 
+	SDL_free(style);
+
 	return retBuf;
 }
 
--- a/test/test-automation/xml.h	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/xml.h	Thu Jun 30 17:11:39 2011 +0300
@@ -39,7 +39,7 @@
  * \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.
--- a/test/test-automation/xml_logger.c	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/xml_logger.c	Thu Jun 30 17:11:39 2011 +0300
@@ -105,9 +105,18 @@
 }
 
 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);
--- a/test/test-automation/xml_logger.h	Thu Jun 30 16:08:37 2011 +0300
+++ b/test/test-automation/xml_logger.h	Thu Jun 30 17:11:39 2011 +0300
@@ -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