Skip to content

Commit

Permalink
Add a test for app.Lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 7, 2024
1 parent 8ec37b3 commit 907af94
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/app/lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package app

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestLifecycle(t *testing.T) {
life := &Lifecycle{}

// Not setting anything should not panic.
assert.NotPanics(t, life.TriggerEnteredForeground)
assert.NotPanics(t, life.TriggerExitedForeground)
assert.NotPanics(t, life.TriggerStarted)
assert.NotPanics(t, life.TriggerStopped)

var entered, exited, start, stop, hookedStop bool
life.SetOnEnteredForeground(func() { entered = true })
life.TriggerEnteredForeground()
assert.True(t, entered)

life.SetOnExitedForeground(func() { exited = true })
life.TriggerExitedForeground()
assert.True(t, exited)

life.SetOnStarted(func() { start = true })
life.TriggerStarted()
assert.True(t, start)

life.SetOnStopped(func() { stop = true })
life.TriggerStopped()
assert.True(t, stop)

stop = false
life.SetOnStoppedHookExecuted(func() { hookedStop = true })
life.TriggerStopped()
assert.True(t, stop && hookedStop)

// Setting back to nil should not panic.
life.SetOnEnteredForeground(nil)
life.SetOnExitedForeground(nil)
life.SetOnStarted(nil)
life.SetOnStopped(nil)
life.SetOnStoppedHookExecuted(nil)

assert.NotPanics(t, life.TriggerEnteredForeground)
assert.NotPanics(t, life.TriggerExitedForeground)
assert.NotPanics(t, life.TriggerStarted)
assert.NotPanics(t, life.TriggerStopped)
}

0 comments on commit 907af94

Please sign in to comment.