linkedTokenBuilder.test.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { monacoTypes } from '@grafana/ui';
  2. import { multiLineFullQuery, singleLineFullQuery } from '../__mocks__/cloudwatch-sql-test-data';
  3. import MonacoMock from '../__mocks__/monarch/Monaco';
  4. import TextModel from '../__mocks__/monarch/TextModel';
  5. import { SQLTokenTypes } from '../cloudwatch-sql/completion/types';
  6. import cloudWatchSqlLanguageDefinition from '../cloudwatch-sql/definition';
  7. import { DESC, SELECT } from '../cloudwatch-sql/language';
  8. import { linkedTokenBuilder } from './linkedTokenBuilder';
  9. describe('linkedTokenBuilder', () => {
  10. describe('singleLineFullQuery', () => {
  11. const testModel = TextModel(singleLineFullQuery.query);
  12. it('should add correct references to next LinkedToken', () => {
  13. const position: monacoTypes.IPosition = { lineNumber: 1, column: 0 };
  14. const current = linkedTokenBuilder(
  15. MonacoMock,
  16. cloudWatchSqlLanguageDefinition,
  17. testModel as monacoTypes.editor.ITextModel,
  18. position,
  19. SQLTokenTypes
  20. );
  21. expect(current?.is(SQLTokenTypes.Keyword, SELECT)).toBeTruthy();
  22. expect(current?.getNextNonWhiteSpaceToken()?.is(SQLTokenTypes.Function, 'AVG')).toBeTruthy();
  23. });
  24. it('should add correct references to previous LinkedToken', () => {
  25. const position: monacoTypes.IPosition = { lineNumber: 1, column: singleLineFullQuery.query.length };
  26. const current = linkedTokenBuilder(
  27. MonacoMock,
  28. cloudWatchSqlLanguageDefinition,
  29. testModel as monacoTypes.editor.ITextModel,
  30. position,
  31. SQLTokenTypes
  32. );
  33. expect(current?.is(SQLTokenTypes.Number, '10')).toBeTruthy();
  34. expect(current?.getPreviousNonWhiteSpaceToken()?.is(SQLTokenTypes.Keyword, 'LIMIT')).toBeTruthy();
  35. expect(
  36. current?.getPreviousNonWhiteSpaceToken()?.getPreviousNonWhiteSpaceToken()?.is(SQLTokenTypes.Keyword, DESC)
  37. ).toBeTruthy();
  38. });
  39. });
  40. describe('multiLineFullQuery', () => {
  41. const testModel = TextModel(multiLineFullQuery.query);
  42. it('should add LinkedToken with whitespace in case empty lines', () => {
  43. const position: monacoTypes.IPosition = { lineNumber: 3, column: 0 };
  44. const current = linkedTokenBuilder(
  45. MonacoMock,
  46. cloudWatchSqlLanguageDefinition,
  47. testModel as monacoTypes.editor.ITextModel,
  48. position,
  49. SQLTokenTypes
  50. );
  51. expect(current).not.toBeNull();
  52. expect(current?.isWhiteSpace()).toBeTruthy();
  53. });
  54. it('should add correct references to next LinkedToken', () => {
  55. const position: monacoTypes.IPosition = { lineNumber: 1, column: 0 };
  56. const current = linkedTokenBuilder(
  57. MonacoMock,
  58. cloudWatchSqlLanguageDefinition,
  59. testModel as monacoTypes.editor.ITextModel,
  60. position,
  61. SQLTokenTypes
  62. );
  63. expect(current?.is(SQLTokenTypes.Keyword, SELECT)).toBeTruthy();
  64. expect(current?.getNextNonWhiteSpaceToken()?.is(SQLTokenTypes.Function, 'AVG')).toBeTruthy();
  65. });
  66. it('should add correct references to previous LinkedToken even when references spans over multiple lines', () => {
  67. const position: monacoTypes.IPosition = { lineNumber: 6, column: 7 };
  68. const current = linkedTokenBuilder(
  69. MonacoMock,
  70. cloudWatchSqlLanguageDefinition,
  71. testModel as monacoTypes.editor.ITextModel,
  72. position,
  73. SQLTokenTypes
  74. );
  75. expect(current?.is(SQLTokenTypes.Number, '10')).toBeTruthy();
  76. expect(current?.getPreviousNonWhiteSpaceToken()?.is(SQLTokenTypes.Keyword, 'LIMIT')).toBeTruthy();
  77. expect(
  78. current?.getPreviousNonWhiteSpaceToken()?.getPreviousNonWhiteSpaceToken()?.is(SQLTokenTypes.Keyword, DESC)
  79. ).toBeTruthy();
  80. });
  81. });
  82. });