From 08cc907f07fe81fee5e974042553ffc62e261276 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 10 Jul 2023 22:22:04 -0700 Subject: [PATCH] more test --- d2compiler/compile_test.go | 11 + d2ir/compile.go | 5 +- d2ir/import_test.go | 11 + .../d2ir/TestCompile/imports/vars/1.exp.json | 392 ++++++++++++++++++ 4 files changed, 417 insertions(+), 2 deletions(-) create mode 100644 testdata/d2ir/TestCompile/imports/vars/1.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index f6518d0fa..7638e9d22 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3458,6 +3458,17 @@ vars: { x: hey } hi: ${z} +`, "d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable z") + }, + }, + { + name: "key", + run: func(t *testing.T) { + assertCompile(t, ` +vars: { + x: hey +} +${x} `, "d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable z") }, }, diff --git a/d2ir/compile.go b/d2ir/compile.go index bf5d91d8b..bc5450f1e 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -170,6 +170,9 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) { } for _, n := range ast.Nodes { switch { + case n.Substitution != nil: + println("\033[1;31m--- DEBUG:", "=======================", "\033[m") + c.errorf(n.Substitution, "only values can use substitutions") case n.MapKey != nil: c.compileKey(&RefContext{ Key: n.MapKey, @@ -196,8 +199,6 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) { } } } - case n.Substitution != nil: - panic("TODO") } } } diff --git a/d2ir/import_test.go b/d2ir/import_test.go index 38b3a3a2c..c04cb6a25 100644 --- a/d2ir/import_test.go +++ b/d2ir/import_test.go @@ -138,6 +138,17 @@ label: meow`, assertQuery(t, m, 0, 0, nil, "q.jon") }, }, + { + name: "vars/1", + run: func(t testing.TB) { + m, err := compileFS(t, "index.d2", map[string]string{ + "index.d2": "vars: { ...@x }; q: ${meow}", + "x.d2": "meow: var replaced", + }) + assert.Success(t, err) + assertQuery(t, m, 0, 0, "var replaced", "q") + }, + }, } runa(t, tca) diff --git a/testdata/d2ir/TestCompile/imports/vars/1.exp.json b/testdata/d2ir/TestCompile/imports/vars/1.exp.json new file mode 100644 index 000000000..de94a458d --- /dev/null +++ b/testdata/d2ir/TestCompile/imports/vars/1.exp.json @@ -0,0 +1,392 @@ +{ + "fields": [ + { + "name": "vars", + "composite": { + "fields": [ + { + "name": "meow", + "primary": { + "value": { + "range": "x.d2,0:6:6-0:18:18", + "value": [ + { + "string": "var replaced", + "raw_string": "var replaced" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,0:0:0-0:4:4", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + }, + "key_path": { + "range": "x.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:4:4", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,0:0:0-0:18:18", + "key": { + "range": "x.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:4:4", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,0:6:6-0:18:18", + "value": [ + { + "string": "var replaced", + "raw_string": "var replaced" + } + ] + } + } + } + } + }, + { + "string": { + "range": "x.d2,0:0:0-0:4:4", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + }, + "key_path": { + "range": "x.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:4:4", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,0:0:0-0:18:18", + "key": { + "range": "x.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:4:4", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,0:6:6-0:18:18", + "value": [ + { + "string": "var replaced", + "raw_string": "var replaced" + } + ] + } + } + } + } + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "index.d2,0:0:0-0:4:4", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:4:4", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:15:15", + "key": { + "range": "index.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:4:4", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "index.d2,0:6:6-0:15:15", + "nodes": [ + { + "import": { + "range": "index.d2,0:8:8-0:14:14", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:12:12-0:13:13", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + } + } + ] + } + } + } + } + }, + { + "string": { + "range": "index.d2,0:0:0-0:4:4", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:4:4", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:15:15", + "key": { + "range": "index.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:4:4", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "index.d2,0:6:6-0:15:15", + "nodes": [ + { + "import": { + "range": "index.d2,0:8:8-0:14:14", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:12:12-0:13:13", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + } + } + ] + } + } + } + } + } + ] + }, + { + "name": "q", + "primary": { + "value": { + "range": "x.d2,0:6:6-0:18:18", + "value": [ + { + "string": "var replaced", + "raw_string": "var replaced" + } + ] + } + }, + "references": [ + { + "string": { + "range": "index.d2,0:17:17-0:18:18", + "value": [ + { + "string": "q", + "raw_string": "q" + } + ] + }, + "key_path": { + "range": "index.d2,0:17:17-0:18:18", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:17:17-0:18:18", + "value": [ + { + "string": "q", + "raw_string": "q" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:17:17-0:27:27", + "key": { + "range": "index.d2,0:17:17-0:18:18", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:17:17-0:18:18", + "value": [ + { + "string": "q", + "raw_string": "q" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "substitution": { + "range": "index.d2,0:20:20-0:27:27", + "spread": false, + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:22:22-0:26:26", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + ] + } + } + } + } + } + ] + } + ], + "edges": null +}