diff_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package diff
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // TestApplyHighlighting tests the applyHighlighting function with various ANSI sequences
  9. func TestApplyHighlighting(t *testing.T) {
  10. t.Parallel()
  11. // Mock theme colors for testing
  12. mockHighlightBg := lipgloss.AdaptiveColor{
  13. Dark: "#FF0000", // Red background for highlighting
  14. Light: "#FF0000",
  15. }
  16. // Test cases
  17. tests := []struct {
  18. name string
  19. content string
  20. segments []Segment
  21. segmentType LineType
  22. expectContains string
  23. }{
  24. {
  25. name: "Simple text with no ANSI",
  26. content: "This is a test",
  27. segments: []Segment{{Start: 0, End: 4, Type: LineAdded}},
  28. segmentType: LineAdded,
  29. // Should contain full reset sequence after highlighting
  30. expectContains: "\x1b[0m",
  31. },
  32. {
  33. name: "Text with existing ANSI foreground",
  34. content: "This \x1b[32mis\x1b[0m a test", // "is" in green
  35. segments: []Segment{{Start: 5, End: 7, Type: LineAdded}},
  36. segmentType: LineAdded,
  37. // Should contain full reset sequence after highlighting
  38. expectContains: "\x1b[0m",
  39. },
  40. {
  41. name: "Text with existing ANSI background",
  42. content: "This \x1b[42mis\x1b[0m a test", // "is" with green background
  43. segments: []Segment{{Start: 5, End: 7, Type: LineAdded}},
  44. segmentType: LineAdded,
  45. // Should contain full reset sequence after highlighting
  46. expectContains: "\x1b[0m",
  47. },
  48. {
  49. name: "Text with complex ANSI styling",
  50. content: "This \x1b[1;32;45mis\x1b[0m a test", // "is" bold green on magenta
  51. segments: []Segment{{Start: 5, End: 7, Type: LineAdded}},
  52. segmentType: LineAdded,
  53. // Should contain full reset sequence after highlighting
  54. expectContains: "\x1b[0m",
  55. },
  56. }
  57. for _, tc := range tests {
  58. tc := tc // Capture range variable for parallel testing
  59. t.Run(tc.name, func(t *testing.T) {
  60. t.Parallel()
  61. result := applyHighlighting(tc.content, tc.segments, tc.segmentType, mockHighlightBg)
  62. // Verify the result contains the expected sequence
  63. assert.Contains(t, result, tc.expectContains,
  64. "Result should contain full reset sequence")
  65. // Print the result for manual inspection if needed
  66. if t.Failed() {
  67. fmt.Printf("Original: %q\nResult: %q\n", tc.content, result)
  68. }
  69. })
  70. }
  71. }
  72. // TestApplyHighlightingWithMultipleSegments tests highlighting multiple segments
  73. func TestApplyHighlightingWithMultipleSegments(t *testing.T) {
  74. t.Parallel()
  75. // Mock theme colors for testing
  76. mockHighlightBg := lipgloss.AdaptiveColor{
  77. Dark: "#FF0000", // Red background for highlighting
  78. Light: "#FF0000",
  79. }
  80. content := "This is a test with multiple segments to highlight"
  81. segments := []Segment{
  82. {Start: 0, End: 4, Type: LineAdded}, // "This"
  83. {Start: 8, End: 9, Type: LineAdded}, // "a"
  84. {Start: 15, End: 23, Type: LineAdded}, // "multiple"
  85. }
  86. result := applyHighlighting(content, segments, LineAdded, mockHighlightBg)
  87. // Verify the result contains the full reset sequence
  88. assert.Contains(t, result, "\x1b[0m",
  89. "Result should contain full reset sequence")
  90. }