From 95c004fa27cff07cc2fb8f36841cb6d7ef63b7dc Mon Sep 17 00:00:00 2001 From: Fernando Dodino Date: Thu, 26 Dec 2024 23:28:58 -0300 Subject: [PATCH] Spitting into several tests & fix inherits keyword --- .../client/src/highlighter/tokenProvider.ts | 50 ++--- .../{tokenProvider.test.ts => class.test.ts} | 158 ++++++++-------- .../highlighter-samples/inheritanceSample.wlk | 9 + .../src/test/highlighter/inheritance.test.ts | 56 ++++++ .../src/test/highlighter/object.test.ts | 175 ++++++++++++++++++ packages/client/src/test/highlighter/utils.ts | 14 ++ 6 files changed, 357 insertions(+), 105 deletions(-) rename packages/client/src/test/highlighter/{tokenProvider.test.ts => class.test.ts} (57%) create mode 100644 packages/client/src/test/highlighter/highlighter-samples/inheritanceSample.wlk create mode 100644 packages/client/src/test/highlighter/inheritance.test.ts create mode 100644 packages/client/src/test/highlighter/object.test.ts create mode 100644 packages/client/src/test/highlighter/utils.ts diff --git a/packages/client/src/highlighter/tokenProvider.ts b/packages/client/src/highlighter/tokenProvider.ts index e522c97..33d3b02 100644 --- a/packages/client/src/highlighter/tokenProvider.ts +++ b/packages/client/src/highlighter/tokenProvider.ts @@ -57,6 +57,8 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) return plotter({ ln: line, col, len: token.length }, kind) } + const defaultKeywordPlotter = (node: Node) => keywordPlotter(node, keywords[node.kind]) + const saveReference = (node: NamedNode) => ({ name: node.name, type: node.kind }) const dropSingleReference = (node: WollokNodePlotter): HighlightingResult => dropReference([node]) const dropReference = (node: WollokNodePlotter[]): HighlightingResult => ({ result: node, references: undefined }) @@ -77,40 +79,40 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) } if(node.kind === 'New' || node.kind === 'Self'){ //por alguna razon no hace match - return dropSingleReference(keywordPlotter(node, keywords[node.kind])) + return dropSingleReference(defaultKeywordPlotter(node)) } if (node.is(If)) { - const ifKeywords = [keywordPlotter(node, keywords[node.kind])] + const ifKeywords = [defaultKeywordPlotter(node)] // if(node.elseBody) // if_keywords.push(keyword_plotter(node, keywords['Else'])) return dropReference(ifKeywords) } if (node.is(Describe) || node.is(Test)) { return dropReference([ - keywordPlotter(node, keywords[node.kind]), + defaultKeywordPlotter(node), generatePlotter(node), ]) } return match(node)( - when(Class)(node => { - const acum = [] - acum.push(keywordPlotter(node, 'class')) - node.supertypes.length>0 && acum.push(keywordPlotter(node, 'inherits')) - acum.push(generatePlotter(node)) - return { result: acum, references: saveReference(node) } - }), + when(Class)(node => ({ result: [ + defaultKeywordPlotter(node), + ].concat( + node.supertypes.length ? keywordPlotter(node, 'inherits') : [] + ).concat(generatePlotter(node)), + references: saveReference(node) }) + ), when(Singleton)(node => { if (node.sourceMap == undefined) return nullHighlighting const currentNode = { ...node, name: node.name ?? '', } as unknown as NamedNode - const acum = [] - node.members.reduce((prev, curr) => !curr.name.startsWith('<') && prev, true) - && acum.push(keywordPlotter(node, keywords[node.kind])) - if (node.name) acum.push(generatePlotter(node as unknown as NamedNode)) - return { result: acum, references: saveReference(currentNode) } + return { result: [ + !node.isClosure() ? defaultKeywordPlotter(node) : [], + node.supertypes.length ? keywordPlotter(node, 'inherits') : [], + node.name ? generatePlotter(node as unknown as NamedNode) : [], + ], references: saveReference(currentNode) } }), when(Field)(node => node.isSynthetic ? nullHighlighting : resultForReference(node) @@ -138,7 +140,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) when(Assignment)(node => { return { result: [ - keywordPlotter(node, keywords[node.kind]), + defaultKeywordPlotter(node), ], references: undefined, } }), @@ -161,7 +163,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) return { result: [ plotter({ ln: line, col, len: node.name.length }, node.kind), - keywordPlotter(node, keywords[node.kind]), + defaultKeywordPlotter(node), ], references: undefined, } }), @@ -191,7 +193,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) } }), when(Return)(node => { - return dropSingleReference(keywordPlotter(node, keywords[node.kind])) + return dropSingleReference(defaultKeywordPlotter(node)) }), when(Literal)(node => { if(node.isSynthetic) return nullHighlighting @@ -240,7 +242,7 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) try { //alternativamente examinar si el keyword tiene indice negativo return { result: [ - keywordPlotter(node, keywords[node.kind]), + defaultKeywordPlotter(node), generatePlotter(node), ], references: saveReference(node), }} @@ -251,25 +253,25 @@ function processNode(node: Node, documentoStr: string[], context: NodeContext[]) when(Import)(node => { return { result: [ - keywordPlotter(node, keywords[node.kind]), + defaultKeywordPlotter(node), generatePlotter(node.entity), ], references: saveReference(node.entity), } }), when(Program)(node => { return dropReference([ - keywordPlotter(node, keywords[node.kind]), + defaultKeywordPlotter(node), generatePlotter(node), ]) }), when(Describe)(node => { - return dropSingleReference(keywordPlotter(node, keywords[node.kind])) + return dropSingleReference(defaultKeywordPlotter(node)) }), when(Test)(node => { - return dropSingleReference(keywordPlotter(node, keywords[node.kind])) + return dropSingleReference(defaultKeywordPlotter(node)) }), when(If)(node => { - return dropSingleReference(keywordPlotter(node, keywords[node.kind])) + return dropSingleReference(defaultKeywordPlotter(node)) }), when(Node)(_ => nullHighlighting) ) diff --git a/packages/client/src/test/highlighter/tokenProvider.test.ts b/packages/client/src/test/highlighter/class.test.ts similarity index 57% rename from packages/client/src/test/highlighter/tokenProvider.test.ts rename to packages/client/src/test/highlighter/class.test.ts index fe07ec5..03b37b4 100644 --- a/packages/client/src/test/highlighter/tokenProvider.test.ts +++ b/packages/client/src/test/highlighter/class.test.ts @@ -1,74 +1,6 @@ -import { excludeNullish, parse } from 'wollok-ts' -import { readFileSync } from 'fs' -import { processCode } from '../../highlighter/tokenProvider' import { WollokNodePlotter } from '../../highlighter/utils' import { expect } from 'expect' - -const readFileForTokenProvider = (filePath: string) => { - const parsedFile = parse.File(filePath) - const docText = readFileSync(filePath, { encoding: 'utf-8' }) - const tp = parsedFile.tryParse(docText) - const splittedLines = docText.split('\n') - return excludeNullish(processCode(tp.members[0], splittedLines)) -} - -suite('an object sample', () => { - - let processed: WollokNodePlotter[] - - setup(() => { - processed = readFileForTokenProvider('src/test/highlighter/highlighter-samples/objectSample.wlk') - }) - - test('highlights object keyword', () => { - console.info(JSON.stringify(processed.filter(token => token.tokenType === 'keyword'))) - const keywordsTokens = processed.filter(token => token.tokenType === 'keyword').values() - - const nextRange = () => keywordsTokens.next().value.range - - const objectRange = nextRange() - expect(objectRange.start).toEqual({ line: 0, column: 0 }) - expect(objectRange.end).toEqual({ line: 0, column: 6 }) - - const varRange = nextRange() - expect(varRange.start).toEqual({ line: 1, column: 2 }) - expect(varRange.end).toEqual({ line: 1, column: 5 }) - - const propertyRange = nextRange() - expect(propertyRange.start).toEqual({ line: 1, column: 6 }) - expect(propertyRange.end).toEqual({ line: 1, column: 14 }) - - const constRange = nextRange() - expect(constRange.start).toEqual({ line: 2, column: 2 }) - expect(constRange.end).toEqual({ line: 2, column: 7 }) - - const methodFlyRange = nextRange() - expect(methodFlyRange.start).toEqual({ line: 4, column: 2 }) - expect(methodFlyRange.end).toEqual({ line: 4, column: 8 }) - - const equalRange = nextRange() - expect(equalRange.start).toEqual({ line: 5, column: 11 }) - expect(equalRange.end).toEqual({ line: 5, column: 12 }) - - const methodRealEnergyRange = nextRange() - expect(methodRealEnergyRange.start).toEqual({ line: 8, column: 2 }) - expect(methodRealEnergyRange.end).toEqual({ line: 8, column: 8 }) - - const selfRange = nextRange() - expect(selfRange.start).toEqual({ line: 8, column: 33 }) - expect(selfRange.end).toEqual({ line: 8, column: 37 }) - - const methodNameValueRange = nextRange() - expect(methodNameValueRange.start).toEqual({ line: 9, column: 2 }) - expect(methodNameValueRange.end).toEqual({ line: 9, column: 8 }) - }) - - test('highlights properties', () => { - const propertyTokens = processed.filter(token => token.tokenType === 'property') - expect(propertyTokens.length).toBe(6) - }) - -}) +import { processedByTokenType, readFileForTokenProvider } from './utils' suite('a class sample', () => { @@ -79,7 +11,7 @@ suite('a class sample', () => { }) test('highlights class keyword', () => { - const keywordsTokens = processed.filter(token => token.tokenType === 'keyword').values() + const keywordsTokens = processedByTokenType(processed, 'keyword') const nextRange = () => keywordsTokens.next().value.range @@ -128,13 +60,8 @@ suite('a class sample', () => { expect(returnValueForIsYoungMethodRange.end).toEqual({ line: 10, column: 10 }) }) - test('highlights properties', () => { - const propertyTokens = processed.filter(token => token.tokenType === 'property') - expect(propertyTokens.length).toBe(5) - }) - test('highlights class name', () => { - const classTokens = processed.filter(token => token.tokenType === 'class').values() + const classTokens = processedByTokenType(processed, 'class') const nextRange = () => classTokens.next().value.range @@ -144,7 +71,7 @@ suite('a class sample', () => { }) test('highlights property name', () => { - const propertyTokens = processed.filter(token => token.tokenType === 'property').values() + const propertyTokens = processedByTokenType(processed, 'property') const nextRange = () => propertyTokens.next().value.range @@ -169,8 +96,8 @@ suite('a class sample', () => { expect(birthdateInIsYoungMethodRange.end).toEqual({ line: 9, column: 49 }) }) - test('highlights methods name', () => { - const methodTokens = processed.filter(token => token.tokenType === 'method').values() + test('highlights method names', () => { + const methodTokens = processedByTokenType(processed, 'method') const nextRange = () => methodTokens.next().value.range @@ -188,7 +115,7 @@ suite('a class sample', () => { }) test('highlights parameters', () => { - const parameterTokens = processed.filter(token => token.tokenType === 'parameter').values() + const parameterTokens = processedByTokenType(processed, 'parameter') const nextRange = () => parameterTokens.next().value.range @@ -201,5 +128,74 @@ suite('a class sample', () => { expect(minutesUsageParameterRange.end).toEqual({ line: 5, column: 35 }) }) + test('highlights operators', () => { + const operatorTokens = processedByTokenType(processed, 'operator') + + const nextRange = () => operatorTokens.next().value.range + + const plusOperatorRange = nextRange() + expect(plusOperatorRange.start).toEqual({ line: 5, column: 20 }) + expect(plusOperatorRange.end).toEqual({ line: 5, column: 21 }) + + const multiplyOperatorRange = nextRange() + expect(multiplyOperatorRange.start).toEqual({ line: 5, column: 26 }) + expect(multiplyOperatorRange.end).toEqual({ line: 5, column: 27 }) + + const divideOperatorRange = nextRange() + expect(divideOperatorRange.start).toEqual({ line: 9, column: 51 }) + expect(divideOperatorRange.end).toEqual({ line: 9, column: 52 }) + + const lessThanOperatorRange = nextRange() + expect(lessThanOperatorRange.start).toEqual({ line: 10, column: 17 }) + expect(lessThanOperatorRange.end).toEqual({ line: 10, column: 18 }) + }) + + test('highlights local variables', () => { + const operatorTokens = processedByTokenType(processed, 'variable') + + const nextRange = () => operatorTokens.next().value.range + + const yearsDefinitionRange = nextRange() + expect(yearsDefinitionRange.start).toEqual({ line: 9, column: 10 }) + expect(yearsDefinitionRange.end).toEqual({ line: 9, column: 15 }) + + const yearsInMessageRange = nextRange() + expect(yearsInMessageRange.start).toEqual({ line: 10, column: 11 }) + expect(yearsInMessageRange.end).toEqual({ line: 10, column: 16 }) + }) + + test('highlights numbers', () => { + const numberTokens = processedByTokenType(processed, 'number') + + const nextRange = () => numberTokens.next().value.range + + const numberAsAValueOfFieldRange = nextRange() + expect(numberAsAValueOfFieldRange.start).toEqual({ line: 1, column: 15 }) + expect(numberAsAValueOfFieldRange.end).toEqual({ line: 1, column: 18 }) + + const numberAsValueOfParameter1Range = nextRange() + expect(numberAsValueOfParameter1Range.start).toEqual({ line: 2, column: 42 }) + expect(numberAsValueOfParameter1Range.end).toEqual({ line: 2, column: 43 }) + + const numberAsValueOfParameter2Range = nextRange() + expect(numberAsValueOfParameter2Range.start).toEqual({ line: 2, column: 53 }) + expect(numberAsValueOfParameter2Range.end).toEqual({ line: 2, column: 54 }) -}) \ No newline at end of file + const numberAsValueOfParameter3Range = nextRange() + expect(numberAsValueOfParameter3Range.start).toEqual({ line: 2, column: 63 }) + expect(numberAsValueOfParameter3Range.end).toEqual({ line: 2, column: 67 }) + + const numberAsReceiverOfMessageRange = nextRange() + expect(numberAsReceiverOfMessageRange.start).toEqual({ line: 5, column: 23 }) + expect(numberAsReceiverOfMessageRange.end).toEqual({ line: 5, column: 25 }) + + const numberAsParameterOfOperator1Range = nextRange() + expect(numberAsParameterOfOperator1Range.start).toEqual({ line: 9, column: 53 }) + expect(numberAsParameterOfOperator1Range.end).toEqual({ line: 9, column: 56 }) + + const numberAsParameterOfOperator2Range = nextRange() + expect(numberAsParameterOfOperator2Range.start).toEqual({ line: 10, column: 19 }) + expect(numberAsParameterOfOperator2Range.end).toEqual({ line: 10, column: 20 }) + }) + +}) diff --git a/packages/client/src/test/highlighter/highlighter-samples/inheritanceSample.wlk b/packages/client/src/test/highlighter/highlighter-samples/inheritanceSample.wlk new file mode 100644 index 0000000..654011a --- /dev/null +++ b/packages/client/src/test/highlighter/highlighter-samples/inheritanceSample.wlk @@ -0,0 +1,9 @@ +class Bird { + var energy = 10 +} + +object pepita inherits Bird(energy = 100) {} + +class MockingBird inherits Bird(energy = 120) {} + +const someBird = object inherits Bird(energy = 90) {} diff --git a/packages/client/src/test/highlighter/inheritance.test.ts b/packages/client/src/test/highlighter/inheritance.test.ts new file mode 100644 index 0000000..f305f38 --- /dev/null +++ b/packages/client/src/test/highlighter/inheritance.test.ts @@ -0,0 +1,56 @@ +import { WollokNodePlotter } from '../../highlighter/utils' +import { expect } from 'expect' +import { processedByTokenType, readFileForTokenProvider } from './utils' + +suite('inheritance sample', () => { + + let processed: WollokNodePlotter[] + + setup(() => { + processed = readFileForTokenProvider('src/test/highlighter/highlighter-samples/inheritanceSample.wlk') + }) + + test('highlights class keyword', () => { + console.info(JSON.stringify(processed.filter(t => t.tokenType === 'keyword'))) + const keywordsTokens = processedByTokenType(processed, 'keyword') + + const nextRange = () => keywordsTokens.next().value.range + + const birdClassRange = nextRange() + expect(birdClassRange.start).toEqual({ line: 0, column: 0 }) + expect(birdClassRange.end).toEqual({ line: 0, column: 5 }) + + const energyVarRange = nextRange() + expect(energyVarRange.start).toEqual({ line: 1, column: 2 }) + expect(energyVarRange.end).toEqual({ line: 1, column: 5 }) + + const wkoRange = nextRange() + expect(wkoRange.start).toEqual({ line: 4, column: 0 }) + expect(wkoRange.end).toEqual({ line: 4, column: 6 }) + + const wkoInheritsClassRange = nextRange() + expect(wkoInheritsClassRange.start).toEqual({ line: 4, column: 14 }) + expect(wkoInheritsClassRange.end).toEqual({ line: 4, column: 22 }) + + const classRange = nextRange() + expect(classRange.start).toEqual({ line: 6, column: 0 }) + expect(classRange.end).toEqual({ line: 6, column: 5 }) + + const classInheritsSuperclassRange = nextRange() + expect(classInheritsSuperclassRange.start).toEqual({ line: 6, column: 18 }) + expect(classInheritsSuperclassRange.end).toEqual({ line: 6, column: 26 }) + + const constForUnnamedObjectRange = nextRange() + expect(constForUnnamedObjectRange.start).toEqual({ line: 8, column: 0 }) + expect(constForUnnamedObjectRange.end).toEqual({ line: 8, column: 5 }) + + const unnamedObjectRange = nextRange() + expect(unnamedObjectRange.start).toEqual({ line: 8, column: 17 }) + expect(unnamedObjectRange.end).toEqual({ line: 8, column: 23 }) + + const unnamedObjectInheritsSuperclassRange = nextRange() + expect(unnamedObjectInheritsSuperclassRange.start).toEqual({ line: 8, column: 24 }) + expect(unnamedObjectInheritsSuperclassRange.end).toEqual({ line: 8, column: 32 }) + }) + +}) \ No newline at end of file diff --git a/packages/client/src/test/highlighter/object.test.ts b/packages/client/src/test/highlighter/object.test.ts new file mode 100644 index 0000000..78de1ad --- /dev/null +++ b/packages/client/src/test/highlighter/object.test.ts @@ -0,0 +1,175 @@ +import { WollokNodePlotter } from '../../highlighter/utils' +import { expect } from 'expect' +import { processedByTokenType, readFileForTokenProvider } from './utils' + +suite('an object sample', () => { + + let processed: WollokNodePlotter[] + + setup(() => { + processed = readFileForTokenProvider('src/test/highlighter/highlighter-samples/objectSample.wlk') + }) + + test('highlights object keyword', () => { + const keywordsTokens = processedByTokenType(processed, 'keyword') + + const nextRange = () => keywordsTokens.next().value.range + + const objectRange = nextRange() + expect(objectRange.start).toEqual({ line: 0, column: 0 }) + expect(objectRange.end).toEqual({ line: 0, column: 6 }) + + const varRange = nextRange() + expect(varRange.start).toEqual({ line: 1, column: 2 }) + expect(varRange.end).toEqual({ line: 1, column: 5 }) + + const propertyRange = nextRange() + expect(propertyRange.start).toEqual({ line: 1, column: 6 }) + expect(propertyRange.end).toEqual({ line: 1, column: 14 }) + + const constRange = nextRange() + expect(constRange.start).toEqual({ line: 2, column: 2 }) + expect(constRange.end).toEqual({ line: 2, column: 7 }) + + const methodFlyRange = nextRange() + expect(methodFlyRange.start).toEqual({ line: 4, column: 2 }) + expect(methodFlyRange.end).toEqual({ line: 4, column: 8 }) + + const equalRange = nextRange() + expect(equalRange.start).toEqual({ line: 5, column: 11 }) + expect(equalRange.end).toEqual({ line: 5, column: 12 }) + + const methodRealEnergyRange = nextRange() + expect(methodRealEnergyRange.start).toEqual({ line: 8, column: 2 }) + expect(methodRealEnergyRange.end).toEqual({ line: 8, column: 8 }) + + const selfRange = nextRange() + expect(selfRange.start).toEqual({ line: 8, column: 33 }) + expect(selfRange.end).toEqual({ line: 8, column: 37 }) + + const methodNameValueRange = nextRange() + expect(methodNameValueRange.start).toEqual({ line: 9, column: 2 }) + expect(methodNameValueRange.end).toEqual({ line: 9, column: 8 }) + }) + + test('highlights properties', () => { + const propertyTokens = processedByTokenType(processed, 'property') + + const nextRange = () => propertyTokens.next().value.range + + const energyDefinitionRange = nextRange() + expect(energyDefinitionRange.start).toEqual({ line: 1, column: 15 }) + expect(energyDefinitionRange.end).toEqual({ line: 1, column: 21 }) + + const nameDefinitionRange = nextRange() + expect(nameDefinitionRange.start).toEqual({ line: 2, column: 8 }) + expect(nameDefinitionRange.end).toEqual({ line: 2, column: 12 }) + + const energyInAssignmentRange = nextRange() + expect(energyInAssignmentRange.start).toEqual({ line: 5, column: 4 }) + expect(energyInAssignmentRange.end).toEqual({ line: 5, column: 10 }) + + const energyInMessageRange = nextRange() + expect(energyInMessageRange.start).toEqual({ line: 5, column: 13 }) + expect(energyInMessageRange.end).toEqual({ line: 5, column: 19 }) + + const energyInMessage2Range = nextRange() + expect(energyInMessage2Range.start).toEqual({ line: 8, column: 24 }) + expect(energyInMessage2Range.end).toEqual({ line: 8, column: 30 }) + + const nameInMessageRange = nextRange() + expect(nameInMessageRange.start).toEqual({ line: 9, column: 23 }) + expect(nameInMessageRange.end).toEqual({ line: 9, column: 27 }) + }) + + test('highlights object name', () => { + const classTokens = processedByTokenType(processed, 'object') + + const nextRange = () => classTokens.next().value.range + + const objectRange = nextRange() + expect(objectRange.start).toEqual({ line: 0, column: 7 }) + expect(objectRange.end).toEqual({ line: 0, column: 13 }) + }) + + test('highlights method names', () => { + const methodTokens = processedByTokenType(processed, 'method') + + const nextRange = () => methodTokens.next().value.range + + const flyMethodRange = nextRange() + expect(flyMethodRange.start).toEqual({ line: 4, column: 9 }) + expect(flyMethodRange.end).toEqual({ line: 4, column: 12 }) + + const realEnergyRange = nextRange() + expect(realEnergyRange.start).toEqual({ line: 8, column: 9 }) + expect(realEnergyRange.end).toEqual({ line: 8, column: 19 }) + + const nameValueMessageRange = nextRange() + expect(nameValueMessageRange.start).toEqual({ line: 8, column: 38 }) + expect(nameValueMessageRange.end).toEqual({ line: 8, column: 47 }) + + const nameValueRange = nextRange() + expect(nameValueRange.start).toEqual({ line: 9, column: 9 }) + expect(nameValueRange.end).toEqual({ line: 9, column: 18 }) + + const lengthMessageRange = nextRange() + expect(lengthMessageRange.start).toEqual({ line: 9, column: 28 }) + expect(lengthMessageRange.end).toEqual({ line: 9, column: 34 }) + }) + + test('highlights operators', () => { + const operatorTokens = processedByTokenType(processed, 'operator') + + const nextRange = () => operatorTokens.next().value.range + + const plusOperatorRange = nextRange() + expect(plusOperatorRange.start).toEqual({ line: 5, column: 20 }) + expect(plusOperatorRange.end).toEqual({ line: 5, column: 21 }) + + const multiplyOperatorRange = nextRange() + expect(multiplyOperatorRange.start).toEqual({ line: 5, column: 26 }) + expect(multiplyOperatorRange.end).toEqual({ line: 5, column: 27 }) + + const multiplyOperator2Range = nextRange() + expect(multiplyOperator2Range.start).toEqual({ line: 8, column: 31 }) + expect(multiplyOperator2Range.end).toEqual({ line: 8, column: 32 }) + }) + + + test('highlights parameters', () => { + const parameterTokens = processedByTokenType(processed, 'parameter') + + const nextRange = () => parameterTokens.next().value.range + + const minutesParameterRange = nextRange() + expect(minutesParameterRange.start).toEqual({ line: 4, column: 13 }) + expect(minutesParameterRange.end).toEqual({ line: 4, column: 20 }) + }) + + test('highlights numbers', () => { + const numberTokens = processedByTokenType(processed, 'number') + + const nextRange = () => numberTokens.next().value.range + + const numberAsAValueOfFieldRange = nextRange() + expect(numberAsAValueOfFieldRange.start).toEqual({ line: 1, column: 24 }) + expect(numberAsAValueOfFieldRange.end).toEqual({ line: 1, column: 27 }) + + const numberAsReceiverOfMessageRange = nextRange() + expect(numberAsReceiverOfMessageRange.start).toEqual({ line: 5, column: 23 }) + expect(numberAsReceiverOfMessageRange.end).toEqual({ line: 5, column: 25 }) + }) + + test('highlights strings', () => { + const numberTokens = processedByTokenType(processed, 'string') + + const nextRange = () => numberTokens.next().value.range + + const numberAsAValueOfFieldRange = nextRange() + expect(numberAsAValueOfFieldRange.start).toEqual({ line: 2, column: 15 }) + expect(numberAsAValueOfFieldRange.end).toEqual({ line: 2, column: 23 }) + }) + + +}) diff --git a/packages/client/src/test/highlighter/utils.ts b/packages/client/src/test/highlighter/utils.ts new file mode 100644 index 0000000..1dd0816 --- /dev/null +++ b/packages/client/src/test/highlighter/utils.ts @@ -0,0 +1,14 @@ +import { excludeNullish, parse } from 'wollok-ts' +import { readFileSync } from 'fs' +import { processCode } from '../../highlighter/tokenProvider' +import { WollokNodePlotter } from '../../highlighter/utils' + +export const readFileForTokenProvider = (filePath: string): WollokNodePlotter[] => { + const parsedFile = parse.File(filePath) + const docText = readFileSync(filePath, { encoding: 'utf-8' }) + const tp = parsedFile.tryParse(docText) + const splittedLines = docText.split('\n') + return excludeNullish(processCode(tp.members[0], splittedLines)) +} + +export const processedByTokenType = (processed: WollokNodePlotter[], tokenType: string): ArrayIterator => processed.filter(token => token.tokenType === tokenType).values()