JUnitExport.icl 3.19 KB
Newer Older
Camil Staps's avatar
Camil Staps committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
implementation module Testing.JUnitExport

import StdEnv
import StdMaybe

import Data.Func
import System.FilePath
import Testing.Options
import Testing.TestEvents
from Text import class Text(lpad), instance Text String

import Testing.Util

resultsToJUnitExport :: ![RunResult] -> JUnitExport
resultsToJUnitExport results
# suites = map resultsToSuite results
=
	{ jue_tests    = sum [s.jus_tests \\ s <- suites]
	, jue_failures = sum [s.jus_failures \\ s <- suites]
	, jue_suites   = suites
	, jue_time     = sum [s.jus_time \\ s <- suites]
	}
where
	resultsToSuite :: !RunResult -> JUnitSuite
	resultsToSuite res =
		{ jus_id       = res.run.TestRun.name
		, jus_name     = res.run.TestRun.name
		, jus_tests    = max 1 (length res.RunResult.children)
		, jus_failures = sum [1 \\ {event=Failed _} <- res.RunResult.children]
		, jus_time     = 0 // TODO
		, jus_cases    = map (eventToCase res.run.TestRun.name) res.RunResult.children
		}

	eventToCase :: !String !EndEvent -> JUnitCase
	eventToCase suite_name event =
		{ juc_id       = event.EndEvent.name
		, juc_name     = event.EndEvent.name
		, juc_time     = 0 // TODO
		, juc_failures = case event.event of
			Failed Nothing  -> [failure "unknown reason"]
			Failed (Just r) -> [failure msg]
				with
					msg = case r of
						FailedAssertions _ -> "failed assertions"
						CounterExamples _  -> "counterexample(s)"
						FailedChildren _   -> "failed child(ren) test(s)"
						Crashed            -> "crashed"
			_               -> []
		}
	where
		failure msg =
			{ juf_file     = suite_name // TODO
			, juf_line     = 0 // TODO
			, juf_category = "failed unit test"
			, juf_message  = msg
			, juf_severity = "ERROR"
			}

instance <<< JUnitExport
where
	<<< f jue
	# f = f <<< "<?xml version=\"1.0\"?>\n"
	# f = f <<< "<testsuites tests=\"" <<< jue.jue_tests
		<<< "\" failures=\"" <<< jue.jue_failures
		<<< "\" time=\"" <<< time jue.jue_time
		<<< "\">\n"
	# f = seqSt printTestSuite jue.jue_suites f
	# f = f <<< "</testsuites>\n"
	= f
	where
		time :: !Int -> String
		time ms = toString (ms/1000) +++ "." +++ lpad (toString (ms rem 1000)) 3 '0'

		printTestSuite :: !JUnitSuite !*File -> *File
		printTestSuite jus f
		# f = f <<< "\t<testsuite id=\"" <<< jus.jus_id
			<<< "\" name=\"" <<< jus.jus_name
			<<< "\" tests=\"" <<< jus.jus_tests
			<<< "\" failures=\"" <<< jus.jus_failures
			<<< "\" time=\"" <<< jus.jus_time
			<<< "\">\n"
		# f = seqSt printTestCase jus.jus_cases f
		# f = f <<< "\t</testsuite>\n"
		= f

		printTestCase :: !JUnitCase !*File -> *File
		printTestCase juc f
		# f = f <<< "\t\t<testcase id=\"" <<< juc.juc_id
			<<< "\" name=\"" <<< juc.juc_name
			<<< "\" time=\"" <<< juc.juc_time
			<<< "\">\n"
		# f = seqSt printTestFailure juc.juc_failures f
		# f = f <<< "\t\t</testcase>\n"
		= f

		printTestFailure :: !JUnitFailure !*File -> *File
		printTestFailure juf f = f
			<<< "\t\t\t<failure type=\"" <<< juf.juf_severity
			<<< "\" message=\"" <<< juf.juf_file <<< ":" <<< juf.juf_line <<< " " <<< juf.juf_message
			<<< "\">\n"
			<<< juf.juf_severity <<< ": " <<< juf.juf_message <<< "\n"
			<<< "Category: " <<< juf.juf_category <<< "\n"
			<<< "File: " <<< juf.juf_file <<< "\n"
			<<< "Line: " <<< juf.juf_line <<< "\n"
			<<< "\t\t\t</failure>\n"