Skip to content

Commit

Permalink
Merge pull request #5126 from flimzy/markup
Browse files Browse the repository at this point in the history
test: Add RenderObjectToMarkup and RenderToMarkup functions
  • Loading branch information
andydotxyz authored Sep 17, 2024
2 parents d4c3099 + d459ecb commit c2cc495
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func AssertObjectRendersToImage(t *testing.T, masterFilename string, o fyne.Canv
return AssertRendersToImage(t, masterFilename, c, msgAndArgs...)
}

// RenderObjectToMarkup renders the given [fyne.io/fyne/v2.CanvasObject] to a markup string.
//
// Since: 2.6
func RenderObjectToMarkup(o fyne.CanvasObject) string {
c := NewCanvas()
c.SetPadded(false)
size := o.MinSize().Max(o.Size())
c.SetContent(o)
c.Resize(size) // ensure we are large enough for current size

return snapshot(c)
}

// AssertObjectRendersToMarkup asserts that the given `CanvasObject` renders the same markup as the one stored in the master file.
// The master filename is relative to the `testdata` directory which is relative to the test.
// The test `t` fails if the rendered markup is not equal to the loaded master markup.
Expand Down Expand Up @@ -89,6 +102,13 @@ func AssertRendersToImage(t *testing.T, masterFilename string, c fyne.Canvas, ms
return test.AssertImageMatches(t, masterFilename, c.Capture(), msgAndArgs...)
}

// RenderToMarkup renders the given [fyne.io/fyne/v2.Canvas] to a markup string.
//
// Since: 2.6
func RenderToMarkup(c fyne.Canvas) string {
return snapshot(c)
}

// AssertRendersToMarkup asserts that the given canvas renders the same markup as the one stored in the master file.
// The master filename is relative to the `testdata` directory which is relative to the test.
// The test `t` fails if the rendered markup is not equal to the loaded master markup.
Expand Down
26 changes: 26 additions & 0 deletions test/test_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package test_test

import (
"bytes"
"image/color"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,6 +41,18 @@ func TestAssertObjectRendersToImage(t *testing.T) {
test.AssertObjectRendersToImage(t, "circle.png", obj)
}

func TestRenderObjectToMarkup(t *testing.T) {
obj := canvas.NewCircle(color.Black)
obj.Resize(fyne.NewSize(20, 20))

want, err := os.ReadFile("testdata/circle.xml")
require.NoError(t, err)
// Fix Windows newlines
want = bytes.ReplaceAll(want, []byte("\r\n"), []byte("\n"))
got := strings.ReplaceAll(test.RenderObjectToMarkup(obj), "\r\n", "\n")
assert.Equal(t, string(want), got, "existing master is equal to rendered markup")
}

func TestAssertObjectRendersToMarkup(t *testing.T) {
obj := canvas.NewCircle(color.Black)
obj.Resize(fyne.NewSize(20, 20))
Expand Down Expand Up @@ -82,6 +96,18 @@ func TestAssertRendersToImage(t *testing.T) {
}
}

func TestRenderToMarkup(t *testing.T) {
c := test.NewCanvas()
c.SetContent(canvas.NewCircle(color.Black))

want, err := os.ReadFile("testdata/markup_master.xml")
require.NoError(t, err)
// Fix Windows newlines
want = bytes.ReplaceAll(want, []byte("\r\n"), []byte("\n"))
got := strings.ReplaceAll(test.RenderToMarkup(c), "\r\n", "\n")
assert.Equal(t, string(want), got, "existing master is equal to rendered markup")
}

func TestAssertRendersToMarkup(t *testing.T) {
c := test.NewCanvas()
c.SetContent(canvas.NewCircle(color.Black))
Expand Down

0 comments on commit c2cc495

Please sign in to comment.