Skip to content

Commit

Permalink
Merge pull request #641 from sopherio/master
Browse files Browse the repository at this point in the history
SequenceDiagram: Add support for multiple alt else statements
  • Loading branch information
tylerlong authored Mar 16, 2018
2 parents 234d39c + 3b4e48e commit 93201b2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 59 deletions.
13 changes: 12 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.
Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?
alt either this
Alice->>John: Yes
else or this
Alice->>John: No
else or this will happen
Alice->John: Maybe
end
par this happens in parallel
Alice -->> Bob: Parallel message 1
and
Alice -->> John: Parallel message 2
end
</div>

<hr/>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"upgrade": "yarn-upgrade-all",
"lint": "standard",
"test": "yarn lint && jest",
"test:tdd": "yarn lint && jest --watch",
"jison": "node -r babel-register node_modules/.bin/gulp jison",
"prepublishOnly": "yarn build && yarn release && yarn test",
"prepush": "yarn test"
Expand Down
13 changes: 7 additions & 6 deletions src/diagrams/sequence/parser/sequenceDiagram.jison
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,13 @@ statement
$3.unshift({type: 'optStart', optText:$2, signalType: yy.LINETYPE.OPT_START});
$3.push({type: 'optEnd', optText:$2, signalType: yy.LINETYPE.OPT_END});
$$=$3;}
| alt restOfLine document else restOfLine document end
| alt restOfLine else_sections end
{
// Alt start
$3.unshift({type: 'altStart', altText:$2, signalType: yy.LINETYPE.ALT_START});
// Content in alt is already in $3
// Else
$3.push({type: 'else', altText:$5, signalType: yy.LINETYPE.ALT_ELSE});
// Content in other alt
$3 = $3.concat($6);
// End
$3.push({type: 'altEnd', signalType: yy.LINETYPE.ALT_END});
$$=$3;}
| par restOfLine par_sections end
{
Expand All @@ -133,6 +128,12 @@ par_sections
{ $$ = $1.concat([{type: 'and', parText:$3, signalType: yy.LINETYPE.PAR_AND}, $4]); }
;

else_sections
: document
| document else restOfLine else_sections
{ $$ = $1.concat([{type: 'else', altText:$3, signalType: yy.LINETYPE.ALT_ELSE}, $4]); }
;

note_statement
: 'note' placement actor text2
{
Expand Down
96 changes: 47 additions & 49 deletions src/diagrams/sequence/parser/sequenceDiagram.js

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

30 changes: 27 additions & 3 deletions src/diagrams/sequence/sequenceDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('when parsing a sequenceDiagram', function () {
expect(messages[2].from).toBe('Bob')
expect(messages[2].to).toBe('Alice')
})
it('it should handle loop statements a sequenceDiagram', function () {
it('it should handle loop statements', function () {
const str = 'sequenceDiagram\n' +
'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
Expand All @@ -369,7 +369,7 @@ describe('when parsing a sequenceDiagram', function () {
expect(messages[0].from).toBe('Alice')
expect(messages[1].from).toBe('Bob')
})
it('it should handle opt statements a sequenceDiagram', function () {
it('it should handle opt statements', function () {
const str = 'sequenceDiagram\n' +
'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
Expand All @@ -389,7 +389,7 @@ describe('when parsing a sequenceDiagram', function () {
expect(messages[0].from).toBe('Alice')
expect(messages[1].from).toBe('Bob')
})
it('it should handle alt statements a sequenceDiagram', function () {
it('it should handle alt statements', function () {
const str = 'sequenceDiagram\n' +
'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
Expand All @@ -412,6 +412,30 @@ describe('when parsing a sequenceDiagram', function () {
expect(messages[0].from).toBe('Alice')
expect(messages[1].from).toBe('Bob')
})
it('it should handle alt statements with multiple elses', function () {
const str = 'sequenceDiagram\n' +
'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
'Note right of Bob: Bob thinks\n' +
'alt isWell\n\n' +
'Bob-->Alice: I am good thanks!\n' +
'else isSick\n' +
'Bob-->Alice: Feel sick...\n' +
'else default\n' +
'Bob-->Alice: :-)\n' +
'end'
parser.parse(str)
const messages = parser.yy.getMessages()
expect(messages.length).toBe(9)
expect(messages[1].from).toBe('Bob')
expect(messages[2].type).toBe(parser.yy.LINETYPE.ALT_START)
expect(messages[3].from).toBe('Bob')
expect(messages[4].type).toBe(parser.yy.LINETYPE.ALT_ELSE)
expect(messages[5].from).toBe('Bob')
expect(messages[6].type).toBe(parser.yy.LINETYPE.ALT_ELSE)
expect(messages[7].from).toBe('Bob')
expect(messages[8].type).toBe(parser.yy.LINETYPE.ALT_END)
})
it('it should handle par statements a sequenceDiagram', function () {
const str = 'sequenceDiagram\n' +
'par Parallel one\n' +
Expand Down

0 comments on commit 93201b2

Please sign in to comment.