github.com/jxskiss/gopkg@v0.17.3/strutil/bom_test.go (about)

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