unittests.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE html>
  2. <!--
  3. Test Harness for CodeMirror
  4. This HTML file provides a minimal environment within which one can execute
  5. JSUnit-style tests.
  6. -->
  7. <html>
  8. <head>
  9. <title>Test Harness for CodeMirror</title>
  10. <script type="text/javascript" src="unittests.js"></script>
  11. <script type="text/javascript">
  12. // Counters for unit test results.
  13. var test_good = 0;
  14. var test_bad = 0;
  15. // If expected and actual are the identical, print 'Ok', otherwise 'Fail!'
  16. function assertEquals(msg, expected, actual) {
  17. if (typeof actual == 'undefined') {
  18. // msg is optional.
  19. actual = expected;
  20. expected = msg;
  21. msg = 'Expected: \'' + expected + '\' Actual: \'' + actual + '\'';
  22. }
  23. var html;
  24. if (expected === actual) {
  25. html = '<FONT COLOR="#009900">Ok</FONT>';
  26. test_good++;
  27. } else {
  28. html = '<FONT COLOR="#990000"><BIG>Fail!</BIG></FONT><BR>';
  29. msg += ' Expected: \'' + expected + '\' Actual: \'' + actual + '\'';
  30. msg = msg.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  31. msg = msg.replace(/\r\n/g, '&para;');
  32. msg = msg.replace(/\n/g, '&para;');
  33. msg = msg.replace(/\r/g, '&para;');
  34. msg = msg.replace(/\u00A0/g, '&middot;');
  35. html += '<code>' + msg + '</code>';
  36. test_bad++;
  37. }
  38. printDiv(html);
  39. }
  40. // If expected and actual are the equivalent, pass the test.
  41. function assertEquivalent(msg, expected, actual) {
  42. if (typeof actual == 'undefined') {
  43. // msg is optional.
  44. actual = expected;
  45. expected = msg;
  46. msg = 'Expected: \'' + expected + '\' Actual: \'' + actual + '\'';
  47. }
  48. if (_equivalent(expected, actual)) {
  49. assertEquals(msg, String.toString(expected), String.toString(actual));
  50. } else {
  51. assertEquals(msg, expected, actual);
  52. }
  53. }
  54. // Are a and b the equivalent? -- Recursive.
  55. function _equivalent(a, b) {
  56. if (a == b) {
  57. return true;
  58. }
  59. if (typeof a == 'object' && typeof b == 'object' && a !== null && b !== null) {
  60. if (a.toString() != b.toString()) {
  61. return false;
  62. }
  63. for (var p in a) {
  64. if (!_equivalent(a[p], b[p])) {
  65. return false;
  66. }
  67. }
  68. for (var p in b) {
  69. if (!_equivalent(a[p], b[p])) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. return false;
  76. }
  77. function runTests() {
  78. for (var x = 0; x < tests.length; x++) {
  79. printDiv('<H3>' + tests[x] + ':</H3>');
  80. eval(tests[x] + '()');
  81. }
  82. }
  83. function initTests() {
  84. var startTime = (new Date()).getTime();
  85. runTests();
  86. var endTime = (new Date()).getTime();
  87. var html = '<H3>Done.</H3>';
  88. html += '<P>Tests passed: ' + test_good + '<BR>Tests failed: ' + test_bad + '</P>';
  89. html += '<P>Total time: ' + (endTime - startTime) + ' ms</P>';
  90. printDiv(html);
  91. }
  92. function printDiv(html) {
  93. var div = document.createElement('div');
  94. div.innerHTML = html;
  95. document.getElementById('testoutput').appendChild(div);
  96. }
  97. </script>
  98. </head>
  99. <body onload="initTests()">
  100. <h1>Test Harness for CodeMirror</h1>
  101. <div id="testoutput"></div>
  102. <hr>
  103. <div style="border: 1px solid black;">
  104. <textarea id="inputfield"></textarea>
  105. </div>
  106. <script type="text/javascript" src="js/codemirror.js"></script>
  107. <script>
  108. var editor = new CodeMirror(CodeMirror.replace(document.getElementById('inputfield')), {
  109. parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
  110. path: "js/"
  111. });
  112. </script>
  113. </body>
  114. </html>