Skip to content

Commit

Permalink
Add Screens.Empty() for checking if a Screens has no content
Browse files Browse the repository at this point in the history
  • Loading branch information
rohansingh committed Sep 30, 2024
1 parent 29745c1 commit ebb90c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit ebb90c3

Please sign in to comment.