Skip to content

Commit e985ec9

Browse files
mikelorantmaaslalani
authored andcommitted
Add tests for textarea view
Add improved unit tests for textarea view function. The end of buffer unit test was flawed and failed to properly check whether the end of buffer character was in the correct location. This test has been removed as this feature is now verified as part of these test cases. Signed-off-by: Michael Lorant <[email protected]>
1 parent 3f2cd55 commit e985ec9

File tree

3 files changed

+228
-9
lines changed

3 files changed

+228
-9
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/charmbracelet/bubbles
33
go 1.18
44

55
require (
6+
github.com/MakeNowJust/heredoc v1.0.0
7+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
68
github.com/atotto/clipboard v0.1.4
79
github.com/charmbracelet/bubbletea v0.25.0
810
github.com/charmbracelet/harmonica v0.2.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
2+
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
3+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
4+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
15
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
26
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
37
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=

textarea/textarea_test.go

Lines changed: 222 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package textarea
33
import (
44
"strings"
55
"testing"
6+
"unicode"
67

8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/acarl005/stripansi"
710
tea "github.com/charmbracelet/bubbletea"
811
)
912

@@ -416,15 +419,210 @@ func TestVerticalNavigationShouldRememberPositionWhileTraversing(t *testing.T) {
416419
}
417420
}
418421

419-
func TestRendersEndOfLineBuffer(t *testing.T) {
420-
textarea := newTextArea()
421-
textarea.ShowLineNumbers = true
422-
textarea.SetWidth(20)
423-
424-
view := textarea.View()
425-
if !strings.Contains(view, "~") {
426-
t.Log(view)
427-
t.Fatal("Expected to see a tilde at the end of the line")
422+
func TestView(t *testing.T) {
423+
tests := []struct {
424+
name string
425+
modelFunc func(Model) Model
426+
expected string
427+
}{
428+
{
429+
name: "placeholder",
430+
expected: heredoc.Doc(`
431+
> 1 Hello, World!
432+
> ~
433+
> ~
434+
> ~
435+
> ~
436+
> ~
437+
`),
438+
},
439+
{
440+
name: "single line",
441+
modelFunc: func(m Model) Model {
442+
m.SetValue("the first line")
443+
444+
return m
445+
},
446+
expected: heredoc.Doc(`
447+
> 1 the first line
448+
> ~
449+
> ~
450+
> ~
451+
> ~
452+
> ~
453+
`),
454+
},
455+
{
456+
name: "multiple lines",
457+
modelFunc: func(m Model) Model {
458+
m.SetValue("the first line\nthe second line\nthe third line")
459+
460+
return m
461+
},
462+
expected: heredoc.Doc(`
463+
> 1 the first line
464+
> 2 the second line
465+
> 3 the third line
466+
> ~
467+
> ~
468+
> ~
469+
`),
470+
},
471+
{
472+
name: "single line without line numbers",
473+
modelFunc: func(m Model) Model {
474+
m.SetValue("the first line")
475+
m.ShowLineNumbers = false
476+
477+
return m
478+
},
479+
expected: heredoc.Doc(`
480+
> the first line
481+
>
482+
>
483+
>
484+
>
485+
>
486+
`),
487+
},
488+
{
489+
name: "multipline lines without line numbers",
490+
modelFunc: func(m Model) Model {
491+
m.SetValue("the first line\nthe second line\nthe third line")
492+
m.ShowLineNumbers = false
493+
494+
return m
495+
},
496+
expected: heredoc.Doc(`
497+
> the first line
498+
> the second line
499+
> the third line
500+
>
501+
>
502+
>
503+
`),
504+
},
505+
{
506+
name: "single line and custom end of buffer character",
507+
modelFunc: func(m Model) Model {
508+
m.SetValue("the first line")
509+
m.EndOfBufferCharacter = '*'
510+
511+
return m
512+
},
513+
expected: heredoc.Doc(`
514+
> 1 the first line
515+
> *
516+
> *
517+
> *
518+
> *
519+
> *
520+
`),
521+
},
522+
{
523+
name: "multiple lines and custom end of buffer character",
524+
modelFunc: func(m Model) Model {
525+
m.SetValue("the first line\nthe second line\nthe third line")
526+
m.EndOfBufferCharacter = '*'
527+
528+
return m
529+
},
530+
expected: heredoc.Doc(`
531+
> 1 the first line
532+
> 2 the second line
533+
> 3 the third line
534+
> *
535+
> *
536+
> *
537+
`),
538+
},
539+
{
540+
name: "single line without line numbers and custom end of buffer character",
541+
modelFunc: func(m Model) Model {
542+
m.SetValue("the first line")
543+
m.ShowLineNumbers = false
544+
m.EndOfBufferCharacter = '*'
545+
546+
return m
547+
},
548+
expected: heredoc.Doc(`
549+
> the first line
550+
>
551+
>
552+
>
553+
>
554+
>
555+
`),
556+
},
557+
{
558+
name: "multiple lines without line numbers and custom end of buffer character",
559+
modelFunc: func(m Model) Model {
560+
m.SetValue("the first line\nthe second line\nthe third line")
561+
m.ShowLineNumbers = false
562+
m.EndOfBufferCharacter = '*'
563+
564+
return m
565+
},
566+
expected: heredoc.Doc(`
567+
> the first line
568+
> the second line
569+
> the third line
570+
>
571+
>
572+
>
573+
`),
574+
},
575+
{
576+
name: "single line and custom prompt",
577+
modelFunc: func(m Model) Model {
578+
m.SetValue("the first line")
579+
m.Prompt = "* "
580+
581+
return m
582+
},
583+
expected: heredoc.Doc(`
584+
* 1 the first line
585+
* ~
586+
* ~
587+
* ~
588+
* ~
589+
* ~
590+
`),
591+
},
592+
{
593+
name: "multiple lines and custom prompt",
594+
modelFunc: func(m Model) Model {
595+
m.SetValue("the first line\nthe second line\nthe third line")
596+
m.Prompt = "* "
597+
598+
return m
599+
},
600+
expected: heredoc.Doc(`
601+
* 1 the first line
602+
* 2 the second line
603+
* 3 the third line
604+
* ~
605+
* ~
606+
* ~
607+
`),
608+
},
609+
}
610+
611+
for _, tt := range tests {
612+
t.Run(tt.name, func(t *testing.T) {
613+
textarea := newTextArea()
614+
615+
if tt.modelFunc != nil {
616+
textarea = tt.modelFunc(textarea)
617+
}
618+
619+
view := stripString(textarea.View())
620+
expected := stripString(tt.expected)
621+
622+
if view != expected {
623+
t.Fatalf("Expected:\n%v\nGot:\n%v\n", expected, view)
624+
}
625+
})
428626
}
429627
}
430628

@@ -444,3 +642,18 @@ func newTextArea() Model {
444642
func keyPress(key rune) tea.Msg {
445643
return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{key}, Alt: false}
446644
}
645+
646+
func stripString(str string) string {
647+
s := stripansi.Strip(str)
648+
ss := strings.Split(s, "\n")
649+
650+
var lines []string
651+
for _, l := range ss {
652+
trim := strings.TrimRightFunc(l, unicode.IsSpace)
653+
if trim != "" {
654+
lines = append(lines, trim)
655+
}
656+
}
657+
658+
return strings.Join(lines, "\n")
659+
}

0 commit comments

Comments
 (0)