document-matchers.tests.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var assert = require("assert");
  2. var test = require("../test")(module);
  3. var documentMatchers = require("../../lib/styles/document-matchers");
  4. var documents = require("../../lib/documents");
  5. var Paragraph = documents.Paragraph;
  6. test("paragraph with no options matches any paragraph", function() {
  7. var matcher = documentMatchers.paragraph();
  8. assert.ok(matcher.matches(new Paragraph()));
  9. assert.ok(matcher.matches(paragraphWithStyle("Heading1", "Heading 1")));
  10. });
  11. test("paragraph style ID only matches paragraphs with that style ID", function() {
  12. var matcher = documentMatchers.paragraph({styleId: "Heading1"});
  13. assert.ok(!matcher.matches(new Paragraph()));
  14. assert.ok(matcher.matches(paragraphWithStyle("Heading1", "Heading 1")));
  15. assert.ok(!matcher.matches(paragraphWithStyle("Heading2", "Heading 2")));
  16. });
  17. test("paragraph style name only matches paragraphs with that style name", function() {
  18. var matcher = documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")});
  19. assert.ok(!matcher.matches(new Paragraph()));
  20. assert.ok(matcher.matches(paragraphWithStyle("Heading1", "Heading 1")));
  21. assert.ok(!matcher.matches(paragraphWithStyle("Heading2", "Heading 2")));
  22. });
  23. test("ordered-list(index) matches an ordered list with specified level index", function() {
  24. var matcher = documentMatchers.paragraph({list: {isOrdered: true, levelIndex: 1}});
  25. assert.ok(!matcher.matches(new Paragraph()));
  26. assert.ok(matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: true}})));
  27. assert.ok(!matcher.matches(new Paragraph([], {numbering: {level: 0, isOrdered: true}})));
  28. assert.ok(!matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: false}})));
  29. });
  30. test("unordered-list(index) matches an unordered list with specified level index", function() {
  31. var matcher = documentMatchers.paragraph({list: {isOrdered: false, levelIndex: 1}});
  32. assert.ok(!matcher.matches(new Paragraph()));
  33. assert.ok(matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: false}})));
  34. assert.ok(!matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: true}})));
  35. });
  36. test("matchers for lists with index 0 do not match elements that are not lists", function() {
  37. var matcher = documentMatchers.paragraph({list: {isOrdered: true, levelIndex: 0}});
  38. assert.ok(!matcher.matches(new Paragraph()));
  39. });
  40. test("highlight matcher does not match non-highlight elements", function() {
  41. var matcher = documentMatchers.highlight();
  42. assert.ok(!matcher.matches(new Paragraph()));
  43. });
  44. test("highlight matcher without color matches all highlight elements", function() {
  45. var matcher = documentMatchers.highlight({});
  46. assert.ok(matcher.matches({type: "highlight", color: "yellow"}));
  47. });
  48. test("highlight matcher with color matches highlight with that color", function() {
  49. var matcher = documentMatchers.highlight({color: "yellow"});
  50. assert.ok(matcher.matches({type: "highlight", color: "yellow"}));
  51. });
  52. test("highlight matcher with color does not match highlights with other colors", function() {
  53. var matcher = documentMatchers.highlight({color: "yellow"});
  54. assert.ok(!matcher.matches({type: "highlight", color: "red"}));
  55. });
  56. function paragraphWithStyle(styleId, styleName) {
  57. return new Paragraph([], {styleId: styleId, styleName: styleName});
  58. }
  59. test("equalTo matcher is case insensitive", function() {
  60. var matcher = documentMatchers.equalTo("Heading 1");
  61. assert.ok(matcher.operator(matcher.operand, "heaDING 1"));
  62. assert.ok(!matcher.operator(matcher.operand, "heaDING 2"));
  63. });
  64. test("startsWith matches strings with prefix", function() {
  65. var matcher = documentMatchers.startsWith("Heading");
  66. assert.ok(matcher.operator(matcher.operand, "Heading"));
  67. assert.ok(matcher.operator(matcher.operand, "Heading 1"));
  68. assert.ok(!matcher.operator(matcher.operand, "Custom Heading"));
  69. assert.ok(!matcher.operator(matcher.operand, "Head"));
  70. assert.ok(!matcher.operator(matcher.operand, "Header 2"));
  71. });
  72. test("startsWith matcher is case insensitive", function() {
  73. var matcher = documentMatchers.startsWith("Heading");
  74. assert.ok(matcher.operator(matcher.operand, "heaDING"));
  75. });