parsingUtils.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { parser } from 'lezer-promql';
  2. import { getLeftMostChild, getString, replaceVariables } from './parsingUtils';
  3. describe('getLeftMostChild', () => {
  4. it('return left most child', () => {
  5. const tree = parser.parse('sum_over_time(foo{bar="baz"}[5m])');
  6. const child = getLeftMostChild(tree.topNode);
  7. expect(child).toBeDefined();
  8. expect(child!.name).toBe('SumOverTime');
  9. });
  10. });
  11. describe('replaceVariables', () => {
  12. it('should replace variables', () => {
  13. expect(replaceVariables('sum_over_time([[metric_var]]{bar="${app}"}[$__interval])')).toBe(
  14. 'sum_over_time(__V_1__metric_var__V__{bar="__V_2__app__V__"}[__V_0____interval__V__])'
  15. );
  16. });
  17. });
  18. describe('getString', () => {
  19. it('should return correct string representation of the node', () => {
  20. const expr = 'sum_over_time(foo{bar="baz"}[5m])';
  21. const tree = parser.parse(expr);
  22. const child = getLeftMostChild(tree.topNode);
  23. expect(getString(expr, child)).toBe('sum_over_time');
  24. });
  25. it('should return string with correct variables', () => {
  26. const expr = 'sum_over_time(__V_1__metric_var__V__{bar="__V_2__app__V__"}[__V_0____interval__V__])';
  27. const tree = parser.parse(expr);
  28. expect(getString(expr, tree.topNode)).toBe('sum_over_time([[metric_var]]{bar="${app}"}[$__interval])');
  29. });
  30. it('is symmetrical with replaceVariables', () => {
  31. const expr = 'sum_over_time([[metric_var]]{bar="${app}"}[$__interval])';
  32. const replaced = replaceVariables(expr);
  33. const tree = parser.parse(replaced);
  34. expect(getString(replaced, tree.topNode)).toBe(expr);
  35. });
  36. });