statementPosition.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { monacoTypes } from '@grafana/ui';
  2. import * as MetricMathTestQueries from '../../__mocks__/metric-math-test-data';
  3. import MonacoMock from '../../__mocks__/monarch/Monaco';
  4. import TextModel from '../../__mocks__/monarch/TextModel';
  5. import { linkedTokenBuilder } from '../../monarch/linkedTokenBuilder';
  6. import { StatementPosition } from '../../monarch/types';
  7. import cloudWatchSqlLanguageDefinition from '../definition';
  8. import { getStatementPosition } from './statementPosition';
  9. import { MetricMathTokenTypes } from './types';
  10. describe('statementPosition', () => {
  11. function createToken(query: string, position: monacoTypes.IPosition) {
  12. const testModel = TextModel(query);
  13. return linkedTokenBuilder(
  14. MonacoMock,
  15. cloudWatchSqlLanguageDefinition,
  16. testModel as monacoTypes.editor.ITextModel,
  17. position,
  18. MetricMathTokenTypes
  19. );
  20. }
  21. it('returns PredefinedFunction when at the beginning of an empty query', () => {
  22. const token = createToken(
  23. MetricMathTestQueries.singleLineEmptyQuery.query,
  24. MetricMathTestQueries.singleLineEmptyQuery.position
  25. );
  26. expect(getStatementPosition(token)).toEqual(StatementPosition.PredefinedFunction);
  27. });
  28. it('returns PredefinedFuncSecondArg when in the second arg of a predefined function', () => {
  29. const token = createToken(
  30. MetricMathTestQueries.secondArgQuery.query,
  31. MetricMathTestQueries.secondArgQuery.position
  32. );
  33. expect(getStatementPosition(token)).toEqual(StatementPosition.PredefinedFuncSecondArg);
  34. });
  35. it('returns SearchFuncSecondArg when in the second arg of a Search function', () => {
  36. const token = createToken(
  37. MetricMathTestQueries.secondArgAfterSearchQuery.query,
  38. MetricMathTestQueries.secondArgAfterSearchQuery.position
  39. );
  40. expect(getStatementPosition(token)).toEqual(StatementPosition.SearchFuncSecondArg);
  41. });
  42. it('returns SearchFuncThirdArg when in the third arg of a Search function', () => {
  43. const token = createToken(
  44. MetricMathTestQueries.thirdArgAfterSearchQuery.query,
  45. MetricMathTestQueries.thirdArgAfterSearchQuery.position
  46. );
  47. expect(getStatementPosition(token)).toEqual(StatementPosition.SearchFuncThirdArg);
  48. });
  49. it('returns AfterFunction when after a function', () => {
  50. const token = createToken(
  51. MetricMathTestQueries.afterFunctionQuery.query,
  52. MetricMathTestQueries.afterFunctionQuery.position
  53. );
  54. expect(getStatementPosition(token)).toEqual(StatementPosition.AfterFunction);
  55. });
  56. it('returns WithinString when within a string', () => {
  57. const token = createToken(
  58. MetricMathTestQueries.withinStringQuery.query,
  59. MetricMathTestQueries.withinStringQuery.position
  60. );
  61. expect(getStatementPosition(token)).toEqual(StatementPosition.WithinString);
  62. });
  63. });