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