diff --git a/src/parser/MessageContextListener.spec.ts b/src/parser/MessageContextListener.spec.ts index 28c12d48..902ac628 100644 --- a/src/parser/MessageContextListener.spec.ts +++ b/src/parser/MessageContextListener.spec.ts @@ -3,100 +3,92 @@ import { MessageContextListener } from "./MessageContextListener"; import { RootContext } from "@/parser/index"; import { OwnableMessageType } from "@/parser/OwnableMessage"; -describe("MessageListener", () => { - it("can handle Message and Creation", () => { - const code = ` - res = A.method(E.m) { - B->C.method - } - var = res - new B - C->D: message - `; +describe("MessageContextListener", () => { + // Helper function to parse code and get results + const parseCode = (code: string) => { const rootContext = RootContext(code); const walker = antlr4.tree.ParseTreeWalker.DEFAULT; + const listener = new MessageContextListener(); + walker.walk(listener, rootContext); + return listener.result(); + }; - const messageContextListener = new MessageContextListener(); - // @ts-ignore - walker.walk(messageContextListener, rootContext); + describe("message parsing", () => { + it("parses various message types and creation statements", () => { + const code = ` + res = A.method(E.m) { + B->C.method + } + var = res + new B + C->D: message + `; - expect(messageContextListener.result()).toStrictEqual([ - { - from: "_STARTER_", - label: "method(E.m)", - signature: "method(E.m)", - to: "A", - type: OwnableMessageType.SyncMessage, - }, - { - from: "B", - label: "method", - signature: "method", - to: "C", - type: OwnableMessageType.SyncMessage, - }, - { - from: "_STARTER_", - label: "var=res", - signature: "res", - to: "_STARTER_", - type: OwnableMessageType.SyncMessage, - }, - { - from: "_STARTER_", - label: "«create»", - signature: "«create»", - to: "B", - type: OwnableMessageType.CreationMessage, - }, - { - from: "C", - label: " message", - signature: " message", - to: "D", - type: OwnableMessageType.AsyncMessage, - }, - ]); - }); - - it("ignores expression in parameters", () => { - const code = `A.m(new B, - C.m)`; - const rootContext = RootContext(code); - const walker = antlr4.tree.ParseTreeWalker.DEFAULT; + expect(parseCode(code)).toStrictEqual([ + { + from: "_STARTER_", + label: "method(E.m)", + signature: "method(E.m)", + to: "A", + type: OwnableMessageType.SyncMessage, + }, + { + from: "B", + label: "method", + signature: "method", + to: "C", + type: OwnableMessageType.SyncMessage, + }, + { + from: "_STARTER_", + label: "var=res", + signature: "res", + to: "_STARTER_", + type: OwnableMessageType.SyncMessage, + }, + { + from: "_STARTER_", + label: "«create»", + signature: "«create»", + to: "B", + type: OwnableMessageType.CreationMessage, + }, + { + from: "C", + label: " message", + signature: " message", + to: "D", + type: OwnableMessageType.AsyncMessage, + }, + ]); + }); - const messageContextListener = new MessageContextListener(); - // @ts-ignore - walker.walk(messageContextListener, rootContext); + it("handles parameter expressions without creating additional messages", () => { + const code = `A.m(new B, C.m)`; - expect(messageContextListener.result()).toStrictEqual([ - { - from: "_STARTER_", - signature: "m(new B,C.m)", - label: "m(new B,C.m)", - to: "A", - type: 0, - }, - ]); - }); - - it("ignores expression in conditions", () => { - const code = `if(A.isGood()) {B.m}`; - const rootContext = RootContext(code); - const walker = antlr4.tree.ParseTreeWalker.DEFAULT; + expect(parseCode(code)).toStrictEqual([ + { + from: "_STARTER_", + signature: "m(new B,C.m)", + label: "m(new B,C.m)", + to: "A", + type: 0, + }, + ]); + }); - const messageContextListener = new MessageContextListener(); - // @ts-ignore - walker.walk(messageContextListener, rootContext); + it("parses messages in conditional blocks while ignoring condition expressions", () => { + const code = `if(A.isGood()) {B.m}`; - expect(messageContextListener.result()).toStrictEqual([ - { - from: "_STARTER_", - signature: "m", - label: "m", - to: "B", - type: 0, - }, - ]); + expect(parseCode(code)).toStrictEqual([ + { + from: "_STARTER_", + signature: "m", + label: "m", + to: "B", + type: 0, + }, + ]); + }); }); }); diff --git a/src/parser/index.js b/src/parser/index.js index 422e03fe..c717fb8d 100644 --- a/src/parser/index.js +++ b/src/parser/index.js @@ -32,7 +32,7 @@ function rootContext(code) { const tokens = new antlr4.CommonTokenStream(lexer); const parser = new sequenceParser(tokens); parser.addErrorListener(new SeqErrorListener()); - return parser._syntaxErrors ? null : parser.prog(); + return parser.prog(); } antlr4.ParserRuleContext.prototype.getFormattedText = function () {