Created
October 28, 2022 21:42
-
-
Save adaam2/cb022c715997207118891ea6956f2a79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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