utils_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. func TestSetDefaults(t *testing.T) {
  9. x := &struct {
  10. A string `default:"string"`
  11. B int `default:"2"`
  12. C float64 `default:"2.2"`
  13. D bool `default:"true"`
  14. }{}
  15. if x.A != "" {
  16. t.Error("string failed")
  17. } else if x.B != 0 {
  18. t.Error("int failed")
  19. } else if x.C != 0 {
  20. t.Errorf("float failed")
  21. } else if x.D != false {
  22. t.Errorf("bool failed")
  23. }
  24. if err := SetDefaults(x); err != nil {
  25. t.Error(err)
  26. }
  27. if x.A != "string" {
  28. t.Error("string failed")
  29. } else if x.B != 2 {
  30. t.Error("int failed")
  31. } else if x.C != 2.2 {
  32. t.Errorf("float failed")
  33. } else if x.D != true {
  34. t.Errorf("bool failed")
  35. }
  36. }
  37. func TestUniqueStrings(t *testing.T) {
  38. tests := []struct {
  39. input []string
  40. expected []string
  41. }{
  42. {
  43. []string{"a", "b"},
  44. []string{"a", "b"},
  45. },
  46. {
  47. []string{"a", "a"},
  48. []string{"a"},
  49. },
  50. {
  51. []string{"a", "a", "a", "a"},
  52. []string{"a"},
  53. },
  54. {
  55. nil,
  56. nil,
  57. },
  58. {
  59. []string{"b", "a"},
  60. []string{"a", "b"},
  61. },
  62. {
  63. []string{" a ", " a ", "b ", " b"},
  64. []string{"a", "b"},
  65. },
  66. }
  67. for _, test := range tests {
  68. result := UniqueStrings(test.input)
  69. if len(result) != len(test.expected) {
  70. t.Errorf("%s != %s", result, test.expected)
  71. }
  72. for i := range result {
  73. if test.expected[i] != result[i] {
  74. t.Errorf("%s != %s", result, test.expected)
  75. }
  76. }
  77. }
  78. }
  79. func TestFillNillSlices(t *testing.T) {
  80. // Nil
  81. x := &struct {
  82. A []string `default:"a,b"`
  83. }{}
  84. if x.A != nil {
  85. t.Error("not nil")
  86. }
  87. if err := FillNilSlices(x); err != nil {
  88. t.Error(err)
  89. }
  90. if len(x.A) != 2 {
  91. t.Error("length")
  92. }
  93. // Already provided
  94. y := &struct {
  95. A []string `default:"c,d,e"`
  96. }{[]string{"a", "b"}}
  97. if len(y.A) != 2 {
  98. t.Error("length")
  99. }
  100. if err := FillNilSlices(y); err != nil {
  101. t.Error(err)
  102. }
  103. if len(y.A) != 2 {
  104. t.Error("length")
  105. }
  106. // Non-nil but empty
  107. z := &struct {
  108. A []string `default:"c,d,e"`
  109. }{[]string{}}
  110. if len(z.A) != 0 {
  111. t.Error("length")
  112. }
  113. if err := FillNilSlices(z); err != nil {
  114. t.Error(err)
  115. }
  116. if len(z.A) != 0 {
  117. t.Error("length")
  118. }
  119. }
  120. func TestAddress(t *testing.T) {
  121. tests := []struct {
  122. network string
  123. host string
  124. result string
  125. }{
  126. {"tcp", "google.com", "tcp://google.com"},
  127. {"foo", "google", "foo://google"},
  128. {"123", "456", "123://456"},
  129. }
  130. for _, test := range tests {
  131. result := Address(test.network, test.host)
  132. if result != test.result {
  133. t.Errorf("%s != %s", result, test.result)
  134. }
  135. }
  136. }