From 07b3d5e4648d2e0920071d6f1663751ce24d87d3 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 30 Mar 2025 08:08:10 -0600 Subject: [PATCH] add test for and ampersand --- d2compiler/compile_test.go | 31 + .../TestCompile2/globs/and-filter.exp.json | 750 ++++++++++++++++++ 2 files changed, 781 insertions(+) create mode 100644 testdata/d2compiler/TestCompile2/globs/and-filter.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 0871d4b33..5a56d1e89 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -5687,6 +5687,37 @@ c assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Fill) }, }, + { + name: "and-filter", + run: func(t *testing.T) { + g, _ := assertCompile(t, ` +*: { + &shape: person + &connected: true + style.fill: red +} +(** -> **)[*]: { + &src: a + &dst: c + style.stroke: yellow +} +a -> b +a.shape: person +a -> c +`, ``) + assert.Equal(t, "a", g.Objects[0].ID) + assert.Equal(t, "red", g.Objects[0].Attributes.Style.Fill.Value) + assert.Equal(t, "b", g.Objects[1].ID) + assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[1].Attributes.Style.Fill) + assert.Equal(t, "c", g.Objects[2].ID) + assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Fill) + + assert.Equal(t, "(a -> b)[0]", g.Edges[0].AbsID()) + assert.Equal(t, (*d2graph.Scalar)(nil), g.Edges[0].Attributes.Style.Stroke) + assert.Equal(t, "(a -> c)[0]", g.Edges[1].AbsID()) + assert.Equal(t, "yellow", g.Edges[1].Attributes.Style.Stroke.Value) + }, + }, { name: "glob-filter", run: func(t *testing.T) { diff --git a/testdata/d2compiler/TestCompile2/globs/and-filter.exp.json b/testdata/d2compiler/TestCompile2/globs/and-filter.exp.json new file mode 100644 index 000000000..84ab92222 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/globs/and-filter.exp.json @@ -0,0 +1,750 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,0:0:0-14:0:154", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,1:0:1-5:1:61", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,1:0:1-1:1:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,1:0:1-1:1:2", + "value": [ + { + "string": "*", + "raw_string": "*" + } + ], + "pattern": [ + "*" + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,1:3:4-5:1:61", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,2:2:8-2:16:22", + "ampersand": true, + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,2:3:9-2:8:14", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,2:3:9-2:8:14", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,2:10:16-2:16:22", + "value": [ + { + "string": "person", + "raw_string": "person" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,3:2:25-3:18:41", + "ampersand": true, + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,3:3:26-3:12:35", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,3:3:26-3:12:35", + "value": [ + { + "string": "connected", + "raw_string": "connected" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "boolean": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,3:14:37-3:18:41", + "value": true + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,4:2:44-4:17:59", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,4:2:44-4:12:54", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,4:2:44-4:7:49", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,4:8:50-4:12:54", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,4:14:56-4:17:59", + "value": [ + { + "string": "red", + "raw_string": "red" + } + ] + } + } + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:0:62-10:1:123", + "edges": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:1:63-6:9:71", + "src": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:1:63-6:3:65", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:1:63-6:3:65", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:7:69-6:9:71", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:7:69-6:9:71", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "edge_index": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:10:72-6:13:75", + "int": null, + "glob": true + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,6:15:77-10:1:123", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,7:2:81-7:9:88", + "ampersand": true, + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,7:3:82-7:6:85", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,7:3:82-7:6:85", + "value": [ + { + "string": "src", + "raw_string": "src" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,7:8:87-7:9:88", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,8:2:91-8:9:98", + "ampersand": true, + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,8:3:92-8:6:95", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,8:3:92-8:6:95", + "value": [ + { + "string": "dst", + "raw_string": "dst" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,8:8:97-8:9:98", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,9:2:101-9:22:121", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,9:2:101-9:14:113", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,9:2:101-9:7:106", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,9:8:107-9:14:113", + "value": [ + { + "string": "stroke", + "raw_string": "stroke" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,9:16:115-9:22:121", + "value": [ + { + "string": "yellow", + "raw_string": "yellow" + } + ] + } + } + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:0:124-11:6:130", + "edges": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:0:124-11:6:130", + "src": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:0:124-11:1:125", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:0:124-11:1:125", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:5:129-11:6:130", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:5:129-11:6:130", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:0:131-12:15:146", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:0:131-12:7:138", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:0:131-12:1:132", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:2:133-12:7:138", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:9:140-12:15:146", + "value": [ + { + "string": "person", + "raw_string": "person" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:0:147-13:6:153", + "edges": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:0:147-13:6:153", + "src": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:0:147-13:1:148", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:0:147-13:1:148", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:5:152-13:6:153", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:5:152-13:6:153", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": [ + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + }, + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": { + "stroke": { + "value": "yellow" + } + }, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ], + "objects": [ + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:0:124-11:1:125", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:0:124-11:1:125", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:0:131-12:7:138", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:0:131-12:1:132", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,12:2:133-12:7:138", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:0:147-13:1:148", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:0:147-13:1:148", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": { + "fill": { + "value": "red" + } + }, + "near_key": null, + "shape": { + "value": "person" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:5:129-11:6:130", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,11:5:129-11:6:130", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "c", + "id_val": "c", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:5:152-13:6:153", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/and-filter.d2,13:5:152-13:6:153", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "c" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +}