evidence_runner.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (function(){
  2. var ConsoleTestRunner = Evidence.AutoRunner.RUNNERS.console,
  3. ConsoleTestResult = Evidence.UI.Console.TestResult,
  4. AutoRunner = Evidence.AutoRunner,
  5. printf = Evidence.UI.printf
  6. function inherit(superclass, extra) {
  7. var klass = function(){
  8. this.initialize.apply(this, arguments)
  9. }
  10. var tmp = function(){}
  11. tmp.prototype = superclass.prototype
  12. klass.prototype = new tmp()
  13. klass.prototype.constructor = klass
  14. klass.prototype.initialize = function(){
  15. superclass.apply(this, arguments)
  16. }
  17. if (extra) {
  18. var methods = extra.call(klass, superclass.prototype)
  19. for (var method in methods) klass.prototype[method] = methods[method]
  20. }
  21. return klass
  22. }
  23. var TestRunner = inherit(ConsoleTestRunner, function(_super) {
  24. AutoRunner.RUNNERS.zepto = this
  25. return {
  26. _makeResult: function() { return new TestResult(this.logger) }
  27. }
  28. })
  29. var TestResult = inherit(ConsoleTestResult, function(_super) {
  30. return {
  31. start: function(t0) {
  32. Evidence.TestResult.prototype.start.call(this, t0)
  33. this.logger.debug('Started tests.')
  34. },
  35. stop: function(t1) {
  36. _super.stop.call(this, t1)
  37. displayResults(this, (t1-this.t0)/1000)
  38. checkLeakedGlobals()
  39. },
  40. pauseTest: function(testcase) {
  41. this.logger.debug('Paused testcase ' + testcase + '.')
  42. },
  43. restartTest: function(testcase) {
  44. this.logger.debug('Restarted testcase ' + testcase + '.')
  45. },
  46. startSuite: function(suite) {
  47. this.logger.debug('Started suite ' + suite + '.')
  48. },
  49. addFailure: function(testcase, reason) {
  50. this.logToServer(testcase, reason)
  51. _super.addFailure.call(this, testcase, reason)
  52. },
  53. addError: function(testcase, error) {
  54. this.logToServer(testcase, error)
  55. _super.addError.call(this, testcase, error)
  56. },
  57. logToServer: function(testcase, error) {
  58. if (location.protocol == 'file:') return
  59. var name = testcase.parent.name + '#' + testcase.name,
  60. message = error.template ?
  61. Evidence.UI.printf(error.template, error.args) + (error.message ? ' ' + error.message : '') :
  62. error.message,
  63. body = 'name=' + encodeURIComponent(name) + '&' +
  64. 'message=' + encodeURIComponent(message) + '&' +
  65. 'trace=' + encodeURIComponent(error.stack || '')
  66. var xhr = new XMLHttpRequest()
  67. xhr.open('POST', '/test/log', true)
  68. xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
  69. xhr.send(body)
  70. }
  71. }
  72. })
  73. Evidence.TestCase.prototype.debug = function(message) {
  74. this._result.logToServer(this, { message: message })
  75. }
  76. // HACK: force our test runner as default
  77. ;(function(){
  78. var _super = AutoRunner.prototype.retrieveOptions
  79. AutoRunner.prototype.retrieveOptions = function() {
  80. var options = _super.call(this)
  81. if (!options.runner) options.runner = 'zepto'
  82. return options
  83. }
  84. })()
  85. function $(id) { return document.getElementById(id) }
  86. function displayResults(results, seconds) {
  87. var container = $('results')
  88. if (container) {
  89. if (results.failureCount || results.errorCount) {
  90. container.className = 'failed'
  91. container.innerHTML = printf("Finished in %d s. &ndash; <em>%d failures, %d errors</em> (%d assertions)",
  92. [seconds, results.failureCount, results.errorCount, results.assertionCount])
  93. } else {
  94. container.className = 'passed'
  95. container.innerHTML = printf("Finished in %d s. &ndash; <em>%d tests passed</em> (%d assertions)",
  96. [seconds, results.testCount, results.assertionCount])
  97. }
  98. container.className += ' finished'
  99. }
  100. }
  101. var globals = [], expected = ['Zepto', '$', 'Evidence', '_zid', 'jsonpDummy']
  102. for (var key in window) globals.push(key)
  103. function checkLeakedGlobals() {
  104. var opera = /^Opera\b/.test(navigator.userAgent)
  105. for (var key in window)
  106. if ( globals.indexOf(key) < 0 && expected.indexOf(key) < 0 &&
  107. (!opera || typeof window[key] != 'object' || window[key].id != key) &&
  108. window.console && console.warn
  109. )
  110. console.warn("unexpected global: " + key)
  111. }
  112. })()