From e23212e1f90e526f89bd5d72a2b8c3214367d9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Tue, 22 Oct 2024 09:38:10 +1300 Subject: [PATCH 1/2] Add dot-dsl exercise --- config.json | 8 + .../dot-dsl/.docs/instructions.append.md | 32 ++++ .../practice/dot-dsl/.docs/instructions.md | 30 +++ exercises/practice/dot-dsl/.meta/Example.roc | 63 +++++++ exercises/practice/dot-dsl/.meta/config.json | 19 ++ exercises/practice/dot-dsl/DotDsl.roc | 24 +++ exercises/practice/dot-dsl/dot-dsl-test.roc | 171 ++++++++++++++++++ 7 files changed, 347 insertions(+) create mode 100644 exercises/practice/dot-dsl/.docs/instructions.append.md create mode 100644 exercises/practice/dot-dsl/.docs/instructions.md create mode 100644 exercises/practice/dot-dsl/.meta/Example.roc create mode 100644 exercises/practice/dot-dsl/.meta/config.json create mode 100644 exercises/practice/dot-dsl/DotDsl.roc create mode 100644 exercises/practice/dot-dsl/dot-dsl-test.roc diff --git a/config.json b/config.json index 052498c8..651d8b60 100644 --- a/config.json +++ b/config.json @@ -563,6 +563,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "dot-dsl", + "name": "DOT DSL", + "uuid": "08cf7c8e-a997-4e66-9af4-97faa5dc5a97", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "eliuds-eggs", "name": "Eliud's Eggs", diff --git a/exercises/practice/dot-dsl/.docs/instructions.append.md b/exercises/practice/dot-dsl/.docs/instructions.append.md new file mode 100644 index 00000000..fed0bd3d --- /dev/null +++ b/exercises/practice/dot-dsl/.docs/instructions.append.md @@ -0,0 +1,32 @@ +# Instructions append + +## Description of DSL + +Here's an example of what the DSL should look like: + +```roc +graph = buildGraph { bgColor: Yellow } [ + node "a" { color: Red }, + node "b" { color: Green }, + node "c" {}, + edge "a" "b" { color: Red, style: Dotted }, + edge "b" "c" { color: Blue }, + edge "a" "c" { color: Green }, +] +``` + +This code should build a graph with a yellow background, and three nodes, "a", "b", and "c", respectively colored red, green, and black (which is the default color). They should be connected by 3 edges of different colors and styles: the edge between "a" and "b" should be red and dotted, and the edges between "b" and "c" and between "a" and "c" should be solid (which is the default style) and green. + +## The `node` and `edge` Functions + +The `node` function should simply create an `AddNode` value with the arguments as payload. This is a DSL command used only by the `buildGraph` function. For example, `node "a" { color: Red }` should return `AddNode "a" { color: Red } `. If an attribute is missing, its default value should be used. For example, `node "c" {}` should return `AddNode "c" { color: Black }`. + +Similarly, the `edge` function should create an `AddEdge` value. For example, `edge "a" "b" {}` should return `AddEdge "a" "b" {color: Default, style: Solid}`. + +These two simple functions make the DSL code much more pleasant to read & write. + +## Objective + +Once you have implemented the `node` and `edge` functions (they should be easy), your main goal is to write the `buildGraph` function: it must go through the list of DSL commands and produce the desired graph, represented as a record `{ bgColor: ..., nodes: ..., edges: ...}`. + +To double the fun, you can optionally try to implement a `toDot` function that converts the graph to a `Str` with using the Dot format! diff --git a/exercises/practice/dot-dsl/.docs/instructions.md b/exercises/practice/dot-dsl/.docs/instructions.md new file mode 100644 index 00000000..b3a63996 --- /dev/null +++ b/exercises/practice/dot-dsl/.docs/instructions.md @@ -0,0 +1,30 @@ +# Instructions + +A [Domain Specific Language (DSL)][dsl] is a small language optimized for a specific domain. +Since a DSL is targeted, it can greatly impact productivity/understanding by allowing the writer to declare _what_ they want rather than _how_. + +One problem area where they are applied are complex customizations/configurations. + +For example the [DOT language][dot-language] allows you to write a textual description of a graph which is then transformed into a picture by one of the [Graphviz][graphviz] tools (such as `dot`). +A simple graph looks like this: + + graph { + graph [bgcolor="yellow"] + a [color="red"] + b [color="blue"] + a -- b [color="green"] + } + +Putting this in a file `example.dot` and running `dot example.dot -T png -o example.png` creates an image `example.png` with red and blue circle connected by a green line on a yellow background. + +Write a Domain Specific Language similar to the Graphviz dot language. + +Our DSL is similar to the Graphviz dot language in that our DSL will be used to create graph data structures. +However, unlike the DOT Language, our DSL will be an internal DSL for use only in our language. + +More information about the difference between internal and external DSLs can be found [here][fowler-dsl]. + +[dsl]: https://en.wikipedia.org/wiki/Domain-specific_language +[dot-language]: https://en.wikipedia.org/wiki/DOT_(graph_description_language) +[graphviz]: https://graphviz.org/ +[fowler-dsl]: https://martinfowler.com/bliki/DomainSpecificLanguage.html diff --git a/exercises/practice/dot-dsl/.meta/Example.roc b/exercises/practice/dot-dsl/.meta/Example.roc new file mode 100644 index 00000000..f483900d --- /dev/null +++ b/exercises/practice/dot-dsl/.meta/Example.roc @@ -0,0 +1,63 @@ +module [buildGraph, node, edge] + +Color : [Black, Red, Green, Blue, Yellow] +Style : [Solid, Dotted] + +Graph : { + bgColor : Color, + nodes : Dict Str { color : Color }, + edges : Dict (Str, Str) { color : Color, style : Style }, +} + +DslCommand : [AddNode Str { color : Color }, AddEdge Str Str { color : Color, style : Style }] + +node : Str, { color ? Color } -> [AddNode Str { color : Color }] +node = \id, { color ? Black } -> + AddNode id { color } + +edge : Str, Str, { color ? Color, style ? Style } -> [AddEdge Str Str { color : Color, style : Style }] +edge = \id1, id2, { color ? Black, style ? Solid } -> + AddEdge id1 id2 { color, style } + +buildGraph : { bgColor ? Color }, List DslCommand -> Graph +buildGraph = \{ bgColor ? Black }, dslCommands -> + dslCommands + |> List.walk { bgColor, nodes: Dict.empty {}, edges: Dict.empty {} } \state, command -> + when command is + AddNode id attributes -> + nodes = state.nodes |> Dict.insert id attributes + { state & nodes } + + AddEdge id1 id2 attributes -> + nodes = + state.nodes + |> Dict.update id1 \maybeAttrs -> + when maybeAttrs is + Ok existingAttrs -> Ok existingAttrs + Err Missing -> Ok { color: Black } + |> Dict.update id2 \maybeAttrs -> + when maybeAttrs is + Ok existingAttrs -> Ok existingAttrs + Err Missing -> Ok { color: Black } + edgeId = if compareStrings id1 id2 == LT then (id1, id2) else (id2, id1) + edges = + state.edges + |> Dict.insert edgeId attributes + { state & nodes, edges } + +## Compare two strings, first by their UTF8 representations, then by length: +## "" < "ABC" < "abc" < "abcdef" +## This is used to sort the users in the JSON outputs +compareStrings : Str, Str -> [LT, EQ, GT] +compareStrings = \string1, string2 -> + b1 = string1 |> Str.toUtf8 + b2 = string2 |> Str.toUtf8 + result = + List.map2 b1 b2 \c1, c2 -> Num.compare c1 c2 + |> List.walkTry (Ok EQ) \_state, cmp -> + when cmp is + EQ -> Ok EQ + res -> Err res + when result is + Ok _cmp -> Num.compare (List.len b1) (List.len b2) + Err res -> res diff --git a/exercises/practice/dot-dsl/.meta/config.json b/exercises/practice/dot-dsl/.meta/config.json new file mode 100644 index 00000000..f1fb0961 --- /dev/null +++ b/exercises/practice/dot-dsl/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "ageron" + ], + "files": { + "solution": [ + "DotDsl.roc" + ], + "test": [ + "dot-dsl-test.roc" + ], + "example": [ + ".meta/Example.roc" + ] + }, + "blurb": "Write a Domain Specific Language similar to the Graphviz dot language.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/DOT_(graph_description_language)" +} diff --git a/exercises/practice/dot-dsl/DotDsl.roc b/exercises/practice/dot-dsl/DotDsl.roc new file mode 100644 index 00000000..870cca34 --- /dev/null +++ b/exercises/practice/dot-dsl/DotDsl.roc @@ -0,0 +1,24 @@ +module [buildGraph, node, edge] + +Color : [Black, Red, Green, Blue, Yellow] +Style : [Solid, Dotted] + +Graph : { + bgColor : Color, + nodes : Dict Str { color : Color }, + edges : Dict (Str, Str) { color : Color, style : Style }, +} + +DslCommand : [AddNode Str { color : Color }, AddEdge Str Str { color : Color, style : Style }] + +node : Str, { color ? Color } -> [AddNode Str { color : Color }] +node = \id, { color ? Black } -> + crash "Please implement the 'node' function" + +edge : Str, Str, { color ? Color, style ? Style } -> [AddEdge Str Str { color : Color, style : Style }] +edge = \id1, id2, { color ? Black, style ? Solid } -> + crash "Please implement the 'edge' function" + +buildGraph : { bgColor ? Color }, List DslCommand -> Graph +buildGraph = \{ bgColor ? Black }, dslCommands -> + crash "Please implement the 'buildGraph' function" diff --git a/exercises/practice/dot-dsl/dot-dsl-test.roc b/exercises/practice/dot-dsl/dot-dsl-test.roc new file mode 100644 index 00000000..bc9cb828 --- /dev/null +++ b/exercises/practice/dot-dsl/dot-dsl-test.roc @@ -0,0 +1,171 @@ +# File last updated on 2024-10-21 +app [main] { + pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br", +} + +main = + Task.ok {} + +import DotDsl exposing [buildGraph, node, edge] + +## The following function is a temporary workaround for Roc issue #7144: +## comparing records containing dicts may return the wrong result depending on +## the internal order of the dict data, so we have to extract the dicts and +## compare them directly. +isEq = \graph1, graph2 -> + (graph1.bgColor == graph2.bgColor) + && (graph1.nodes == graph2.nodes) + && (graph1.edges == graph2.edges) + +# Can create an AddNode command +expect + result = node "a" { color: Red } + expected = AddNode "a" { color: Red } + result == expected + +# Can create an AddNode command with the default color +expect + result = node "b" {} + expected = AddNode "b" { color: Black } + result == expected + +# Can create an AddEdge command +expect + result = edge "a" "b" { color: Red, style: Dotted } + expected = AddEdge "a" "b" { color: Red, style: Dotted } + result == expected + +# Can create an AddEdge command with the default color and style +expect + result = edge "c" "d" {} + expected = AddEdge "c" "d" { color: Black, style: Solid } + result == expected + +# Can create an empty graph +expect + result = buildGraph {} [] + expected = { + bgColor: Black, + nodes: Dict.empty {}, + edges: Dict.empty {}, + } + result |> isEq expected + +# can set the background color +expect + result = buildGraph { bgColor: Red } [] + expected = { + bgColor: Red, + nodes: Dict.empty {}, + edges: Dict.empty {}, + } + result |> isEq expected + +# can create a graph with a few nodes of various colors +expect + result = buildGraph {} [ + node "a" {}, + node "b" { color: Green }, + node "c" { color: Blue }, + ] + expected = { + bgColor: Black, + nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Green }), ("c", { color: Blue })], + edges: Dict.empty {}, + } + result |> isEq expected + +# can create a graph with a two nodes connected by one edge +expect + result = buildGraph {} [ + node "a" {}, + node "b" {}, + edge "a" "b" { color: Yellow, style: Dotted }, + ] + expected = { + bgColor: Black, + nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Black })], + edges: Dict.fromList [(("a", "b"), { color: Yellow, style: Dotted })], + } + result |> isEq expected + +# creating an edge automatically creates the nodes if they don't exist yet +expect + result = buildGraph {} [ + edge "a" "b" {}, + ] + expected = { + bgColor: Black, + nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Black })], + edges: Dict.fromList [(("a", "b"), { color: Black, style: Solid })], + } + result |> isEq expected + +# creating a node after an edge it's connected to is possible +expect + result = buildGraph {} [ + edge "a" "b" { color: Red }, + node "a" { color: Blue }, + ] + expected = { + bgColor: Black, + nodes: Dict.fromList [("a", { color: Blue }), ("b", { color: Black })], + edges: Dict.fromList [(("a", "b"), { color: Red, style: Solid })], + } + result |> isEq expected + +# can create a multicolor triangle +expect + result = buildGraph { bgColor: Yellow } [ + node "a" { color: Red }, + node "b" { color: Green }, + node "c" { color: Blue }, + edge "a" "b" { color: Red, style: Dotted }, + edge "b" "c" { color: Blue }, + edge "a" "c" { color: Green }, + ] + expected = { + bgColor: Yellow, + nodes: Dict.fromList [("a", { color: Red }), ("b", { color: Green }), ("c", { color: Blue })], + edges: Dict.fromList [ + (("a", "b"), { color: Red, style: Dotted }), + (("b", "c"), { color: Blue, style: Solid }), + (("a", "c"), { color: Green, style: Solid }), + ], + } + result |> isEq expected + +# edge ids are sorted alphabetically +expect + result = buildGraph {} [ + edge "b" "a" {}, + edge "c" "b" {}, + edge "c" "a" {}, + ] + expected = { + bgColor: Black, + nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Black }), ("c", { color: Black })], + edges: Dict.fromList [ + (("a", "b"), { color: Black, style: Solid }), + (("b", "c"), { color: Black, style: Solid }), + (("a", "c"), { color: Black, style: Solid }), + ], + } + result |> isEq expected + +# adding the same node or edge multiple times only keeps the last occurrence +expect + result = buildGraph {} [ + node "a" { color: Blue }, + node "a" { color: Red }, + node "a" { color: Green }, + edge "a" "b" { color: Yellow }, + ] + expected = { + bgColor: Black, + nodes: Dict.fromList [("a", { color: Green }), ("b", { color: Black })], + edges: Dict.fromList [ + (("a", "b"), { color: Yellow, style: Solid }), + ], + } + result |> isEq expected From 8611ed1711dc25cd0757649628e7036e9e0c3dc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Thu, 4 Dec 2025 12:12:29 +1300 Subject: [PATCH 2/2] In progress --- bin/generate_tests.py | 6 +- exercises/practice/dot-dsl/.meta/Example.roc | 56 +++++-- exercises/practice/dot-dsl/DotDsl.roc | 41 +++-- exercises/practice/dot-dsl/dot-dsl-test.roc | 166 +++++++------------ 4 files changed, 140 insertions(+), 129 deletions(-) diff --git a/bin/generate_tests.py b/bin/generate_tests.py index e963430d..0d7ec816 100755 --- a/bin/generate_tests.py +++ b/bin/generate_tests.py @@ -17,8 +17,7 @@ import sys -_py = sys.version_info -if _py.major < 3 or (_py.major == 3 and _py.minor < 7): +if sys.version_info < (3, 7): print("Python version must be at least 3.7") sys.exit(1) @@ -167,6 +166,7 @@ def to_roc_tuple(values: Any): list_content = ", ".join([to_roc(v) for v in tuple(values)]) return f"({list_content})" + def to_roc_record(obj: Dict[str, Any]): items = [] for key, value in obj.items(): @@ -568,6 +568,8 @@ def generate( env.tests["error_case"] = error_case result = True for exercise in sorted(Path("exercises/practice").glob(exercise_glob)): + if not exercise.is_dir(): + continue if not generate_exercise(env, spec_path, exercise, check): result = False if stop_on_failure: diff --git a/exercises/practice/dot-dsl/.meta/Example.roc b/exercises/practice/dot-dsl/.meta/Example.roc index f483900d..12a71178 100644 --- a/exercises/practice/dot-dsl/.meta/Example.roc +++ b/exercises/practice/dot-dsl/.meta/Example.roc @@ -1,4 +1,10 @@ -module [buildGraph, node, edge] +module [ + buildGraph, + dslForRedBgColor, + dslForYellowBgColorAndColoredNodesABC, + dslForGreenTriangleBCD, + dslForDottedRedEdgeBC, +] Color : [Black, Red, Green, Blue, Yellow] Style : [Solid, Dotted] @@ -9,21 +15,16 @@ Graph : { edges : Dict (Str, Str) { color : Color, style : Style }, } -DslCommand : [AddNode Str { color : Color }, AddEdge Str Str { color : Color, style : Style }] +DslCommand : [SetBgColor Color, AddNode Str { color : Color }, AddEdge Str Str { color : Color, style : Style }] -node : Str, { color ? Color } -> [AddNode Str { color : Color }] -node = \id, { color ? Black } -> - AddNode id { color } - -edge : Str, Str, { color ? Color, style ? Style } -> [AddEdge Str Str { color : Color, style : Style }] -edge = \id1, id2, { color ? Black, style ? Solid } -> - AddEdge id1 id2 { color, style } - -buildGraph : { bgColor ? Color }, List DslCommand -> Graph -buildGraph = \{ bgColor ? Black }, dslCommands -> +buildGraph : List DslCommand -> Graph +buildGraph = \dslCommands -> dslCommands - |> List.walk { bgColor, nodes: Dict.empty {}, edges: Dict.empty {} } \state, command -> + |> List.walk { bgColor: Black, nodes: Dict.empty {}, edges: Dict.empty {} } \state, command -> when command is + SetBgColor newBgColor -> + { state & bgColor: newBgColor } + AddNode id attributes -> nodes = state.nodes |> Dict.insert id attributes { state & nodes } @@ -45,6 +46,35 @@ buildGraph = \{ bgColor ? Black }, dslCommands -> |> Dict.insert edgeId attributes { state & nodes, edges } +dslForRedBgColor : List DslCommand +dslForRedBgColor = [ + SetBgColor Red, +] + +dslForYellowBgColorAndColoredNodesABC : List DslCommand +dslForYellowBgColorAndColoredNodesABC = [ + SetBgColor Yellow, + AddNode "a" { color: Red }, + AddNode "b" { color: Green }, + AddNode "c" { color: Blue }, +] + +dslForGreenTriangleBCD : List DslCommand +dslForGreenTriangleBCD = [ + SetBgColor Yellow, + AddNode "a" { color: Green }, + AddNode "b" { color: Green }, + AddNode "c" { color: Green }, + AddEdge "a" "b" { color: Green, style: Solid }, + AddEdge "b" "c" { color: Green, style: Solid }, + AddEdge "a" "c" { color: Green, style: Solid }, +] + +dslForDottedRedEdgeBC : List DslCommand +dslForDottedRedEdgeBC = [ + AddEdge "b" "c" { color: Red, style: Dotted }, +] + ## Compare two strings, first by their UTF8 representations, then by length: ## "" < "ABC" < "abc" < "abcdef" ## This is used to sort the users in the JSON outputs diff --git a/exercises/practice/dot-dsl/DotDsl.roc b/exercises/practice/dot-dsl/DotDsl.roc index 870cca34..42c22ba7 100644 --- a/exercises/practice/dot-dsl/DotDsl.roc +++ b/exercises/practice/dot-dsl/DotDsl.roc @@ -1,4 +1,10 @@ -module [buildGraph, node, edge] +module [ + buildGraph, + dslForRedBgColor, + dslForYellowBgColorAndColoredNodesABC, + dslForGreenTriangleBCD, + dslForDottedRedEdgeBC, +] Color : [Black, Red, Green, Blue, Yellow] Style : [Solid, Dotted] @@ -9,16 +15,29 @@ Graph : { edges : Dict (Str, Str) { color : Color, style : Style }, } -DslCommand : [AddNode Str { color : Color }, AddEdge Str Str { color : Color, style : Style }] +# TODO: change this DslCommand type however you need +DslCommand : [DslCommandTodo1, DslCommandTodo2, DslCommandTodo3] -node : Str, { color ? Color } -> [AddNode Str { color : Color }] -node = \id, { color ? Black } -> - crash "Please implement the 'node' function" +buildGraph : List DslCommand -> Graph +buildGraph = \dslCommands -> + crash "Please implement the 'buildGraph' function" -edge : Str, Str, { color ? Color, style ? Style } -> [AddEdge Str Str { color : Color, style : Style }] -edge = \id1, id2, { color ? Black, style ? Solid } -> - crash "Please implement the 'edge' function" +dslForRedBgColor : List DslCommand +dslForRedBgColor = [ + # TODO: define this list of DSL commands to get the desired effect +] -buildGraph : { bgColor ? Color }, List DslCommand -> Graph -buildGraph = \{ bgColor ? Black }, dslCommands -> - crash "Please implement the 'buildGraph' function" +dslForYellowBgColorAndColoredNodesABC : List DslCommand +dslForYellowBgColorAndColoredNodesABC = [ + # TODO: define this list of DSL commands to get the desired effect +] + +dslForGreenTriangleBCD : List DslCommand +dslForGreenTriangleBCD = [ + # TODO: define this list of DSL commands to get the desired effect +] + +dslForDottedRedEdgeBC : List DslCommand +dslForDottedRedEdgeBC = [ + # TODO: define this list of DSL commands to get the desired effect +] diff --git a/exercises/practice/dot-dsl/dot-dsl-test.roc b/exercises/practice/dot-dsl/dot-dsl-test.roc index bc9cb828..631fb522 100644 --- a/exercises/practice/dot-dsl/dot-dsl-test.roc +++ b/exercises/practice/dot-dsl/dot-dsl-test.roc @@ -6,7 +6,13 @@ app [main] { main = Task.ok {} -import DotDsl exposing [buildGraph, node, edge] +import DotDsl exposing [ + buildGraph, + dslForRedBgColor, + dslForYellowBgColorAndColoredNodesABC, + dslForGreenTriangleBCD, + dslForDottedRedEdgeBC, +] ## The following function is a temporary workaround for Roc issue #7144: ## comparing records containing dicts may return the wrong result depending on @@ -17,33 +23,9 @@ isEq = \graph1, graph2 -> && (graph1.nodes == graph2.nodes) && (graph1.edges == graph2.edges) -# Can create an AddNode command -expect - result = node "a" { color: Red } - expected = AddNode "a" { color: Red } - result == expected - -# Can create an AddNode command with the default color -expect - result = node "b" {} - expected = AddNode "b" { color: Black } - result == expected - -# Can create an AddEdge command -expect - result = edge "a" "b" { color: Red, style: Dotted } - expected = AddEdge "a" "b" { color: Red, style: Dotted } - result == expected - -# Can create an AddEdge command with the default color and style -expect - result = edge "c" "d" {} - expected = AddEdge "c" "d" { color: Black, style: Solid } - result == expected - # Can create an empty graph expect - result = buildGraph {} [] + result = buildGraph [] expected = { bgColor: Black, nodes: Dict.empty {}, @@ -53,7 +35,7 @@ expect # can set the background color expect - result = buildGraph { bgColor: Red } [] + result = buildGraph dslForRedBgColor expected = { bgColor: Red, nodes: Dict.empty {}, @@ -61,111 +43,89 @@ expect } result |> isEq expected -# can create a graph with a few nodes of various colors +# can create a graph with yellow background and three separate nodes of various colors expect - result = buildGraph {} [ - node "a" {}, - node "b" { color: Green }, - node "c" { color: Blue }, - ] + result = buildGraph dslForYellowBgColorAndColoredNodesABC expected = { - bgColor: Black, - nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Green }), ("c", { color: Blue })], + bgColor: Yellow, + nodes: Dict.fromList [ + ("a", { color: Red }), + ("b", { color: Green }), + ("c", { color: Blue }), + ], edges: Dict.empty {}, } result |> isEq expected -# can create a graph with a two nodes connected by one edge +# can create a graph of a triangle BCD with green nodes and edges (and with the default black background) expect - result = buildGraph {} [ - node "a" {}, - node "b" {}, - edge "a" "b" { color: Yellow, style: Dotted }, - ] + result = buildGraph dslForGreenTriangleBCD expected = { bgColor: Black, - nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Black })], - edges: Dict.fromList [(("a", "b"), { color: Yellow, style: Dotted })], - } - result |> isEq expected - -# creating an edge automatically creates the nodes if they don't exist yet -expect - result = buildGraph {} [ - edge "a" "b" {}, - ] - expected = { - bgColor: Black, - nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Black })], - edges: Dict.fromList [(("a", "b"), { color: Black, style: Solid })], + nodes: Dict.fromList [("b", { color: Green }), ("c", { color: Green }), ("d", { color: Green })], + edges: Dict.fromList [ + (("b", "c"), { color: Green, style: Solid }), + (("b", "d"), { color: Green, style: Solid }), + (("c", "d"), { color: Green, style: Solid }), + ], } result |> isEq expected -# creating a node after an edge it's connected to is possible +# creating an edge automatically creates the connected nodes if needed, black by default expect - result = buildGraph {} [ - edge "a" "b" { color: Red }, - node "a" { color: Blue }, - ] + result = buildGraph dslForDottedRedEdgeBC expected = { bgColor: Black, - nodes: Dict.fromList [("a", { color: Blue }), ("b", { color: Black })], - edges: Dict.fromList [(("a", "b"), { color: Red, style: Solid })], + # default to black background + nodes: Dict.fromList [("b", { color: Black }), ("c", { color: Black })], + edges: Dict.fromList [(("b", "c"), { color: Red, style: Dotted })], } result |> isEq expected -# can create a multicolor triangle +# DSL commands can be chained, and existing nodes and edges get updated in the given order expect - result = buildGraph { bgColor: Yellow } [ - node "a" { color: Red }, - node "b" { color: Green }, - node "c" { color: Blue }, - edge "a" "b" { color: Red, style: Dotted }, - edge "b" "c" { color: Blue }, - edge "a" "c" { color: Green }, - ] + allCommands = + dslForYellowBgColorAndColoredNodesABC + |> List.concat dslForGreenTriangleBCD + |> List.concat dslForDottedRedEdgeBC + |> List.concat dslForRedBgColor + result = buildGraph allCommands expected = { - bgColor: Yellow, - nodes: Dict.fromList [("a", { color: Red }), ("b", { color: Green }), ("c", { color: Blue })], - edges: Dict.fromList [ - (("a", "b"), { color: Red, style: Dotted }), - (("b", "c"), { color: Blue, style: Solid }), - (("a", "c"), { color: Green, style: Solid }), + bgColor: Red, + nodes: Dict.fromList [ + ("a", { color: Red }), + ("b", { color: Green }), + ("c", { color: Green }), + ("d", { color: Green }), ], - } - result |> isEq expected - -# edge ids are sorted alphabetically -expect - result = buildGraph {} [ - edge "b" "a" {}, - edge "c" "b" {}, - edge "c" "a" {}, - ] - expected = { - bgColor: Black, - nodes: Dict.fromList [("a", { color: Black }), ("b", { color: Black }), ("c", { color: Black })], edges: Dict.fromList [ - (("a", "b"), { color: Black, style: Solid }), - (("b", "c"), { color: Black, style: Solid }), - (("a", "c"), { color: Black, style: Solid }), + (("b", "c"), { color: Red, style: Dotted }), + (("b", "d"), { color: Green, style: Solid }), + (("c", "d"), { color: Green, style: Solid }), ], } result |> isEq expected -# adding the same node or edge multiple times only keeps the last occurrence +# Running the same DSL commands in a different order changes the result expect - result = buildGraph {} [ - node "a" { color: Blue }, - node "a" { color: Red }, - node "a" { color: Green }, - edge "a" "b" { color: Yellow }, - ] + allCommands = + dslForDottedRedEdgeBC + |> List.concat dslForRedBgColor + |> List.concat dslForGreenTriangleBCD + |> List.concat dslForYellowBgColorAndColoredNodesABC + result = buildGraph allCommands expected = { - bgColor: Black, - nodes: Dict.fromList [("a", { color: Green }), ("b", { color: Black })], + bgColor: Yellow, + nodes: Dict.fromList [ + ("a", { color: Red }), + ("b", { color: Green }), + ("c", { color: Blue }), + ("d", { color: Green }), + ], edges: Dict.fromList [ - (("a", "b"), { color: Yellow, style: Solid }), + (("b", "c"), { color: Green, style: Solid }), + (("b", "d"), { color: Green, style: Solid }), + (("c", "d"), { color: Green, style: Solid }), ], } result |> isEq expected