attribute_parser.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import attributeParser from '../src/public/app/services/attribute_parser.js';
  2. import {describe, it, expect, execute} from './mini_test.js';
  3. describe("Lexing", () => {
  4. it("simple label", () => {
  5. expect(attributeParser.lex("#label").map(t => t.text))
  6. .toEqual(["#label"]);
  7. });
  8. it("simple label with trailing spaces", () => {
  9. expect(attributeParser.lex(" #label ").map(t => t.text))
  10. .toEqual(["#label"]);
  11. });
  12. it("inherited label", () => {
  13. expect(attributeParser.lex("#label(inheritable)").map(t => t.text))
  14. .toEqual(["#label", "(", "inheritable", ")"]);
  15. expect(attributeParser.lex("#label ( inheritable ) ").map(t => t.text))
  16. .toEqual(["#label", "(", "inheritable", ")"]);
  17. });
  18. it("label with value", () => {
  19. expect(attributeParser.lex("#label=Hallo").map(t => t.text))
  20. .toEqual(["#label", "=", "Hallo"]);
  21. });
  22. it("label with value", () => {
  23. const tokens = attributeParser.lex("#label=Hallo");
  24. expect(tokens[0].startIndex).toEqual(0);
  25. expect(tokens[0].endIndex).toEqual(5);
  26. });
  27. it("relation with value", () => {
  28. expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map(t => t.text))
  29. .toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
  30. });
  31. it("use quotes to define value", () => {
  32. expect(attributeParser.lex("#'label a'='hello\"` world'").map(t => t.text))
  33. .toEqual(["#label a", "=", 'hello"` world']);
  34. expect(attributeParser.lex('#"label a" = "hello\'` world"').map(t => t.text))
  35. .toEqual(["#label a", "=", "hello'` world"]);
  36. expect(attributeParser.lex('#`label a` = `hello\'" world`').map(t => t.text))
  37. .toEqual(["#label a", "=", "hello'\" world"]);
  38. });
  39. });
  40. describe("Parser", () => {
  41. it("simple label", () => {
  42. const attrs = attributeParser.parse(["#token"].map(t => ({text: t})));
  43. expect(attrs.length).toEqual(1);
  44. expect(attrs[0].type).toEqual('label');
  45. expect(attrs[0].name).toEqual('token');
  46. expect(attrs[0].isInheritable).toBeFalsy();
  47. expect(attrs[0].value).toBeFalsy();
  48. });
  49. it("inherited label", () => {
  50. const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map(t => ({text: t})));
  51. expect(attrs.length).toEqual(1);
  52. expect(attrs[0].type).toEqual('label');
  53. expect(attrs[0].name).toEqual('token');
  54. expect(attrs[0].isInheritable).toBeTruthy();
  55. expect(attrs[0].value).toBeFalsy();
  56. });
  57. it("label with value", () => {
  58. const attrs = attributeParser.parse(["#token", "=", "val"].map(t => ({text: t})));
  59. expect(attrs.length).toEqual(1);
  60. expect(attrs[0].type).toEqual('label');
  61. expect(attrs[0].name).toEqual('token');
  62. expect(attrs[0].value).toEqual("val");
  63. });
  64. it("relation", () => {
  65. let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map(t => ({text: t})));
  66. expect(attrs.length).toEqual(1);
  67. expect(attrs[0].type).toEqual('relation');
  68. expect(attrs[0].name).toEqual("token");
  69. expect(attrs[0].value).toEqual('NFi2gL4xtPxM');
  70. attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map(t => ({text: t})));
  71. expect(attrs.length).toEqual(1);
  72. expect(attrs[0].type).toEqual('relation');
  73. expect(attrs[0].name).toEqual("token");
  74. expect(attrs[0].value).toEqual('NFi2gL4xtPxM');
  75. });
  76. });
  77. describe("error cases", () => {
  78. it("error cases", () => {
  79. expect(() => attributeParser.lexAndParse('~token'))
  80. .toThrow('Relation "~token" in "~token" should point to a note.');
  81. expect(() => attributeParser.lexAndParse("#a&b/s"))
  82. .toThrow(`Attribute name "a&b/s" contains disallowed characters, only alphanumeric characters, colon and underscore are allowed.`);
  83. expect(() => attributeParser.lexAndParse("#"))
  84. .toThrow(`Attribute name is empty, please fill the name.`);
  85. });
  86. });
  87. execute();