Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Screens.Empty() for checking if a Screens has no content #1092

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func ScreensFromImages(images ...image.Image) *Screens {
return &screens
}

// Empty returns true if there are no render roots or images in this screen.
func (s *Screens) Empty() bool {
return len(s.roots) == 0 && len(s.images) == 0
}

// Hash returns a hash of the render roots for this screen. This can be used for
// testing whether two render trees are exactly equivalent, without having to
// do the actual rendering.
Expand Down
23 changes: 23 additions & 0 deletions encode/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestHash(t *testing.T) {

roots, err := app.Run(context.Background())
require.NoError(t, err)
assert.False(t, ScreensFromRoots(roots).Empty())

// ensure we can calculate a hash
hash, err := ScreensFromRoots(roots).Hash()
Expand All @@ -185,11 +186,33 @@ func TestHash(t *testing.T) {

// ensure we can calculate a hash on the new app
hash2, err := ScreensFromRoots(roots2).Hash()
require.NoError(t, err)

// ensure hashes are different
require.NotEqual(t, hash, hash2)
}

func TestHashEmptyApp(t *testing.T) {
app, err := runtime.NewApplet("test.star", []byte(`def main(): return []`))
require.NoError(t, err)

roots, err := app.Run(context.Background())
require.NoError(t, err)
assert.True(t, ScreensFromRoots(roots).Empty())

// ensure we can calculate a hash
hash, err := ScreensFromRoots(roots).Hash()
require.NoError(t, err)
require.True(t, len(hash) > 0)

// ensure the hash doesn't change
for i := 0; i < 20; i++ {
h, err := ScreensFromRoots(roots).Hash()
assert.NoError(t, err)
assert.Equal(t, hash, h)
}
}

func TestHashDelayAndMaxAge(t *testing.T) {
r := []render.Root{{Child: &render.Text{Content: "derp"}}}

Expand Down
Loading