parser.spec.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {joinTokens, parseFormula} from 'sentry/views/ddm/formulaParser/parser';
  2. import {TokenType} from 'sentry/views/ddm/formulaParser/types';
  3. const complexFormula = '2 / ((foo + 3) * c - (3 / 5))';
  4. const complexTokenList = [
  5. {type: TokenType.NUMBER, content: '2'},
  6. {type: TokenType.WHITESPACE, content: ' '},
  7. {type: TokenType.DIVIDE, content: '/'},
  8. {type: TokenType.WHITESPACE, content: ' '},
  9. {type: TokenType.OPENPAREN, content: '('},
  10. {type: TokenType.WHITESPACE, content: ''},
  11. {type: TokenType.OPENPAREN, content: '('},
  12. {type: TokenType.WHITESPACE, content: ''},
  13. {type: TokenType.VARIABLE, content: 'foo'},
  14. {type: TokenType.WHITESPACE, content: ' '},
  15. {type: TokenType.PLUS, content: '+'},
  16. {type: TokenType.WHITESPACE, content: ' '},
  17. {type: TokenType.NUMBER, content: '3'},
  18. {type: TokenType.WHITESPACE, content: ''},
  19. {type: TokenType.CLOSEPAREN, content: ')'},
  20. {type: TokenType.WHITESPACE, content: ' '},
  21. {type: TokenType.MULTIPLY, content: '*'},
  22. {type: TokenType.WHITESPACE, content: ' '},
  23. {type: TokenType.VARIABLE, content: 'c'},
  24. {type: TokenType.WHITESPACE, content: ' '},
  25. {type: TokenType.MINUS, content: '-'},
  26. {type: TokenType.WHITESPACE, content: ' '},
  27. {type: TokenType.OPENPAREN, content: '('},
  28. {type: TokenType.WHITESPACE, content: ''},
  29. {type: TokenType.NUMBER, content: '3'},
  30. {type: TokenType.WHITESPACE, content: ' '},
  31. {type: TokenType.DIVIDE, content: '/'},
  32. {type: TokenType.WHITESPACE, content: ' '},
  33. {type: TokenType.NUMBER, content: '5'},
  34. {type: TokenType.WHITESPACE, content: ''},
  35. {type: TokenType.CLOSEPAREN, content: ')'},
  36. {type: TokenType.WHITESPACE, content: ''},
  37. {type: TokenType.CLOSEPAREN, content: ')'},
  38. ];
  39. describe('formula > parseFormula', () => {
  40. it('parses a simple formula', () => {
  41. expect(parseFormula('1 + 1')).toEqual([
  42. {type: TokenType.NUMBER, content: '1'},
  43. {type: TokenType.WHITESPACE, content: ' '},
  44. {type: TokenType.PLUS, content: '+'},
  45. {type: TokenType.WHITESPACE, content: ' '},
  46. {type: TokenType.NUMBER, content: '1'},
  47. ]);
  48. });
  49. it('parses a variable', () => {
  50. expect(parseFormula('a')).toEqual([{type: TokenType.VARIABLE, content: 'a'}]);
  51. expect(parseFormula('bar')).toEqual([{type: TokenType.VARIABLE, content: 'bar'}]);
  52. });
  53. it('parses a number', () => {
  54. expect(parseFormula('1')).toEqual([{type: TokenType.NUMBER, content: '1'}]);
  55. expect(parseFormula('1.1')).toEqual([{type: TokenType.NUMBER, content: '1.1'}]);
  56. expect(parseFormula('1.01')).toEqual([{type: TokenType.NUMBER, content: '1.01'}]);
  57. expect(parseFormula('1.010')).toEqual([{type: TokenType.NUMBER, content: '1.010'}]);
  58. });
  59. it('parses a more complex formula', () => {
  60. expect(parseFormula(complexFormula)).toEqual(complexTokenList);
  61. });
  62. it('fails to parse an invalid formula', () => {
  63. expect(() => parseFormula('1 1')).toThrow();
  64. });
  65. });
  66. describe('formula > joinTokens', () => {
  67. it('joins a list of tokens', () => {
  68. expect(joinTokens(complexTokenList)).toEqual(complexFormula);
  69. });
  70. });