|
| 1 | +package streaming |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/charmbracelet/glamour" |
| 8 | +) |
| 9 | + |
| 10 | +// TestGlamourParity verifies streaming output exactly matches glamour. |
| 11 | +func TestGlamourParity(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + input string |
| 15 | + }{ |
| 16 | + {"simple heading", "# Hello\n"}, |
| 17 | + {"heading and paragraph", "# Hello\n\nWorld\n"}, |
| 18 | + {"two paragraphs", "Hello\n\nWorld\n"}, |
| 19 | + {"heading paragraph list", "# Title\n\nParagraph\n\n- Item 1\n- Item 2\n\nDone.\n"}, |
| 20 | + {"code block", "```go\nfmt.Println(\"hi\")\n```\n"}, |
| 21 | + {"mixed content", "# Heading\n\nThis is a paragraph.\n\n- Item 1\n- Item 2\n\n```\ncode\n```\n\nDone.\n"}, |
| 22 | + } |
| 23 | + |
| 24 | + for _, tt := range tests { |
| 25 | + t.Run(tt.name, func(t *testing.T) { |
| 26 | + // Glamour direct render |
| 27 | + tr, err := glamour.NewTermRenderer(glamour.WithStandardStyle("dark")) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Failed to create glamour renderer: %v", err) |
| 30 | + } |
| 31 | + glamourOut, err := tr.RenderBytes([]byte(tt.input)) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Glamour render failed: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Streaming render (all at once) |
| 37 | + var buf bytes.Buffer |
| 38 | + sr, err := NewRenderer(&buf, glamour.WithStandardStyle("dark")) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("Failed to create streaming renderer: %v", err) |
| 41 | + } |
| 42 | + sr.Write([]byte(tt.input)) |
| 43 | + sr.Close() |
| 44 | + |
| 45 | + if buf.String() != string(glamourOut) { |
| 46 | + t.Errorf("Parity failed\nInput: %q\nGlamour len: %d, newlines: %d\nStreaming len: %d, newlines: %d\nGlamour: %q\nStreaming: %q", |
| 47 | + tt.input, |
| 48 | + len(glamourOut), bytes.Count(glamourOut, []byte("\n")), |
| 49 | + buf.Len(), bytes.Count(buf.Bytes(), []byte("\n")), |
| 50 | + glamourOut, |
| 51 | + buf.String()) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestGlamourParityChunked verifies streaming output matches glamour even when chunked. |
| 58 | +func TestGlamourParityChunked(t *testing.T) { |
| 59 | + input := "# Hello\n\nWorld\n" |
| 60 | + |
| 61 | + // Glamour direct render |
| 62 | + tr, _ := glamour.NewTermRenderer(glamour.WithStandardStyle("dark")) |
| 63 | + glamourOut, _ := tr.RenderBytes([]byte(input)) |
| 64 | + |
| 65 | + // Streaming render byte-by-byte |
| 66 | + var buf bytes.Buffer |
| 67 | + sr, _ := NewRenderer(&buf, glamour.WithStandardStyle("dark")) |
| 68 | + for i := 0; i < len(input); i++ { |
| 69 | + sr.Write([]byte{input[i]}) |
| 70 | + } |
| 71 | + sr.Close() |
| 72 | + |
| 73 | + if buf.String() != string(glamourOut) { |
| 74 | + t.Errorf("Chunked parity failed\nGlamour: %q\nStreaming: %q", glamourOut, buf.String()) |
| 75 | + } |
| 76 | +} |
0 commit comments