Sfoglia il codice sorgente

Updated tui tests to reflect db721eb

arianagiroux 1 settimana fa
parent
commit
ff53708bca
2 ha cambiato i file con 37 aggiunte e 137 eliminazioni
  1. 4 4
      tui.go
  2. 33 133
      tui_test.go

+ 4 - 4
tui.go

@@ -138,7 +138,7 @@ func (m Model) Init() tea.Cmd {
 	return m.Poll()
 }
 
-func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
+func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	var cmd tea.Cmd
 	var cmds []tea.Cmd
 
@@ -174,16 +174,16 @@ func (m Model) View() tea.View {
 
 			// Linechart
 			// set chartHeight - vertical margin
-			chartHeight := m.Height - 2 // m.getVerticalMargin()
+			// chartHeight := m.Height - 2 // m.getVerticalMargin()
 
 			var slc streamlinechart.Model
 
 			if m.ChartHeight == 0 && len(m.Addresses) == 1 { // catch user specified fullscreen
 				// render chart at fullscreen
-				slc = streamlinechart.New(m.Width, chartHeight)
+				slc = streamlinechart.New(m.Width, m.Height)
 			} else if m.ChartHeight == 0 && len(m.Addresses) > 1 { // catch user specified fullscreen
 				// render chart at fullscreen minus a few lines to hint at scrolling
-				slc = streamlinechart.New(m.Width, chartHeight-5)
+				slc = streamlinechart.New(m.Width, m.Height-5)
 			} else {
 				slc = streamlinechart.New(m.Width, m.ChartHeight)
 			}

+ 33 - 133
tui_test.go

@@ -3,24 +3,30 @@ package pingo
 import (
 	"strings"
 	"testing"
-	"time"
 
 	tea "charm.land/bubbletea/v2"
-	"charm.land/lipgloss/v2"
 	"github.com/stretchr/testify/assert"
 )
 
+func spawnTestModel(addresses []string, points []float64, width, height, cHeight int) Model {
+	testModel := InitialModel(addresses, cHeight, width, height)
+	for i := range testModel.Addresses {
+		testModel.Addresses[i].Results = points
+	}
+
+	return testModel
+}
+
 func Test_InitialModel(t *testing.T) {
 	addresses := []string{"127.0.0.1", "cantresolvethisever"}
-	speed := time.Second * 1
 	chartHeight := 10
-	model := InitialModel(addresses, speed, chartHeight)
+	model := InitialModel(addresses, 80, 24, chartHeight)
 
 	assert.Implements(t, new(tea.Model), model)
 
-	assert.True(t, model.viewport.MouseWheelEnabled)
-	assert.Equal(t, speed, model.UpdateSpeed)
 	assert.Equal(t, chartHeight, model.ChartHeight)
+	assert.Equal(t, 80, model.Width)
+	assert.Equal(t, 24, model.Height)
 	assert.Equal(t, 2, len(model.Addresses))
 	for i, address := range model.Addresses {
 		assert.Equal(t, 80, address.MaxResults)
@@ -28,73 +34,6 @@ func Test_InitialModel(t *testing.T) {
 	}
 }
 
-// skipped until a testing paradigm can be determined (func has sig)
-func Test_Model_Init(t *testing.T) {
-	t.Skip()
-	addresses := []string{"127.0.0.1", "cantresolvethisever"}
-	speed := time.Millisecond * 1
-	chartHeight := 10
-	model := InitialModel(addresses, speed, chartHeight)
-	cmd := model.Init()
-	msg := cmd()
-	assert.IsType(t, new(tickMsg), msg)
-}
-
-func Test_Model_Update_scroll_on_key(t *testing.T) {
-	testModel := spawnTestModel(
-		[]string{"127.0.0.1", "asdfasdf"}, []float64{}, 80, 200, 10)
-	assert.True(t, testModel.viewport.YOffset() == 0, testModel.viewport.YOffset())
-	testJ := tea.KeyPressMsg(tea.Key{Text: "j"})
-	testK := tea.KeyPressMsg(tea.Key{Text: "k"})
-
-	model, _ := testModel.Update(testJ)
-	viewport := model.(Model).viewport
-	assert.True(t, viewport.YOffset() > 0, viewport.YOffset())
-	model, _ = testModel.Update(testK)
-	viewport = model.(Model).viewport
-	assert.True(t, viewport.YOffset() == 0, viewport.YOffset())
-}
-
-func Test_Model_Update_quit_on_key(t *testing.T) {
-	testModel := spawnTestModel(
-		[]string{"127.0.0.1", "asdfasdf"}, []float64{}, 80, 200, 10)
-	testQuitMsg := tea.KeyPressMsg(tea.Key{Text: "ctrl+c"})
-	_, cmd := testModel.Update(testQuitMsg)
-	assert.IsType(t, *new(tea.QuitMsg), cmd())
-}
-
-func Test_Model_Update_handle_windowsize(t *testing.T) {
-	testAddresses := []string{"127.0.0.1", "cantresolvethisever"}
-	testSpeed := time.Second * 1
-
-	testModel := InitialModel(testAddresses, testSpeed, 10)
-	assert.True(t, testModel.viewport.Height() == 0)
-
-	testMsg := tea.KeyPressMsg(tea.Key{Text: "ctrl+c"})
-	assert.IsType(t, *new(tea.KeyPressMsg), testMsg)
-	testSize := tea.WindowSizeMsg{Width: 20, Height: 200}
-	assert.IsType(t, *new(tea.WindowSizeMsg), testSize)
-
-	model, cmd := testModel.Update(testSize)
-	assert.Nil(t, cmd)
-	assert.Equal(t, 200-2, model.(Model).viewport.Height())
-}
-
-// skipped until a testing paradigm can be determined (func has sig)
-func Test_Model_Update_handle_tickMsg(t *testing.T) {}
-
-func spawnTestModel(addresses []string, points []float64, width, height, cHeight int) Model {
-	speed := time.Second * 1
-	testModel := InitialModel(addresses, speed, cHeight)
-	for i := range testModel.Addresses {
-		testModel.Addresses[i].Results = points
-	}
-	testModel.Width = width
-	testModel.Height = height
-
-	return testModel
-}
-
 func Test_Model_Update_handle_pollMsg(t *testing.T) {
 	testModel := spawnTestModel(
 		[]string{"127.0.0.1"}, []float64{}, 0, 20, 10)
@@ -116,16 +55,12 @@ func Test_Model_View(t *testing.T) {
 		[]string{"127.0.0.1", "cantresolvethisever"}, []float64{1.0, 2.0, 3.0}, 20, 20, 10)
 	result := testModel.View()
 	assert.IsType(t, *new(tea.View), result)
-	assert.True(t, strings.Contains(result.Content, "pingo"))
-	assert.True(t, strings.Contains(result.Content, "j/k: down/up"))
-	assert.True(t, strings.Contains(result.Content, "ctrl-c"))
-	assert.True(t, strings.Contains(result.Content, "quit"))
 }
 
-func Test_Model_Render_init_state(t *testing.T) {
+func Test_Model_View_init_state(t *testing.T) {
 	testModel := spawnTestModel(
 		[]string{"127.0.0.1", "cantresolvethisever"}, []float64{}, 0, 20, 10)
-	result := testModel.Render()
+	result := testModel.View().Content
 	assert.IsType(t, *new(string), result)
 
 	assert.True(t, strings.Contains(result, "127.0.0.1"))
@@ -139,18 +74,18 @@ func Test_Model_Render_init_state(t *testing.T) {
 	}
 }
 
-func Test_Model_Render_connection_unstable(t *testing.T) {
-	testModel := spawnTestModel([]string{"doesntmatter"}, []float64{-1}, 200, 20, 10)
+func Test_Model_View_connection_unstable(t *testing.T) {
+	testModel := spawnTestModel([]string{"doesntmatter"}, []float64{-1}, 200, 200, 200)
 
-	result := testModel.Render()
+	result := testModel.View().Content
 	assert.IsType(t, *new(string), result)
-	assert.True(t, strings.Contains(result, "connection unstable"))
+	assert.True(t, strings.Contains(result, "connection unstable"), result)
 }
 
-func Test_Model_Render_has_charts(t *testing.T) {
+func Test_Model_View_has_charts(t *testing.T) {
 	testModel := spawnTestModel(
-		[]string{"127.0.0.1", "cantresolvethisever"}, make([]float64, 20), 20, 20, 10)
-	result := testModel.Render()
+		[]string{"127.0.0.1", "cantresolvethisever"}, make([]float64, 20), 200, 200, 200)
+	result := testModel.View().Content
 	assert.IsType(t, *new(string), result)
 
 	assert.True(t, strings.Contains(result, "127.0.0.1"))
@@ -166,25 +101,26 @@ func Test_Model_Render_has_charts(t *testing.T) {
 	}
 }
 
-func Test_Model_Render_has_big_chart(t *testing.T) {
+func Test_Model_View_has_big_chart(t *testing.T) {
 	testModel := spawnTestModel(
-		[]string{"127.0.0.1"}, make([]float64, 20), 20, 20, 0)
-	result := testModel.Render()
+		[]string{"127.0.0.1"}, make([]float64, 20), 20, 20, 200)
+	result := testModel.View().Content
+	assert.IsType(t, *new(string), result)
 	assert.IsType(t, *new(string), result)
+
 	assert.True(t, strings.Contains(result, "127.0.0.1"))
-	assert.False(t, strings.Contains(result, "pingo"))
-	assert.False(t, strings.Contains(result, "j/k: up/down"))
+	assert.True(t, strings.Contains(result, "127.0.0.1"), result)
 
 	lines := strings.Split(result, "\n")
 	assert.True(t, strings.Contains(lines[1], "127.0.0.1"), lines[1])
 	assert.False(t, strings.Contains(lines[1], "loading..."))
-	assert.True(t, strings.Contains(lines[1+18], "0├"), lines[1+18])
+	assert.True(t, strings.Contains(lines[21], "0│"), result)
 }
 
-func Test_Model_Render_has_big_charts(t *testing.T) {
+func Test_Model_View_has_big_charts(t *testing.T) {
 	testModel := spawnTestModel(
-		[]string{"127.0.0.1", "cantresolvethisever"}, make([]float64, 20), 20, 20, 0)
-	result := testModel.Render()
+		[]string{"127.0.0.1", "cantresolvethisever"}, make([]float64, 20), 20, 20, 200)
+	result := testModel.View().Content
 	assert.IsType(t, *new(string), result)
 
 	assert.True(t, strings.Contains(result, "127.0.0.1"))
@@ -195,51 +131,15 @@ func Test_Model_Render_has_big_charts(t *testing.T) {
 	for i, line := range lines {
 		if strings.Contains(line, "127.0.0.1") || strings.Contains(line, "cantresolvethisever") {
 			assert.False(t, strings.Contains(line, "loading..."))
-			assert.True(t, strings.Contains(lines[i+18-5], "0├"), line) // assert bottom of chart is where we expect it to be
+			assert.True(t, strings.Contains(lines[i+20], "0│"), lines[i+20]) // assert bottom of chart is where we expect it to be
 		}
 	}
 }
 
-func Test_Model_header_and_footer(t *testing.T) {
-	addresses := []string{"127.0.0.1", "cantresolvethisever"}
-	speed := time.Second * 1
-	chartHeight := 10
-	testModel := InitialModel(addresses, speed, chartHeight)
-	result := testModel.header()
-	assert.IsType(t, *new(string), result)
-	assert.Equal(t, 1, lipgloss.Height(result))
-	result = testModel.footer()
-	assert.IsType(t, *new(string), result)
-	assert.Equal(t, 1, lipgloss.Height(result))
-}
-
-func Test_Model_getVerticalMargin(t *testing.T) {
-	addresses := []string{"127.0.0.1", "cantresolvethisever"}
-	speed := time.Second * 1
-	chartHeight := 10
-	testModel := InitialModel(addresses, speed, chartHeight)
-	testModel.Width = 20
-	testModel.Height = 20
-	result := testModel.getVerticalMargin()
-	assert.IsType(t, *new(int), result)
-	assert.Equal(t, 2, result)
-}
-
-// skipped until a testing paradigm can be determined (func has sig)
-func Test_Model_cmd_Tick(t *testing.T) {
-	t.Skip()
-	addresses := []string{"127.0.0.1", "cantresolvethisever"}
-	speed := time.Second * 1
-	chartHeight := 10
-	model := InitialModel(addresses, speed, chartHeight)
-	cmd := model.Tick()
-	assert.Nil(t, cmd)
-}
 func Test_Model_cmd_Poll(t *testing.T) {
 	addresses := []string{"127.0.0.1", "cantresolvethisever"}
-	speed := time.Second * 1
 	chartHeight := 10
-	model := InitialModel(addresses, speed, chartHeight)
+	model := InitialModel(addresses, chartHeight, 80, 24)
 	batch := model.Poll()
 	msgs := []tea.Cmd(batch().(tea.BatchMsg)) // type assert for legibility
 	for i, cmd := range msgs {