Skip to content

Commit

Permalink
Fixed MessageCollector to handle async return messages correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCoder committed Nov 22, 2024
1 parent c87eabf commit 381bcf1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/parser/MessageCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export class MessageCollector extends sequenceParserListener {
this._addOwnedMessage(OwnableMessageType.AsyncMessage)(ctx);
enterCreation = (ctx: any) =>
this._addOwnedMessage(OwnableMessageType.CreationMessage)(ctx);
enterRet = (ctx: any) =>
enterRet = (ctx: any) => {
if (ctx.asyncMessage()) {
// it will visit the asyncMessage later
return;
}
this._addOwnedMessage(OwnableMessageType.ReturnMessage)(ctx);
};

private _addOwnedMessage = (type: OwnableMessageType) => (ctx: any) => {
if (this.isBlind) {
Expand Down
12 changes: 12 additions & 0 deletions src/positioning/MessageCollector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ describe("MessageCollector", () => {
};

describe("Return", () => {
it("@return A->B:m", () => {
const code = `@return A->B:m`;

expect(collectMessages(code)).toStrictEqual([
{
from: "A",
signature: "m",
to: "B",
type: OwnableMessageType.AsyncMessage,
},
]);
});
it("should handle one return message", () => {
const code = `
return result
Expand Down
7 changes: 7 additions & 0 deletions test/unit/parser/participant/Participants.Order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ import {
_STARTER_,
OrderedParticipants,
} from "../../../../src/parser/OrderedParticipants";
import { expect } from "vitest";

function getFlattenedParticipants(code: string) {
const rootContext = RootContext(code);
return OrderedParticipants(rootContext);
}

describe("Participants.Order", () => {
it("@return", () => {
expect(getFlattenedParticipants("@return A->B:m")).toEqual([
{ name: "A", left: "" },
{ name: "B", left: "A" },
]);
});
it("should return the order of participants", () => {
expect(getFlattenedParticipants("A as A1 B C.m")).toEqual([
{ name: _STARTER_, left: "" },
Expand Down
6 changes: 6 additions & 0 deletions test/unit/parser/to-collector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import ToCollector from "../../../src/parser/ToCollector";
import { expect } from "vitest";
import { blankParticipant } from "../../../src/parser/Participants";

test("@return", () => {
let participants = getParticipants("@return A->B.m");
expect(participants.Size()).toBe(2);
expect(participants.First().name).toBe("A");
});

test("if block", () => {
let participants = getParticipants("if(x) { A->B.m }");
expect(participants.Size()).toBe(2);
Expand Down

0 comments on commit 381bcf1

Please sign in to comment.