-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypography_test.go
More file actions
1794 lines (1581 loc) · 46.8 KB
/
typography_test.go
File metadata and controls
1794 lines (1581 loc) · 46.8 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 herald
import (
"regexp"
"strings"
"testing"
"charm.land/lipgloss/v2"
)
// stripANSI removes all ANSI escape sequences from a string so that
// plain-text assertions work regardless of styling.
var ansiRe = regexp.MustCompile(`\x1b\[[0-9;]*m`)
func stripANSI(s string) string {
return ansiRe.ReplaceAllString(s, "")
}
func newTestTypography() *Typography {
return New()
}
func TestNew(t *testing.T) {
ty := New()
if ty == nil {
t.Fatal("New() returned nil")
}
}
func TestNewWithOptions(t *testing.T) {
ty := New(WithHRWidth(60), WithBulletChar("-"))
if ty.theme.HRWidth != 60 {
t.Errorf("expected HRWidth 60, got %d", ty.theme.HRWidth)
}
}
func TestThemeAccessor(t *testing.T) {
ty := New(WithHRWidth(99))
theme := ty.Theme()
if theme.HRWidth != 99 {
t.Errorf("Theme() HRWidth: expected 99, got %d", theme.HRWidth)
}
}
// ---------------------------------------------------------------------------
// Headings
// ---------------------------------------------------------------------------
func TestHeadings(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
fn func(string) string
text string
}{
{"H1", ty.H1, "Heading 1"},
{"H2", ty.H2, "Heading 2"},
{"H3", ty.H3, "Heading 3"},
{"H4", ty.H4, "Heading 4"},
{"H5", ty.H5, "Heading 5"},
{"H6", ty.H6, "Heading 6"},
{"H1 empty", ty.H1, ""},
{"H1 special chars", ty.H1, "<script>alert('xss')</script>"},
{"H1 unicode", ty.H1, "Bonjour le monde"},
{"H1 long", ty.H1, strings.Repeat("A", 500)},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.fn(tc.text)
if tc.text != "" && !strings.Contains(stripANSI(result), tc.text) {
t.Errorf("expected result to contain %q, got %q", tc.text, stripANSI(result))
}
})
}
}
func TestHeadingUnderlines(t *testing.T) {
ty := newTestTypography()
t.Run("H1 has double-line underline", func(t *testing.T) {
result := stripANSI(ty.H1("Hello"))
if !strings.Contains(result, strings.Repeat("═", 5)) {
t.Errorf("H1 should contain ═ underline, got %q", result)
}
})
t.Run("H2 has single-line underline", func(t *testing.T) {
result := stripANSI(ty.H2("Hello"))
if !strings.Contains(result, strings.Repeat("─", 5)) {
t.Errorf("H2 should contain ─ underline, got %q", result)
}
})
t.Run("H3 has dotted underline", func(t *testing.T) {
result := stripANSI(ty.H3("Hello"))
if !strings.Contains(result, strings.Repeat("·", 5)) {
t.Errorf("H3 should contain · underline, got %q", result)
}
})
t.Run("H4 has bar prefix", func(t *testing.T) {
result := stripANSI(ty.H4("Hello"))
if !strings.Contains(result, "▎ Hello") {
t.Errorf("H4 should have bar prefix, got %q", result)
}
})
t.Run("H5 has bar prefix", func(t *testing.T) {
result := stripANSI(ty.H5("Hello"))
if !strings.Contains(result, "▎ Hello") {
t.Errorf("H5 should have bar prefix, got %q", result)
}
})
t.Run("H6 has bar prefix", func(t *testing.T) {
result := stripANSI(ty.H6("Hello"))
if !strings.Contains(result, "▎ Hello") {
t.Errorf("H6 should have bar prefix, got %q", result)
}
})
}
func TestCustomHeadingDecorations(t *testing.T) {
ty := New(
WithH1UnderlineChar("="),
WithH2UnderlineChar("-"),
WithH3UnderlineChar("."),
WithHeadingBarChar("|"),
)
t.Run("custom H1 underline", func(t *testing.T) {
result := stripANSI(ty.H1("Hi"))
if !strings.Contains(result, "==") {
t.Errorf("expected custom H1 underline '==', got %q", result)
}
})
t.Run("custom H2 underline", func(t *testing.T) {
result := stripANSI(ty.H2("Hi"))
if !strings.Contains(result, "--") {
t.Errorf("expected custom H2 underline '--', got %q", result)
}
})
t.Run("custom H3 underline", func(t *testing.T) {
result := stripANSI(ty.H3("Hi"))
if !strings.Contains(result, "..") {
t.Errorf("expected custom H3 underline '..', got %q", result)
}
})
t.Run("custom bar prefix", func(t *testing.T) {
result := stripANSI(ty.H4("Hi"))
if !strings.Contains(result, "| Hi") {
t.Errorf("expected custom bar prefix '| Hi', got %q", result)
}
})
}
// ---------------------------------------------------------------------------
// Paragraph
// ---------------------------------------------------------------------------
func TestP(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
}{
{"normal", "Hello, world."},
{"empty", ""},
{"multiline", "Line one.\nLine two."},
{"special chars", "a < b && c > d"},
{"long", strings.Repeat("word ", 200)},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.P(tc.text)
if tc.text != "" && !strings.Contains(stripANSI(result), tc.text) {
t.Errorf("P(%q) missing text in output", tc.text)
}
})
}
}
// ---------------------------------------------------------------------------
// Blockquote
// ---------------------------------------------------------------------------
func TestBlockquote(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
contains string
}{
{"simple", "A wise quote.", ty.theme.BlockquoteBar},
{"multiline", "Line 1\nLine 2", ty.theme.BlockquoteBar},
{"empty", "", ty.theme.BlockquoteBar},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.Blockquote(tc.text)
if !strings.Contains(stripANSI(result), tc.contains) {
t.Errorf("Blockquote should contain %q, got %q", tc.contains, stripANSI(result))
}
})
}
}
func TestBlockquoteMultilineHasBars(t *testing.T) {
ty := New(WithBlockquoteBar("|"))
result := ty.Blockquote("Line 1\nLine 2\nLine 3")
// Each line should have the bar
if strings.Count(stripANSI(result), "|") < 3 {
t.Errorf("expected at least 3 bars in multiline blockquote, got %q", stripANSI(result))
}
}
// ---------------------------------------------------------------------------
// Lists
// ---------------------------------------------------------------------------
func TestUL(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
items []string
}{
{"three items", []string{"Apples", "Bananas", "Cherries"}},
{"single item", []string{"Only one"}},
{"empty list", nil},
{"empty strings", []string{"", "", ""}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.UL(tc.items...)
if len(tc.items) == 0 {
if result != "" {
t.Errorf("UL with no items should be empty, got %q", result)
}
return
}
for _, item := range tc.items {
if item != "" && !strings.Contains(stripANSI(result), item) {
t.Errorf("UL missing item %q in %q", item, stripANSI(result))
}
}
})
}
}
func TestULCustomBullet(t *testing.T) {
ty := New(WithBulletChar("-"))
result := ty.UL("Item")
if !strings.Contains(stripANSI(result), "-") {
t.Errorf("expected custom bullet '-' in %q", stripANSI(result))
}
}
func TestOL(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
items []string
}{
{"three items", []string{"First", "Second", "Third"}},
{"single item", []string{"Only"}},
{"empty list", nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.OL(tc.items...)
if len(tc.items) == 0 {
if result != "" {
t.Errorf("OL with no items should be empty, got %q", result)
}
return
}
// Check numbering
if !strings.Contains(stripANSI(result), "1.") {
t.Errorf("OL should contain '1.' in %q", stripANSI(result))
}
for _, item := range tc.items {
if item != "" && !strings.Contains(stripANSI(result), item) {
t.Errorf("OL missing item %q in %q", item, stripANSI(result))
}
}
})
}
}
func TestOLNumbering(t *testing.T) {
ty := newTestTypography()
result := ty.OL("A", "B", "C")
for _, n := range []string{"1.", "2.", "3."} {
if !strings.Contains(stripANSI(result), n) {
t.Errorf("OL missing number %q in %q", n, stripANSI(result))
}
}
}
// ---------------------------------------------------------------------------
// Code
// ---------------------------------------------------------------------------
func TestCode(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
}{
{"simple", "fmt.Println()"},
{"empty", ""},
{"special", "x := map[string]int{}"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.Code(tc.text)
if tc.text != "" && !strings.Contains(stripANSI(result), tc.text) {
t.Errorf("Code(%q) missing text in %q", tc.text, stripANSI(result))
}
})
}
}
func TestCodeWithLanguage(t *testing.T) {
formatter := func(code, language string) string {
return "«" + language + ":" + code + "»"
}
tests := []struct {
name string
formatter func(code, language string) string
lang []string
text string
contains string
excludes string
}{
{
name: "no formatter no lang",
formatter: nil,
lang: nil,
text: "x := 1",
contains: "x := 1",
},
{
name: "formatter but no lang",
formatter: formatter,
lang: nil,
text: "x := 1",
contains: "x := 1",
excludes: "«",
},
{
name: "formatter with empty lang",
formatter: formatter,
lang: []string{""},
text: "x := 1",
contains: "x := 1",
excludes: "«",
},
{
name: "formatter with lang",
formatter: formatter,
lang: []string{"go"},
text: "x := 1",
contains: "«go:x := 1»",
},
{
name: "nil formatter with lang",
formatter: nil,
lang: []string{"go"},
text: "x := 1",
contains: "x := 1",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var opts []Option
if tc.formatter != nil {
opts = append(opts, WithCodeFormatter(tc.formatter))
}
ty := New(opts...)
result := stripANSI(ty.Code(tc.text, tc.lang...))
if !strings.Contains(result, tc.contains) {
t.Errorf("Code: expected %q in %q", tc.contains, result)
}
if tc.excludes != "" && strings.Contains(result, tc.excludes) {
t.Errorf("Code: did not expect %q in %q", tc.excludes, result)
}
})
}
}
func TestCodeBlock(t *testing.T) {
ty := newTestTypography()
code := "func main() {\n\tfmt.Println(\"hello\")\n}"
result := ty.CodeBlock(code)
if !strings.Contains(stripANSI(result), "func main()") {
t.Errorf("CodeBlock should contain source code, got %q", stripANSI(result))
}
}
func TestCodeBlockWithLanguage(t *testing.T) {
formatter := func(code, language string) string {
return "«" + language + ":" + code + "»"
}
tests := []struct {
name string
formatter func(code, language string) string
lang []string
text string
contains string
excludes string
}{
{
name: "no formatter no lang",
formatter: nil,
lang: nil,
text: "fmt.Println()",
contains: "fmt.Println()",
},
{
name: "formatter but no lang",
formatter: formatter,
lang: nil,
text: "fmt.Println()",
contains: "fmt.Println()",
excludes: "«",
},
{
name: "formatter with lang",
formatter: formatter,
lang: []string{"go"},
text: "fmt.Println()",
contains: "«go:fmt.Println()»",
},
{
name: "nil formatter with lang",
formatter: nil,
lang: []string{"go"},
text: "fmt.Println()",
contains: "fmt.Println()",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var opts []Option
if tc.formatter != nil {
opts = append(opts, WithCodeFormatter(tc.formatter))
}
ty := New(opts...)
result := stripANSI(ty.CodeBlock(tc.text, tc.lang...))
if !strings.Contains(result, tc.contains) {
t.Errorf("CodeBlock: expected %q in %q", tc.contains, result)
}
if tc.excludes != "" && strings.Contains(result, tc.excludes) {
t.Errorf("CodeBlock: did not expect %q in %q", tc.excludes, result)
}
})
}
}
func TestCodeBlockLineNumbers(t *testing.T) {
t.Run("disabled by default", func(t *testing.T) {
ty := New()
result := stripANSI(ty.CodeBlock("line one\nline two"))
if strings.Contains(result, "1│") || strings.Contains(result, "1"+DefaultCodeLineNumberSep) {
t.Errorf("line numbers should be off by default, got %q", result)
}
})
t.Run("enabled shows line numbers", func(t *testing.T) {
ty := New(WithCodeLineNumbers(true))
result := stripANSI(ty.CodeBlock("aaa\nbbb\nccc"))
if !strings.Contains(result, "1"+DefaultCodeLineNumberSep+" aaa") {
t.Errorf("expected line 1 with separator, got %q", result)
}
if !strings.Contains(result, "3"+DefaultCodeLineNumberSep+" ccc") {
t.Errorf("expected line 3 with separator, got %q", result)
}
})
t.Run("multi-digit line numbers are right-aligned", func(t *testing.T) {
lines := make([]string, 12)
for i := range lines {
lines[i] = "x"
}
ty := New(WithCodeLineNumbers(true))
result := stripANSI(ty.CodeBlock(strings.Join(lines, "\n")))
// Single-digit lines should be padded: " 1│"
if !strings.Contains(result, " 1"+DefaultCodeLineNumberSep) {
t.Errorf("expected padded single-digit line number, got %q", result)
}
if !strings.Contains(result, "12"+DefaultCodeLineNumberSep) {
t.Errorf("expected line 12, got %q", result)
}
})
t.Run("custom separator", func(t *testing.T) {
ty := New(WithCodeLineNumbers(true), WithCodeLineNumberSep(":"))
result := stripANSI(ty.CodeBlock("hello"))
if !strings.Contains(result, "1: hello") {
t.Errorf("expected custom separator ':', got %q", result)
}
})
t.Run("with formatter", func(t *testing.T) {
formatter := func(code, lang string) string {
return "[" + code + "]"
}
ty := New(WithCodeLineNumbers(true), WithCodeFormatter(formatter))
result := stripANSI(ty.CodeBlock("x := 1", "go"))
if !strings.Contains(result, "1"+DefaultCodeLineNumberSep+" [x := 1]") {
t.Errorf("line numbers should wrap formatted content, got %q", result)
}
})
t.Run("single line", func(t *testing.T) {
ty := New(WithCodeLineNumbers(true))
result := stripANSI(ty.CodeBlock("only one line"))
if !strings.Contains(result, "1"+DefaultCodeLineNumberSep+" only one line") {
t.Errorf("expected single line number, got %q", result)
}
})
}
func TestCodeBlockLineNumberOffset(t *testing.T) {
t.Run("custom offset", func(t *testing.T) {
ty := New(WithCodeLineNumbers(true), WithCodeLineNumberOffset(42))
result := stripANSI(ty.CodeBlock("aaa\nbbb\nccc"))
if !strings.Contains(result, "42"+DefaultCodeLineNumberSep+" aaa") {
t.Errorf("expected line 42, got %q", result)
}
if !strings.Contains(result, "44"+DefaultCodeLineNumberSep+" ccc") {
t.Errorf("expected line 44, got %q", result)
}
})
t.Run("offset widens gutter", func(t *testing.T) {
ty := New(WithCodeLineNumbers(true), WithCodeLineNumberOffset(99))
result := stripANSI(ty.CodeBlock("x\ny\nz"))
// Lines 99, 100, 101 -> width 3 -> "99" should be padded to " 99"
if !strings.Contains(result, " 99"+DefaultCodeLineNumberSep) {
t.Errorf("expected padded line 99, got %q", result)
}
if !strings.Contains(result, "101"+DefaultCodeLineNumberSep) {
t.Errorf("expected line 101, got %q", result)
}
})
t.Run("offset ignored when line numbers disabled", func(t *testing.T) {
ty := New(WithCodeLineNumberOffset(10))
result := stripANSI(ty.CodeBlock("hello"))
if strings.Contains(result, "10") {
t.Errorf("offset should have no effect when line numbers disabled, got %q", result)
}
})
}
// ---------------------------------------------------------------------------
// HR
// ---------------------------------------------------------------------------
func TestHR(t *testing.T) {
ty := newTestTypography()
result := ty.HR()
if result == "" {
t.Error("HR should not be empty")
}
}
func TestHRCustomWidth(t *testing.T) {
ty := New(WithHRWidth(10), WithHRChar("-"))
result := ty.HR()
if !strings.Contains(stripANSI(result), "----------") {
t.Errorf("HR with width 10 and char '-' should contain 10 dashes, got %q", stripANSI(result))
}
}
func TestHRWithLabel(t *testing.T) {
tests := []struct {
name string
label string
hrWidth int
hrChar string
wantHR bool // true if we expect HR chars in output
wantText string
}{
{
name: "basic label",
label: "Section",
hrWidth: 40,
hrChar: "-",
wantHR: true,
wantText: "Section",
},
{
name: "empty label falls back to HR",
label: "",
hrWidth: 40,
hrChar: "-",
wantHR: true,
wantText: "",
},
{
name: "label longer than width",
label: "This is a very long label that exceeds the HR width",
hrWidth: 10,
hrChar: "-",
wantHR: false,
wantText: "This is a very long label that exceeds the HR width",
},
{
name: "HR chars on both sides",
label: "Mid",
hrWidth: 20,
hrChar: "=",
wantHR: true,
wantText: "Mid",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ty := New(WithHRWidth(tc.hrWidth), WithHRChar(tc.hrChar))
result := stripANSI(ty.HRWithLabel(tc.label))
if tc.wantText != "" && !strings.Contains(result, tc.wantText) {
t.Errorf("expected result to contain %q, got %q", tc.wantText, result)
}
if tc.wantHR && !strings.Contains(result, tc.hrChar) {
t.Errorf("expected HR char %q in result, got %q", tc.hrChar, result)
}
})
}
t.Run("empty label matches HR output", func(t *testing.T) {
ty := New(WithHRWidth(20), WithHRChar("-"))
hrResult := stripANSI(ty.HR())
labelResult := stripANSI(ty.HRWithLabel(""))
if hrResult != labelResult {
t.Errorf("empty label should match HR(), got HR=%q, HRWithLabel=%q", hrResult, labelResult)
}
})
t.Run("HR chars appear on both sides of label", func(t *testing.T) {
ty := New(WithHRWidth(20), WithHRChar("-"))
result := stripANSI(ty.HRWithLabel("X"))
// Split around the label
parts := strings.SplitN(result, "X", 2)
if len(parts) != 2 {
t.Fatalf("expected label 'X' in result, got %q", result)
}
if !strings.Contains(parts[0], "-") {
t.Errorf("expected HR chars before label, got %q", parts[0])
}
if !strings.Contains(parts[1], "-") {
t.Errorf("expected HR chars after label, got %q", parts[1])
}
})
}
// ---------------------------------------------------------------------------
// Inline styles
// ---------------------------------------------------------------------------
func TestInlineStyles(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
fn func(string) string
text string
}{
{"Bold", ty.Bold, "bold text"},
{"Bold empty", ty.Bold, ""},
{"Italic", ty.Italic, "italic text"},
{"Underline", ty.Underline, "underlined"},
{"Strikethrough", ty.Strikethrough, "removed"},
{"Small", ty.Small, "fine print"},
{"Mark", ty.Mark, "highlighted"},
{"Kbd", ty.Kbd, "Ctrl+C"},
{"Kbd empty", ty.Kbd, ""},
{"Cite", ty.Cite, "citation text"},
{"Samp", ty.Samp, "sample output"},
{"Var", ty.Var, "myVariable"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.fn(tc.text)
if tc.text != "" && !strings.Contains(stripANSI(result), tc.text) {
t.Errorf("%s(%q) missing text in output %q", tc.name, tc.text, stripANSI(result))
}
})
}
}
// ---------------------------------------------------------------------------
// Link
// ---------------------------------------------------------------------------
func TestLink(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
label string
url []string
contains string
}{
{"label only", "Click here", nil, "Click here"},
{"label and url", "Go", []string{"https://go.dev"}, "Go"},
{"same label and url", "https://go.dev", []string{"https://go.dev"}, "https://go.dev"},
{"empty url", "Link", []string{""}, "Link"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.Link(tc.label, tc.url...)
if !strings.Contains(stripANSI(result), tc.contains) {
t.Errorf("Link: expected %q in %q", tc.contains, stripANSI(result))
}
})
}
}
func TestLinkWithURL(t *testing.T) {
ty := newTestTypography()
result := ty.Link("Go website", "https://go.dev")
if !strings.Contains(stripANSI(result), "https://go.dev") {
t.Errorf("Link with separate URL should contain the URL, got %q", stripANSI(result))
}
}
// ---------------------------------------------------------------------------
// Abbr
// ---------------------------------------------------------------------------
func TestAbbr(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
abbr string
desc []string
contains string
}{
{"no desc", "HTML", nil, "HTML"},
{"with desc", "CSS", []string{"Cascading Style Sheets"}, "Cascading Style Sheets"},
{"empty desc", "JS", []string{""}, "JS"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ty.Abbr(tc.abbr, tc.desc...)
if !strings.Contains(stripANSI(result), tc.contains) {
t.Errorf("Abbr: expected %q in %q", tc.contains, stripANSI(result))
}
})
}
}
// ---------------------------------------------------------------------------
// Sub / Sup
// ---------------------------------------------------------------------------
func TestSubSup(t *testing.T) {
ty := newTestTypography()
t.Run("Sub", func(t *testing.T) {
result := ty.Sub("2")
if !strings.Contains(stripANSI(result), "_2") {
t.Errorf("Sub should contain '_2', got %q", result)
}
})
t.Run("Sup", func(t *testing.T) {
result := ty.Sup("2")
if !strings.Contains(stripANSI(result), "^2") {
t.Errorf("Sup should contain '^2', got %q", result)
}
})
t.Run("Sub empty", func(t *testing.T) {
result := ty.Sub("")
if !strings.Contains(stripANSI(result), "_") {
t.Errorf("Sub('') should contain '_', got %q", result)
}
})
t.Run("Sup empty", func(t *testing.T) {
result := ty.Sup("")
if !strings.Contains(stripANSI(result), "^") {
t.Errorf("Sup('') should contain '^', got %q", result)
}
})
}
// ---------------------------------------------------------------------------
// Ins / Del
// ---------------------------------------------------------------------------
func TestInsDel(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
fn func(string) string
text string
contains string
}{
{"Ins basic", ty.Ins, "added line", "+added line"},
{"Del basic", ty.Del, "removed line", "-removed line"},
{"Ins empty", ty.Ins, "", "+"},
{"Del empty", ty.Del, "", "-"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := stripANSI(tc.fn(tc.text))
if !strings.Contains(result, tc.contains) {
t.Errorf("expected %q in %q", tc.contains, result)
}
})
}
}
func TestInsDelCustomPrefix(t *testing.T) {
ty := New(WithInsPrefix("++ "), WithDelPrefix("-- "))
t.Run("custom ins prefix", func(t *testing.T) {
result := stripANSI(ty.Ins("new"))
if !strings.Contains(result, "++ new") {
t.Errorf("expected '++ new' in %q", result)
}
})
t.Run("custom del prefix", func(t *testing.T) {
result := stripANSI(ty.Del("old"))
if !strings.Contains(result, "-- old") {
t.Errorf("expected '-- old' in %q", result)
}
})
}
// ---------------------------------------------------------------------------
// Q / Cite / Samp / Var
// ---------------------------------------------------------------------------
func TestQ(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
contains string
}{
{"basic", "hello world", "\u201Chello world\u201D"},
{"empty", "", "\u201C\u201D"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := stripANSI(ty.Q(tc.text))
if !strings.Contains(result, tc.contains) {
t.Errorf("Q(%q): expected %q in %q", tc.text, tc.contains, result)
}
})
}
}
func TestQCustomQuotes(t *testing.T) {
ty := New(WithQuoteOpen("<<"), WithQuoteClose(">>"))
result := stripANSI(ty.Q("test"))
if !strings.Contains(result, "<<test>>") {
t.Errorf("expected custom quotes in %q", result)
}
}
func TestCite(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
}{
{"basic", "The Go Programming Language"},
{"empty", ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := stripANSI(ty.Cite(tc.text))
if tc.text != "" && !strings.Contains(result, tc.text) {
t.Errorf("Cite(%q): expected text in %q", tc.text, result)
}
})
}
}
func TestSamp(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
}{
{"basic", "Error: file not found"},
{"empty", ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := stripANSI(ty.Samp(tc.text))
if tc.text != "" && !strings.Contains(result, tc.text) {
t.Errorf("Samp(%q): expected text in %q", tc.text, result)
}
})
}
}
func TestVar(t *testing.T) {
ty := newTestTypography()
tests := []struct {
name string
text string
}{
{"basic", "maxRetries"},
{"empty", ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := stripANSI(ty.Var(tc.text))
if tc.text != "" && !strings.Contains(result, tc.text) {
t.Errorf("Var(%q): expected text in %q", tc.text, result)
}
})
}
}
// ---------------------------------------------------------------------------
// Figure
// ---------------------------------------------------------------------------
func TestFigure(t *testing.T) {
ty := newTestTypography()
t.Run("caption at bottom", func(t *testing.T) {
result := stripANSI(ty.Figure("content here", "Figure 1: description"))
lines := strings.Split(result, "\n")
if len(lines) < 2 {
t.Fatalf("expected at least 2 lines, got %d", len(lines))
}
if !strings.Contains(lines[0], "content here") {
t.Errorf("expected content on first line, got %q", lines[0])
}
if !strings.Contains(lines[len(lines)-1], "Figure 1: description") {
t.Errorf("expected caption on last line, got %q", lines[len(lines)-1])
}
})
t.Run("empty caption", func(t *testing.T) {
result := stripANSI(ty.Figure("content", ""))
if !strings.Contains(result, "content") {
t.Errorf("expected content in result, got %q", result)
}
})
t.Run("empty content", func(t *testing.T) {
result := stripANSI(ty.Figure("", "caption"))
if !strings.Contains(result, "caption") {
t.Errorf("expected caption in result, got %q", result)
}
})
}
func TestFigureTop(t *testing.T) {