Skip to content

Commit

Permalink
test: annotation calls and named types referencing declarations in ot…
Browse files Browse the repository at this point in the history
…her files (#587)

Closes partially #540

### Summary of Changes

Add tests to ensure that annotation calls and named types can reference
declarations in other files. No changes to the implementation of the
scope provider were needed.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot authored Oct 1, 2023
1 parent 6b30de5 commit 928b520
Show file tree
Hide file tree
Showing 51 changed files with 489 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/language/scoping/safe-ds-scope-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,18 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
}

private getScopeForMemberAccessMember(node: SdsMemberAccess): Scope {
let currentScope = EMPTY_SCOPE;

// Static access
const declaration = this.getUniqueReferencedDeclarationForExpression(node.receiver);
if (isSdsClass(declaration)) {
currentScope = this.createScopeForNodes(classMembersOrEmpty(declaration, isStatic));
return this.createScopeForNodes(classMembersOrEmpty(declaration, isStatic));

// val superTypeMembers = receiverDeclaration.superClassMembers()
// .filter { it.isStatic() }
// .toList()
//
// return Scopes.scopeFor(members, Scopes.scopeFor(superTypeMembers))
} else if (isSdsEnum(declaration)) {
currentScope = this.createScopeForNodes(enumVariantsOrEmpty(declaration));
return this.createScopeForNodes(enumVariantsOrEmpty(declaration));
}

// // Call results
Expand Down Expand Up @@ -176,7 +174,7 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
// else -> resultScope
// }

return currentScope;
return EMPTY_SCOPE;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/language/formatting/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Diagnostic } from 'vscode-languageserver-types';
import { createSafeDsServices } from '../../../src/language/safe-ds-module.js';
import { EmptyFileSystem } from 'langium';
import { getSyntaxErrors } from '../../helpers/diagnostics.js';
import { clearDocuments } from 'langium/test';

const services = createSafeDsServices(EmptyFileSystem).SafeDs;
const root = 'formatting';
Expand All @@ -29,12 +30,14 @@ const createFormattingTest = async (relativeResourcePath: string): Promise<Forma
const expectedFormattedCode = normalizeLineBreaks(parts[1]).trim();

// Original code must not contain syntax errors
await clearDocuments(services);
const syntaxErrorsInOriginalCode = await getSyntaxErrors(services, originalCode);
if (syntaxErrorsInOriginalCode.length > 0) {
return invalidTest(relativeResourcePath, new SyntaxErrorsInOriginalCodeError(syntaxErrorsInOriginalCode));
}

// Expected formatted code must not contain syntax errors
await clearDocuments(services);
const syntaxErrorsInExpectedFormattedCode = await getSyntaxErrors(services, expectedFormattedCode);
if (syntaxErrorsInExpectedFormattedCode.length > 0) {
return invalidTest(
Expand Down
2 changes: 2 additions & 0 deletions tests/language/partialEvaluation/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { URI } from 'vscode-uri';
import { getSyntaxErrors, SyntaxErrorsInCodeError } from '../../helpers/diagnostics.js';
import { EmptyFileSystem } from 'langium';
import { createSafeDsServices } from '../../../src/language/safe-ds-module.js';
import { clearDocuments } from 'langium/test';

const services = createSafeDsServices(EmptyFileSystem).SafeDs;
const root = 'partial evaluation';
Expand Down Expand Up @@ -40,6 +41,7 @@ const createPartialEvaluationTest = async (
const code = fs.readFileSync(absolutePath).toString();

// File must not contain any syntax errors
await clearDocuments(services);
const syntaxErrors = await getSyntaxErrors(services, code);
if (syntaxErrors.length > 0) {
return invalidTest(
Expand Down
2 changes: 1 addition & 1 deletion tests/language/scoping/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ const createScopingTest = async (
const code = fs.readFileSync(absolutePath).toString();

// File must not contain any syntax errors
await clearDocuments(services);
const syntaxErrors = await getSyntaxErrors(services, code);
if (syntaxErrors.length > 0) {
return invalidTest(
`INVALID TEST FILE [${relativeResourcePath}]`,
new SyntaxErrorsInCodeError(syntaxErrors),
);
}
await clearDocuments(services);

const checksResult = findTestChecks(code, uri, { failIfFewerRangesThanComments: true });

Expand Down
2 changes: 2 additions & 0 deletions tests/language/typing/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { URI } from 'vscode-uri';
import { getSyntaxErrors, SyntaxErrorsInCodeError } from '../../helpers/diagnostics.js';
import { EmptyFileSystem } from 'langium';
import { createSafeDsServices } from '../../../src/language/safe-ds-module.js';
import { clearDocuments } from 'langium/test';

const services = createSafeDsServices(EmptyFileSystem).SafeDs;
const root = 'typing';
Expand Down Expand Up @@ -39,6 +40,7 @@ const createTypingTest = async (
const code = fs.readFileSync(absolutePath).toString();

// File must not contain any syntax errors
await clearDocuments(services);
const syntaxErrors = await getSyntaxErrors(services, code);
if (syntaxErrors.length > 0) {
return invalidTest(
Expand Down
2 changes: 2 additions & 0 deletions tests/language/validation/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getSyntaxErrors, SyntaxErrorsInCodeError } from '../../helpers/diagnost
import { EmptyFileSystem } from 'langium';
import { createSafeDsServices } from '../../../src/language/safe-ds-module.js';
import { DocumentUri, Range } from 'vscode-languageserver-types';
import { clearDocuments } from 'langium/test';

const services = createSafeDsServices(EmptyFileSystem).SafeDs;
const root = 'validation';
Expand Down Expand Up @@ -38,6 +39,7 @@ const createValidationTest = async (
const code = fs.readFileSync(absolutePath).toString();

// File must not contain any syntax errors
await clearDocuments(services);
const syntaxErrors = await getSyntaxErrors(services, code);
if (syntaxErrors.length > 0) {
return invalidTest(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tests.scoping.annotationCalls.acrossFiles

// $TEST$ references same_MyAnnotation
@»MyAnnotation«

// $TEST$ references same_AnnotationInSamePackage
@»AnnotationInSamePackage«

// $TEST$ references safeds_AnnotationInSafeDsPackage
@»AnnotationInSafeDsPackage«

// $TEST$ unresolved
@»AnnotationInAnotherPackage«

// $TEST$ unresolved
@»AnnotationWithoutPackage«
pipeline myPipeline {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tests.scoping.annotationCalls.acrossFiles

import safeds.scoping.annotationCalls.acrossFiles.MyAnnotation as MyOwnAnnotation
import tests.scoping.annotationCalls.acrossFiles.other.AnnotationInAnotherPackage

// $TEST$ target own_MyOwnAnnotation
annotation »MyOwnAnnotation«

// $TEST$ references own_MyOwnAnnotation
@»MyOwnAnnotation«

// $TEST$ references same_AnnotationInSamePackage
@»AnnotationInSamePackage«

// $TEST$ references safeds_AnnotationInSafeDsPackage
@»AnnotationInSafeDsPackage«

// $TEST$ references other_AnnotationInAnotherPackage
@»AnnotationInAnotherPackage«

// $TEST$ unresolved
@»AnnotationWithoutPackage«
pipeline myPipeline {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tests.scoping.annotationCalls.acrossFiles

import safeds.scoping.annotationCalls.acrossFiles.MyAnnotation
import tests.scoping.annotationCalls.acrossFiles.other.MyAnnotation
import tests.scoping.annotationCalls.acrossFiles.MyAnnotation

pipeline myPipeline {
// $TEST$ references safeds_MyAnnotation
»MyAnnotation«;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tests.scoping.annotationCalls.acrossFiles

import tests.scoping.annotationCalls.acrossFiles.MyAnnotation as MyAnnotationInSamePackage
import safeds.scoping.annotationCalls.acrossFiles.MyAnnotation as MyAnnotationInSafeDsPackage
import tests.scoping.annotationCalls.acrossFiles.other.MyAnnotation as MyAnnotationInAnotherPackage

// $TEST$ references same_MyAnnotation
@»MyAnnotation«


// $TEST$ references same_AnnotationInSamePackage
@»AnnotationInSamePackage«

// $TEST$ references safeds_AnnotationInSafeDsPackage
@»AnnotationInSafeDsPackage«

// $TEST$ unresolved
@»AnnotationInAnotherPackage«

// $TEST$ unresolved
@»AnnotationWithoutPackage«

// $TEST$ references same_MyAnnotation
@»MyAnnotationInSamePackage«

// $TEST$ references safeds_MyAnnotation
@»MyAnnotationInSafeDsPackage«

// $TEST$ references other_MyAnnotation
@»MyAnnotationInAnotherPackage«
pipeline myPipeline {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tests.scoping.annotationCalls.acrossFiles

import safeds.scoping.annotationCalls.acrossFiles.MyAnnotation
import tests.scoping.annotationCalls.acrossFiles.other.AnnotationInAnotherPackage
import AnnotationWithoutPackage

// $TEST$ references safeds_MyAnnotation
@»MyAnnotation«

// $TEST$ references same_AnnotationInSamePackage
@»AnnotationInSamePackage«

// $TEST$ references safeds_AnnotationInSafeDsPackage
@»AnnotationInSafeDsPackage«

// $TEST$ references other_AnnotationInAnotherPackage
@»AnnotationInAnotherPackage«

// $TEST$ unresolved
@»AnnotationWithoutPackage«
pipeline myPipeline {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests.scoping.annotationCalls.acrossFiles

import safeds.scoping.annotationCalls.acrossFiles.*

// $TEST$ references safeds_MyAnnotation
@»MyAnnotation«

// $TEST$ references same_AnnotationInSamePackage
@»AnnotationInSamePackage«

// $TEST$ references safeds_AnnotationInSafeDsPackage
@»AnnotationInSafeDsPackage«

// $TEST$ unresolved
@»AnnotationInAnotherPackage«

// $TEST$ unresolved
@»AnnotationWithoutPackage«
pipeline myPipeline {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tests.scoping.annotationCalls.acrossFiles.other

// $TEST$ target other_MyAnnotation
annotation »MyAnnotation«

// $TEST$ target other_AnnotationInAnotherPackage
annotation »AnnotationInAnotherPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package safeds.scoping.annotationCalls.acrossFiles

// $TEST$ target safeds_MyAnnotation
annotation »MyAnnotation«

// $TEST$ target safeds_AnnotationInSafeDsPackage
annotation »AnnotationInSafeDsPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tests.scoping.annotationCalls.acrossFiles

// $TEST$ target same_MyAnnotation
annotation »MyAnnotation«

// $TEST$ target same_AnnotationInSamePackage
annotation »AnnotationInSamePackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// $TEST$ target without_MyAnnotation
annotation »MyAnnotation«

// $TEST$ target without_AnnotationWithoutPackage
annotation »AnnotationWithoutPackage«
10 changes: 10 additions & 0 deletions tests/resources/scoping/annotation calls/on parameter/main.sdstest
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ fun myFunction(
myParameter: Int
)

pipeline myPipeline {
val alias = Before;

// $TEST$ unresolved
(@»alias« myParameter) {};

// $TEST$ unresolved
(@»alias« myParameter) -> 1;
}

// $TEST$ target after
annotation »After«

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tests.scoping.namedTypes.acrossFiles.toGlobalClasses

segment mySegment(
// $TEST$ references same_MyClass
p1: »MyClass«,

// $TEST$ references same_ClassInSamePackage
p2: »ClassInSamePackage«,

// $TEST$ references safeds_ClassInSafeDsPackage
p3: »ClassInSafeDsPackage«,

// $TEST$ unresolved
p4: »ClassInAnotherPackage«,

// $TEST$ unresolved
p5: »ClassWithoutPackage«,
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tests.scoping.namedTypes.acrossFiles.toGlobalClasses

import safeds.scoping.namedTypes.acrossFiles.toGlobalClasses.MyClass as MyOwnClass
import tests.scoping.namedTypes.acrossFiles.toGlobalClasses.other.ClassInAnotherPackage

// $TEST$ target own_MyOwnClass
class »MyOwnClass«

segment mySegment(
// $TEST$ references own_MyOwnClass
p1: »MyOwnClass«,

// $TEST$ references same_ClassInSamePackage
p2: »ClassInSamePackage«,

// $TEST$ references safeds_ClassInSafeDsPackage
p3: »ClassInSafeDsPackage«,

// $TEST$ references other_ClassInAnotherPackage
p4: »ClassInAnotherPackage«,

// $TEST$ unresolved
p5: »ClassWithoutPackage«,
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tests.scoping.namedTypes.acrossFiles.toGlobalClasses

import safeds.scoping.namedTypes.acrossFiles.toGlobalClasses.MyClass
import tests.scoping.namedTypes.acrossFiles.toGlobalClasses.other.MyClass
import tests.scoping.namedTypes.acrossFiles.toGlobalClasses.MyClass

segment mySegment(
// $TEST$ references safeds_MyClass
p: »MyClass«,
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tests.scoping.namedTypes.acrossFiles.toGlobalClasses

import tests.scoping.namedTypes.acrossFiles.toGlobalClasses.MyClass as MyClassInSamePackage
import safeds.scoping.namedTypes.acrossFiles.toGlobalClasses.MyClass as MyClassInSafeDsPackage
import tests.scoping.namedTypes.acrossFiles.toGlobalClasses.other.MyClass as MyClassInAnotherPackage

segment mySegment(
// $TEST$ references same_MyClass
p1: »MyClass«,


// $TEST$ references same_ClassInSamePackage
p2: »ClassInSamePackage«,

// $TEST$ references safeds_ClassInSafeDsPackage
p3: »ClassInSafeDsPackage«,

// $TEST$ unresolved
p4: »ClassInAnotherPackage«,

// $TEST$ unresolved
p5: »ClassWithoutPackage«,

// $TEST$ references same_MyClass
p6: »MyClassInSamePackage«,

// $TEST$ references safeds_MyClass
p7: »MyClassInSafeDsPackage«,

// $TEST$ references other_MyClass
p8: »MyClassInAnotherPackage«,
) {}
Loading

0 comments on commit 928b520

Please sign in to comment.