123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import {Operation, parseArithmetic} from 'sentry/components/arithmeticInput/parser';
- describe('arithmeticInput/parser', function () {
- it('errors on too many operators', () => {
- expect(parseArithmetic('1+1+1+1+1+1+1+1+1+1+1+1').error).toEqual(
- 'Maximum operators exceeded'
- );
- });
- it('errors on divide by 0', () => {
- expect(parseArithmetic('1/0').error).toEqual('Division by 0 is not allowed');
- });
- it('handles one term', () => {
- expect(parseArithmetic('1').result).toStrictEqual('1');
- });
- it('handles some addition', () => {
- expect(parseArithmetic('1 + 2').result).toStrictEqual(
- new Operation({
- operator: 'plus',
- lhs: '1',
- rhs: '2',
- })
- );
- });
- it('handles three term addition', () => {
- expect(parseArithmetic('1 + 2 + 3').result).toStrictEqual(
- new Operation({
- operator: 'plus',
- lhs: new Operation({
- operator: 'plus',
- lhs: '1',
- rhs: '2',
- }),
- rhs: '3',
- })
- );
- });
- it('handles some multiplication', () => {
- expect(parseArithmetic('1 * 2').result).toStrictEqual(
- new Operation({
- operator: 'multiply',
- lhs: '1',
- rhs: '2',
- })
- );
- });
- it('handles three term multiplication', () => {
- expect(parseArithmetic('1 * 2 * 3').result).toStrictEqual(
- new Operation({
- operator: 'multiply',
- lhs: new Operation({
- operator: 'multiply',
- lhs: '1',
- rhs: '2',
- }),
- rhs: '3',
- })
- );
- });
- it('handles brackets', () => {
- expect(parseArithmetic('1 * (2 + 3)').result).toStrictEqual(
- new Operation({
- operator: 'multiply',
- lhs: '1',
- rhs: new Operation({
- operator: 'plus',
- lhs: '2',
- rhs: '3',
- }),
- })
- );
- expect(parseArithmetic('(1 + 2) / 3').result).toStrictEqual(
- new Operation({
- operator: 'divide',
- lhs: new Operation({
- operator: 'plus',
- lhs: '1',
- rhs: '2',
- }),
- rhs: '3',
- })
- );
- });
- it('handles order of operations', () => {
- expect(parseArithmetic('1 + 2 * 3').result).toStrictEqual(
- new Operation({
- operator: 'plus',
- lhs: '1',
- rhs: new Operation({
- operator: 'multiply',
- lhs: '2',
- rhs: '3',
- }),
- })
- );
- expect(parseArithmetic('1 / 2 - 3').result).toStrictEqual(
- new Operation({
- operator: 'minus',
- lhs: new Operation({
- operator: 'divide',
- lhs: '1',
- rhs: '2',
- }),
- rhs: '3',
- })
- );
- });
- it('handles fields and functions', () => {
- expect(parseArithmetic('spans.db + measurements.lcp').result).toStrictEqual(
- new Operation({
- operator: 'plus',
- lhs: 'spans.db',
- rhs: 'measurements.lcp',
- })
- );
- expect(parseArithmetic('failure_count() + count_unique(user)').result).toStrictEqual(
- new Operation({
- operator: 'plus',
- lhs: 'failure_count()',
- rhs: 'count_unique(user)',
- })
- );
- });
- });
|