|
| 1 | +// Package normalization_test provides parity tests for normalization helpers. |
| 2 | +// Mirrors the behaviour of src/apm_cli/utils/normalization.py. |
| 3 | +package normalization_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/githubnext/apm/internal/utils/normalization" |
| 10 | +) |
| 11 | + |
| 12 | +// TestParityNormalizationStripBOM verifies BOM stripping matches Python. |
| 13 | +func TestParityNormalizationStripBOM(t *testing.T) { |
| 14 | + bom := normalization.BOM |
| 15 | + cases := []struct { |
| 16 | + name string |
| 17 | + input []byte |
| 18 | + expected []byte |
| 19 | + }{ |
| 20 | + {"no bom", []byte("hello"), []byte("hello")}, |
| 21 | + {"bom prefix", append(append([]byte{}, bom...), []byte("hello")...), []byte("hello")}, |
| 22 | + {"bom only", bom, []byte{}}, |
| 23 | + {"bom in middle not stripped", []byte("hel\xef\xbb\xbflo"), []byte("hel\xef\xbb\xbflo")}, |
| 24 | + } |
| 25 | + for _, tc := range cases { |
| 26 | + t.Run(tc.name, func(t *testing.T) { |
| 27 | + got := normalization.StripBOM(tc.input) |
| 28 | + if !bytes.Equal(got, tc.expected) { |
| 29 | + t.Errorf("got %q, want %q", got, tc.expected) |
| 30 | + } |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// TestParityNormalizationCRLF verifies CRLF-to-LF conversion matches Python. |
| 36 | +func TestParityNormalizationCRLF(t *testing.T) { |
| 37 | + cases := []struct { |
| 38 | + name string |
| 39 | + input []byte |
| 40 | + expected []byte |
| 41 | + }{ |
| 42 | + {"no crlf", []byte("hello\nworld\n"), []byte("hello\nworld\n")}, |
| 43 | + {"crlf", []byte("hello\r\nworld\r\n"), []byte("hello\nworld\n")}, |
| 44 | + {"bare cr preserved", []byte("hello\rworld"), []byte("hello\rworld")}, |
| 45 | + {"mixed", []byte("a\r\nb\nc\r\n"), []byte("a\nb\nc\n")}, |
| 46 | + } |
| 47 | + for _, tc := range cases { |
| 48 | + t.Run(tc.name, func(t *testing.T) { |
| 49 | + got := normalization.NormalizeLineEndings(tc.input) |
| 50 | + if !bytes.Equal(got, tc.expected) { |
| 51 | + t.Errorf("got %q, want %q", got, tc.expected) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestParityNormalizationStripBuildID verifies Build ID header stripping. |
| 58 | +func TestParityNormalizationStripBuildID(t *testing.T) { |
| 59 | + cases := []struct { |
| 60 | + name string |
| 61 | + input string |
| 62 | + expected string |
| 63 | + }{ |
| 64 | + {"no build id", "hello world", "hello world"}, |
| 65 | + {"build id", "<!-- Build ID: abc123 -->\nhello", "hello"}, |
| 66 | + {"build id uppercase", "<!-- BUILD ID: abc123 -->\nhello", "hello"}, |
| 67 | + {"build id no newline", "<!-- Build ID: abc123 -->hello", "hello"}, |
| 68 | + {"build id with spaces", "<!-- Build ID: abc123 -->\nhello", "hello"}, |
| 69 | + } |
| 70 | + for _, tc := range cases { |
| 71 | + t.Run(tc.name, func(t *testing.T) { |
| 72 | + got := string(normalization.StripBuildID([]byte(tc.input))) |
| 73 | + if got != tc.expected { |
| 74 | + t.Errorf("got %q, want %q", got, tc.expected) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestParityNormalizationNormalize verifies composite Normalize function. |
| 81 | +func TestParityNormalizationNormalize(t *testing.T) { |
| 82 | + bom := normalization.BOM |
| 83 | + // Build a payload with BOM + Build ID + CRLF |
| 84 | + input := append(append([]byte{}, bom...), []byte("<!-- Build ID: deadbeef -->\r\nhello\r\nworld\r\n")...) |
| 85 | + expected := []byte("hello\nworld\n") |
| 86 | + got := normalization.Normalize(input) |
| 87 | + if !bytes.Equal(got, expected) { |
| 88 | + t.Errorf("got %q, want %q", got, expected) |
| 89 | + } |
| 90 | +} |
0 commit comments