d2/d2ir/compile_test.go

748 lines
17 KiB
Go
Raw Normal View History

package d2ir_test
import (
"fmt"
"math/big"
"path/filepath"
"strings"
"testing"
2023-01-11 19:05:50 +00:00
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff"
"oss.terrastruct.com/util-go/mapfs"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2ir"
"oss.terrastruct.com/d2/d2parser"
)
2023-01-16 11:52:37 +00:00
func TestCompile(t *testing.T) {
t.Parallel()
2023-01-18 10:06:44 +00:00
t.Run("fields", testCompileFields)
2023-02-06 21:32:08 +00:00
t.Run("classes", testCompileClasses)
2023-01-18 10:06:44 +00:00
t.Run("edges", testCompileEdges)
2023-01-18 15:15:16 +00:00
t.Run("layers", testCompileLayers)
t.Run("scenarios", testCompileScenarios)
t.Run("steps", testCompileSteps)
t.Run("imports", testCompileImports)
t.Run("patterns", testCompilePatterns)
2023-07-29 22:09:29 +00:00
t.Run("filters", testCompileFilters)
2024-08-17 00:48:06 +00:00
t.Run("vars", testCompileVars)
}
2023-01-16 12:48:45 +00:00
type testCase struct {
name string
2023-01-18 01:39:31 +00:00
run func(testing.TB)
}
2023-01-11 19:05:50 +00:00
func runa(t *testing.T, tca []testCase) {
for _, tc := range tca {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
2023-01-18 01:39:31 +00:00
tc.run(t)
})
}
}
2023-01-18 11:51:16 +00:00
func compile(t testing.TB, text string) (*d2ir.Map, error) {
2023-01-11 19:05:50 +00:00
t.Helper()
2023-01-18 01:39:31 +00:00
d2Path := fmt.Sprintf("%v.d2", t.Name())
return compileFS(t, d2Path, map[string]string{d2Path: text})
}
func compileFS(t testing.TB, path string, mfs map[string]string) (*d2ir.Map, error) {
t.Helper()
ast, err := d2parser.Parse(path, strings.NewReader(mfs[path]), nil)
2023-06-06 21:01:32 +00:00
if err != nil {
return nil, err
}
fs, err := mapfs.New(mfs)
assert.Success(t, err)
2023-06-06 21:01:32 +00:00
t.Cleanup(func() {
err = fs.Close()
assert.Success(t, err)
})
2023-12-25 05:48:14 +00:00
m, _, err := d2ir.Compile(ast, &d2ir.CompileOptions{
FS: fs,
})
if err != nil {
2023-01-18 01:39:31 +00:00
return nil, err
}
2023-01-11 19:05:50 +00:00
2023-01-18 11:51:16 +00:00
err = diff.TestdataJSON(filepath.Join("..", "testdata", "d2ir", t.Name()), m)
2023-01-18 01:39:31 +00:00
if err != nil {
return nil, err
}
2023-01-18 11:51:16 +00:00
return m, nil
}
2023-01-18 15:20:21 +00:00
func assertQuery(t testing.TB, n d2ir.Node, nfields, nedges int, primary interface{}, idStr string) d2ir.Node {
t.Helper()
2023-01-18 15:15:16 +00:00
m := n.Map()
p := n.Primary()
2023-07-29 23:54:44 +00:00
var na []d2ir.Node
2023-01-18 15:15:16 +00:00
if idStr != "" {
var err error
2023-07-29 23:54:44 +00:00
na, err = m.QueryAll(idStr)
2023-01-18 15:15:16 +00:00
assert.Success(t, err)
assert.NotEqual(t, n, nil)
2023-07-29 23:54:44 +00:00
} else {
na = append(na, n)
}
2023-07-29 23:54:44 +00:00
for _, n := range na {
m = n.Map()
p = n.Primary()
assert.Equal(t, nfields, m.FieldCountRecursive())
assert.Equal(t, nedges, m.EdgeCountRecursive())
if !makeScalar(p).Equal(makeScalar(primary)) {
t.Fatalf("expected primary %#v but got %s", primary, p)
}
}
if len(na) == 0 {
2023-08-20 04:20:32 +00:00
t.Fatalf("query didn't match anything")
}
2023-07-29 23:54:44 +00:00
return na[0]
}
func makeScalar(v interface{}) *d2ir.Scalar {
s := &d2ir.Scalar{}
switch v := v.(type) {
2023-01-11 21:20:05 +00:00
case *d2ir.Scalar:
if v == nil {
s.Value = &d2ast.Null{}
return s
}
return v
case bool:
s.Value = &d2ast.Boolean{
Value: v,
}
case float64:
bv := &big.Rat{}
bv.SetFloat64(v)
s.Value = &d2ast.Number{
2023-02-06 21:32:08 +00:00
Raw: fmt.Sprint(v),
Value: bv,
}
case int:
s.Value = &d2ast.Number{
2023-02-06 21:32:08 +00:00
Raw: fmt.Sprint(v),
Value: big.NewRat(int64(v), 1),
}
case string:
s.Value = d2ast.FlatDoubleQuotedString(v)
default:
if v != nil {
panic(fmt.Sprintf("d2ir: unexpected type to makeScalar: %#v", v))
}
s.Value = &d2ast.Null{}
}
return s
}
2023-01-16 12:48:45 +00:00
2023-01-18 10:06:44 +00:00
func testCompileFields(t *testing.T) {
2023-01-16 12:48:45 +00:00
t.Parallel()
tca := []testCase{
{
name: "root",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, nil, "")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, nil, "x")
2023-01-16 12:48:45 +00:00
},
},
{
name: "label",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x: yes`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, nil, "")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, "yes", "x")
2023-01-16 12:48:45 +00:00
},
},
{
name: "nested",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x.y: yes`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 2, 0, nil, "")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, nil, "x")
assertQuery(t, m, 0, 0, "yes", "x.y")
2023-01-16 12:48:45 +00:00
},
},
{
name: "array",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x: [1;2;3;4]`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, nil, "")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
f := assertQuery(t, m, 0, 0, nil, "x").(*d2ir.Field)
assert.String(t, `[1; 2; 3; 4]`, f.Composite.String())
2023-01-16 12:48:45 +00:00
},
},
2024-11-24 02:40:36 +00:00
{
name: "quoted",
run: func(t testing.TB) {
m, err := compile(t, `my_table: {
shape: sql_table
width: 200
height: 200
"shape": string
"icon": string
"width": int
"height": int
}`)
assert.Success(t, err)
assertQuery(t, m, 0, 0, "sql_table", "my_table.shape")
assertQuery(t, m, 0, 0, "string", `my_table."shape"`)
},
},
2023-01-16 12:48:45 +00:00
{
2023-01-18 15:15:16 +00:00
name: "null",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 15:15:16 +00:00
m, err := compile(t, `pq: pq
pq: null`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-06-27 20:36:33 +00:00
assertQuery(t, m, 0, 0, nil, "")
2023-01-16 12:48:45 +00:00
},
},
}
runa(t, tca)
2023-01-18 15:15:16 +00:00
t.Run("primary", func(t *testing.T) {
t.Parallel()
tca := []testCase{
{
name: "root",
run: func(t testing.TB) {
m, err := compile(t, `x: yes { pqrs }`)
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 2, 0, nil, "")
2023-01-18 15:15:16 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, "yes", "x")
assertQuery(t, m, 0, 0, nil, "x.pqrs")
2023-01-18 15:15:16 +00:00
},
},
{
name: "nested",
run: func(t testing.TB) {
m, err := compile(t, `x.y: yes { pqrs }`)
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 3, 0, nil, "")
2023-01-18 15:15:16 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 2, 0, nil, "x")
assertQuery(t, m, 1, 0, "yes", "x.y")
assertQuery(t, m, 0, 0, nil, "x.y.pqrs")
2023-01-18 15:15:16 +00:00
},
},
}
runa(t, tca)
})
2023-01-16 12:48:45 +00:00
}
2023-01-18 10:06:44 +00:00
func testCompileEdges(t *testing.T) {
2023-01-16 12:48:45 +00:00
t.Parallel()
tca := []testCase{
{
2023-01-17 12:44:14 +00:00
name: "root",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x -> y`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 2, 1, nil, "")
assertQuery(t, m, 0, 0, nil, `(x -> y)[0]`)
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, nil, "x")
assertQuery(t, m, 0, 0, nil, "y")
2023-01-16 12:48:45 +00:00
},
},
{
name: "nested",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x.y -> z.p`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 4, 1, nil, "")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, nil, "x")
assertQuery(t, m, 0, 0, nil, "x.y")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 1, 0, nil, "z")
assertQuery(t, m, 0, 0, nil, "z.p")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, nil, "(x.y -> z.p)[0]")
2023-01-16 12:48:45 +00:00
},
},
{
name: "underscore",
2023-01-18 01:39:31 +00:00
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `p: { _.x -> z }`)
2023-01-16 12:48:45 +00:00
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 3, 1, nil, "")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, nil, "x")
assertQuery(t, m, 1, 0, nil, "p")
2023-01-16 12:48:45 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, nil, "(x -> p.z)[0]")
2023-01-18 15:15:16 +00:00
},
},
{
name: "chain",
run: func(t testing.TB) {
m, err := compile(t, `a -> b -> c -> d`)
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 4, 3, nil, "")
assertQuery(t, m, 0, 0, nil, "a")
assertQuery(t, m, 0, 0, nil, "b")
assertQuery(t, m, 0, 0, nil, "c")
assertQuery(t, m, 0, 0, nil, "d")
assertQuery(t, m, 0, 0, nil, "(a -> b)[0]")
assertQuery(t, m, 0, 0, nil, "(b -> c)[0]")
assertQuery(t, m, 0, 0, nil, "(c -> d)[0]")
2023-01-18 10:06:44 +00:00
},
},
}
runa(t, tca)
2023-01-18 15:15:16 +00:00
t.Run("errs", func(t *testing.T) {
t.Parallel()
tca := []testCase{
{
name: "bad_edge",
run: func(t testing.TB) {
_, err := compile(t, `(x -> y): { p -> q }`)
assert.ErrorString(t, err, `TestCompile/edges/errs/bad_edge.d2:1:13: cannot create edge inside edge`)
},
},
}
runa(t, tca)
})
2023-01-18 10:06:44 +00:00
}
func testCompileLayers(t *testing.T) {
t.Parallel()
2023-01-18 15:15:16 +00:00
tca := []testCase{
{
name: "root",
run: func(t testing.TB) {
m, err := compile(t, `x -> y
layers: {
bingo: { p.q.z }
}`)
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 7, 1, nil, "")
assertQuery(t, m, 0, 0, nil, `(x -> y)[0]`)
2023-01-18 15:15:16 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 0, 0, nil, "x")
assertQuery(t, m, 0, 0, nil, "y")
2023-01-18 15:15:16 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 3, 0, nil, "layers.bingo")
2023-01-18 15:15:16 +00:00
},
},
}
runa(t, tca)
2023-01-18 12:32:12 +00:00
t.Run("errs", func(t *testing.T) {
2023-01-18 15:15:16 +00:00
t.Parallel()
2023-01-18 12:32:12 +00:00
tca := []testCase{
{
2023-01-28 01:19:12 +00:00
name: "1/bad_edge",
2023-01-18 12:32:12 +00:00
run: func(t testing.TB) {
_, err := compile(t, `layers.x -> layers.y`)
2023-01-28 01:19:12 +00:00
assert.ErrorString(t, err, `TestCompile/layers/errs/1/bad_edge.d2:1:1: cannot create edges between boards`)
2023-01-18 12:32:12 +00:00
},
},
{
2023-01-28 01:19:12 +00:00
name: "2/bad_edge",
2023-01-18 12:32:12 +00:00
run: func(t testing.TB) {
_, err := compile(t, `layers -> scenarios`)
2023-01-28 01:19:12 +00:00
assert.ErrorString(t, err, `TestCompile/layers/errs/2/bad_edge.d2:1:1: edge with board keyword alone doesn't make sense`)
2023-01-18 12:32:12 +00:00
},
},
2023-01-18 12:42:34 +00:00
{
2023-01-28 01:19:12 +00:00
name: "3/bad_edge",
2023-01-18 12:42:34 +00:00
run: func(t testing.TB) {
_, err := compile(t, `layers.x.y -> steps.z.p`)
2023-01-28 01:19:12 +00:00
assert.ErrorString(t, err, `TestCompile/layers/errs/3/bad_edge.d2:1:1: cannot create edges between boards`)
},
},
{
name: "4/good_edge",
run: func(t testing.TB) {
_, err := compile(t, `layers.x.y -> layers.x.y`)
assert.Success(t, err)
2023-01-18 12:42:34 +00:00
},
},
2023-01-18 12:32:12 +00:00
}
runa(t, tca)
})
2023-01-18 15:15:16 +00:00
}
func testCompileScenarios(t *testing.T) {
t.Parallel()
2023-01-18 10:06:44 +00:00
tca := []testCase{
{
name: "root",
run: func(t testing.TB) {
2023-01-18 11:51:16 +00:00
m, err := compile(t, `x -> y
2023-01-18 15:15:16 +00:00
scenarios: {
2023-01-18 10:06:44 +00:00
bingo: { p.q.z }
2023-01-18 15:15:16 +00:00
nuclear: { quiche }
2023-01-18 10:06:44 +00:00
}`)
assert.Success(t, err)
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 13, 3, nil, "")
assertQuery(t, m, 0, 0, nil, "x")
assertQuery(t, m, 0, 0, nil, "y")
assertQuery(t, m, 0, 0, nil, `(x -> y)[0]`)
assertQuery(t, m, 5, 1, nil, "scenarios.bingo")
assertQuery(t, m, 0, 0, nil, "scenarios.bingo.x")
assertQuery(t, m, 0, 0, nil, "scenarios.bingo.y")
assertQuery(t, m, 0, 0, nil, `scenarios.bingo.(x -> y)[0]`)
assertQuery(t, m, 2, 0, nil, "scenarios.bingo.p")
assertQuery(t, m, 1, 0, nil, "scenarios.bingo.p.q")
assertQuery(t, m, 0, 0, nil, "scenarios.bingo.p.q.z")
assertQuery(t, m, 3, 1, nil, "scenarios.nuclear")
assertQuery(t, m, 0, 0, nil, "scenarios.nuclear.x")
assertQuery(t, m, 0, 0, nil, "scenarios.nuclear.y")
assertQuery(t, m, 0, 0, nil, `scenarios.nuclear.(x -> y)[0]`)
assertQuery(t, m, 0, 0, nil, "scenarios.nuclear.quiche")
2023-01-18 15:15:16 +00:00
},
},
2023-03-25 19:06:06 +00:00
{
name: "edge",
run: func(t testing.TB) {
m, err := compile(t, `a -> b
scenarios: {
1: {
(a -> b)[0].style.opacity: 0.1
}
}`)
assert.Success(t, err)
assertQuery(t, m, 8, 2, nil, "")
2023-03-25 19:06:06 +00:00
assertQuery(t, m, 0, 0, nil, "(a -> b)[0]")
},
},
{
name: "multiple-scenario-map",
run: func(t testing.TB) {
m, err := compile(t, `a -> b: { style.opacity: 0.3 }
scenarios: {
1: {
(a -> b)[0].style.opacity: 0.1
}
1: {
z
}
}`)
assert.Success(t, err)
assertQuery(t, m, 11, 2, nil, "")
assertQuery(t, m, 0, 0, 0.1, "scenarios.1.(a -> b)[0].style.opacity")
},
},
2023-01-18 15:15:16 +00:00
}
runa(t, tca)
}
2023-01-18 10:06:44 +00:00
2023-01-18 15:15:16 +00:00
func testCompileSteps(t *testing.T) {
t.Parallel()
tca := []testCase{
{
name: "root",
run: func(t testing.TB) {
m, err := compile(t, `x -> y
steps: {
bingo: { p.q.z }
nuclear: { quiche }
}`)
assert.Success(t, err)
2023-01-18 10:06:44 +00:00
2023-01-18 15:20:21 +00:00
assertQuery(t, m, 16, 3, nil, "")
assertQuery(t, m, 0, 0, nil, "x")
assertQuery(t, m, 0, 0, nil, "y")
assertQuery(t, m, 0, 0, nil, `(x -> y)[0]`)
assertQuery(t, m, 5, 1, nil, "steps.bingo")
assertQuery(t, m, 0, 0, nil, "steps.bingo.x")
assertQuery(t, m, 0, 0, nil, "steps.bingo.y")
assertQuery(t, m, 0, 0, nil, `steps.bingo.(x -> y)[0]`)
assertQuery(t, m, 2, 0, nil, "steps.bingo.p")
assertQuery(t, m, 1, 0, nil, "steps.bingo.p.q")
assertQuery(t, m, 0, 0, nil, "steps.bingo.p.q.z")
assertQuery(t, m, 6, 1, nil, "steps.nuclear")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.x")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.y")
assertQuery(t, m, 0, 0, nil, `steps.nuclear.(x -> y)[0]`)
assertQuery(t, m, 2, 0, nil, "steps.nuclear.p")
assertQuery(t, m, 1, 0, nil, "steps.nuclear.p.q")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.p.q.z")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.quiche")
2023-01-16 12:48:45 +00:00
},
},
2023-02-22 22:59:05 +00:00
{
name: "steps_panic",
run: func(t testing.TB) {
_, err := compile(t, `steps: {
shape: sql_table
id: int {constraint: primary_key}
}
scenarios: {
shape: sql_table
hey: int {constraint: primary_key}
}`)
2023-03-25 19:06:06 +00:00
assert.ErrorString(t, err, `TestCompile/steps/steps_panic.d2:3:3: invalid step
TestCompile/steps/steps_panic.d2:7:3: invalid scenario`)
2023-02-22 22:59:05 +00:00
},
},
2023-01-18 15:44:34 +00:00
{
name: "recursive",
run: func(t testing.TB) {
m, err := compile(t, `x -> y
steps: {
bingo: { p.q.z }
nuclear: {
quiche
scenarios: {
bavarian: {
perseverance
}
}
}
}`)
assert.Success(t, err)
2023-01-18 15:47:42 +00:00
assertQuery(t, m, 25, 4, nil, "")
2023-01-18 15:44:34 +00:00
assertQuery(t, m, 0, 0, nil, "x")
assertQuery(t, m, 0, 0, nil, "y")
assertQuery(t, m, 0, 0, nil, `(x -> y)[0]`)
assertQuery(t, m, 5, 1, nil, "steps.bingo")
assertQuery(t, m, 0, 0, nil, "steps.bingo.x")
assertQuery(t, m, 0, 0, nil, "steps.bingo.y")
assertQuery(t, m, 0, 0, nil, `steps.bingo.(x -> y)[0]`)
assertQuery(t, m, 2, 0, nil, "steps.bingo.p")
assertQuery(t, m, 1, 0, nil, "steps.bingo.p.q")
assertQuery(t, m, 0, 0, nil, "steps.bingo.p.q.z")
2023-01-18 15:47:42 +00:00
assertQuery(t, m, 15, 2, nil, "steps.nuclear")
2023-01-18 15:44:34 +00:00
assertQuery(t, m, 0, 0, nil, "steps.nuclear.x")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.y")
assertQuery(t, m, 0, 0, nil, `steps.nuclear.(x -> y)[0]`)
assertQuery(t, m, 2, 0, nil, "steps.nuclear.p")
assertQuery(t, m, 1, 0, nil, "steps.nuclear.p.q")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.p.q.z")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.quiche")
2023-01-18 15:47:42 +00:00
assertQuery(t, m, 7, 1, nil, "steps.nuclear.scenarios.bavarian")
2023-01-18 15:44:34 +00:00
assertQuery(t, m, 0, 0, nil, "steps.nuclear.scenarios.bavarian.x")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.scenarios.bavarian.y")
assertQuery(t, m, 0, 0, nil, `steps.nuclear.scenarios.bavarian.(x -> y)[0]`)
assertQuery(t, m, 2, 0, nil, "steps.nuclear.scenarios.bavarian.p")
assertQuery(t, m, 1, 0, nil, "steps.nuclear.scenarios.bavarian.p.q")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.scenarios.bavarian.p.q.z")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.scenarios.bavarian.quiche")
assertQuery(t, m, 0, 0, nil, "steps.nuclear.scenarios.bavarian.perseverance")
},
},
2023-01-16 12:48:45 +00:00
}
runa(t, tca)
}
2023-02-06 21:32:08 +00:00
func testCompileClasses(t *testing.T) {
t.Parallel()
tca := []testCase{
{
name: "basic",
run: func(t testing.TB) {
_, err := compile(t, `x
classes: {
mango: {
style.fill: orange
}
}
`)
assert.Success(t, err)
},
},
{
name: "nonroot",
run: func(t testing.TB) {
_, err := compile(t, `x: {
classes: {
mango: {
style.fill: orange
}
}
}
`)
2025-03-23 13:30:24 +00:00
assert.ErrorString(t, err, `TestCompile/classes/nonroot.d2:2:3: classes must be declared at a board root scope`)
2023-02-06 21:32:08 +00:00
},
},
{
name: "merge",
run: func(t testing.TB) {
m, err := compile(t, `classes: {
mango: {
style.fill: orange
width: 10
}
}
layers: {
hawaii: {
classes: {
mango: {
width: 9000
}
}
}
}
`)
assert.Success(t, err)
assertQuery(t, m, 3, 0, nil, "layers.hawaii.classes.mango")
assertQuery(t, m, 0, 0, "orange", "layers.hawaii.classes.mango.style.fill")
assertQuery(t, m, 0, 0, 9000, "layers.hawaii.classes.mango.width")
},
},
{
name: "nested",
run: func(t testing.TB) {
m, err := compile(t, `classes: {
mango: {
style.fill: orange
}
}
layers: {
hawaii: {
layers: {
maui: {
x
}
}
}
}
`)
assert.Success(t, err)
assertQuery(t, m, 3, 0, nil, "layers.hawaii.classes")
assertQuery(t, m, 3, 0, nil, "layers.hawaii.layers.maui.classes")
},
},
{
name: "inherited",
run: func(t testing.TB) {
m, err := compile(t, `classes: {
mango: {
style.fill: orange
}
}
scenarios: {
hawaii: {
steps: {
1: {
classes: {
cherry: {
style.fill: red
}
}
x
}
2: {
y
}
3: {
classes: {
cherry: {
style.fill: blue
}
}
y
}
4: {
layers: {
deep: {
x
}
}
x
}
}
}
}
`)
assert.Success(t, err)
assertQuery(t, m, 3, 0, nil, "scenarios.hawaii.classes")
assertQuery(t, m, 2, 0, nil, "scenarios.hawaii.steps.2.classes.mango")
assertQuery(t, m, 2, 0, nil, "scenarios.hawaii.steps.2.classes.cherry")
assertQuery(t, m, 0, 0, "blue", "scenarios.hawaii.steps.4.classes.cherry.style.fill")
assertQuery(t, m, 0, 0, "blue", "scenarios.hawaii.steps.4.layers.deep.classes.cherry.style.fill")
},
},
{
name: "layer-modify",
run: func(t testing.TB) {
m, err := compile(t, `classes: {
orb: {
style.fill: yellow
}
}
layers: {
x: {
classes.orb.style.stroke: red
}
}
`)
assert.Success(t, err)
assertQuery(t, m, 0, 0, "yellow", "layers.x.classes.orb.style.fill")
assertQuery(t, m, 0, 0, "red", "layers.x.classes.orb.style.stroke")
},
},
}
runa(t, tca)
}
2024-08-17 00:48:06 +00:00
func testCompileVars(t *testing.T) {
t.Parallel()
tca := []testCase{
{
name: "spread-in-place",
run: func(t testing.TB) {
m, err := compile(t, `vars: {
person-shape: {
grid-columns: 1
grid-rows: 2
grid-gap: 0
head
body
}
}
dora: {
...${person-shape}
body
}
`)
assert.Success(t, err)
2024-11-24 02:50:30 +00:00
assert.Equal(t, "grid-columns", m.Fields[1].Map().Fields[0].Name.ScalarString())
2024-08-17 00:48:06 +00:00
},
},
}
runa(t, tca)
}