mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-22 16:59:48 +02:00
Merge pull request #1207 from mermaid-js/bug/1205_activations_error
#1205 Adding parsing exception for then an inactive participant is inactivated
This commit is contained in:
@@ -16,6 +16,25 @@ export const addActor = function(id, name, description) {
|
||||
actors[id] = { name: name, description: description };
|
||||
};
|
||||
|
||||
const activationCount = part => {
|
||||
let i = 0;
|
||||
let count = 0;
|
||||
for (i = 0; i < messages.length; i++) {
|
||||
// console.warn(i, messages[i]);
|
||||
if (messages[i].type === LINETYPE.ACTIVE_START) {
|
||||
if (messages[i].from.actor === part) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (messages[i].type === LINETYPE.ACTIVE_END) {
|
||||
if (messages[i].from.actor === part) {
|
||||
count--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
};
|
||||
|
||||
export const addMessage = function(idFrom, idTo, message, answer) {
|
||||
messages.push({ from: idFrom, to: idTo, message: message, answer: answer });
|
||||
};
|
||||
@@ -24,7 +43,25 @@ export const addSignal = function(idFrom, idTo, message, messageType) {
|
||||
logger.debug(
|
||||
'Adding message from=' + idFrom + ' to=' + idTo + ' message=' + message + ' type=' + messageType
|
||||
);
|
||||
|
||||
if (messageType === LINETYPE.ACTIVE_END) {
|
||||
const cnt = activationCount(idFrom.actor);
|
||||
logger.debug('Adding message from=', messages, cnt);
|
||||
if (cnt < 1) {
|
||||
// Bail out as there is an activation signal from an inactive participant
|
||||
var error = new Error('Trying to inactivate an inactive participant (' + idFrom.actor + ')');
|
||||
error.hash = {
|
||||
text: '->>-',
|
||||
token: '->>-',
|
||||
line: '1',
|
||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||
expected: ["'ACTIVE_PARTICIPANT'"]
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
messages.push({ from: idFrom, to: idTo, message: message, type: messageType });
|
||||
return true;
|
||||
};
|
||||
|
||||
export const getMessages = function() {
|
||||
|
@@ -214,6 +214,33 @@ describe('when parsing a sequenceDiagram', function() {
|
||||
expect(messages[7].type).toBe(parser.yy.LINETYPE.ACTIVE_END);
|
||||
expect(messages[7].from.actor).toBe('Carol');
|
||||
});
|
||||
it('it should handle fail parsing when activating an inactive participant', function() {
|
||||
const str =
|
||||
`sequenceDiagram
|
||||
participant user as End User
|
||||
participant Server as Server
|
||||
participant System as System
|
||||
participant System2 as System2
|
||||
|
||||
user->>+Server: Test
|
||||
user->>+Server: Test2
|
||||
user->>System: Test
|
||||
Server->>-user: Test
|
||||
Server->>-user: Test2
|
||||
|
||||
%% The following deactivation of Server will fail
|
||||
Server->>-user: Test3`;
|
||||
|
||||
let error = false;
|
||||
try {
|
||||
parser.parse(str);
|
||||
} catch(e) {
|
||||
console.log(e.hash);
|
||||
error = true;
|
||||
}
|
||||
expect(error).toBe(true);
|
||||
});
|
||||
|
||||
it('it should handle comments in a sequenceDiagram', function() {
|
||||
const str =
|
||||
'sequenceDiagram\n' +
|
||||
|
Reference in New Issue
Block a user