name_test.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package vcard
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/suite"
  6. )
  7. type NameTestSuite struct {
  8. suite.Suite
  9. }
  10. func (suite *NameTestSuite) TestToVCard() {
  11. t := suite.T()
  12. name := Name{
  13. FirstName: "f",
  14. LastName: "l",
  15. AdditionalName: "a",
  16. Suffix: "s",
  17. Prefix: "p",
  18. }
  19. expected := "l;f;a;p;s"
  20. assert.Equal(t, expected, name.ToVCard())
  21. }
  22. func TestNameTestSuite(t *testing.T) {
  23. suite.Run(t, new(NameTestSuite))
  24. }
  25. type NameFromStringTestSuite struct {
  26. suite.Suite
  27. }
  28. func (suite *NameFromStringTestSuite) TestInvalidFormat() {
  29. t := suite.T()
  30. name, err := nameFromString(";First Name;")
  31. assert.Nil(t, name)
  32. assert.Error(t, err)
  33. assert.Equal(t, ErrInvalidNameFormat, err)
  34. }
  35. func (suite *NameFromStringTestSuite) TestOnlyFirstName() {
  36. t := suite.T()
  37. name, err := nameFromString(";First name;;;")
  38. assert.NotNil(t, name)
  39. assert.NoError(t, err)
  40. assert.Equal(t, Name{FirstName: "First name"}, *name)
  41. }
  42. func (suite *NameFromStringTestSuite) TestAllParts() {
  43. t := suite.T()
  44. expected := Name{
  45. FirstName: "First name",
  46. LastName: "Last name",
  47. AdditionalName: "Additional name",
  48. Suffix: "Suffix",
  49. Prefix: "Prefix",
  50. }
  51. name, err := nameFromString(expected.ToVCard())
  52. assert.NotNil(t, name)
  53. assert.NoError(t, err)
  54. assert.Equal(t, expected, *name)
  55. }
  56. func TestNameFromStringTestSuite(t *testing.T) {
  57. suite.Run(t, new(NameFromStringTestSuite))
  58. }