-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathtable_test.go
More file actions
1433 lines (1289 loc) · 63.7 KB
/
table_test.go
File metadata and controls
1433 lines (1289 loc) · 63.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package table
import (
"fmt"
"strings"
"testing"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/exp/golden"
"github.com/muesli/termenv"
)
var TableStyle = func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Center)
case row%2 == 0:
return lipgloss.NewStyle().Padding(0, 1)
default:
return lipgloss.NewStyle().Padding(0, 1)
}
}
func TestTable(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableExample(t *testing.T) {
HeaderStyle := lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Center)
EvenRowStyle := lipgloss.NewStyle().Padding(0, 1)
OddRowStyle := lipgloss.NewStyle().Padding(0, 1)
rows := [][]string{
{"Chinese", "您好", "你好"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return HeaderStyle
case row%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
}).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
// You can also add tables row-by-row
table.Row("English", "You look absolutely fabulous.", "How's it going?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableEmpty(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableNoStyleFunc(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(nil).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableMarginAndRightAlignment(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(func(row, col int) lipgloss.Style {
return lipgloss.NewStyle().Margin(0, 1).Align(lipgloss.Right)
}).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Arabic", "أهلين", "أهلا").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableOffset(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?").
Offset(1)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableBorder(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.DoubleBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableSetRows(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestMoreCellsThanHeaders(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestMoreCellsThanHeadersExtra(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet", "Privet", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableNoHeaders(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableNoColumnSeparators(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
BorderColumn(false).
StyleFunc(TableStyle).
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableNoColumnSeparatorsWithHeaders(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
BorderColumn(false).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestBorderColumnsWithExtraRows(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet", "Privet", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
BorderColumn(false).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestNew(t *testing.T) {
table := New()
expected := ""
if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
}
}
func TestTableUnsetBorders(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...).
BorderTop(false).
BorderBottom(false).
BorderLeft(false).
BorderRight(false)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableUnsetHeaderSeparator(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...).
BorderHeader(false).
BorderTop(false).
BorderBottom(false).
BorderLeft(false).
BorderRight(false)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableUnsetHeaderSeparatorWithBorder(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...).
BorderHeader(false)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableRowSeparators(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
t.Run("no overflow", func(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
BorderRow(true).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
})
t.Run("with overflow", func(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
BorderRow(true).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...).
Height(8)
golden.RequireEqual(t, []byte(table.String()))
})
}
func TestTableHeights(t *testing.T) {
styleFunc := func(row, col int) lipgloss.Style {
if row == HeaderRow {
return lipgloss.NewStyle().Padding(0, 1)
}
if col == 0 {
return lipgloss.NewStyle().Width(18).Padding(1)
}
return lipgloss.NewStyle().Width(25).Padding(1, 2)
}
rows := [][]string{
{"Chutar o balde", `Literally translates to "kick the bucket." It's used when someone gives up or loses patience.`},
{"Engolir sapos", `Literally means "to swallow frogs." It's used to describe someone who has to tolerate or endure unpleasant situations.`},
{"Arroz de festa", `Literally means "party rice." It´s used to refer to someone who shows up everywhere.`},
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(styleFunc).
Headers("EXPRESSION", "MEANING").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableMultiLineRowSeparator(t *testing.T) {
styleFunc := func(row, col int) lipgloss.Style {
if row == HeaderRow {
return lipgloss.NewStyle().Padding(0, 1)
}
if col == 0 {
return lipgloss.NewStyle().Width(18).Padding(1)
}
return lipgloss.NewStyle().Width(25).Padding(1, 2)
}
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(styleFunc).
Headers("EXPRESSION", "MEANING").
BorderRow(true).
Row("Chutar o balde", `Literally translates to "kick the bucket." It's used when someone gives up or loses patience.`).
Row("Engolir sapos", `Literally means "to swallow frogs." It's used to describe someone who has to tolerate or endure unpleasant situations.`).
Row("Arroz de festa", `Literally means "party rice." It´s used to refer to someone who shows up everywhere.`)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthExpand(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Width(80).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
if lipgloss.Width(table.String()) != 80 {
t.Fatalf("expected table width to be 80, got %d", lipgloss.Width(table.String()))
}
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthExpandNoColumn(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Width(80).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
BorderColumn(false).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
if lipgloss.Width(table.String()) != 80 {
t.Fatalf("expected table width to be 80, got %d", lipgloss.Width(table.String()))
}
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthShrink(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Width(30).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthSmartCrop(t *testing.T) {
rows := [][]string{
{"Kini", "40", "New York"},
{"Eli", "30", "London"},
{"Iris", "20", "Paris"},
}
table := New().
Width(25).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("Name", "Age of Person", "Location").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthSmartCropExtensive(t *testing.T) {
rows := [][]string{
{"Chinese", "您好", "你好"},
{"Japanese", "こんにちは", "やあ"},
{"Arabic", "أهلين", "أهلا"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
{"English", "You look absolutely fabulous.", "How's it going?"},
}
table := New().
Width(18).
StyleFunc(TableStyle).
Border(lipgloss.ThickBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Wrap(false).
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthSmartCropTiny(t *testing.T) {
rows := [][]string{
{"Chinese", "您好", "你好"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
{"English", "You look absolutely fabulous.", "How's it going?"},
}
table := New().
Width(1).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidths(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Width(30).
StyleFunc(TableStyle).
BorderLeft(false).
BorderRight(false).
Border(lipgloss.NormalBorder()).
BorderColumn(false).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableWidthShrinkNoBorders(t *testing.T) {
rows := [][]string{
{"Chinese", "Nǐn hǎo", "Nǐ hǎo"},
{"French", "Bonjour", "Salut"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Width(30).
StyleFunc(TableStyle).
BorderLeft(false).
BorderRight(false).
Border(lipgloss.NormalBorder()).
BorderColumn(false).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestFilter(t *testing.T) {
data := NewStringData().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにちは", "やあ").
Item("Russian", "Zdravstvuyte", "Privet").
Item("Spanish", "Hola", "¿Qué tal?")
filter := NewFilter(data).Filter(func(row int) bool {
return data.At(row, 0) != "French"
})
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Data(filter)
golden.RequireEqual(t, []byte(table.String()))
}
func TestFilterInverse(t *testing.T) {
data := NewStringData().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにちは", "やあ").
Item("Russian", "Zdravstvuyte", "Privet").
Item("Spanish", "Hola", "¿Qué tal?")
filter := NewFilter(data).Filter(func(row int) bool {
return data.At(row, 0) == "French"
})
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Data(filter)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableANSI(t *testing.T) {
const code = "\x1b[31mC\x1b[0m\x1b[32mo\x1b[0m\x1b[34md\x1b[0m\x1b[33me\x1b[0m"
rows := [][]string{
{"Apple", "Red", "\x1b[31m31\x1b[0m"},
{"Lime", "Green", "\x1b[32m32\x1b[0m"},
{"Banana", "Yellow", "\x1b[33m33\x1b[0m"},
{"Blueberry", "Blue", "\x1b[34m34\x1b[0m"},
}
table := New().
Width(29).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("Fruit", "Color", code).
Rows(rows...)
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableHeightExact(t *testing.T) {
table := New().
Height(9).
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableHeightExtra(t *testing.T) {
table := New().
Height(100).
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableHeightShrink(t *testing.T) {
table := New().
Height(8).
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableHeightMinimum(t *testing.T) {
table := New().
Height(0).
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("ID", "LANGUAGE", "FORMAL", "INFORMAL").
Row("1", "Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("2", "French", "Bonjour", "Salut").
Row("3", "Japanese", "こんにちは", "やあ").
Row("4", "Russian", "Zdravstvuyte", "Privet").
Row("5", "Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableHeightMinimumShowData(t *testing.T) {
table := New().
Height(0).
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo")
golden.RequireEqual(t, []byte(table.String()))
}
func TestTableHeightWithOffset(t *testing.T) {
// This test exists to check for a bug/edge case when the table has an
// offset and the height is set.
table := New().
Height(8).
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?").
Offset(1)
golden.RequireEqual(t, []byte(table.String()))
}
func TestStyleFunc(t *testing.T) {
lipgloss.SetColorProfile(termenv.TrueColor)
tests := []struct {
name string
style StyleFunc
}{
{
"RightAlignedTextWithMargins",
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
default:
return lipgloss.NewStyle().Margin(0, 1).Align(lipgloss.Right)
}
},
},
{
"MarginAndPaddingSet",
// this test case uses background colors to differentiate margins
// and padding.
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
default:
return lipgloss.NewStyle().
Padding(1).
Margin(1).
// keeping right-aligned text as it's the most likely to
// be broken when truncated.
Align(lipgloss.Right).
Background(lipgloss.Color("#874bfc"))
}
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(tc.style).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")
golden.RequireEqual(t, []byte(table.String()))
})
}
}
func TestClearRows(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("had to recover: %v", r)
}
}()
table := New().
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo")
table.ClearRows()
table.Row("French", "Bonjour", "Salut")
// String() will try to get the rows from table.data
table.String()
}
func TestContentWrapping(t *testing.T) {
tests := []struct {
name string
headers []string
data [][]string
wrap bool
styleFunc StyleFunc
}{
{
"LongRowContent",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
true,
TableStyle,
},
{
"LongRowContentNoWrap",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
false,
TableStyle,
},
{
"LongRowContentNoWrapNoMargins",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
false,
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Padding(0).Align(lipgloss.Center)
default:
return lipgloss.NewStyle().Padding(0)
}
},
},
{
"LongRowContentNoWrapCustomMargins",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
false,
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Padding(0, 2).Align(lipgloss.Center)
default:
return lipgloss.NewStyle().Padding(0, 2)
}
},
},
{
"MissingRowContent",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "", ""}},
true,
TableStyle,
},
{
"LongHeaderContentLongAndShortRows",
[]string{"Destination", "Why are you going on this trip? Is it a hot or cold climate?", "Affordability"},
[][]string{
{"Mexico", "I want to go somewhere hot, dry, and affordable. Mexico has really good food, just don't drink tap water!", "$"},
{"New York", "I'm thinking about going during the Christmas season to check out Rockefeller center. Might be cold though...", "$$$"},
{"California", "", "$$$"},
},
true,
TableStyle,
},
{
"LongTextDifferentLanguages",
[]string{"Hello", "你好", "مرحبًا", "안녕하세요"},
[][]string{
{
"Lorem ipsum dolor sit amet, regione detracto eos an. Has ei quidam hendrerit intellegebat, id tamquam iudicabit necessitatibus ius, at errem officiis hendrerit mei. Exerci noster at has, sit id tota convenire, vel ex rebum inciderint liberavisse. Quaeque delectus corrumpit cu cum.",
`耐許ヱヨカハ調出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
"شيء قد للحكومة والكوري الأوروبيّون, بوابة تعديل واعتلاء ضرب بـ. إذ أسر اتّجة اعلان, ٣٠ اكتوبر العصبة استمرار ومن. أفاق للسيطرة التاريخ، مع بحث, كلّ اتّجة القوى مع. فبعد ايطاليا، تم حتى, لكل تم جسيمة الإحتفاظ وباستثناء, عل فرنسا وانتهاءً الإقتصادية عرض. ونتج دأبوا إحكام بال إذ. لغات عملية وتم مع, وصل بداية وبغطاء البرية بل, أي قررت بلاده فكانت حدى",
"版応道潟部中幕爆営報門案名見壌府。博健必権次覧編仕断青場内凄新東深簿代供供。守聞書神秀同浜東波恋闘秀。未格打好作器来利阪持西焦朝三女。権幽問季負娘購合旧資健載員式活陸。未倍校朝遺続術吉迎暮広知角亡志不説空住。法省当死年勝絡聞方北投健。室分性山天態意画詳知浅方裁。変激伝阜中野品省載嗅闘額端反。中必台際造事寄民経能前作臓",
"각급 선거관리위원회의 조직·직무범위 기타 필요한 사항은 법률로 정한다. 임시회의 회기는 30일을 초과할 수 없다. 국가는 여자의 복지와 권익의 향상을 위하여 노력하여야 한다. 국군의 조직과 편성은 법률로 정한다.",
},
},
true,
TableStyle,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
table := New().
Headers(tc.headers...).
Rows(tc.data...).
Width(80).
StyleFunc(tc.styleFunc).
Wrap(tc.wrap)
golden.RequireEqual(t, []byte(table.String()))
})
}
}
func TestContentWrapping_WithPadding(t *testing.T) {
tests := []struct {
name string
headers []string
data [][]string
}{
{
"LongRowContent",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
},
{
"MissingRowContent",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "", ""}},
},
{
"LongHeaderContentLongAndShortRows",
[]string{"Destination", "Why are you going on this trip? Is it a hot or cold climate?", "Affordability"},
[][]string{
{"Mexico", "I want to go somewhere hot, dry, and affordable. Mexico has really good food, just don't drink tap water!", "$"},
{"New York", "I'm thinking about going during the Christmas season to check out Rockefeller center. Might be cold though...", "$$$"},
{"California", "", "$$$"},
},
},
{
"LongTextDifferentLanguages",
[]string{"Hello", "你好", "مرحبًا", "안녕하세요"},
[][]string{
{
"",
`耐許ヱヨカハ調出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
"شيء قد للحكومة والكوري الأوروبيّون, بوابة تعديل واعتلاء ضرب بـ. إذ أسر اتّجة اعلان, ٣٠ اكتوبر العصبة استمرار ومن. أفاق للسيطرة التاريخ، مع بحث, كلّ اتّجة القوى مع. فبعد ايطاليا، تم حتى, لكل تم جسيمة الإحتفاظ وباستثناء, عل فرنسا وانتهاءً الإقتصادية عرض. ونتج دأبوا إحكام بال إذ. لغات عملية وتم مع, وصل بداية وبغطاء البرية بل, أي قررت بلاده فكانت حدى",
"版応道潟部中幕爆営報門案名見壌府。博健必権次覧編仕断青場内凄新東深簿代供供。守聞書神秀同浜東波恋闘秀。未格打好作器来利阪持西焦朝三女。権幽問季負娘購合旧資健載員式活陸。未倍校朝遺続術吉迎暮広知角亡志不説空住。法省当死年勝絡聞方北投健。室分性山天態意画詳知浅方裁。変激伝阜中野品省載嗅闘額端反。中必台際造事寄民経能前作臓",
"각급 선거관리위원회의 조직·직무범위 기타 필요한 사항은 법률로 정한다. 임시회의 회기는 30일을 초과할 수 없다. 국가는 여자의 복지와 권익의 향상을 위하여 노력하여야 한다. 국군의 조직과 편성은 법률로 정한다.",
},
},
},
}
defaultWidth := 80
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
table := New().
Headers(tc.headers...).
Rows(tc.data...).
StyleFunc(func(_, col int) lipgloss.Style {
return lipgloss.NewStyle().Padding(0, 1)
})
table.Width(defaultWidth)
// check total width.
if got := lipgloss.Width(table.String()); got != defaultWidth {
t.Fatalf("Table is not the correct width. got %d, want %d", got, defaultWidth)
t.Log(table.String())
}
golden.RequireEqual(t, []byte(table.String()))
})
}
}
func TestContentWrapping_WithMargins(t *testing.T) {
tests := []struct {
name string
headers []string
data [][]string
}{
{
"LongRowContent",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
},
{
"MissingRowContent",
[]string{"Name", "Description", "Type", "Required", "Default"},
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "", ""}},
},
{
"LongHeaderContentLongAndShortRows",
[]string{"Destination", "Why are you going on this trip? Is it a hot or cold climate?", "Affordability"},
[][]string{
{"Mexico", "I want to go somewhere hot, dry, and affordable. Mexico has really good food, just don't drink tap water!", "$"},
{"New York", "I'm thinking about going during the Christmas season to check out Rockefeller center. Might be cold though...", "$$$"},
{"California", "", "$$$"},
},
},
{
"LongTextDifferentLanguages",
[]string{"Hello", "你好", "مرحبًا", "안녕하세요"},
[][]string{
{
"Lorem ipsum dolor sit amet, regione detracto eos an. Has ei quidam hendrerit intellegebat, id tamquam iudicabit necessitatibus ius, at errem officiis hendrerit mei. Exerci noster at has, sit id tota convenire, vel ex rebum inciderint liberavisse. Quaeque delectus corrumpit cu cum.",
`耐許ヱヨカハ調出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
"شيء قد للحكومة والكوري الأوروبيّون, بوابة تعديل واعتلاء ضرب بـ. إذ أسر اتّجة اعلان, ٣٠ اكتوبر العصبة استمرار ومن. أفاق للسيطرة التاريخ، مع بحث, كلّ اتّجة القوى مع. فبعد ايطاليا، تم حتى, لكل تم جسيمة الإحتفاظ وباستثناء, عل فرنسا وانتهاءً الإقتصادية عرض. ونتج دأبوا إحكام بال إذ. لغات عملية وتم مع, وصل بداية وبغطاء البرية بل, أي قررت بلاده فكانت حدى",
"版応道潟部中幕爆営報門案名見壌府。博健必権次覧編仕断青場内凄新東深簿代供供。守聞書神秀同浜東波恋闘秀。未格打好作器来利阪持西焦朝三女。権幽問季負娘購合旧資健載員式活陸。未倍校朝遺続術吉迎暮広知角亡志不説空住。法省当死年勝絡聞方北投健。室分性山天態意画詳知浅方裁。変激伝阜中野品省載嗅闘額端反。中必台際造事寄民経能前作臓",