parser.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { Parser } from '../parser';
  2. describe('when parsing', () => {
  3. it('simple metric expression', () => {
  4. const parser = new Parser('metric.test.*.asd.count');
  5. const rootNode = parser.getAst();
  6. expect(rootNode.type).toBe('metric');
  7. expect(rootNode.segments.length).toBe(5);
  8. expect(rootNode.segments[0].value).toBe('metric');
  9. });
  10. it('simple metric expression with numbers in segments', () => {
  11. const parser = new Parser('metric.10.15_20.5');
  12. const rootNode = parser.getAst();
  13. expect(rootNode.type).toBe('metric');
  14. expect(rootNode.segments.length).toBe(4);
  15. expect(rootNode.segments[1].value).toBe('10');
  16. expect(rootNode.segments[2].value).toBe('15_20');
  17. expect(rootNode.segments[3].value).toBe('5');
  18. });
  19. it('simple metric expression with curly braces', () => {
  20. const parser = new Parser('metric.se1-{count, max}');
  21. const rootNode = parser.getAst();
  22. expect(rootNode.type).toBe('metric');
  23. expect(rootNode.segments.length).toBe(2);
  24. expect(rootNode.segments[1].value).toBe('se1-{count,max}');
  25. });
  26. it('simple metric expression with curly braces at start of segment and with post chars', () => {
  27. const parser = new Parser('metric.{count, max}-something.count');
  28. const rootNode = parser.getAst();
  29. expect(rootNode.type).toBe('metric');
  30. expect(rootNode.segments.length).toBe(3);
  31. expect(rootNode.segments[1].value).toBe('{count,max}-something');
  32. });
  33. it('simple function', () => {
  34. const parser = new Parser('sum(test)');
  35. const rootNode = parser.getAst();
  36. expect(rootNode.type).toBe('function');
  37. expect(rootNode.params.length).toBe(1);
  38. });
  39. it('simple function2', () => {
  40. const parser = new Parser('offset(test.metric, -100)');
  41. const rootNode = parser.getAst();
  42. expect(rootNode.type).toBe('function');
  43. expect(rootNode.params[0].type).toBe('metric');
  44. expect(rootNode.params[1].type).toBe('number');
  45. });
  46. it('simple function with string arg', () => {
  47. const parser = new Parser("randomWalk('test')");
  48. const rootNode = parser.getAst();
  49. expect(rootNode.type).toBe('function');
  50. expect(rootNode.params.length).toBe(1);
  51. expect(rootNode.params[0].type).toBe('string');
  52. });
  53. it('function with multiple args', () => {
  54. const parser = new Parser("sum(test, 1, 'test')");
  55. const rootNode = parser.getAst();
  56. expect(rootNode.type).toBe('function');
  57. expect(rootNode.params.length).toBe(3);
  58. expect(rootNode.params[0].type).toBe('metric');
  59. expect(rootNode.params[1].type).toBe('number');
  60. expect(rootNode.params[2].type).toBe('string');
  61. });
  62. it('function with nested function', () => {
  63. const parser = new Parser('sum(scaleToSeconds(test, 1))');
  64. const rootNode = parser.getAst();
  65. expect(rootNode.type).toBe('function');
  66. expect(rootNode.params.length).toBe(1);
  67. expect(rootNode.params[0].type).toBe('function');
  68. expect(rootNode.params[0].name).toBe('scaleToSeconds');
  69. expect(rootNode.params[0].params.length).toBe(2);
  70. expect(rootNode.params[0].params[0].type).toBe('metric');
  71. expect(rootNode.params[0].params[1].type).toBe('number');
  72. });
  73. it('function with multiple series', () => {
  74. const parser = new Parser('sum(test.test.*.count, test.timers.*.count)');
  75. const rootNode = parser.getAst();
  76. expect(rootNode.type).toBe('function');
  77. expect(rootNode.params.length).toBe(2);
  78. expect(rootNode.params[0].type).toBe('metric');
  79. expect(rootNode.params[1].type).toBe('metric');
  80. });
  81. it('function with templated series', () => {
  82. const parser = new Parser('sum(test.[[server]].count)');
  83. const rootNode = parser.getAst();
  84. expect(rootNode.message).toBe(undefined);
  85. expect(rootNode.params[0].type).toBe('metric');
  86. expect(rootNode.params[0].segments[1].type).toBe('segment');
  87. expect(rootNode.params[0].segments[1].value).toBe('[[server]]');
  88. });
  89. it('invalid metric expression', () => {
  90. const parser = new Parser('metric.test.*.asd.');
  91. const rootNode = parser.getAst();
  92. expect(rootNode.message).toBe('Expected metric identifier instead found end of string');
  93. expect(rootNode.pos).toBe(19);
  94. });
  95. it('invalid function expression missing closing parenthesis', () => {
  96. const parser = new Parser('sum(test');
  97. const rootNode = parser.getAst();
  98. expect(rootNode.message).toBe('Expected closing parenthesis instead found end of string');
  99. expect(rootNode.pos).toBe(9);
  100. });
  101. it('unclosed string in function', () => {
  102. const parser = new Parser("sum('test)");
  103. const rootNode = parser.getAst();
  104. expect(rootNode.message).toBe('Unclosed string parameter');
  105. expect(rootNode.pos).toBe(11);
  106. });
  107. it('handle issue #69', () => {
  108. const parser = new Parser('cactiStyle(offset(scale(net.192-168-1-1.192-168-1-9.ping_value.*,0.001),-100))');
  109. const rootNode = parser.getAst();
  110. expect(rootNode.type).toBe('function');
  111. });
  112. it('handle float function arguments', () => {
  113. const parser = new Parser('scale(test, 0.002)');
  114. const rootNode = parser.getAst();
  115. expect(rootNode.type).toBe('function');
  116. expect(rootNode.params[1].type).toBe('number');
  117. expect(rootNode.params[1].value).toBe(0.002);
  118. });
  119. it('handle curly brace pattern at start', () => {
  120. const parser = new Parser('{apps}.test');
  121. const rootNode = parser.getAst();
  122. expect(rootNode.type).toBe('metric');
  123. expect(rootNode.segments[0].value).toBe('{apps}');
  124. expect(rootNode.segments[1].value).toBe('test');
  125. });
  126. it('series parameters', () => {
  127. const parser = new Parser('asPercent(#A, #B)');
  128. const rootNode = parser.getAst();
  129. expect(rootNode.type).toBe('function');
  130. expect(rootNode.params[0].type).toBe('series-ref');
  131. expect(rootNode.params[0].value).toBe('#A');
  132. expect(rootNode.params[1].value).toBe('#B');
  133. });
  134. it('series parameters, issue 2788', () => {
  135. const parser = new Parser("summarize(diffSeries(#A, #B), '10m', 'sum', false)");
  136. const rootNode = parser.getAst();
  137. expect(rootNode.type).toBe('function');
  138. expect(rootNode.params[0].type).toBe('function');
  139. expect(rootNode.params[1].value).toBe('10m');
  140. expect(rootNode.params[3].type).toBe('bool');
  141. });
  142. it('should parse metric expression with ip number segments', () => {
  143. const parser = new Parser('5.10.123.5');
  144. const rootNode = parser.getAst();
  145. expect(rootNode.segments[0].value).toBe('5');
  146. expect(rootNode.segments[1].value).toBe('10');
  147. expect(rootNode.segments[2].value).toBe('123');
  148. expect(rootNode.segments[3].value).toBe('5');
  149. });
  150. });