|
| 1 | +import * as assert from 'assert' |
| 2 | +import { of, throwError } from 'rxjs' |
| 3 | +import { TestScheduler } from 'rxjs/testing' |
| 4 | +import { Position } from 'vscode-languageserver-types' |
| 5 | +import { TextDocumentDecoration, TextDocumentDecorationParams } from '../../protocol' |
| 6 | +import { getDecorations, ProvideTextDocumentDecorationSignature } from './decoration' |
| 7 | +import { FIXTURE as COMMON_FIXTURE } from './textDocument.test' |
| 8 | + |
| 9 | +// const DELAY = 100 // msec |
| 10 | + |
| 11 | +const FIXTURE = { |
| 12 | + ...COMMON_FIXTURE, |
| 13 | + TextDocumentDecorationParams: { textDocument: { uri: 'file:///f' } } as TextDocumentDecorationParams, |
| 14 | +} |
| 15 | + |
| 16 | +const FIXTURE_RESULT: TextDocumentDecoration[] | null = [ |
| 17 | + { |
| 18 | + range: { start: Position.create(1, 2), end: Position.create(3, 4) }, |
| 19 | + backgroundColor: 'red', |
| 20 | + }, |
| 21 | +] |
| 22 | + |
| 23 | +const scheduler = () => new TestScheduler((a, b) => assert.deepStrictEqual(a, b)) |
| 24 | + |
| 25 | +describe('getDecorations', () => { |
| 26 | + describe('0 providers', () => { |
| 27 | + it('returns null', () => |
| 28 | + scheduler().run(({ cold, expectObservable }) => |
| 29 | + expectObservable( |
| 30 | + getDecorations( |
| 31 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { a: [] }), |
| 32 | + FIXTURE.TextDocumentDecorationParams |
| 33 | + ) |
| 34 | + ).toBe('-a-|', { |
| 35 | + a: null, |
| 36 | + }) |
| 37 | + )) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('1 provider', () => { |
| 41 | + it('returns null result from provider', () => |
| 42 | + scheduler().run(({ cold, expectObservable }) => |
| 43 | + expectObservable( |
| 44 | + getDecorations( |
| 45 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { a: [() => of(null)] }), |
| 46 | + FIXTURE.TextDocumentDecorationParams |
| 47 | + ) |
| 48 | + ).toBe('-a-|', { |
| 49 | + a: null, |
| 50 | + }) |
| 51 | + )) |
| 52 | + |
| 53 | + it('returns result from provider', () => |
| 54 | + scheduler().run(({ cold, expectObservable }) => |
| 55 | + expectObservable( |
| 56 | + getDecorations( |
| 57 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { |
| 58 | + a: [() => of(FIXTURE_RESULT)], |
| 59 | + }), |
| 60 | + FIXTURE.TextDocumentDecorationParams |
| 61 | + ) |
| 62 | + ).toBe('-a-|', { |
| 63 | + a: FIXTURE_RESULT, |
| 64 | + }) |
| 65 | + )) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('2 providers', () => { |
| 69 | + it('returns null result if both providers return null', () => |
| 70 | + scheduler().run(({ cold, expectObservable }) => |
| 71 | + expectObservable( |
| 72 | + getDecorations( |
| 73 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { |
| 74 | + a: [() => of(null), () => of(null)], |
| 75 | + }), |
| 76 | + FIXTURE.TextDocumentDecorationParams |
| 77 | + ) |
| 78 | + ).toBe('-a-|', { |
| 79 | + a: null, |
| 80 | + }) |
| 81 | + )) |
| 82 | + |
| 83 | + it('omits null result from 1 provider', () => |
| 84 | + scheduler().run(({ cold, expectObservable }) => |
| 85 | + expectObservable( |
| 86 | + getDecorations( |
| 87 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { |
| 88 | + a: [() => of(FIXTURE_RESULT), () => of(null)], |
| 89 | + }), |
| 90 | + FIXTURE.TextDocumentDecorationParams |
| 91 | + ) |
| 92 | + ).toBe('-a-|', { |
| 93 | + a: FIXTURE_RESULT, |
| 94 | + }) |
| 95 | + )) |
| 96 | + |
| 97 | + it('skips errors from providers', () => |
| 98 | + scheduler().run(({ cold, expectObservable }) => |
| 99 | + expectObservable( |
| 100 | + getDecorations( |
| 101 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { |
| 102 | + a: [() => of(FIXTURE_RESULT), () => throwError('error')], |
| 103 | + }), |
| 104 | + FIXTURE.TextDocumentDecorationParams |
| 105 | + ) |
| 106 | + ).toBe('-a-|', { |
| 107 | + a: FIXTURE_RESULT, |
| 108 | + }) |
| 109 | + )) |
| 110 | + |
| 111 | + it('merges results from providers', () => |
| 112 | + scheduler().run(({ cold, expectObservable }) => |
| 113 | + expectObservable( |
| 114 | + getDecorations( |
| 115 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-|', { |
| 116 | + a: [() => of(FIXTURE_RESULT), () => of(FIXTURE_RESULT)], |
| 117 | + }), |
| 118 | + FIXTURE.TextDocumentDecorationParams |
| 119 | + ) |
| 120 | + ).toBe('-a-|', { |
| 121 | + a: [...FIXTURE_RESULT!, ...FIXTURE_RESULT!], |
| 122 | + }) |
| 123 | + )) |
| 124 | + }) |
| 125 | + |
| 126 | + describe('multiple emissions', () => { |
| 127 | + it('returns stream of results', () => |
| 128 | + scheduler().run(({ cold, expectObservable }) => |
| 129 | + expectObservable( |
| 130 | + getDecorations( |
| 131 | + cold<ProvideTextDocumentDecorationSignature[]>('-a-b-|', { |
| 132 | + a: [() => of(FIXTURE_RESULT)], |
| 133 | + b: [() => of(null)], |
| 134 | + }), |
| 135 | + FIXTURE.TextDocumentDecorationParams |
| 136 | + ) |
| 137 | + ).toBe('-a-b-|', { |
| 138 | + a: FIXTURE_RESULT, |
| 139 | + b: null, |
| 140 | + }) |
| 141 | + )) |
| 142 | + }) |
| 143 | +}) |
0 commit comments