decode_test.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package vcard
  2. import (
  3. "bufio"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/suite"
  8. )
  9. type LineTestSuite struct {
  10. suite.Suite
  11. }
  12. func (suite *LineTestSuite) TestString() {
  13. t := suite.T()
  14. l := line{Key: "key", Value: "value"}
  15. assert.Equal(t, "key:value", l.String())
  16. }
  17. func TestLineTestSuite(t *testing.T) {
  18. suite.Run(t, new(LineTestSuite))
  19. }
  20. type SplitLineTestSuite struct {
  21. suite.Suite
  22. }
  23. func (suite *SplitLineTestSuite) TestNoColon() {
  24. t := suite.T()
  25. l, err := splitLine("")
  26. assert.Nil(t, l)
  27. assert.Error(t, err)
  28. assert.Equal(t, ErrNoColons, err)
  29. }
  30. func (suite *SplitLineTestSuite) TestMultipleColons() {
  31. t := suite.T()
  32. expected := line{Key: "key", Value: "value:wtf"}
  33. l, err := splitLine(expected.String())
  34. assert.NoError(t, err)
  35. assert.NotNil(t, l)
  36. assert.Equal(t, expected, *l)
  37. }
  38. func (suite *SplitLineTestSuite) TestOneColon() {
  39. t := suite.T()
  40. expected := line{Key: "key", Value: "value"}
  41. l, err := splitLine(expected.String())
  42. assert.NoError(t, err)
  43. assert.NotNil(t, l)
  44. assert.Equal(t, expected, *l)
  45. }
  46. func TestSplitLineTestSuite(t *testing.T) {
  47. suite.Run(t, new(SplitLineTestSuite))
  48. }
  49. type GetvCardLinesTestSuite struct {
  50. suite.Suite
  51. }
  52. func (suite *GetvCardLinesTestSuite) TestNoMatchingEnd() {
  53. t := suite.T()
  54. l := line{Key: BeginKey, Value: VCard}
  55. r := strings.NewReader(l.String())
  56. s := bufio.NewScanner(r)
  57. ls, err := getvCardLines(s)
  58. assert.Error(t, err)
  59. assert.Equal(t, ErrNoMatchingEnd, err)
  60. assert.Nil(t, ls)
  61. }
  62. func (suite *GetvCardLinesTestSuite) TestNoColon() {
  63. t := suite.T()
  64. r := strings.NewReader("no colon")
  65. s := bufio.NewScanner(r)
  66. ls, err := getvCardLines(s)
  67. assert.Error(t, err)
  68. assert.Equal(t, ErrNoColons, err)
  69. assert.Nil(t, ls)
  70. }
  71. func (suite *GetvCardLinesTestSuite) TestSuccess() {
  72. t := suite.T()
  73. expected := vCardLines{
  74. line{
  75. Key: "key1",
  76. Value: "value1",
  77. },
  78. line{
  79. Key: "key1",
  80. Value: "value2",
  81. },
  82. }
  83. r := strings.NewReader(strings.Join(
  84. []string{
  85. expected[0].String(),
  86. expected[1].String(),
  87. line{Key: EndKey, Value: VCard}.String(),
  88. },
  89. "\n",
  90. ))
  91. s := bufio.NewScanner(r)
  92. ls, err := getvCardLines(s)
  93. assert.NoError(t, err)
  94. assert.NotNil(t, ls)
  95. assert.Equal(t, expected, ls)
  96. }
  97. func (suite *GetvCardLinesTestSuite) TestNoLines() {
  98. t := suite.T()
  99. s := bufio.NewScanner(strings.NewReader(""))
  100. ls, err := getvCardLines(s)
  101. assert.Nil(t, ls)
  102. assert.Error(t, err)
  103. assert.Equal(t, ErrNoMatchingEnd, err)
  104. }
  105. func TestGetvCardLinesTestSuite(t *testing.T) {
  106. suite.Run(t, new(GetvCardLinesTestSuite))
  107. }
  108. type GetvCardLinesSliceTestSuite struct {
  109. suite.Suite
  110. }
  111. func (suite *GetvCardLinesSliceTestSuite) TestNoLines() {
  112. t := suite.T()
  113. r := strings.NewReader("")
  114. ls, err := getvCardLinesSlice(r)
  115. assert.NotNil(t, ls)
  116. assert.NoError(t, err)
  117. assert.Equal(t, []vCardLines{}, ls)
  118. }
  119. func (suite *GetvCardLinesSliceTestSuite) TestNoMatchingEnd() {
  120. t := suite.T()
  121. r := strings.NewReader(line{Key: BeginKey, Value: VCard}.String())
  122. ls, err := getvCardLinesSlice(r)
  123. assert.Nil(t, ls)
  124. assert.Error(t, err)
  125. assert.Equal(t, ErrNoMatchingEnd, err)
  126. }
  127. func (suite *GetvCardLinesSliceTestSuite) TestNoMatchingBegin() {
  128. t := suite.T()
  129. r := strings.NewReader(line{Key: EndKey, Value: VCard}.String())
  130. ls, err := getvCardLinesSlice(r)
  131. assert.Nil(t, ls)
  132. assert.Error(t, err)
  133. assert.Equal(t, ErrNoMatchingBegin, err)
  134. }
  135. func (suite *GetvCardLinesSliceTestSuite) TestSuccess() {
  136. t := suite.T()
  137. expected := []vCardLines{
  138. vCardLines{
  139. line{Key: VersionKey, Value: VCardVersion},
  140. },
  141. vCardLines{
  142. line{Key: VersionKey, Value: VCardVersion},
  143. },
  144. }
  145. r := strings.NewReader(strings.Join(
  146. []string{
  147. line{Key: BeginKey, Value: VCard}.String(),
  148. expected[0].String(),
  149. line{Key: EndKey, Value: VCard}.String(),
  150. line{Key: BeginKey, Value: VCard}.String(),
  151. expected[1].String(),
  152. line{Key: EndKey, Value: VCard}.String(),
  153. },
  154. "\n",
  155. ))
  156. ls, err := getvCardLinesSlice(r)
  157. assert.NoError(t, err)
  158. assert.NotNil(t, ls)
  159. assert.Equal(t, expected, ls)
  160. }
  161. func (suite *GetvCardLinesSliceTestSuite) TestErrorInGetLines() {
  162. t := suite.T()
  163. r := strings.NewReader(strings.Join(
  164. []string{
  165. line{Key: BeginKey, Value: VCard}.String(),
  166. line{Key: BeginKey, Value: VCard}.String(),
  167. },
  168. "\n",
  169. ))
  170. ls, err := getvCardLinesSlice(r)
  171. assert.Error(t, err)
  172. assert.Equal(t, ErrNoMatchingEnd, err)
  173. assert.Nil(t, ls)
  174. }
  175. func TestGetvCardLinesSliceTestSuite(t *testing.T) {
  176. suite.Run(t, new(GetvCardLinesSliceTestSuite))
  177. }