Skip to content

Instantly share code, notes, and snippets.

@adaam2
Last active October 28, 2022 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaam2/b52a5780799d3a67508cf15c83a26ca2 to your computer and use it in GitHub Desktop.
Save adaam2/b52a5780799d3a67508cf15c83a26ca2 to your computer and use it in GitHub Desktop.
package testing
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)
type UITestCase struct {
TestName string
FilePath string
Completed bool
StatusStr string
spinner spinner.Model
}
type TestModel struct {
quitting bool
err error
tests []*UITestCase
}
type errMsg error
func InitialTestModel() TestModel {
return TestModel{}
}
func (m TestModel) Init() tea.Cmd {
return nil
}
func (m TestModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
allCmds := []tea.Cmd{}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
return m, nil
}
case spinner.TickMsg:
for i, t := range m.tests {
s, cmd := t.spinner.Update(msg)
m.tests[i].spinner = s
allCmds = append(allCmds, cmd)
}
return m, tea.Batch(allCmds...)
case []*Event:
// we know we're dealing with collected tests
if msg[0].EventStatus == EventStatusPending {
for _, evt := range msg {
s := spinner.New()
s.Spinner = spinner.Points
allCmds = append(allCmds, s.Tick)
m.tests = append(m.tests, &UITestCase{
TestName: evt.Meta.TestName,
spinner: s,
})
}
} else {
evt := msg[0]
for i, test := range m.tests {
caseMatch := strings.EqualFold(test.TestName, evt.Result.TestName)
if caseMatch {
m.tests[i].Completed = true
m.tests[i].StatusStr = evt.Result.Status
m.tests[i].spinner.Spinner = spinner.Globe
allCmds = append(allCmds, m.tests[i].spinner.Tick)
}
}
}
return m, tea.Batch(allCmds...)
case errMsg:
m.err = msg
return m, nil
default:
return m, nil
}
}
func (m TestModel) View() string {
if m.err != nil {
return m.err.Error()
}
str := ""
for _, result := range m.tests {
str += fmt.Sprintf("%s %s\n", result.spinner.View(), result.TestName)
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment