Skip to content

Commit

Permalink
Merge pull request #837 from drik98/bugfix/fix-creation-of-new-child-…
Browse files Browse the repository at this point in the history
…with-single-child-accessor

fix(ts-model-api): new single childs can have correct concept
  • Loading branch information
Oleksandr Dzhychko authored Jun 21, 2024
2 parents 87b73e4 + e1a12ca commit 1623be4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Note: Ideally, this test should be placed in the ts-model-api module.
* However, due to the complexity of testing the code without the context
* of the generated languages, we will conduct the test here for now.
*/

import {
BaseCommentAttribute,
C_BaseCommentAttribute,
C_BaseConcept,
C_TypeAnnotated,
isOfConcept_BaseConcept,
isOfConcept_TypeAnnotated,
} from "../build/typescript_src/L_jetbrains_mps_lang_core";
import { useFakeNode } from "./test-helpers";

const NODE_DATA_WITH_SINGLE_CHILD_ACCESSOR = {
root: {
children: [
{
concept: C_BaseCommentAttribute.getUID(),
role: "withSingleChildAccessor",
properties: {
name: "aName",
},
},
],
},
};

test("new element can be added to SingleChildAccessor when provided the correct base concept", () => {
const { typedNode } = useFakeNode<BaseCommentAttribute>(
"withSingleChildAccessor",
NODE_DATA_WITH_SINGLE_CHILD_ACCESSOR
);
const childNode = typedNode.commentedNode.setNew(C_BaseConcept);
expect(isOfConcept_BaseConcept(childNode)).toBeTruthy();
});

test("new element can be added to SingleChildAccessor when provided a sub concept", () => {
const { typedNode } = useFakeNode<BaseCommentAttribute>(
"withSingleChildAccessor",
NODE_DATA_WITH_SINGLE_CHILD_ACCESSOR
);
const childNode = typedNode.commentedNode.setNew(C_TypeAnnotated);
expect(isOfConcept_TypeAnnotated(childNode)).toBeTruthy();
expect(isOfConcept_BaseConcept(childNode)).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { org } from "@modelix/model-client";
import { LanguageRegistry } from "@modelix/ts-model-api";
import { ITypedNode, LanguageRegistry } from "@modelix/ts-model-api";
import { registerLanguages } from "../build/typescript_src";

const DEFAULT_NODE_DATA = {
Expand Down Expand Up @@ -31,8 +31,8 @@ export function useFakeRootNode(nodeData: object = DEFAULT_NODE_DATA) {
return rootNode.getChildren(role)[0];
}

function getTypedNode(role?: string) {
return LanguageRegistry.INSTANCE.wrapNode(getUntypedNode(role));
function getTypedNode<T extends ITypedNode>(role?: string) {
return LanguageRegistry.INSTANCE.wrapNode(getUntypedNode(role)) as T;
}

return {
Expand All @@ -42,11 +42,14 @@ export function useFakeRootNode(nodeData: object = DEFAULT_NODE_DATA) {
};
}

export function useFakeNode(role?: string, nodeData?: object) {
export function useFakeNode<T extends ITypedNode>(
role?: string,
nodeData?: object
) {
const { getUntypedNode, getTypedNode, rootNode } = useFakeRootNode(nodeData);
return {
rootNode,
untypedNode: getUntypedNode(role),
typedNode: getTypedNode(role),
typedNode: getTypedNode<T>(role),
};
}
10 changes: 5 additions & 5 deletions model-api-gen-gradle-test/vue-integration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ts-model-api/src/ChildrenAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export class SingleChildAccessor<ChildT extends ITypedNode> extends ChildrenAcce
return children.length === 0 ? undefined : children[0]
}

public setNew(): ChildT {
public setNew(subconcept?: IConceptJS | undefined): ChildT {
const existing = this.get();
if (existing !== undefined) {
existing.remove();
}
return this.wrapChild(this.parentNode.addNewChild(this.role, 0, undefined))
return this.wrapChild(this.parentNode.addNewChild(this.role, 0, subconcept))
}
}
8 changes: 4 additions & 4 deletions vue-model-api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1623be4

Please sign in to comment.