Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(textinput): make textinput.Model satisfy tea.Model interface #492

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

twpayne
Copy link
Contributor

@twpayne twpayne commented Mar 1, 2024

An early step towards #483.

@twpayne
Copy link
Contributor Author

twpayne commented Mar 1, 2024

The CI failures are in a different package, so probably unrelated to this PR 😕

@bashbunni
Copy link
Member

Hey @twpayne! Heads up, we're figuring out a solution to this right now. When we try to make the bubbles implement the tea.Model interface, we end up having to do a lot of type assertions. For example, here's what it would look like to update a viewport bubble that implements tea.Model:

// Pretend viewport.Model implements
// tea.Model here
type model {
    vp viewport.Model
}

// Some time later...
newModel, cmd := m.vp.Update(msg)
if newVPModel, ok := newModel(viewport.Model); ok {
    m.vp = newCVPModel
}

by comparison, this is the solution with the way to update the viewport model right now:

m.vp, cmd := m.vp.Update(msg) // DONE

Just wanted to be transparent on this one as any activity on this PR is likely going to be on hold until these refactors are ironed out :)

@twpayne
Copy link
Contributor Author

twpayne commented Mar 14, 2024

Ah, thanks for the info. I wanted to add this so I could easily add tests in a follow-up PR. I've been using the following generic function in my tests for my small collection of bubbles:

func testRunModelWithInput[M tea.Model](t *testing.T, model M, input string) M {
	t.Helper()
	for _, msg := range makeKeyMsgs(input) {
		m, _ := model.Update(msg)
		var ok bool
		model, ok = m.(M)
		assert.True(t, ok)
	}
	return model
}

This allows me to write generic tests without type assertions, like this:

		t.Run(tc.name, func(t *testing.T) {
			actualModel := testRunModelWithInput(t, NewBoolInputModel("prompt", tc.defaultValue), tc.input)
			assert.Equal(t, tc.expectedCanceled, actualModel.Canceled())
			assert.Equal(t, tc.expectedValue, actualModel.Value())
		})

I'm sure you've already considering this and similar approaches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants