Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Jul 16, 2023
1 parent 3547fbc commit ab81df7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion d2renderers/d2animate/d2animate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Wrap(rootDiagram *d2target.Diagram, svgs [][]byte, renderOpts d2svg.RenderO

d2svg.EmbedFonts(buf, diagramHash, svgsStr, rootDiagram.FontFamily, rootDiagram.GetNestedCorpus())

themeStylesheet, err := d2svg.ThemeCSS(diagramHash, *renderOpts.ThemeID, renderOpts.DarkThemeID)
themeStylesheet, err := d2svg.ThemeCSS(diagramHash, renderOpts.ThemeID, renderOpts.DarkThemeID)
if err != nil {
return nil, err
}
Expand Down
13 changes: 9 additions & 4 deletions d2renderers/d2svg/d2svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,9 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
return nil, err
}
}
themeID = *opts.ThemeID
if opts.ThemeID != nil {
themeID = *opts.ThemeID
}
darkThemeID = opts.DarkThemeID
scale = opts.Scale
}
Expand Down Expand Up @@ -1790,7 +1792,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
upperBuf := &bytes.Buffer{}
if opts.MasterID == "" {
EmbedFonts(upperBuf, diagramHash, buf.String(), diagram.FontFamily, diagram.GetCorpus()) // EmbedFonts *must* run before `d2sketch.DefineFillPatterns`, but after all elements are appended to `buf`
themeStylesheet, err := ThemeCSS(diagramHash, themeID, darkThemeID)
themeStylesheet, err := ThemeCSS(diagramHash, &themeID, darkThemeID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1952,8 +1954,11 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
}

// TODO include only colors that are being used to reduce size
func ThemeCSS(diagramHash string, themeID int64, darkThemeID *int64) (stylesheet string, err error) {
out, err := singleThemeRulesets(diagramHash, themeID)
func ThemeCSS(diagramHash string, themeID *int64, darkThemeID *int64) (stylesheet string, err error) {
if themeID == nil {
themeID = &d2themescatalog.NeutralDefault.ID
}
out, err := singleThemeRulesets(diagramHash, *themeID)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion d2target/d2target.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type ThemeOverrides struct {

type Diagram struct {
Name string `json:"name"`
Config *Config `json:"config"`
Config *Config `json:"config,omitempty"`
// See docs on the same field in d2graph to understand what it means.
IsFolderOnly bool `json:"isFolderOnly"`
Description string `json:"description,omitempty"`
Expand Down
13 changes: 9 additions & 4 deletions e2etests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type testCase struct {
dagreFeatureError string
elkFeatureError string
expErr string
themeID int64
themeID *int64
}

func runa(t *testing.T, tcs []testCase) {
Expand All @@ -109,7 +109,7 @@ func runa(t *testing.T, tcs []testCase) {
func serde(t *testing.T, tc testCase, ruler *textmeasure.Ruler) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
g, err := d2compiler.Compile("", strings.NewReader(tc.script), &d2compiler.CompileOptions{
g, _, err := d2compiler.Compile("", strings.NewReader(tc.script), &d2compiler.CompileOptions{
UTF16: false,
})
trequire.Nil(t, err)
Expand Down Expand Up @@ -161,13 +161,18 @@ func run(t *testing.T, tc testCase) {
plugin = &d2plugin.ELKPlugin
}

diagram, g, err := d2lib.Compile(ctx, tc.script, &d2lib.CompileOptions{
opts := &d2lib.CompileOptions{
Ruler: ruler,
MeasuredTexts: tc.mtexts,
Layout: layout,
ThemeID: tc.themeID,
})
}

var diagram *d2target.Diagram
g, _, err := d2lib.Compile(ctx, tc.script, opts)
if err == nil {
diagram, err = d2lib.Export(ctx, g, opts)
}
if tc.expErr != "" {
assert.Error(t, err)
assert.ErrorString(t, err, tc.expErr)
Expand Down
8 changes: 4 additions & 4 deletions e2etests/themes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func testThemes(t *testing.T) {
tcs := []testCase{
{
name: "dark terrastruct flagship",
themeID: d2themescatalog.DarkFlagshipTerrastruct.ID,
themeID: &d2themescatalog.DarkFlagshipTerrastruct.ID,
script: `
network: {
cell tower: {
Expand Down Expand Up @@ -118,7 +118,7 @@ ex: |tex
},
{
name: "terminal",
themeID: d2themescatalog.Terminal.ID,
themeID: &d2themescatalog.Terminal.ID,
script: `
network: {
cell tower: {
Expand Down Expand Up @@ -225,7 +225,7 @@ ex: |tex
},
{
name: "terminal_grayscale",
themeID: d2themescatalog.TerminalGrayscale.ID,
themeID: &d2themescatalog.TerminalGrayscale.ID,
script: `
network: {
cell tower: {
Expand Down Expand Up @@ -279,7 +279,7 @@ network.data processor -> api server
},
{
name: "origami",
themeID: d2themescatalog.Origami.ID,
themeID: &d2themescatalog.Origami.ID,
script: `
network: 通信網 {
cell tower: {
Expand Down

0 comments on commit ab81df7

Please sign in to comment.