more test

This commit is contained in:
Alexander Wang 2023-07-10 22:22:04 -07:00
parent 0347dff7a4
commit 08cc907f07
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 417 additions and 2 deletions

View file

@ -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")
},
},

View file

@ -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")
}
}
}

View file

@ -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)

392
testdata/d2ir/TestCompile/imports/vars/1.exp.json generated vendored Normal file
View file

@ -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
}