From 897c4259ed8f341da9e589d0c7858f8136cc72c9 Mon Sep 17 00:00:00 2001 From: Tali Herzka Date: Fri, 8 Apr 2022 18:44:22 +0000 Subject: [PATCH] add title getters and setters, add spec for stateDiagram-v2 --- src/diagrams/state/stateDb.js | 15 +- src/diagrams/state/stateDiagram-v2.spec.js | 376 +++++++++++++++++++++ src/diagrams/state/stateDiagram.spec.js | 16 +- 3 files changed, 404 insertions(+), 3 deletions(-) create mode 100644 src/diagrams/state/stateDiagram-v2.spec.js diff --git a/src/diagrams/state/stateDb.js b/src/diagrams/state/stateDb.js index 0b031a127..14eea7896 100644 --- a/src/diagrams/state/stateDb.js +++ b/src/diagrams/state/stateDb.js @@ -4,6 +4,8 @@ import mermaidAPI from '../../mermaidAPI'; import common from '../common/common'; import * as configApi from '../../config'; +const sanitizeText = (txt) => common.sanitizeText(txt, configApi.getConfig()); + const clone = (o) => JSON.parse(JSON.stringify(o)); let rootDoc = []; @@ -115,10 +117,19 @@ let startCnt = 0; let endCnt = 0; // eslint-disable-line // let stateCnt = 0; +let title = 'State diagram'; let description = ''; +const setTitle = function (txt) { + title = sanitizeText(txt); +}; + +const getTitle = function () { + return title; +}; + const setAccDescription = function (txt) { - description = txt; + description = sanitizeText(txt); }; const getAccDescription = function () { @@ -290,6 +301,8 @@ export default { getRootDocV2, extract, trimColon, + getTitle, + setTitle, getAccDescription, setAccDescription, }; diff --git a/src/diagrams/state/stateDiagram-v2.spec.js b/src/diagrams/state/stateDiagram-v2.spec.js new file mode 100644 index 000000000..7c013b9b2 --- /dev/null +++ b/src/diagrams/state/stateDiagram-v2.spec.js @@ -0,0 +1,376 @@ +import { parser } from './parser/stateDiagram'; +import stateDb from './stateDb'; + +describe('state diagram, ', function () { + describe('when parsing an info graph it', function () { + beforeEach(function () { + parser.yy = stateDb; + }); + + it('super simple', function () { + const str = ` + stateDiagram-v2 + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + }); + it('simple', function () { + const str = `stateDiagram-v2\n + State1 : this is another string + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + const description = stateDb.getAccDescription(); + expect(description).toBe(''); + }); + it('simple with accDescription', function () { + const str = `stateDiagram-v2\n + accDescription a simple description of the diagram + State1 : this is another string + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + const description = stateDb.getAccDescription(); + expect(description).toBe('a simple description of the diagram'); + }); + it('simple with title', function () { + const str = `stateDiagram-v2\n + title a simple title of the diagram + State1 : this is another string + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + const title = stateDb.getTitle(); + expect(title).toBe('a simple title of the diagram'); + }); + it('simple with directive', function () { + const str = `%%{init: {'logLevel': 0 }}%% + stateDiagram-v2\n + State1 : this is another string + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + }); + it('should handle relation definitions', function () { + const str = `stateDiagram-v2\n + [*] --> State1 + State1 --> [*] + State1 : this is a string + State1 : this is another string + + State1 --> State2 + State2 --> [*] + `; + + parser.parse(str); + }); + it('hide empty description', function () { + const str = `stateDiagram-v2\n + hide empty description + [*] --> State1 + State1 --> [*] + State1 : this is a string + State1 : this is another string + + State1 --> State2 + State2 --> [*] + `; + + parser.parse(str); + }); + + it('handle "as" in state names', function () { + const str = `stateDiagram-v2 + assemble + state assemble + `; + + parser.parse(str); + }); + it('handle "as" in state names 1', function () { + const str = `stateDiagram-v2 + assemble + state assemble + `; + + parser.parse(str); + }); + it('handle "as" in state names 2', function () { + const str = `stateDiagram-v2 + assembleas + state assembleas + `; + + parser.parse(str); + }); + it('handle "as" in state names 3', function () { + const str = `stateDiagram-v2 + state "as" as as + `; + + parser.parse(str); + }); + + it('scale', function () { + const str = `stateDiagram-v2\n + scale 350 width + [*] --> State1 + State1 --> [*] + State1 : this is a string with - in it + State1 : this is another string + + State1 --> State2 + State2 --> [*] + `; + + parser.parse(str); + }); + + it('description after second state', function () { + const str = `stateDiagram-v2\n + scale 350 width + [*] --> State1 : This is the description with - in it + State1 --> [*] + `; + + parser.parse(str); + }) + it('shall handle descriptions including minus signs', function () { + const str = `stateDiagram-v2\n + scale 350 width + [*] --> State1 : This is the description +-! + State1 --> [*] + `; + + parser.parse(str); + }); + it('should handle state statements', function () { + const str = `stateDiagram-v2\n + state Configuring { + [*] --> NewValueSelection + NewValueSelection --> NewValuePreview : EvNewValue + NewValuePreview --> NewValueSelection : EvNewValueRejected + NewValuePreview --> NewValueSelection : EvNewValueSaved1 + } + `; + + parser.parse(str); + }); + it('should handle recursive state definitions', function () { + const str = `stateDiagram-v2\n + state Configuring { + [*] --> NewValueSelection + NewValueSelection --> NewValuePreview : EvNewValue + NewValuePreview --> NewValueSelection : EvNewValueRejected + NewValuePreview --> NewValueSelection : EvNewValueSaved + + state NewValuePreview { + State1 --> State2 + } + } + `; + + parser.parse(str); + }); + it('should handle multiple recursive state definitions', function () { + const str = `stateDiagram-v2\n + scale 350 width + [*] --> NotShooting + + state NotShooting { + [*] --> Idle + Idle --> Configuring : EvConfig + Configuring --> Idle : EvConfig + } + + state Configuring { + [*] --> NewValueSelection + NewValueSelection --> NewValuePreview : EvNewValue + NewValuePreview --> NewValueSelection : EvNewValueRejected + NewValuePreview --> NewValueSelection : EvNewValueSaved + + state NewValuePreview { + State1 --> State2 + } + } + `; + + parser.parse(str); + }); + it('should handle state deifintions with separation of id', function () { + const str = `stateDiagram-v2\n + state "Long state description" as state1 + `; + + parser.parse(str); + }); + it('should handle state deifintions with separation of id', function () { + const str = `stateDiagram-v2 + state "Not Shooting State" as NotShooting { + state "Idle mode" as Idle + state "Configuring mode" as Configuring + [*] --> Idle + Idle --> Configuring : EvConfig + Configuring --> Idle : EvConfig + } + `; + + parser.parse(str); + }); + + it('should State definition with quotes', function () { + const str = `stateDiagram-v2\n + scale 600 width + + [*] --> State1 + State1 --> State2 : Succeeded + State1 --> [*] : Aborted + State2 --> State3 : Succeeded + State2 --> [*] : Aborted + state State3 { + state "Accumulate Enough Data\nLong State Name" as long1 + long1 : Just a test + [*] --> long1 + long1 --> long1 : New Data + long1 --> ProcessData : Enough Data + } + State3 --> State3 : Failed + State3 --> [*] : Succeeded / Save Result + State3 --> [*] : Aborted + `; + + parser.parse(str); + }); + it('should handle fork statements', function () { + const str = `stateDiagram-v2\n + state fork_state <> + [*] --> fork_state + fork_state --> State2 + fork_state --> State3 + + state join_state <> + State2 --> join_state + State3 --> join_state + join_state --> State4 + State4 --> [*] + `; + + parser.parse(str); + }); + it('should handle concurrent state', function () { + const str = `stateDiagram-v2\n + [*] --> Active + + state Active { + [*] --> NumLockOff + NumLockOff --> NumLockOn : EvNumLockPressed + NumLockOn --> NumLockOff : EvNumLockPressed + -- + [*] --> CapsLockOff + CapsLockOff --> CapsLockOn : EvCapsLockPressed + CapsLockOn --> CapsLockOff : EvCapsLockPressed + -- + [*] --> ScrollLockOff + ScrollLockOff --> ScrollLockOn : EvCapsLockPressed + ScrollLockOn --> ScrollLockOff : EvCapsLockPressed + } + `; + + parser.parse(str); + }); + it('should handle concurrent state', function () { + const str = `stateDiagram-v2\n + [*] --> Active + + state Active { + [*] --> NumLockOff + -- + [*] --> CapsLockOff + -- + [*] --> ScrollLockOff + } + `; + + parser.parse(str); + }); + // it('should handle arrow directions definitions', function() { + // const str = `stateDiagram-v2\n + // [*] -up-> First + // First -right-> Second + // Second --> Third + // Third -left-> Last + // `; + + // parser.parse(str); + // }); + it('should handle note statements', function () { + const str = `stateDiagram-v2\n + [*] --> Active + Active --> Inactive + + note left of Active : this is a short
note + + note right of Inactive + A note can also + be defined on + several lines + end note + `; + + parser.parse(str); + }); + it('should handle multiline notes with different line breaks', function () { + const str = `stateDiagram-v2 + State1 + note right of State1 + Line1
Line2
Line3
Line4
Line5 + end note + `; + + parser.parse(str); + }); + it('should handle floating notes', function () { + const str = `stateDiagram-v2 + foo: bar + note "This is a floating note" as N1 + `; + + parser.parse(str); + }); + it('should handle floating notes', function () { + const str = `stateDiagram-v2\n + state foo + note "This is a floating note" as N1 + `; + + parser.parse(str); + }); + it('should handle notes for composit states', function () { + const str = `stateDiagram-v2\n + [*] --> NotShooting + + state "Not Shooting State" as NotShooting { + state "Idle mode" as Idle + state "Configuring mode" as Configuring + [*] --> Idle + Idle --> Configuring : EvConfig + Configuring --> Idle : EvConfig + } + + note right of NotShooting : This is a note on a composite state + `; + + parser.parse(str); + }); + }); +}); diff --git a/src/diagrams/state/stateDiagram.spec.js b/src/diagrams/state/stateDiagram.spec.js index 6b27f63f6..0d7add467 100644 --- a/src/diagrams/state/stateDiagram.spec.js +++ b/src/diagrams/state/stateDiagram.spec.js @@ -27,7 +27,7 @@ describe('state diagram, ', function () { const description = stateDb.getAccDescription(); expect(description).toBe(''); }); - it.only('simple with accDescription', function () { + it('simple with accDescription', function () { const str = `stateDiagram\n accDescription a simple description of the diagram State1 : this is another string @@ -39,6 +39,18 @@ describe('state diagram, ', function () { const description = stateDb.getAccDescription(); expect(description).toBe('a simple description of the diagram'); }); + it('simple with title', function () { + const str = `stateDiagram\n + title a simple title of the diagram + State1 : this is another string + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + const title = stateDb.getTitle(); + expect(title).toBe('a simple title of the diagram'); + }); it('simple with directive', function () { const str = `%%{init: {'logLevel': 0 }}%% stateDiagram\n @@ -133,7 +145,7 @@ describe('state diagram, ', function () { parser.parse(str); }); - it('shall handle descriptions inkluding minus signs', function () { + it('shall handle descriptions including minus signs', function () { const str = `stateDiagram\n scale 350 width [*] --> State1 : This is the description +-!