< Summary - Igor Pro Universal Testing Framework

Information
Class: procedures.igortest-junit
Assembly: procedures
File(s): /builds/mirror/igortest/procedures/igortest-junit.ipf
Tag: 74147b3
Line coverage
76%
Covered lines: 140
Uncovered lines: 43
Coverable lines: 183
Total lines: 265
Line coverage: 76.5%
Branch coverage
68%
Covered branches: 15
Total branches: 22
Branch coverage: 68.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/builds/mirror/igortest/procedures/igortest-junit.ipf

#LineLine coverage
 1#pragma TextEncoding="UTF-8"
 2#pragma rtGlobals=3
 3#pragma rtFunctionErrors=1
 4#pragma version=1.10
 5#pragma ModuleName=IUTF_JUnit
 6
 7/// trim leading and trailing white spaces from
 8/// every line of the given string
 109static Function/S JU_TrimSOUT(input, [listSepStr])
 10  string input
 11  string listSepStr
 12
 1013  variable i, numItems
 1014  string output = ""
 15
 1016  if(ParamIsDefault(listSepStr))
 1017    listSepStr = "\r"
 1018  endif
 19
 1020  numItems = ItemsInList(input, listSepStr)
 1021  for(i = 0; i < numItems; i += 1)
 2022    output += TrimString(StringFromList(i, input, listSepStr))
 2023    output += listSepStr
 2024  endfor
 25
 1026  return output
 1027End
 28
 29/// Formats the specified TimeStamp in the form yyyy-mm-ddThh:mm:ss in local time
 3030static Function/S JU_GetISO8601TimeStamp(localtime)
 31  variable localtime
 32
 3033  return (Secs2Date(localtime, -2) + "T" + Secs2Time(localtime, 3))
 3034End
 35
 536static Function/S JU_AssertionOut(assertionIndex)
 37  variable assertionIndex
 38
 539  string out, message, type, context, severity
 540  variable i, startIndex, endIndex
 41
 542  WAVE/T wvAssertion = IUTF_Reporting#GetTestAssertionWave()
 543  WAVE/T wvInfo      = IUTF_Reporting#GetTestInfoWave()
 44
 545  if(!CmpStr(IUTF_SEVERITY_WARN, wvAssertion[assertionIndex][%SEVERITY]))
 546    // skip JUnit output for warning severity
 547    return ""
 048  endif
 49
 050  startIndex = str2num(wvAssertion[assertionIndex][%CHILD_START])
 051  endIndex   = str2num(wvAssertion[assertionIndex][%CHILD_END])
 052  context    = ""
 053  for(i = startIndex; i < endIndex; i += 1)
 054    context += "\t\t\t\tInfo: " + wvInfo[i][%MESSAGE] + "\n"
 055  endfor
 56
 057  message = IUTF_Utils_XML#ToXMLCharacters(wvAssertion[assertionIndex][%MESSAGE])
 058  type    = IUTF_Utils_XML#ToXMLCharacters(wvAssertion[assertionIndex][%TYPE])
 059  // we are outputing everything as error to keep the same behavior as older versions of IUTF
 60
 061  // strswitch(wvAssertion[i][%TYPE])
 062  //   case IUTF_STATUS_FAIL:
 063  //     s += "\t\t\t<failure message=\"" + message + "\" type=\"" + type + "\"></failure>\n"
 064  //     break
 065  //   case IUTF_STATUS_ERROR:
 066  out  = "\t\t\t<error message=\"" + message + "\" type=\"" + type + "\">\n"
 067  out += context
 068  out += "\t\t\t</error>\n"
 069  //     break
 070  //   default:
 071  //     break
 072  // endswitch
 73
 074  return out
 575End
 76
 77/// Evaluates last Test Case and returns JUNIT XML Output from Test Case
 8078static Function/S JU_CaseToOut(testSuiteIndex, testCaseIndex)
 79  variable testSuiteIndex, testCaseIndex
 80
 8081  string out, name, classname, message, type
 8082  variable i, timeTaken, startIndex, endIndex
 83
 8084  variable skip = 0
 85
 8086  WAVE/T wvTestSuite = IUTF_Reporting#GetTestSuiteWave()
 8087  WAVE/T wvTestCase  = IUTF_Reporting#GetTestCaseWave()
 8088  WAVE/T wvAssertion = IUTF_Reporting#GetTestAssertionWave()
 89
 8090  classname = IUTF_Utils_XML#ToXMLToken(IUTF_Utils_XML#ToXMLCharacters(wvTestCase[testCaseIndex][%NAME]))
 8091  name      = IUTF_Utils_XML#ToXMLToken(IUTF_Utils_XML#ToXMLCharacters(wvTestSuite[testSuiteIndex][%PROCEDURENAME]))
 8092  sprintf name, "%s in %s (%d)", classname, name, testCaseIndex
 8093  timeTaken = str2num(wvTestCase[testCaseIndex][%ENDTIME]) - str2num(wvTestCase[testCaseIndex][%STARTTIME])
 94
 8095  sprintf out, "\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.3f\">\n", name, classname, timeTaken
 8096  if(!CmpStr(IUTF_STATUS_SKIP, wvTestCase[testCaseIndex][%STATUS]))
 097    skip = 1
 8098  endif
 8099  if(!CmpStr(IUTF_STATUS_RETRY, wvTestCase[testCaseIndex][%STATUS]))
 0100    skip = 1
 80101  endif
 102
 80103  if(skip)
 0104    out += "\t\t\t<skipped/>\n"
 80105  else
 80106    startIndex = str2num(wvTestCase[testCaseIndex][%CHILD_START])
 80107    endIndex   = str2num(wvTestCase[testCaseIndex][%CHILD_END])
 80108    for(i = startIndex; i < endIndex; i += 1)
 5109      out += JU_AssertionOut(i)
 5110    endfor
 80111  endif
 112
 80113  message = wvTestCase[testCaseIndex][%STDOUT]
 80114  if(!IUTF_Utils#IsEmpty(message))
 5115    out += "\t\t<system-out>" + IUTF_Utils_XML#ToXMLCharacters(JU_TrimSOUT(message)) + "</system-out>\n"
 80116  endif
 80117  message = wvTestCase[testCaseIndex][%STDERR]
 80118  if(!IUTF_Utils#IsEmpty(message))
 5119    out += "\t\t<system-err>" + IUTF_Utils_XML#ToXMLCharacters(message) + "</system-err>\n"
 80120  endif
 121
 80122  return (out + "\t\t</testcase>\n")
 80123End
 124
 125/// Converts the reference time string that is stored in the result storage waves into an absolute
 126/// time which counts the seconds since 1904-01-01.
 30127static Function JU_ToAbsoluteTime(str)
 128  string str
 129
 30130  // Seconds since the start of the computer
 30131  variable num = str2num(str)
 132
 30133  // This is the difference between two time measuring systems. DateTime is counting the seconds
 30134  // since 1904-01-01 and StopMSTimer(-2) * IUTF_MICRO_TO_ONE since the start of the computer.
 30135  // We need this difference as num is a time stamp in the seconds time system and need to
 30136  // convert it to the first one.
 30137  // During test executing runtime it is very unlikely that this difference change and therefore
 30138  // can be treaded as constant.
 30139  variable difference = DateTime - StopMSTimer(-2) * IUTF_MICRO_TO_ONE
 140
 30141  // Seconds since 1904-01-01
 30142  return num + difference
 30143End
 144
 145/// Returns combined JUNIT XML Output for TestSuite consisting of all TestCases run in Suite
 30146static Function/S JU_ToTestSuiteString(testRunIndex, testSuiteIndex)
 147  variable testRunIndex, testSuiteIndex
 148
 30149  string out, format, s
 30150  string package, name, timestamp, hostname, tests, failures, errors, skipped
 30151  variable i, timeTaken, childStart, childEnd
 152
 30153  WAVE/T wvTestRun   = IUTF_Reporting#GetTestRunWave()
 30154  WAVE/T wvTestSuite = IUTF_Reporting#GetTestSuiteWave()
 155
 30156  package   = IUTF_Utils_XML#ToXMLToken(IUTF_Utils_XML#ToXMLCharacters(wvTestSuite[testSuiteIndex][%PROCEDURENAME]))
 30157  name      = IUTF_Utils_XML#ToXMLToken(IUTF_Utils_XML#ToXMLCharacters(wvTestSuite[testSuiteIndex][%PROCEDURENAME]))
 30158  timestamp = JU_GetISO8601TimeStamp(JU_ToAbsoluteTime(wvTestSuite[testSuiteIndex][%STARTTIME]))
 30159  hostname  = IUTF_Utils_XML#ToXMLToken(IUTF_Utils_XML#ToXMLCharacters(wvTestRun[testRunIndex][%HOSTNAME]))
 30160  tests     = wvTestSuite[testSuiteIndex][%NUM_TESTS]
 30161  failures  = "0" // the number of failures are not tracked right now
 30162  errors    = wvTestSuite[testSuiteIndex][%NUM_ERROR]
 30163  skipped   = wvTestSuite[testSuiteIndex][%NUM_SKIPPED]
 30164  timeTaken = str2num(wvTestSuite[testSuiteIndex][%ENDTIME]) - str2num(wvTestSuite[testSuiteIndex][%STARTTIME])
 165
 30166  format = "\t<testsuite package=\"%s\" id=\"%d\" name=\"%s\" timestamp=\"%s\" hostname=\"%s\" tests=\"%s\" failures=\"%
 30167  sprintf out, format, package, testSuiteIndex, name, timestamp, hostname, tests, failures, errors, skipped, timeTaken
 168
 30169  out += "\t\t<properties>\n"
 30170  out += JU_ToPropertyString("User", wvTestRun[testRunIndex][%USERNAME])
 30171  out += JU_ToPropertyString("System", JU_NicifyList(wvTestRun[testRunIndex][%SYSTEMINFO]))
 30172  out += JU_ToPropertyString("Experiment", wvTestRun[testRunIndex][%EXPERIMENT])
 30173  out += JU_ToPropertyString("IUTFversion", wvTestRun[testRunIndex][%VERSION])
 30174  out += JU_ToPropertyString("IgorInfo", JU_NicifyList(wvTestRun[testRunIndex][%IGORINFO]))
 30175  out += "\t\t</properties>\n"
 176
 30177  childStart = str2num(wvTestSuite[testSuiteIndex][%CHILD_START])
 30178  childEnd   = str2num(wvTestSuite[testSuiteIndex][%CHILD_END])
 30179  for(i = childStart; i < childEnd; i += 1)
 80180    out += JU_CaseToOut(testSuiteIndex, i)
 80181  endfor
 182
 30183  s = wvTestSuite[testSuiteIndex][%STDOUT]
 30184  if(!IUTF_Utils#IsEmpty(s))
 5185    out += "\t\t<system-out>" + IUTF_Utils_XML#ToXMLCharacters(JU_TrimSOUT(s)) + "</system-out>\n"
 30186  endif
 30187  s = wvTestSuite[testSuiteIndex][%STDERR]
 30188  if(!IUTF_Utils#IsEmpty(s))
 5189    out += "\t\t<system-err>" + IUTF_Utils_XML#ToXMLCharacters(s) + "</system-err>\n"
 30190  endif
 191
 30192  return (out + "\t</testsuite>\n")
 30193End
 194
 150195static Function/S JU_ToPropertyString(name, value)
 196  string name, value
 197
 150198  string s
 199
 150200  if(IUTF_Utils#IsEmpty(value))
 0201    return ""
 150202  endif
 203
 150204  name  = IUTF_Utils_XML#ToXMLToken(IUTF_Utils_XML#ToXMLCharacters(name))
 150205  value = IUTF_Utils_XML#ToXMLCharacters(value)
 150206  sprintf s, "\t\t\t<property name=\"%s\" value=\"%s\"/>\n", name, value
 207
 150208  return s
 150209End
 210
 211/// Replaces all chars >= 0x80 by "?" in str and returns the resulting string
 0212static Function/S JU_UTF8Filter(str)
 213  string str
 214
 0215  string sret
 0216  variable i, len
 217
 0218  sret = ""
 0219  len  = strlen(str)
 0220  for(i = 0; i < len; i += 1)
 0221    if(char2num(str[i]) < 0)
 0222      sret += "?"
 0223    else
 0224      sret += str[i]
 0225    endif
 0226  endfor
 0227  return sret
 0228End
 229
 230/// Writes JUNIT XML output to derived file name
 5231static Function JU_WriteOutput()
 5232  variable fnum, i, childStart, childEnd
 5233  string out, juFileName, msg
 234
 5235  WAVE/T wvTestRun = IUTF_Reporting#GetTestRunWave()
 5236  childStart = str2num(wvTestRun[%CURRENT][%CHILD_START])
 5237  childEnd   = str2num(wvTestRun[%CURRENT][%CHILD_END])
 238
 5239  out = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<testsuites>\n"
 5240  for(i = childStart; i < childEnd; i += 1)
 30241    out += JU_ToTestSuiteString(0, i)
 30242  endfor
 5243  out += "</testsuites>\n"
 244#if (IgorVersion() >= 7.0)
 5245  // UTF-8 support
 246#else
 0247  out = JU_UTF8Filter(out)
 248#endif
 249
 5250  IUTF_Utils_XML#WriteXML("JU_", out)
 5251End
 252
 253/// Add a EOL (`\r`) after every element of a `;` separated list.
 254/// Intended for better readability.
 60255static Function/S JU_NicifyList(list)
 256  string list
 257
 60258  list = RemoveEnding(list, ";")
 259
 60260  if(IUTF_Utils#IsEmpty(list))
 0261    return list
 60262  endif
 263
 60264  return ReplaceString(";", list, ";\r")
 60265End