Skip to content

Commit c290597

Browse files
authored
Add Screens.Empty() for checking if a Screens has no content (#1092)
1 parent 29745c1 commit c290597

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

encode/encode.go

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func ScreensFromImages(images ...image.Image) *Screens {
5454
return &screens
5555
}
5656

57+
// Empty returns true if there are no render roots or images in this screen.
58+
func (s *Screens) Empty() bool {
59+
return len(s.roots) == 0 && len(s.images) == 0
60+
}
61+
5762
// Hash returns a hash of the render roots for this screen. This can be used for
5863
// testing whether two render trees are exactly equivalent, without having to
5964
// do the actual rendering.

encode/encode_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func TestHash(t *testing.T) {
162162

163163
roots, err := app.Run(context.Background())
164164
require.NoError(t, err)
165+
assert.False(t, ScreensFromRoots(roots).Empty())
165166

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

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

189191
// ensure hashes are different
190192
require.NotEqual(t, hash, hash2)
191193
}
192194

195+
func TestHashEmptyApp(t *testing.T) {
196+
app, err := runtime.NewApplet("test.star", []byte(`def main(): return []`))
197+
require.NoError(t, err)
198+
199+
roots, err := app.Run(context.Background())
200+
require.NoError(t, err)
201+
assert.True(t, ScreensFromRoots(roots).Empty())
202+
203+
// ensure we can calculate a hash
204+
hash, err := ScreensFromRoots(roots).Hash()
205+
require.NoError(t, err)
206+
require.True(t, len(hash) > 0)
207+
208+
// ensure the hash doesn't change
209+
for i := 0; i < 20; i++ {
210+
h, err := ScreensFromRoots(roots).Hash()
211+
assert.NoError(t, err)
212+
assert.Equal(t, hash, h)
213+
}
214+
}
215+
193216
func TestHashDelayAndMaxAge(t *testing.T) {
194217
r := []render.Root{{Child: &render.Text{Content: "derp"}}}
195218

0 commit comments

Comments
 (0)