2023-07-29 22:09:29 +00:00
|
|
|
package d2ir_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"oss.terrastruct.com/util-go/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func testCompileFilters(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
tca := []testCase{
|
|
|
|
|
{
|
2023-07-29 23:32:30 +00:00
|
|
|
name: "base",
|
2023-07-29 22:09:29 +00:00
|
|
|
run: func(t testing.TB) {
|
|
|
|
|
m, err := compile(t, `jacob: {
|
|
|
|
|
shape: circle
|
|
|
|
|
}
|
|
|
|
|
jeremy: {
|
|
|
|
|
shape: rectangle
|
|
|
|
|
}
|
|
|
|
|
*: {
|
|
|
|
|
&shape: rectangle
|
|
|
|
|
label: I'm a rectangle
|
|
|
|
|
}`)
|
|
|
|
|
assert.Success(t, err)
|
|
|
|
|
assertQuery(t, m, 1, 0, nil, "jacob")
|
2023-07-29 23:32:30 +00:00
|
|
|
assertQuery(t, m, 2, 0, nil, "jeremy")
|
|
|
|
|
assertQuery(t, m, 0, 0, "I'm a rectangle", "jeremy.label")
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "order",
|
|
|
|
|
run: func(t testing.TB) {
|
|
|
|
|
m, err := compile(t, `jacob: {
|
|
|
|
|
shape: circle
|
|
|
|
|
}
|
|
|
|
|
jeremy: {
|
|
|
|
|
shape: rectangle
|
|
|
|
|
}
|
|
|
|
|
*: {
|
|
|
|
|
label: I'm a rectangle
|
|
|
|
|
&shape: rectangle
|
|
|
|
|
}`)
|
|
|
|
|
assert.Success(t, err)
|
|
|
|
|
assertQuery(t, m, 1, 0, nil, "jacob")
|
|
|
|
|
assertQuery(t, m, 2, 0, nil, "jeremy")
|
2023-07-29 22:09:29 +00:00
|
|
|
assertQuery(t, m, 0, 0, "I'm a rectangle", "jeremy.label")
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runa(t, tca)
|
2023-07-30 00:09:20 +00:00
|
|
|
|
|
|
|
|
t.Run("errors", func(t *testing.T) {
|
|
|
|
|
tca := []testCase{
|
|
|
|
|
{
|
|
|
|
|
name: "bad-syntax",
|
|
|
|
|
run: func(t testing.TB) {
|
|
|
|
|
_, err := compile(t, `jacob.style: {
|
|
|
|
|
fill: red
|
|
|
|
|
multiple: true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*.&style: {
|
|
|
|
|
fill: red
|
|
|
|
|
multiple: true
|
|
|
|
|
}
|
|
|
|
|
`)
|
|
|
|
|
assert.ErrorString(t, err, `TestCompile/filters/errors/bad-syntax.d2:6:3: unexpected text after map key
|
|
|
|
|
TestCompile/filters/errors/bad-syntax.d2:9:1: unexpected map termination character } in file map`)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "composite",
|
|
|
|
|
run: func(t testing.TB) {
|
|
|
|
|
_, err := compile(t, `jacob.style: {
|
|
|
|
|
fill: red
|
|
|
|
|
multiple: true
|
|
|
|
|
}
|
|
|
|
|
*: {
|
|
|
|
|
&style: {
|
|
|
|
|
fill: red
|
|
|
|
|
multiple: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`)
|
|
|
|
|
assert.ErrorString(t, err, `TestCompile/filters/errors/composite.d2:6:2: ampersand filters cannot be composites`)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
runa(t, tca)
|
|
|
|
|
})
|
2023-07-29 22:09:29 +00:00
|
|
|
}
|