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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
// Tideland Go Library - String Extensions - Unit Tests
//
// Copyright (C) 2015-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package stringex_test
//--------------------
// IMPORTS
//--------------------
import (
"errors"
"math"
"strconv"
"testing"
"time"
"github.com/tideland/golib/audit"
"github.com/tideland/golib/stringex"
)
//--------------------
// CONSTANTS
//--------------------
const (
maxUint = ^uint(0)
minUint = 0
maxInt = int(maxUint >> 1)
minInt = -maxInt - 1
)
//--------------------
// TESTS
//--------------------
// TestAsString checks the access of string values.
func TestAsString(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("AsString", true)
tests := []struct {
v valuer
dv string
expected string
}{
{valuer{"foo", false}, "bar", "foo"},
{valuer{"foo", true}, "bar", "bar"},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
sv := d.AsString(test.v, test.dv)
assert.Equal(sv, test.expected)
}
}
// TestAsStringSlice checks the access of string slice values.
func TestAsStringSlice(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("AsStringSlice", true)
tests := []struct {
v valuer
sep string
dv []string
expected []string
}{
{valuer{"a/b/c", false}, "/", []string{"a"}, []string{"a", "b", "c"}},
{valuer{"a/b/c", true}, "/", []string{"a"}, []string{"a"}},
{valuer{"a/b/c", false}, "/", nil, []string{"a", "b", "c"}},
{valuer{"a/b/c", true}, "/", nil, nil},
{valuer{"", false}, "/", nil, []string{""}},
{valuer{"", true}, "/", []string{"foo"}, []string{"foo"}},
{valuer{"a/b/c", false}, ":", []string{"a"}, []string{"a/b/c"}},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
sv := d.AsStringSlice(test.v, test.sep, test.dv)
assert.Equal(sv, test.expected)
}
}
// TestAsStringMap checks the access of string map values.
func TestAsStringMap(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("AsStringMap", true)
tests := []struct {
v valuer
rsep string
kvsep string
dv map[string]string
expected map[string]string
}{
{valuer{"a:1/b:2", false}, "/", ":", map[string]string{"a": "1"}, map[string]string{"a": "1", "b": "2"}},
{valuer{"a:1/b:2", true}, "/", ":", map[string]string{"a": "1"}, map[string]string{"a": "1"}},
{valuer{"a:1/b:2", false}, "/", ":", nil, map[string]string{"a": "1", "b": "2"}},
{valuer{"a:1/b:2", true}, "/", ":", nil, nil},
{valuer{"", false}, "/", ":", nil, map[string]string{"": ""}},
{valuer{"", true}, "/", ":", map[string]string{"a": "1"}, map[string]string{"a": "1"}},
{valuer{"a:1/b:2", false}, "|", "=", nil, map[string]string{"a:1/b:2": "a:1/b:2"}},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
sv := d.AsStringMap(test.v, test.rsep, test.kvsep, test.dv)
assert.Equal(sv, test.expected)
}
}
// TestAsBool checks the access of bool values.
func TestAsBool(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("AsBool", true)
tests := []struct {
v valuer
dv bool
expected bool
}{
{valuer{"1", false}, false, true},
{valuer{"t", false}, false, true},
{valuer{"T", false}, false, true},
{valuer{"TRUE", false}, false, true},
{valuer{"true", false}, false, true},
{valuer{"True", false}, false, true},
{valuer{"wahr", false}, true, true},
{valuer{"", true}, true, true},
{valuer{"0", false}, true, false},
{valuer{"f", false}, true, false},
{valuer{"F", false}, true, false},
{valuer{"FALSE", false}, true, false},
{valuer{"false", false}, true, false},
{valuer{"False", false}, true, false},
{valuer{"falsch", false}, false, false},
{valuer{"", true}, false, false},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsBool(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsInt checks the access of int values.
func TestAsInt(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
maxIntS := strconv.FormatInt(int64(maxInt), 10)
minIntS := strconv.FormatInt(int64(minInt), 10)
d := stringex.NewDefaulter("AsInt", true)
tests := []struct {
v valuer
dv int
expected int
}{
{valuer{"0", false}, 0, 0},
{valuer{"1", false}, 0, 1},
{valuer{"-1", false}, 0, -1},
{valuer{maxIntS, false}, 0, maxInt},
{valuer{minIntS, false}, 0, minInt},
{valuer{"999999999999999999999", false}, 1, 1},
{valuer{"-999999999999999999999", false}, 1, 1},
{valuer{"one two three", false}, 1, 1},
{valuer{"1", true}, 2, 2},
{valuer{"-1", true}, -2, -2},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsInt(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsInt64 checks the access of int64 values.
func TestAsInt64(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
maxInt64S := strconv.FormatInt(math.MaxInt64, 10)
minInt64S := strconv.FormatInt(math.MinInt64, 10)
d := stringex.NewDefaulter("AsInt64", true)
tests := []struct {
v valuer
dv int64
expected int64
}{
{valuer{"0", false}, 0, 0},
{valuer{"1", false}, 0, 1},
{valuer{"-1", false}, 0, -1},
{valuer{maxInt64S, false}, 0, math.MaxInt64},
{valuer{minInt64S, false}, 0, math.MinInt64},
{valuer{"999999999999999999999", false}, 1, 1},
{valuer{"-999999999999999999999", false}, 1, 1},
{valuer{"one two three", false}, 1, 1},
{valuer{"1", true}, 2, 2},
{valuer{"-1", true}, -2, -2},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsInt64(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsUint checks the access of uint values.
func TestAsUint(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
maxUintS := strconv.FormatUint(uint64(maxUint), 10)
d := stringex.NewDefaulter("AsUint", true)
tests := []struct {
v valuer
dv uint
expected uint
}{
{valuer{"0", false}, 0, 0},
{valuer{"1", false}, 0, 1},
{valuer{maxUintS, false}, 0, maxUint},
{valuer{"999999999999999999999", false}, 1, 1},
{valuer{"one two three", false}, 1, 1},
{valuer{"-1", true}, 1, 1},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsUint(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsUInt64 checks the access of uint64 values.
func TestAsUInt64(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
maxUInt64S := strconv.FormatUint(math.MaxUint64, 10)
d := stringex.NewDefaulter("AsUInt64", true)
tests := []struct {
v valuer
dv uint64
expected uint64
}{
{valuer{"0", false}, 0, 0},
{valuer{"1", false}, 0, 1},
{valuer{maxUInt64S, false}, 0, math.MaxUint64},
{valuer{"999999999999999999999", false}, 1, 1},
{valuer{"one two three", false}, 1, 1},
{valuer{"-1", true}, 1, 1},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsUint64(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsFloat64 checks the access of float64 values.
func TestAsFloat64(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
maxFloat64S := strconv.FormatFloat(math.MaxFloat64, 'e', -1, 64)
minFloat64S := strconv.FormatFloat(-1*math.MaxFloat64, 'e', -1, 64)
d := stringex.NewDefaulter("AsFloat4", true)
tests := []struct {
v valuer
dv float64
expected float64
}{
{valuer{"0.0", false}, 0.0, 0.0},
{valuer{"1.0", false}, 0.0, 1.0},
{valuer{"-1.0", false}, 0.0, -1.0},
{valuer{maxFloat64S, false}, 0.0, math.MaxFloat64},
{valuer{minFloat64S, false}, 0.0, math.MaxFloat64 * -1.0},
{valuer{"9e+999", false}, 1.0, 1.0},
{valuer{"-9e+999", false}, 1.0, 1.0},
{valuer{"one.two", false}, 1.0, 1.0},
{valuer{"1.0", true}, 2.0, 2.0},
{valuer{"-1.0", true}, -2.0, -2.0},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsFloat64(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsTime checks the access of time values.
func TestAsTime(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
y2k := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
nowStr, now := audit.BuildTime(time.RFC3339Nano, 0)
d := stringex.NewDefaulter("AsTime", true)
tests := []struct {
v valuer
layout string
dv time.Time
expected time.Time
}{
{valuer{nowStr, false}, time.RFC3339Nano, y2k, now},
{valuer{nowStr, true}, time.RFC3339Nano, y2k, y2k},
{valuer{nowStr, false}, "any false layout", y2k, y2k},
{valuer{"", false}, time.RFC3339Nano, y2k, y2k},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsTime(test.v, test.layout, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestAsDuration checks the access of duration values.
func TestAsDuration(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("AsDuration", true)
tests := []struct {
v valuer
dv time.Duration
expected time.Duration
}{
{valuer{"1s", false}, time.Second, time.Second},
{valuer{"1s", true}, time.Minute, time.Minute},
{valuer{"2", false}, time.Minute, time.Minute},
{valuer{"1 hour", false}, time.Minute, time.Minute},
{valuer{"4711h", false}, time.Minute, 4711 * time.Hour},
}
for i, test := range tests {
assert.Logf("test %v %d: %v and default %v", d, i, test.v, test.dv)
bv := d.AsDuration(test.v, test.dv)
assert.Equal(bv, test.expected)
}
}
// TestDefaulterString checks the output of the defaulter as string.
func TestDefaulterString(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("my-id", true)
s := d.String()
assert.Equal(s, "Defaulter{my-id}")
}
// TestStringValuer checks the simple valuer for plain strings.
func StringValuer(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
d := stringex.NewDefaulter("StringValuer", false)
sv := stringex.StringValuer("4711")
assert.Equal(d.AsString(sv, "12345"), "4711")
assert.Equal(d.AsInt(sv, 12345), 4711)
sv = stringex.StringValuer("")
assert.Equal(d.AsString(sv, "12345"), "12345")
assert.Equal(d.AsInt(sv, 12345), 12345)
}
//--------------------
// HELPER
//--------------------
type valuer struct {
value string
err bool
}
func (v valuer) Value() (string, error) {
if v.err {
return "", errors.New(v.value)
}
return v.value, nil
}
func (v valuer) String() string {
if v.err {
return "value '" + v.value + "' with error"
}
return "value '" + v.value + "' without error"
}
// EOF
|