|
| 1 | +// @flow |
| 2 | + |
| 3 | +import OutlineViewAdapter from '../../lib/adapters/outline-view-adapter'; |
| 4 | +import * as ls from '../../lib/languageclient'; |
| 5 | +import sinon from 'sinon'; |
| 6 | +import {expect} from 'chai'; |
| 7 | + |
| 8 | +describe('OutlineViewAdapter', () => { |
| 9 | + const createLocation = (a, b, c, d) => ({ |
| 10 | + uri: '', |
| 11 | + range: {start: {line: a, character: b}, end: {line: c, character: d}}, |
| 12 | + }); |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + global.sinon = sinon.sandbox.create(); |
| 16 | + }); |
| 17 | + afterEach(() => { |
| 18 | + global.sinon.restore(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('canAdapt', () => { |
| 22 | + it('returns true if documentSymbolProvider is supported', () => { |
| 23 | + const result = OutlineViewAdapter.canAdapt({documentSymbolProvider: true}); |
| 24 | + expect(result).to.be.true; |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns false if documentSymbolProvider not supported', () => { |
| 28 | + const result = OutlineViewAdapter.canAdapt({}); |
| 29 | + expect(result).to.be.false; |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('createOutlineTrees', () => { |
| 34 | + it('creates an empty array given an empty array', () => { |
| 35 | + const result = OutlineViewAdapter.createOutlineTrees([]); |
| 36 | + expect(result).to.deep.equal([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('creates a single converted root item from a single source item', () => { |
| 40 | + const sourceItem = {kind: ls.SymbolKind.Namespace, name: 'R', location: createLocation(5, 6, 7, 8)}; |
| 41 | + const expected = OutlineViewAdapter.symbolToOutline(sourceItem); |
| 42 | + const result = OutlineViewAdapter.createOutlineTrees([sourceItem]); |
| 43 | + expect(result).to.deep.equal([expected]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('creates an empty root container with a single source item when containerName missing', () => { |
| 47 | + const sourceItem = {kind: ls.SymbolKind.Class, name: 'Program', location: createLocation(1, 2, 3, 4)}; |
| 48 | + const expected = OutlineViewAdapter.symbolToOutline(sourceItem); |
| 49 | + const missingContainerSource = Object.assign(sourceItem, {containerName: 'missing'}); |
| 50 | + const result = OutlineViewAdapter.createOutlineTrees([missingContainerSource]); |
| 51 | + expect(result.length).to.equal(1); |
| 52 | + expect(result[0].representativeName).to.equal('missing'); |
| 53 | + expect(result[0].startPosition.row).to.equal(0); |
| 54 | + expect(result[0].startPosition.column).to.equal(0); |
| 55 | + expect(result[0].children).to.deep.equal([expected]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('creates an empty root container with a single source item when containerName is missing and matches own name', () => { |
| 59 | + const sourceItem = {kind: ls.SymbolKind.Class, name: 'simple', location: createLocation(1, 2, 3, 4)}; |
| 60 | + const expected = OutlineViewAdapter.symbolToOutline(sourceItem); |
| 61 | + const missingContainerSource = Object.assign(sourceItem, {containerName: 'simple'}); |
| 62 | + const result = OutlineViewAdapter.createOutlineTrees([missingContainerSource]); |
| 63 | + expect(result.length).to.equal(1); |
| 64 | + expect(result[0].representativeName).to.equal('simple'); |
| 65 | + expect(result[0].startPosition.row).to.equal(0); |
| 66 | + expect(result[0].startPosition.column).to.equal(0); |
| 67 | + expect(result[0].children).to.deep.equal([expected]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('creates a simple named hierarchy', () => { |
| 71 | + const sourceItems = [ |
| 72 | + {kind: ls.SymbolKind.Namespace, name: 'java.com', location: createLocation(1, 0, 10, 0)}, |
| 73 | + {kind: ls.SymbolKind.Class, name: 'Program', location: createLocation(2, 0, 7, 0), containerName: 'java.com'}, |
| 74 | + {kind: ls.SymbolKind.Function, name: 'main', location: createLocation(4, 0, 5, 0), containerName: 'Program'}, |
| 75 | + ]; |
| 76 | + const result = OutlineViewAdapter.createOutlineTrees(sourceItems); |
| 77 | + expect(result.length).to.equal(1); |
| 78 | + expect(result[0].children.length).to.equal(1); |
| 79 | + expect(result[0].children[0].representativeName).to.equal('Program'); |
| 80 | + expect(result[0].children[0].children.length).to.equal(1); |
| 81 | + expect(result[0].children[0].children[0].representativeName).to.equal('main'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('retains duplicate named items', () => { |
| 85 | + const sourceItems = [ |
| 86 | + {kind: ls.SymbolKind.Namespace, name: 'duplicate', location: createLocation(1, 0, 5, 0)}, |
| 87 | + {kind: ls.SymbolKind.Namespace, name: 'duplicate', location: createLocation(6, 0, 10, 0)}, |
| 88 | + {kind: ls.SymbolKind.Function, name: 'main', location: createLocation(7, 0, 8, 0), containerName: 'duplicate'}, |
| 89 | + ]; |
| 90 | + const result = OutlineViewAdapter.createOutlineTrees(sourceItems); |
| 91 | + expect(result.length).to.equal(2); |
| 92 | + expect(result[0].representativeName).to.equal('duplicate'); |
| 93 | + expect(result[1].representativeName).to.equal('duplicate'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('disambiguates containerName based on range', () => { |
| 97 | + const sourceItems = [ |
| 98 | + {kind: ls.SymbolKind.Namespace, name: 'duplicate', location: createLocation(1, 0, 5, 0)}, |
| 99 | + {kind: ls.SymbolKind.Namespace, name: 'duplicate', location: createLocation(6, 0, 10, 0)}, |
| 100 | + {kind: ls.SymbolKind.Function, name: 'main', location: createLocation(7, 0, 8, 0), containerName: 'duplicate'}, |
| 101 | + ]; |
| 102 | + const result = OutlineViewAdapter.createOutlineTrees(sourceItems); |
| 103 | + expect(result[1].children.length).to.equal(1); |
| 104 | + expect(result[1].children[0].representativeName).to.equal('main'); |
| 105 | + }); |
| 106 | + |
| 107 | + it("does not become it's own parent", () => { |
| 108 | + const sourceItems = [ |
| 109 | + {kind: ls.SymbolKind.Namespace, name: 'duplicate', location: createLocation(1, 0, 10, 0)}, |
| 110 | + { |
| 111 | + kind: ls.SymbolKind.Namespace, |
| 112 | + name: 'duplicate', |
| 113 | + location: createLocation(6, 0, 7, 0), |
| 114 | + containerName: 'duplicate', |
| 115 | + }, |
| 116 | + ]; |
| 117 | + const result = OutlineViewAdapter.createOutlineTrees(sourceItems); |
| 118 | + expect(result.length).to.equal(1); |
| 119 | + const r = (result: any); |
| 120 | + expect(r[0].endPosition.row).to.equal(10); |
| 121 | + expect(r[0].children.length).to.equal(1); |
| 122 | + expect(r[0].children[0].endPosition.row).to.equal(7); |
| 123 | + }); |
| 124 | + |
| 125 | + it('parents to the innnermost named container', () => { |
| 126 | + const sourceItems = [ |
| 127 | + {kind: ls.SymbolKind.Namespace, name: 'turtles', location: createLocation(1, 0, 10, 0)}, |
| 128 | + { |
| 129 | + kind: ls.SymbolKind.Namespace, |
| 130 | + name: 'turtles', |
| 131 | + location: createLocation(4, 0, 8, 0), |
| 132 | + containerName: 'turtles', |
| 133 | + }, |
| 134 | + {kind: ls.SymbolKind.Class, name: 'disc', location: createLocation(4, 0, 5, 0), containerName: 'turtles'}, |
| 135 | + ]; |
| 136 | + const result = OutlineViewAdapter.createOutlineTrees(sourceItems); |
| 137 | + expect(result.length).to.equal(1); |
| 138 | + const r = (result: any); |
| 139 | + expect(r[0].endPosition.row).to.equal(10); |
| 140 | + expect(r[0].children.length).to.equal(1); |
| 141 | + expect(r[0].children[0].endPosition.row).to.equal(8); |
| 142 | + expect(r[0].children[0].children.length).to.equal(1); |
| 143 | + expect(r[0].children[0].children[0].endPosition.row).to.equal(5); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('symbolToOutline', () => { |
| 148 | + it('converts an individual item', () => { |
| 149 | + const sourceItem = {kind: ls.SymbolKind.Class, name: 'Program', location: createLocation(1, 2, 3, 4)}; |
| 150 | + const result = OutlineViewAdapter.symbolToOutline(sourceItem); |
| 151 | + expect(result.icon).to.equal('type-class'); |
| 152 | + expect(result.representativeName).to.equal('Program'); |
| 153 | + expect(result.children).to.deep.equal([]); |
| 154 | + const r = (result: any); |
| 155 | + expect(r.tokenizedText[0].kind).to.equal('type'); |
| 156 | + expect(r.tokenizedText[0].value).to.equal('Program'); |
| 157 | + expect(r.startPosition.row).to.equal(1); |
| 158 | + expect(r.startPosition.column).to.equal(2); |
| 159 | + expect(r.endPosition.row).to.equal(3); |
| 160 | + expect(r.endPosition.column).to.equal(4); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments