utils_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package util
  7. import "testing"
  8. type Defaulter struct {
  9. Value string
  10. }
  11. func (Defaulter) ParseDefault(v string) (interface{}, error) {
  12. return Defaulter{Value: v}, nil
  13. }
  14. func TestSetDefaults(t *testing.T) {
  15. x := &struct {
  16. A string `default:"string"`
  17. B int `default:"2"`
  18. C float64 `default:"2.2"`
  19. D bool `default:"true"`
  20. E Defaulter `default:"defaulter"`
  21. }{}
  22. if x.A != "" {
  23. t.Error("string failed")
  24. } else if x.B != 0 {
  25. t.Error("int failed")
  26. } else if x.C != 0 {
  27. t.Errorf("float failed")
  28. } else if x.D {
  29. t.Errorf("bool failed")
  30. } else if x.E.Value != "" {
  31. t.Errorf("defaulter failed")
  32. }
  33. if err := SetDefaults(x); err != nil {
  34. t.Error(err)
  35. }
  36. if x.A != "string" {
  37. t.Error("string failed")
  38. } else if x.B != 2 {
  39. t.Error("int failed")
  40. } else if x.C != 2.2 {
  41. t.Errorf("float failed")
  42. } else if !x.D {
  43. t.Errorf("bool failed")
  44. } else if x.E.Value != "defaulter" {
  45. t.Errorf("defaulter failed")
  46. }
  47. }
  48. func TestUniqueStrings(t *testing.T) {
  49. tests := []struct {
  50. input []string
  51. expected []string
  52. }{
  53. {
  54. []string{"a", "b"},
  55. []string{"a", "b"},
  56. },
  57. {
  58. []string{"a", "a"},
  59. []string{"a"},
  60. },
  61. {
  62. []string{"a", "a", "a", "a"},
  63. []string{"a"},
  64. },
  65. {
  66. nil,
  67. nil,
  68. },
  69. {
  70. []string{"b", "a"},
  71. []string{"a", "b"},
  72. },
  73. {
  74. []string{" a ", " a ", "b ", " b"},
  75. []string{"a", "b"},
  76. },
  77. }
  78. for _, test := range tests {
  79. result := UniqueStrings(test.input)
  80. if len(result) != len(test.expected) {
  81. t.Errorf("%s != %s", result, test.expected)
  82. }
  83. for i := range result {
  84. if test.expected[i] != result[i] {
  85. t.Errorf("%s != %s", result, test.expected)
  86. }
  87. }
  88. }
  89. }
  90. func TestFillNillSlices(t *testing.T) {
  91. // Nil
  92. x := &struct {
  93. A []string `default:"a,b"`
  94. }{}
  95. if x.A != nil {
  96. t.Error("not nil")
  97. }
  98. if err := FillNilSlices(x); err != nil {
  99. t.Error(err)
  100. }
  101. if len(x.A) != 2 {
  102. t.Error("length")
  103. }
  104. // Already provided
  105. y := &struct {
  106. A []string `default:"c,d,e"`
  107. }{[]string{"a", "b"}}
  108. if len(y.A) != 2 {
  109. t.Error("length")
  110. }
  111. if err := FillNilSlices(y); err != nil {
  112. t.Error(err)
  113. }
  114. if len(y.A) != 2 {
  115. t.Error("length")
  116. }
  117. // Non-nil but empty
  118. z := &struct {
  119. A []string `default:"c,d,e"`
  120. }{[]string{}}
  121. if len(z.A) != 0 {
  122. t.Error("length")
  123. }
  124. if err := FillNilSlices(z); err != nil {
  125. t.Error(err)
  126. }
  127. if len(z.A) != 0 {
  128. t.Error("length")
  129. }
  130. }
  131. func TestAddress(t *testing.T) {
  132. tests := []struct {
  133. network string
  134. host string
  135. result string
  136. }{
  137. {"tcp", "google.com", "tcp://google.com"},
  138. {"foo", "google", "foo://google"},
  139. {"123", "456", "123://456"},
  140. }
  141. for _, test := range tests {
  142. result := Address(test.network, test.host)
  143. if result != test.result {
  144. t.Errorf("%s != %s", result, test.result)
  145. }
  146. }
  147. }