test_attribute-parsing-01.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Test splitBy from node-attribute-parser.js
  5. const {require} = Components.utils.import("resource://devtools/shared/Loader.jsm", {});
  6. const {splitBy} = require("devtools/client/shared/node-attribute-parser");
  7. const TEST_DATA = [{
  8. value: "this is a test",
  9. splitChar: " ",
  10. expected: [
  11. {value: "this"},
  12. {value: " ", type: "string"},
  13. {value: "is"},
  14. {value: " ", type: "string"},
  15. {value: "a"},
  16. {value: " ", type: "string"},
  17. {value: "test"}
  18. ]
  19. }, {
  20. value: "/path/to/handler",
  21. splitChar: " ",
  22. expected: [
  23. {value: "/path/to/handler"}
  24. ]
  25. }, {
  26. value: "test",
  27. splitChar: " ",
  28. expected: [
  29. {value: "test"}
  30. ]
  31. }, {
  32. value: " test ",
  33. splitChar: " ",
  34. expected: [
  35. {value: " ", type: "string"},
  36. {value: "test"},
  37. {value: " ", type: "string"}
  38. ]
  39. }, {
  40. value: "",
  41. splitChar: " ",
  42. expected: []
  43. }, {
  44. value: " ",
  45. splitChar: " ",
  46. expected: [
  47. {value: " ", type: "string"},
  48. {value: " ", type: "string"},
  49. {value: " ", type: "string"}
  50. ]
  51. }];
  52. function run_test() {
  53. for (let {value, splitChar, expected} of TEST_DATA) {
  54. do_print("Splitting string: " + value);
  55. let tokens = splitBy(value, splitChar);
  56. do_print("Checking that the number of parsed tokens is correct");
  57. do_check_eq(tokens.length, expected.length);
  58. for (let i = 0; i < tokens.length; i++) {
  59. do_print("Checking the data in token " + i);
  60. do_check_eq(tokens[i].value, expected[i].value);
  61. if (expected[i].type) {
  62. do_check_eq(tokens[i].type, expected[i].type);
  63. }
  64. }
  65. }
  66. }