|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; |
| 3 | +import { NodeFileSystem } from 'langium/node'; |
| 4 | +import { clearDocuments } from 'langium/test'; |
| 5 | +import { isSdsClass, SdsClass } from '../../../src/language/generated/ast.js'; |
| 6 | +import { getNodeOfType } from '../../helpers/nodeFinder.js'; |
| 7 | + |
| 8 | +const services = createSafeDsServices(NodeFileSystem).SafeDs; |
| 9 | +const classHierarchy = services.types.ClassHierarchy; |
| 10 | + |
| 11 | +describe('SafeDsClassHierarchy', async () => { |
| 12 | + beforeEach(async () => { |
| 13 | + // Load the builtin library |
| 14 | + await services.shared.workspace.WorkspaceManager.initializeWorkspace([]); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(async () => { |
| 18 | + await clearDocuments(services); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('streamSuperclasses', () => { |
| 22 | + const superclassNames = (node: SdsClass | undefined) => |
| 23 | + classHierarchy |
| 24 | + .streamSuperclasses(node) |
| 25 | + .map((clazz) => clazz.name) |
| 26 | + .toArray(); |
| 27 | + |
| 28 | + it('should return an empty stream if passed undefined', () => { |
| 29 | + expect(superclassNames(undefined)).toStrictEqual([]); |
| 30 | + }); |
| 31 | + |
| 32 | + const testCases = [ |
| 33 | + { |
| 34 | + testName: 'should return "Any" if the class has no parent types', |
| 35 | + code: ` |
| 36 | + class A |
| 37 | + `, |
| 38 | + expected: ['Any'], |
| 39 | + }, |
| 40 | + { |
| 41 | + testName: 'should return "Any" if the first parent type is not a class', |
| 42 | + code: ` |
| 43 | + class A sub E |
| 44 | + enum E |
| 45 | + `, |
| 46 | + expected: ['Any'], |
| 47 | + }, |
| 48 | + { |
| 49 | + testName: 'should return the superclasses of a class (no cycle, implicit any)', |
| 50 | + code: ` |
| 51 | + class A sub B |
| 52 | + class B |
| 53 | + `, |
| 54 | + expected: ['B', 'Any'], |
| 55 | + }, |
| 56 | + { |
| 57 | + testName: 'should return the superclasses of a class (no cycle, explicit any)', |
| 58 | + code: ` |
| 59 | + class A sub Any |
| 60 | + `, |
| 61 | + expected: ['Any'], |
| 62 | + }, |
| 63 | + { |
| 64 | + testName: 'should return the superclasses of a class (cycle)', |
| 65 | + code: ` |
| 66 | + class A sub B |
| 67 | + class B sub C |
| 68 | + class C sub A |
| 69 | + `, |
| 70 | + expected: ['B', 'C', 'A', 'Any'], |
| 71 | + }, |
| 72 | + { |
| 73 | + testName: 'should only consider the first parent type', |
| 74 | + code: ` |
| 75 | + class A sub B, C |
| 76 | + class B |
| 77 | + class C |
| 78 | + `, |
| 79 | + expected: ['B', 'Any'], |
| 80 | + }, |
| 81 | + ]; |
| 82 | + |
| 83 | + it.each(testCases)('$testName', async ({ code, expected }) => { |
| 84 | + const firstClass = await getNodeOfType(services, code, isSdsClass); |
| 85 | + expect(superclassNames(firstClass)).toStrictEqual(expected); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments