bom_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package bom_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "testing"
  6. "go-common/app/admin/main/member/model/bom"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. var testCases = []struct {
  10. Input []byte
  11. Expected []byte
  12. }{
  13. {
  14. Input: nil,
  15. Expected: nil,
  16. },
  17. {
  18. Input: []byte{},
  19. Expected: []byte{},
  20. },
  21. {
  22. Input: []byte{0xef},
  23. Expected: []byte{0xef},
  24. },
  25. {
  26. Input: []byte{0xef, 0xbb},
  27. Expected: []byte{0xef, 0xbb},
  28. },
  29. {
  30. Input: []byte{0xef, 0xbb, 0xbf},
  31. Expected: []byte{},
  32. },
  33. {
  34. Input: []byte{0xef, 0xbb, 0xbf, 0x41, 0x42, 0x43},
  35. Expected: []byte{0x41, 0x42, 0x43},
  36. },
  37. {
  38. Input: []byte{0xef, 0xbb, 0x41, 0x42, 0x43},
  39. Expected: []byte{0xef, 0xbb, 0x41, 0x42, 0x43},
  40. },
  41. {
  42. Input: []byte{0xef, 0x41, 0x42, 0x43},
  43. Expected: []byte{0xef, 0x41, 0x42, 0x43},
  44. },
  45. {
  46. Input: []byte{0x41, 0x42, 0x43},
  47. Expected: []byte{0x41, 0x42, 0x43},
  48. },
  49. }
  50. func TestClean(t *testing.T) {
  51. assert := assert.New(t)
  52. for _, tc := range testCases {
  53. output := bom.Clean(tc.Input)
  54. assert.Equal(tc.Expected, output)
  55. }
  56. }
  57. func TestReader(t *testing.T) {
  58. assert := assert.New(t)
  59. for _, tc := range testCases {
  60. // An input value of nil works differently to the Clean function.
  61. // In this case it results in an empty buffer, not nil.
  62. expected := tc.Expected
  63. if tc.Input == nil {
  64. expected = []byte{}
  65. }
  66. r1 := bytes.NewReader(tc.Input)
  67. r2 := bom.NewReader(r1)
  68. output, err := ioutil.ReadAll(r2)
  69. assert.NoError(err)
  70. assert.Equal(expected, output)
  71. }
  72. }