parser.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {Operation, parseArithmetic} from 'sentry/components/arithmeticInput/parser';
  2. describe('arithmeticInput/parser', function () {
  3. it('errors on too many operators', () => {
  4. expect(parseArithmetic('1+1+1+1+1+1+1+1+1+1+1+1').error).toEqual(
  5. 'Maximum operators exceeded'
  6. );
  7. });
  8. it('errors on divide by 0', () => {
  9. expect(parseArithmetic('1/0').error).toEqual('Division by 0 is not allowed');
  10. });
  11. it('handles one term', () => {
  12. expect(parseArithmetic('1').result).toStrictEqual('1');
  13. });
  14. it('handles some addition', () => {
  15. expect(parseArithmetic('1 + 2').result).toStrictEqual(
  16. new Operation({
  17. operator: 'plus',
  18. lhs: '1',
  19. rhs: '2',
  20. })
  21. );
  22. });
  23. it('handles three term addition', () => {
  24. expect(parseArithmetic('1 + 2 + 3').result).toStrictEqual(
  25. new Operation({
  26. operator: 'plus',
  27. lhs: new Operation({
  28. operator: 'plus',
  29. lhs: '1',
  30. rhs: '2',
  31. }),
  32. rhs: '3',
  33. })
  34. );
  35. });
  36. it('handles some multiplication', () => {
  37. expect(parseArithmetic('1 * 2').result).toStrictEqual(
  38. new Operation({
  39. operator: 'multiply',
  40. lhs: '1',
  41. rhs: '2',
  42. })
  43. );
  44. });
  45. it('handles three term multiplication', () => {
  46. expect(parseArithmetic('1 * 2 * 3').result).toStrictEqual(
  47. new Operation({
  48. operator: 'multiply',
  49. lhs: new Operation({
  50. operator: 'multiply',
  51. lhs: '1',
  52. rhs: '2',
  53. }),
  54. rhs: '3',
  55. })
  56. );
  57. });
  58. it('handles brackets', () => {
  59. expect(parseArithmetic('1 * (2 + 3)').result).toStrictEqual(
  60. new Operation({
  61. operator: 'multiply',
  62. lhs: '1',
  63. rhs: new Operation({
  64. operator: 'plus',
  65. lhs: '2',
  66. rhs: '3',
  67. }),
  68. })
  69. );
  70. expect(parseArithmetic('(1 + 2) / 3').result).toStrictEqual(
  71. new Operation({
  72. operator: 'divide',
  73. lhs: new Operation({
  74. operator: 'plus',
  75. lhs: '1',
  76. rhs: '2',
  77. }),
  78. rhs: '3',
  79. })
  80. );
  81. });
  82. it('handles order of operations', () => {
  83. expect(parseArithmetic('1 + 2 * 3').result).toStrictEqual(
  84. new Operation({
  85. operator: 'plus',
  86. lhs: '1',
  87. rhs: new Operation({
  88. operator: 'multiply',
  89. lhs: '2',
  90. rhs: '3',
  91. }),
  92. })
  93. );
  94. expect(parseArithmetic('1 / 2 - 3').result).toStrictEqual(
  95. new Operation({
  96. operator: 'minus',
  97. lhs: new Operation({
  98. operator: 'divide',
  99. lhs: '1',
  100. rhs: '2',
  101. }),
  102. rhs: '3',
  103. })
  104. );
  105. });
  106. it('handles fields and functions', () => {
  107. expect(parseArithmetic('spans.db + measurements.lcp').result).toStrictEqual(
  108. new Operation({
  109. operator: 'plus',
  110. lhs: 'spans.db',
  111. rhs: 'measurements.lcp',
  112. })
  113. );
  114. expect(parseArithmetic('failure_count() + count_unique(user)').result).toStrictEqual(
  115. new Operation({
  116. operator: 'plus',
  117. lhs: 'failure_count()',
  118. rhs: 'count_unique(user)',
  119. })
  120. );
  121. });
  122. });