From 0c8f7163dbef6595381e35ed42b0a148238acdc0 Mon Sep 17 00:00:00 2001 From: Daniel Bartholomae Date: Tue, 23 Aug 2022 15:58:35 +0200 Subject: [PATCH 1/4] Add first C4 parser test --- src/diagrams/c4/parser/flow.spec.js | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/diagrams/c4/parser/flow.spec.js diff --git a/src/diagrams/c4/parser/flow.spec.js b/src/diagrams/c4/parser/flow.spec.js new file mode 100644 index 000000000..8d76efd9f --- /dev/null +++ b/src/diagrams/c4/parser/flow.spec.js @@ -0,0 +1,43 @@ +import flowDb from '../c4Db'; +import flow from './c4Diagram.jison'; +import { setConfig } from '../../../config'; + +setConfig({ + securityLevel: 'strict', +}); + +describe('parsing a flow chart', function () { + beforeEach(function () { + flow.parser.yy = flowDb; + flow.parser.yy.clear(); + }); + + it('should parse a C4 diagram with one Person correctly', function () { + flow.parser.parse(`C4Context +title System Context diagram for Internet Banking System +Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")`); + + const yy = flow.parser.yy; + expect(yy.getC4Type()).toBe('C4Context'); + expect(yy.getTitle()).toBe('System Context diagram for Internet Banking System'); + + const shapes = yy.getC4ShapeArray(); + expect(shapes.length).toBe(1); + const onlyShape = shapes[0]; + + expect(onlyShape).toEqual({ + alias: 'customerA', + descr: { + text: 'A customer of the bank, with personal bank accounts.', + }, + label: { + text: 'Banking Customer A', + }, + parentBoundary: 'global', + typeC4Shape: { + text: 'person', + }, + wrap: false, + }); + }); +}); From d97ce7eab81a64a5d40d22f515f61537ae9bc3b9 Mon Sep 17 00:00:00 2001 From: Daniel Bartholomae Date: Tue, 23 Aug 2022 16:01:39 +0200 Subject: [PATCH 2/4] Add test for handling trailing whitespaces --- src/diagrams/c4/parser/flow.spec.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/diagrams/c4/parser/flow.spec.js b/src/diagrams/c4/parser/flow.spec.js index 8d76efd9f..3d2bb97df 100644 --- a/src/diagrams/c4/parser/flow.spec.js +++ b/src/diagrams/c4/parser/flow.spec.js @@ -40,4 +40,13 @@ Person(customerA, "Banking Customer A", "A customer of the bank, with personal b wrap: false, }); }); + + it('should handle a trailing whitespaces after statements', function () { + const whitespace = ' '; + const rendered = flow.parser.parse(`C4Context${whitespace} +title System Context diagram for Internet Banking System${whitespace} +Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")${whitespace}`); + + expect(rendered).toBe(true); + }); }); From 2cf9348f536769a08ae9043d356007c6bf24daa5 Mon Sep 17 00:00:00 2001 From: Daniel Bartholomae Date: Tue, 23 Aug 2022 16:07:11 +0200 Subject: [PATCH 3/4] Add test for handling parameter names that are keywords --- src/diagrams/c4/parser/flow.spec.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/diagrams/c4/parser/flow.spec.js b/src/diagrams/c4/parser/flow.spec.js index 3d2bb97df..b1d473916 100644 --- a/src/diagrams/c4/parser/flow.spec.js +++ b/src/diagrams/c4/parser/flow.spec.js @@ -49,4 +49,21 @@ Person(customerA, "Banking Customer A", "A customer of the bank, with personal b expect(rendered).toBe(true); }); + + it('should handle parameter names that are keywords', function () { + flow.parser.parse(`C4Context +title title +Person(Person, "Person", "Person")`); + + const yy = flow.parser.yy; + expect(yy.getTitle()).toBe('title'); + + const shapes = yy.getC4ShapeArray(); + expect(shapes.length).toBe(1); + const onlyShape = shapes[0]; + + expect(onlyShape.alias).toBe('Person'); + expect(onlyShape.descr.text).toBe('Person'); + expect(onlyShape.label.text).toBe('Person'); + }); }); From 8c56f68a06c65004b12b61bdbe5501e07fad0ee2 Mon Sep 17 00:00:00 2001 From: Daniel Bartholomae Date: Tue, 23 Aug 2022 16:15:38 +0200 Subject: [PATCH 4/4] Add test for handling parameter names that are "default" --- src/diagrams/c4/parser/flow.spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/diagrams/c4/parser/flow.spec.js b/src/diagrams/c4/parser/flow.spec.js index b1d473916..c01d99e40 100644 --- a/src/diagrams/c4/parser/flow.spec.js +++ b/src/diagrams/c4/parser/flow.spec.js @@ -66,4 +66,19 @@ Person(Person, "Person", "Person")`); expect(onlyShape.descr.text).toBe('Person'); expect(onlyShape.label.text).toBe('Person'); }); + + it('should allow default in the parameters', function () { + flow.parser.parse(`C4Context +Person(default, "default", "default")`); + + const yy = flow.parser.yy; + + const shapes = yy.getC4ShapeArray(); + expect(shapes.length).toBe(1); + const onlyShape = shapes[0]; + + expect(onlyShape.alias).toBe('default'); + expect(onlyShape.descr.text).toBe('default'); + expect(onlyShape.label.text).toBe('default'); + }); });