github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/strutil/bom_test.go (about) 1 package strutil 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestDetectBOM(t *testing.T) { 12 tests := []struct { 13 b []byte 14 want string 15 }{ 16 {[]byte{0xEF, 0xBB, 0xBF, 'a', 'b', 'c'}, BOM_UTF8}, 17 {[]byte{0xFE, 0xFF, 'a', 'b', 'c'}, BOM_UTF16_BigEndian}, 18 {[]byte{0xFF, 0xFE, 'a', 'b', 'c'}, BOM_UTF16_LittleEndian}, 19 {[]byte{0x00, 0x00, 0xFE, 0xFF, 'a', 'b', 'c'}, BOM_UTF32_BigEndian}, 20 {[]byte{0xFF, 0xFE, 0x00, 0x00, 'a', 'b', 'c'}, BOM_UTF32_LittleEndian}, 21 } 22 23 for _, x := range tests { 24 got := DetectBOM(x.b) 25 assert.Equal(t, x.want, got) 26 } 27 } 28 29 func TestTrimBOM(t *testing.T) { 30 tests := [][]byte{ 31 {0xEF, 0xBB, 0xBF, 'a', 'b', 'c'}, 32 {0xFE, 0xFF, 'a', 'b', 'c'}, 33 {0xFF, 0xFE, 'a', 'b', 'c'}, 34 {0x00, 0x00, 0xFE, 0xFF, 'a', 'b', 'c'}, 35 {0xFF, 0xFE, 0x00, 0x00, 'a', 'b', 'c'}, 36 } 37 want := []byte{'a', 'b', 'c'} 38 for _, x := range tests { 39 got := TrimBOM(x) 40 assert.Equal(t, want, got) 41 } 42 } 43 44 func TestSkipBOM(t *testing.T) { 45 tests := [][]byte{ 46 {0xEF, 0xBB, 0xBF, 'a', 'b', 'c'}, 47 {0xFE, 0xFF, 'a', 'b', 'c'}, 48 {0xFF, 0xFE, 'a', 'b', 'c'}, 49 {0x00, 0x00, 0xFE, 0xFF, 'a', 'b', 'c'}, 50 {0xFF, 0xFE, 0x00, 0x00, 'a', 'b', 'c'}, 51 } 52 want := []byte{'a', 'b', 'c'} 53 for _, x := range tests { 54 rd := bytes.NewBuffer(x) 55 tmp := SkipBOMReader(rd) 56 got, err := io.ReadAll(tmp) 57 assert.Nil(t, err) 58 assert.Equal(t, want, got) 59 } 60 61 tests = [][]byte{ 62 {0xEF, 0xBB, 0xBF}, 63 {0xFE, 0xFF}, 64 {0xFF, 0xFE}, 65 {0x00, 0x00, 0xFE, 0xFF}, 66 {0xFF, 0xFE, 0x00, 0x00}, 67 } 68 want = []byte{} 69 for _, x := range tests { 70 rd := bytes.NewBuffer(x) 71 tmp := SkipBOMReader(rd) 72 got, err := io.ReadAll(tmp) 73 assert.Nil(t, err) 74 assert.Equal(t, want, got) 75 } 76 }