unittests.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * Test Harness for CodeMirror
  3. * JS-unit compatible tests here. The two available assertions are
  4. * assertEquals (strict equality) and assertEquivalent (looser equivalency).
  5. *
  6. * 'editor' is a global object for the CodeMirror editor shared between all
  7. * tests. After manipulating it in each test, try to restore it to
  8. * approximately its original state.
  9. */
  10. function testSetGet() {
  11. var code = 'It was the best of times.\nIt was the worst of times.';
  12. editor.setCode(code);
  13. assertEquals(code, editor.getCode());
  14. editor.setCode('');
  15. assertEquals('', editor.getCode());
  16. }
  17. function testSetStylesheet() {
  18. function cssStatus() {
  19. // Returns a list of tuples, for each CSS link return the filename and
  20. // whether it is enabled.
  21. links = editor.win.document.getElementsByTagName('link');
  22. css = [];
  23. for (var x = 0, link; link = links[x]; x++) {
  24. if (link.rel.indexOf("stylesheet") !== -1) {
  25. css.push([link.href.substring(link.href.lastIndexOf('/') + 1),
  26. !link.disabled])
  27. }
  28. }
  29. return css;
  30. }
  31. assertEquivalent([], cssStatus());
  32. editor.setStylesheet('css/jscolors.css');
  33. assertEquivalent([['jscolors.css', true]], cssStatus());
  34. editor.setStylesheet(['css/csscolors.css', 'css/xmlcolors.css']);
  35. assertEquivalent([['jscolors.css', false], ['csscolors.css', true], ['xmlcolors.css', true]], cssStatus());
  36. editor.setStylesheet([]);
  37. assertEquivalent([['jscolors.css', false], ['csscolors.css', false], ['xmlcolors.css', false]], cssStatus());
  38. }
  39. // Update this list of tests as new ones are added.
  40. var tests = ['testSetGet', 'testSetStylesheet'];