From 6541eb47651aa9f0cc103f2660ac009a3feef506 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Mon, 30 Oct 2023 12:11:47 +0100 Subject: [PATCH] test: `streamSuperclassMembers` --- .../typing/safe-ds-class-hierarchy.test.ts | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/packages/safe-ds-lang/tests/language/typing/safe-ds-class-hierarchy.test.ts b/packages/safe-ds-lang/tests/language/typing/safe-ds-class-hierarchy.test.ts index 1724cef6d..1f7271901 100644 --- a/packages/safe-ds-lang/tests/language/typing/safe-ds-class-hierarchy.test.ts +++ b/packages/safe-ds-lang/tests/language/typing/safe-ds-class-hierarchy.test.ts @@ -1,8 +1,8 @@ -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; -import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; import { NodeFileSystem } from 'langium/node'; import { clearDocuments } from 'langium/test'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { isSdsClass, SdsClass } from '../../../src/language/generated/ast.js'; +import { createSafeDsServices } from '../../../src/language/index.js'; import { getNodeOfType } from '../../helpers/nodeFinder.js'; const services = createSafeDsServices(NodeFileSystem).SafeDs; @@ -131,4 +131,73 @@ describe('SafeDsClassHierarchy', async () => { expect(superclassNames(firstClass)).toStrictEqual(expected); }); }); + + describe('streamSuperclassMembers', () => { + const superclassMemberNames = (node: SdsClass | undefined) => + classHierarchy + .streamSuperclassMembers(node) + .map((member) => member.name) + .toArray(); + + it('should return an empty stream if passed undefined', () => { + expect(superclassMemberNames(undefined)).toStrictEqual([]); + }); + + const testCases = [ + { + testName: 'should return the members of the parent type', + code: ` + class A { + attr a: Int + fun f() + } + + class B sub A + `, + index: 1, + expected: ['a', 'f'], + }, + { + testName: 'should only consider members of the first parent type', + code: ` + class A { + attr a: Int + fun f() + } + + class B { + attr b: Int + fun g() + } + + class C sub A, B + `, + index: 2, + expected: ['a', 'f'], + }, + { + testName: 'should return members of all superclasses', + code: ` + class A { + attr a: Int + fun f() + } + + class B sub A { + attr b: Int + fun g() + } + + class C sub B + `, + index: 2, + expected: ['b', 'g', 'a', 'f'], + }, + ]; + + it.each(testCases)('$testName', async ({ code, index, expected }) => { + const firstClass = await getNodeOfType(services, code, isSdsClass, index); + expect(superclassMemberNames(firstClass)).toStrictEqual(expected); + }); + }); });