Skip to content

Commit

Permalink
testing node to location
Browse files Browse the repository at this point in the history
  • Loading branch information
ivojawer committed Jun 22, 2024
1 parent f687077 commit 9134b40
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
58 changes: 57 additions & 1 deletion server/src/test/text-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from 'expect'
import path from 'path'
import { between, findPackageJSON, relativeFilePath, setWorkspaceUri } from '../utils/text-documents'
import { Position, Range } from 'vscode-languageserver'
import { Literal, Node, Package, SourceMap } from 'wollok-ts'
import { between, findPackageJSON, nodeToLocation, relativeFilePath, setWorkspaceUri } from '../utils/text-documents'

const { join, resolve } = path.posix

Expand Down Expand Up @@ -35,6 +37,60 @@ describe('text document utilities', () => {
})

})

describe('node to location', () => {

const pepitaPackage: Package = new Package({ name: 'pepita', fileName: 'src/pepita.wlk' })
const testNodeLocation = (node: Node, expectedFile: string, expectedRange: Range) => {
const location = nodeToLocation(node)
expect(location.uri).toEqual('examples/example-project/' + expectedFile)
expect(location.range).toEqual(expectedRange)
}

beforeEach(() => {
setWorkspaceUri(join('examples', 'example-project'))
})

it('package location', () => {
testNodeLocation(
pepitaPackage,
'src/pepita.wlk',
Range.create(Position.create(0, 0), Position.create(0, 0))
)
})

it('node location', () => {
const literal = new Literal({
value: 42,
sourceMap: new SourceMap({
start: { line: 1, column: 2, offset: 2 },
end: { line: 1, column: 4, offset: 4 },
}),
parent: pepitaPackage,
})

testNodeLocation(
literal,
'src/pepita.wlk',
Range.create(Position.create(0, 1), Position.create(0, 3))
)
})

it('missing file location', () => {
const brokenPackage = new Package({ name: 'broken' })

expect(() => nodeToLocation(brokenPackage)).toThrowError('No source file found for node')
})

it('missing source map location', () => {
const literal = new Literal({
value: 42,
parent: pepitaPackage,
})

expect(() => nodeToLocation(literal)).toThrowError('No source map found for node')
})
})
})

describe('relative file path', () => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/utils/text-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const nodeToLocation = (node: Node): Location => {
Range.create(Position.create(0, 0), Position.create(0, 0)),
)
}

if (!node.sourceMap) throw new Error('No source map found for node')

return Location.create(
Expand Down

0 comments on commit 9134b40

Please sign in to comment.