@@ -3,7 +3,10 @@ package textarea
3
3
import (
4
4
"strings"
5
5
"testing"
6
+ "unicode"
6
7
8
+ "github.com/MakeNowJust/heredoc"
9
+ "github.com/acarl005/stripansi"
7
10
tea "github.com/charmbracelet/bubbletea"
8
11
)
9
12
@@ -416,15 +419,210 @@ func TestVerticalNavigationShouldRememberPositionWhileTraversing(t *testing.T) {
416
419
}
417
420
}
418
421
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\n the second line\n the 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\n the second line\n the 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\n the second line\n the 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\n the second line\n the 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\n the second line\n the 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\n Got:\n %v\n " , expected , view )
624
+ }
625
+ })
428
626
}
429
627
}
430
628
@@ -444,3 +642,18 @@ func newTextArea() Model {
444
642
func keyPress (key rune ) tea.Msg {
445
643
return tea.KeyMsg {Type : tea .KeyRunes , Runes : []rune {key }, Alt : false }
446
644
}
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