Skip to content

Instantly share code, notes, and snippets.

@adaam2
Created October 28, 2022 21:42
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/cb022c715997207118891ea6956f2a79 to your computer and use it in GitHub Desktop.
Save adaam2/cb022c715997207118891ea6956f2a79 to your computer and use it in GitHub Desktop.
package testing
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
type UITestCase struct {
TestName string
FilePath string
Completed bool
StatusStr string
}
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) {
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 []*Event:
// we know we're dealing with collected tests
if msg[0].EventStatus == EventStatusPending {
for _, evt := range msg {
m.tests = append(m.tests, &UITestCase{
TestName: evt.Meta.TestName,
})
}
} 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
}
}
}
return m, nil
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 {
icon := "🟡"
if result.Completed {
if result.StatusStr == "pass" {
icon = "🟢"
} else {
icon = "🔴"
}
}
str += fmt.Sprintf("%s %s\n", icon, result.TestName)
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment