d2/d2renderers/d2svg/appendix/appendix_test.go

189 lines
4.3 KiB
Go
Raw Normal View History

2022-12-28 04:29:51 +00:00
package appendix_test
import (
"context"
"encoding/xml"
"log/slog"
2022-12-28 04:29:51 +00:00
"os"
"path/filepath"
"strings"
"testing"
tassert "github.com/stretchr/testify/assert"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff"
2023-07-14 20:08:26 +00:00
"oss.terrastruct.com/d2/d2graph"
2022-12-28 04:29:51 +00:00
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2renderers/d2svg/appendix"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
)
func TestAppendix(t *testing.T) {
t.Parallel()
tcs := []testCase{
{
2022-12-28 19:39:20 +00:00
name: "tooltip_wider_than_diagram",
2022-12-28 04:29:51 +00:00
script: `x: { tooltip: Total abstinence is easier than perfect moderation }
y: { tooltip: Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS! }
x -> y
2022-12-28 19:39:20 +00:00
`,
},
{
name: "diagram_wider_than_tooltip",
script: `shape: sequence_diagram
customer
issuer
store: { tooltip: Like starbucks or something }
acquirer: { tooltip: I'm not sure what this is }
network
customer bank
store bank
customer: {shape: person}
customer bank: {
shape: image
icon: https://cdn-icons-png.flaticon.com/512/858/858170.png
}
store bank: {
shape: image
icon: https://cdn-icons-png.flaticon.com/512/858/858170.png
}
initial transaction: {
customer -> store: 1 banana please
store -> customer: '$10 dollars'
}
customer.internal -> customer.internal: "thinking: wow, inflation"
customer.internal -> customer bank: checks bank account
customer bank -> customer.internal: 'Savings: $11'
customer."An error in judgement is about to occur"
customer -> store: I can do that, here's my card
payment processor behind the scenes: {
store -> acquirer: Run this card
acquirer -> network: Process to card issuer
simplified: {
network -> issuer: Process this payment
issuer -> customer bank: '$10 debit'
acquirer -> store bank: '$10 credit'
}
}
2022-12-29 00:33:46 +00:00
`,
},
{
name: "links",
script: `x: { link: https://d2lang.com }
y: { link: https://terrastruct.com; tooltip: Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS! }
x -> y
`,
},
{
name: "links dark",
themeID: 200,
script: `x: { link: https://d2lang.com }
y: { link: https://fosny.eu; tooltip: Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS! }
x -> y
2023-03-29 19:01:40 +00:00
`,
},
{
name: "internal-links",
script: `x: { link: layers.x }
layers: {
x: {
gooo
home.link: _
next.link: steps.next
steps: {
next: {
hi
}
}
}
}
2023-02-26 19:41:50 +00:00
`,
},
{
name: "tooltip_fill",
script: `x: { tooltip: Total abstinence is easier than perfect moderation }
y: { tooltip: Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS! }
x -> y
style.fill: PaleVioletRed
2022-12-28 04:29:51 +00:00
`,
},
}
runa(t, tcs)
}
type testCase struct {
name string
themeID int64
script string
skip bool
2022-12-28 04:29:51 +00:00
}
func runa(t *testing.T, tcs []testCase) {
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skip()
}
t.Parallel()
run(t, tc)
})
}
}
func run(t *testing.T, tc testCase) {
ctx := context.Background()
ctx = log.WithTB(ctx, t)
2022-12-28 04:29:51 +00:00
ctx = log.Leveled(ctx, slog.LevelDebug)
ruler, err := textmeasure.NewRuler()
if !tassert.Nil(t, err) {
return
}
2023-07-14 20:08:26 +00:00
renderOpts := &d2svg.RenderOpts{
ThemeID: &tc.themeID,
}
layoutResolver := func(engine string) (d2graph.LayoutGraph, error) {
return d2dagrelayout.DefaultLayout, nil
}
2022-12-28 04:29:51 +00:00
diagram, _, err := d2lib.Compile(ctx, tc.script, &d2lib.CompileOptions{
2023-07-14 20:08:26 +00:00
Ruler: ruler,
LayoutResolver: layoutResolver,
}, renderOpts)
2022-12-28 04:29:51 +00:00
if !tassert.Nil(t, err) {
return
}
dataPath := filepath.Join("testdata", strings.TrimPrefix(t.Name(), "TestAppendix/"))
pathGotSVG := filepath.Join(dataPath, "sketch.got.svg")
2023-07-14 20:08:26 +00:00
svgBytes, err := d2svg.Render(diagram, renderOpts)
2022-12-28 04:29:51 +00:00
assert.Success(t, err)
2025-01-30 21:48:06 +00:00
svgBytes = appendix.Append(diagram, nil, ruler, svgBytes)
2022-12-28 04:29:51 +00:00
err = os.MkdirAll(dataPath, 0755)
assert.Success(t, err)
2025-01-30 21:48:06 +00:00
err = os.WriteFile(pathGotSVG, svgBytes, 0600)
2022-12-28 04:29:51 +00:00
assert.Success(t, err)
defer os.Remove(pathGotSVG)
var xmlParsed interface{}
err = xml.Unmarshal(svgBytes, &xmlParsed)
assert.Success(t, err)
err = diff.Testdata(filepath.Join(dataPath, "sketch"), ".svg", svgBytes)
assert.Success(t, err)
}