test_getRuleText.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/ */
  4. "use strict";
  5. const {getRuleText} = require("devtools/server/actors/styles");
  6. const TEST_DATA = [
  7. {
  8. desc: "Empty input",
  9. input: "",
  10. line: 1,
  11. column: 1,
  12. throws: true
  13. },
  14. {
  15. desc: "Simplest test case",
  16. input: "#id{color:red;background:yellow;}",
  17. line: 1,
  18. column: 1,
  19. expected: {offset: 4, text: "color:red;background:yellow;"}
  20. },
  21. {
  22. desc: "Multiple rules test case",
  23. input: "#id{color:red;background:yellow;}.class-one .class-two { position:absolute; line-height: 45px}",
  24. line: 1,
  25. column: 34,
  26. expected: {offset: 56, text: " position:absolute; line-height: 45px"}
  27. },
  28. {
  29. desc: "Unclosed rule",
  30. input: "#id{color:red;background:yellow;",
  31. line: 1,
  32. column: 1,
  33. expected: {offset: 4, text: "color:red;background:yellow;"}
  34. },
  35. {
  36. desc: "Null input",
  37. input: null,
  38. line: 1,
  39. column: 1,
  40. throws: true
  41. },
  42. {
  43. desc: "Missing loc",
  44. input: "#id{color:red;background:yellow;}",
  45. throws: true
  46. },
  47. {
  48. desc: "Multi-lines CSS",
  49. input: [
  50. "/* this is a multi line css */",
  51. "body {",
  52. " color: green;",
  53. " background-repeat: no-repeat",
  54. "}",
  55. " /*something else here */",
  56. "* {",
  57. " color: purple;",
  58. "}"
  59. ].join("\n"),
  60. line: 7,
  61. column: 1,
  62. expected: {offset: 116, text: "\n color: purple;\n"}
  63. },
  64. {
  65. desc: "Multi-lines CSS and multi-line rule",
  66. input: [
  67. "/* ",
  68. "* some comments",
  69. "*/",
  70. "",
  71. "body {",
  72. " margin: 0;",
  73. " padding: 15px 15px 2px 15px;",
  74. " color: red;",
  75. "}",
  76. "",
  77. "#header .btn, #header .txt {",
  78. " font-size: 100%;",
  79. "}",
  80. "",
  81. "#header #information {",
  82. " color: #dddddd;",
  83. " font-size: small;",
  84. "}",
  85. ].join("\n"),
  86. line: 5,
  87. column: 1,
  88. expected: {
  89. offset: 30,
  90. text: "\n margin: 0;\n padding: 15px 15px 2px 15px;\n color: red;\n"}
  91. },
  92. {
  93. desc: "Content string containing a } character",
  94. input: " #id{border:1px solid red;content: '}';color:red;}",
  95. line: 1,
  96. column: 4,
  97. expected: {offset: 7, text: "border:1px solid red;content: '}';color:red;"}
  98. },
  99. {
  100. desc: "Rule contains no tokens",
  101. input: "div{}",
  102. line: 1,
  103. column: 1,
  104. expected: {offset: 4, text: ""}
  105. },
  106. ];
  107. function run_test() {
  108. for (let test of TEST_DATA) {
  109. do_print("Starting test: " + test.desc);
  110. do_print("Input string " + test.input);
  111. let output;
  112. try {
  113. output = getRuleText(test.input, test.line, test.column);
  114. if (test.throws) {
  115. do_print("Test should have thrown");
  116. do_check_true(false);
  117. }
  118. } catch (e) {
  119. do_print("getRuleText threw an exception with the given input string");
  120. if (test.throws) {
  121. do_print("Exception expected");
  122. do_check_true(true);
  123. } else {
  124. do_print("Exception unexpected\n" + e);
  125. do_check_true(false);
  126. }
  127. }
  128. if (output) {
  129. deepEqual(output, test.expected);
  130. }
  131. }
  132. }