utils_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 https://mozilla.org/MPL/2.0/.
  6. package util
  7. import (
  8. "testing"
  9. )
  10. type Defaulter struct {
  11. Value string
  12. }
  13. func (d *Defaulter) ParseDefault(v string) error {
  14. *d = Defaulter{Value: v}
  15. return nil
  16. }
  17. func TestSetDefaults(t *testing.T) {
  18. x := &struct {
  19. A string `default:"string"`
  20. B int `default:"2"`
  21. C float64 `default:"2.2"`
  22. D bool `default:"true"`
  23. E Defaulter `default:"defaulter"`
  24. }{}
  25. if x.A != "" {
  26. t.Error("string failed")
  27. } else if x.B != 0 {
  28. t.Error("int failed")
  29. } else if x.C != 0 {
  30. t.Errorf("float failed")
  31. } else if x.D {
  32. t.Errorf("bool failed")
  33. } else if x.E.Value != "" {
  34. t.Errorf("defaulter failed")
  35. }
  36. SetDefaults(x)
  37. if x.A != "string" {
  38. t.Error("string failed")
  39. } else if x.B != 2 {
  40. t.Error("int failed")
  41. } else if x.C != 2.2 {
  42. t.Errorf("float failed")
  43. } else if !x.D {
  44. t.Errorf("bool failed")
  45. } else if x.E.Value != "defaulter" {
  46. t.Errorf("defaulter failed")
  47. }
  48. }
  49. func TestUniqueStrings(t *testing.T) {
  50. tests := []struct {
  51. input []string
  52. expected []string
  53. }{
  54. {
  55. []string{"a", "b"},
  56. []string{"a", "b"},
  57. },
  58. {
  59. []string{"a", "a"},
  60. []string{"a"},
  61. },
  62. {
  63. []string{"a", "a", "a", "a"},
  64. []string{"a"},
  65. },
  66. {
  67. nil,
  68. nil,
  69. },
  70. {
  71. []string{" a ", " a ", "b ", " b"},
  72. []string{"a", "b"},
  73. },
  74. }
  75. for _, test := range tests {
  76. result := UniqueTrimmedStrings(test.input)
  77. if len(result) != len(test.expected) {
  78. t.Errorf("%s != %s", result, test.expected)
  79. }
  80. for i := range result {
  81. if test.expected[i] != result[i] {
  82. t.Errorf("%s != %s", result, test.expected)
  83. }
  84. }
  85. }
  86. }
  87. func TestFillNillSlices(t *testing.T) {
  88. // Nil
  89. x := &struct {
  90. A []string `default:"a,b"`
  91. }{}
  92. if x.A != nil {
  93. t.Error("not nil")
  94. }
  95. if err := FillNilSlices(x); err != nil {
  96. t.Error(err)
  97. }
  98. if len(x.A) != 2 {
  99. t.Error("length")
  100. }
  101. // Already provided
  102. y := &struct {
  103. A []string `default:"c,d,e"`
  104. }{[]string{"a", "b"}}
  105. if len(y.A) != 2 {
  106. t.Error("length")
  107. }
  108. if err := FillNilSlices(y); err != nil {
  109. t.Error(err)
  110. }
  111. if len(y.A) != 2 {
  112. t.Error("length")
  113. }
  114. // Non-nil but empty
  115. z := &struct {
  116. A []string `default:"c,d,e"`
  117. }{[]string{}}
  118. if len(z.A) != 0 {
  119. t.Error("length")
  120. }
  121. if err := FillNilSlices(z); err != nil {
  122. t.Error(err)
  123. }
  124. if len(z.A) != 0 {
  125. t.Error("length")
  126. }
  127. }
  128. func TestAddress(t *testing.T) {
  129. tests := []struct {
  130. network string
  131. host string
  132. result string
  133. }{
  134. {"tcp", "google.com", "tcp://google.com"},
  135. {"foo", "google", "foo://google"},
  136. {"123", "456", "123://456"},
  137. }
  138. for _, test := range tests {
  139. result := Address(test.network, test.host)
  140. if result != test.result {
  141. t.Errorf("%s != %s", result, test.result)
  142. }
  143. }
  144. }
  145. func TestCopyMatching(t *testing.T) {
  146. type Nested struct {
  147. A int
  148. }
  149. type Test struct {
  150. CopyA int
  151. CopyB []string
  152. CopyC Nested
  153. CopyD *Nested
  154. NoCopy int `restart:"true"`
  155. }
  156. from := Test{
  157. CopyA: 1,
  158. CopyB: []string{"friend", "foe"},
  159. CopyC: Nested{
  160. A: 2,
  161. },
  162. CopyD: &Nested{
  163. A: 3,
  164. },
  165. NoCopy: 4,
  166. }
  167. to := Test{
  168. CopyA: 11,
  169. CopyB: []string{"foot", "toe"},
  170. CopyC: Nested{
  171. A: 22,
  172. },
  173. CopyD: &Nested{
  174. A: 33,
  175. },
  176. NoCopy: 44,
  177. }
  178. // Copy empty fields
  179. CopyMatchingTag(&from, &to, "restart", func(v string) bool {
  180. return v != "true"
  181. })
  182. if to.CopyA != 1 {
  183. t.Error("CopyA")
  184. }
  185. if len(to.CopyB) != 2 || to.CopyB[0] != "friend" || to.CopyB[1] != "foe" {
  186. t.Error("CopyB")
  187. }
  188. if to.CopyC.A != 2 {
  189. t.Error("CopyC")
  190. }
  191. if to.CopyD.A != 3 {
  192. t.Error("CopyC")
  193. }
  194. if to.NoCopy != 44 {
  195. t.Error("NoCopy")
  196. }
  197. }
  198. func TestFillNil(t *testing.T) {
  199. type A struct {
  200. Slice []int
  201. Map map[string]string
  202. Chan chan int
  203. }
  204. type B struct {
  205. Slice *[]int
  206. Map *map[string]string
  207. Chan *chan int
  208. }
  209. type C struct {
  210. A A
  211. B *B
  212. D *****[]int
  213. }
  214. c := C{}
  215. FillNil(&c)
  216. if c.A.Slice == nil {
  217. t.Error("c.A.Slice")
  218. }
  219. if c.A.Map == nil {
  220. t.Error("c.A.Slice")
  221. }
  222. if c.A.Chan == nil {
  223. t.Error("c.A.Chan")
  224. }
  225. if c.B == nil {
  226. t.Error("c.B")
  227. }
  228. if c.B.Slice == nil {
  229. t.Error("c.B.Slice")
  230. }
  231. if c.B.Map == nil {
  232. t.Error("c.B.Slice")
  233. }
  234. if c.B.Chan == nil {
  235. t.Error("c.B.Chan")
  236. }
  237. if *c.B.Slice == nil {
  238. t.Error("*c.B.Slice")
  239. }
  240. if *c.B.Map == nil {
  241. t.Error("*c.B.Slice")
  242. }
  243. if *c.B.Chan == nil {
  244. t.Error("*c.B.Chan")
  245. }
  246. if *****c.D == nil {
  247. t.Error("c.D")
  248. }
  249. }
  250. func TestFillNilDoesNotBulldozeSetFields(t *testing.T) {
  251. type A struct {
  252. Slice []int
  253. Map map[string]string
  254. Chan chan int
  255. }
  256. type B struct {
  257. Slice *[]int
  258. Map *map[string]string
  259. Chan *chan int
  260. }
  261. type C struct {
  262. A A
  263. B *B
  264. D **[]int
  265. }
  266. ch := make(chan int, 10)
  267. d := make([]int, 10)
  268. dd := &d
  269. c := C{
  270. A: A{
  271. Slice: []int{1},
  272. Map: map[string]string{
  273. "k": "v",
  274. },
  275. Chan: make(chan int, 10),
  276. },
  277. B: &B{
  278. Slice: &[]int{1},
  279. Map: &map[string]string{
  280. "k": "v",
  281. },
  282. Chan: &ch,
  283. },
  284. D: &dd,
  285. }
  286. FillNil(&c)
  287. if len(c.A.Slice) != 1 {
  288. t.Error("c.A.Slice")
  289. }
  290. if len(c.A.Map) != 1 {
  291. t.Error("c.A.Slice")
  292. }
  293. if cap(c.A.Chan) != 10 {
  294. t.Error("c.A.Chan")
  295. }
  296. if c.B == nil {
  297. t.Error("c.B")
  298. }
  299. if len(*c.B.Slice) != 1 {
  300. t.Error("c.B.Slice")
  301. }
  302. if len(*c.B.Map) != 1 {
  303. t.Error("c.B.Slice")
  304. }
  305. if cap(*c.B.Chan) != 10 {
  306. t.Error("c.B.Chan")
  307. }
  308. if cap(**c.D) != 10 {
  309. t.Error("c.D")
  310. }
  311. }