2014-04-05 09:21:52 +04:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cast
|
|
|
|
|
|
|
|
import (
|
2022-01-15 15:43:35 +03:00
|
|
|
"encoding/json"
|
2020-07-24 00:45:38 +03:00
|
|
|
"errors"
|
2017-03-03 21:39:13 +03:00
|
|
|
"fmt"
|
2014-12-09 16:17:55 +03:00
|
|
|
"html/template"
|
2019-05-30 12:21:02 +03:00
|
|
|
"path"
|
2022-05-11 11:35:17 +03:00
|
|
|
"reflect"
|
2015-11-22 23:21:13 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-01-01 07:48:55 +03:00
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
qt "github.com/frankban/quicktest"
|
2014-04-05 09:21:52 +04:00
|
|
|
)
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
type testStep struct {
|
|
|
|
input interface{}
|
|
|
|
expect interface{}
|
|
|
|
iserr bool
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightpoint31negative interface{}) []testStep {
|
|
|
|
var jeight, jminuseight, jfloateight json.Number
|
|
|
|
_ = json.Unmarshal([]byte("8"), &jeight)
|
|
|
|
_ = json.Unmarshal([]byte("-8"), &jminuseight)
|
|
|
|
_ = json.Unmarshal([]byte("8.0"), &jfloateight)
|
|
|
|
|
|
|
|
kind := reflect.TypeOf(zero).Kind()
|
|
|
|
isUint := kind == reflect.Uint || kind == reflect.Uint8 || kind == reflect.Uint16 || kind == reflect.Uint32 || kind == reflect.Uint64
|
|
|
|
|
|
|
|
// Some precision is lost when converting from float64 to float32.
|
|
|
|
eightpoint31_32 := eightpoint31
|
|
|
|
eightpoint31negative_32 := eightpoint31negative
|
|
|
|
if kind == reflect.Float64 {
|
|
|
|
eightpoint31_32 = float64(float32(eightpoint31.(float64)))
|
|
|
|
eightpoint31negative_32 = float64(float32(eightpoint31negative.(float64)))
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
return []testStep{
|
|
|
|
{int(8), eight, false},
|
|
|
|
{int8(8), eight, false},
|
|
|
|
{int16(8), eight, false},
|
|
|
|
{int32(8), eight, false},
|
|
|
|
{int64(8), eight, false},
|
|
|
|
{time.Weekday(8), eight, false},
|
|
|
|
{time.Month(8), eight, false},
|
|
|
|
{uint(8), eight, false},
|
|
|
|
{uint8(8), eight, false},
|
|
|
|
{uint16(8), eight, false},
|
|
|
|
{uint32(8), eight, false},
|
|
|
|
{uint64(8), eight, false},
|
|
|
|
{float32(8.31), eightpoint31_32, false},
|
|
|
|
{float64(8.31), eightpoint31, false},
|
|
|
|
{true, one, false},
|
|
|
|
{false, zero, false},
|
|
|
|
{"8", eight, false},
|
|
|
|
{nil, zero, false},
|
|
|
|
{int(-8), eightnegative, isUint},
|
|
|
|
{int8(-8), eightnegative, isUint},
|
|
|
|
{int16(-8), eightnegative, isUint},
|
|
|
|
{int32(-8), eightnegative, isUint},
|
|
|
|
{int64(-8), eightnegative, isUint},
|
|
|
|
{float32(-8.31), eightpoint31negative_32, isUint},
|
|
|
|
{float64(-8.31), eightpoint31negative, isUint},
|
|
|
|
{"-8", eightnegative, isUint},
|
|
|
|
{jeight, eight, false},
|
|
|
|
{jminuseight, eightnegative, isUint},
|
|
|
|
{jfloateight, eight, false},
|
|
|
|
{"test", zero, true},
|
|
|
|
{testing.T{}, zero, true},
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
// Maybe Go 1.18 generics will make this less ugly?
|
|
|
|
func runNumberTest(c *qt.C, tests []testStep, tove func(interface{}) (interface{}, error), tov func(interface{}) interface{}) {
|
|
|
|
c.Helper()
|
2017-03-03 22:44:11 +03:00
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i)
|
2017-03-03 22:44:11 +03:00
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
v, err := tove(test.input)
|
2017-03-03 22:44:11 +03:00
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil, errmsg)
|
2017-03-03 22:44:11 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil, errmsg)
|
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 22:44:11 +03:00
|
|
|
|
|
|
|
// Non-E test:
|
2022-05-11 11:35:17 +03:00
|
|
|
v = tov(test.input)
|
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func TestToUintE(t *testing.T) {
|
|
|
|
tests := createNumberTestSteps(uint(0), uint(1), uint(8), uint(0), uint(8), uint(8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToUintE(v) },
|
|
|
|
func(v interface{}) interface{} { return ToUint(v) },
|
|
|
|
)
|
|
|
|
}
|
2017-03-03 22:44:11 +03:00
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func TestToUint64E(t *testing.T) {
|
|
|
|
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
|
|
|
|
|
2024-03-12 17:17:08 +03:00
|
|
|
// Maximum value of uint64
|
|
|
|
tests = append(tests,
|
|
|
|
testStep{"18446744073709551615", uint64(18446744073709551615), false},
|
|
|
|
testStep{"18446744073709551616", uint64(0), true},
|
|
|
|
)
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToUint64E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToUint64(v) },
|
|
|
|
)
|
|
|
|
}
|
2017-03-03 22:44:11 +03:00
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func TestToUint32E(t *testing.T) {
|
|
|
|
tests := createNumberTestSteps(uint32(0), uint32(1), uint32(8), uint32(0), uint32(8), uint32(8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToUint32E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToUint32(v) },
|
|
|
|
)
|
|
|
|
}
|
2017-03-03 22:44:11 +03:00
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func TestToUint16E(t *testing.T) {
|
|
|
|
tests := createNumberTestSteps(uint16(0), uint16(1), uint16(8), uint16(0), uint16(8), uint16(8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToUint16E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToUint16(v) },
|
|
|
|
)
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestToUint8E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(uint8(0), uint8(1), uint8(8), uint8(0), uint8(8), uint8(8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToUint8E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToUint8(v) },
|
|
|
|
)
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToIntE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(int(0), int(1), int(8), int(-8), int(8), int(-8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToIntE(v) },
|
|
|
|
func(v interface{}) interface{} { return ToInt(v) },
|
|
|
|
)
|
2014-04-05 09:21:52 +04:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToInt64E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(int64(0), int64(1), int64(8), int64(-8), int64(8), int64(-8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToInt64E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToInt64(v) },
|
|
|
|
)
|
2016-01-28 21:41:34 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToInt32E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(int32(0), int32(1), int32(8), int32(-8), int32(8), int32(-8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToInt32E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToInt32(v) },
|
|
|
|
)
|
2017-02-10 10:00:46 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToInt16E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(int16(0), int16(1), int16(8), int16(-8), int16(8), int16(-8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToInt16E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToInt16(v) },
|
|
|
|
)
|
2017-02-10 10:00:46 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToInt8E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(int8(0), int8(1), int8(8), int8(-8), int8(8), int8(-8))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToInt8E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToInt8(v) },
|
|
|
|
)
|
2017-02-10 10:00:46 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToFloat64E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(float64(0), float64(1), float64(8), float64(-8), float64(8.31), float64(-8.31))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToFloat64E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToFloat64(v) },
|
|
|
|
)
|
2014-04-07 19:43:25 +04:00
|
|
|
}
|
|
|
|
|
2017-03-03 22:44:11 +03:00
|
|
|
func TestToFloat32E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
tests := createNumberTestSteps(float32(0), float32(1), float32(8), float32(-8), float32(8.31), float32(-8.31))
|
|
|
|
|
|
|
|
runNumberTest(
|
|
|
|
qt.New(t),
|
|
|
|
tests,
|
|
|
|
func(v interface{}) (interface{}, error) { return ToFloat32E(v) },
|
|
|
|
func(v interface{}) interface{} { return ToFloat32(v) },
|
|
|
|
)
|
2017-03-03 22:44:11 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToStringE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2022-01-15 15:43:35 +03:00
|
|
|
var jn json.Number
|
|
|
|
_ = json.Unmarshal([]byte("8"), &jn)
|
2017-03-03 21:39:13 +03:00
|
|
|
type Key struct {
|
|
|
|
k string
|
|
|
|
}
|
|
|
|
key := &Key{"foo"}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect string
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{int(8), "8", false},
|
2017-03-03 22:44:11 +03:00
|
|
|
{int8(8), "8", false},
|
|
|
|
{int16(8), "8", false},
|
|
|
|
{int32(8), "8", false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{int64(8), "8", false},
|
2017-03-03 22:44:11 +03:00
|
|
|
{uint(8), "8", false},
|
|
|
|
{uint8(8), "8", false},
|
|
|
|
{uint16(8), "8", false},
|
|
|
|
{uint32(8), "8", false},
|
|
|
|
{uint64(8), "8", false},
|
|
|
|
{float32(8.31), "8.31", false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{float64(8.31), "8.31", false},
|
2022-01-15 15:43:35 +03:00
|
|
|
{jn, "8", false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{true, "true", false},
|
|
|
|
{false, "false", false},
|
|
|
|
{nil, "", false},
|
|
|
|
{[]byte("one time"), "one time", false},
|
|
|
|
{"one more time", "one more time", false},
|
|
|
|
{template.HTML("one time"), "one time", false},
|
|
|
|
{template.URL("http://somehost.foo"), "http://somehost.foo", false},
|
|
|
|
{template.JS("(1+2)"), "(1+2)", false},
|
|
|
|
{template.CSS("a"), "a", false},
|
|
|
|
{template.HTMLAttr("a"), "a", false},
|
|
|
|
// errors
|
|
|
|
{testing.T{}, "", true},
|
|
|
|
{key, "", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToStringE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil, errmsg)
|
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToString(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
2014-04-05 23:18:23 +04:00
|
|
|
}
|
|
|
|
|
2015-01-01 07:48:55 +03:00
|
|
|
type foo struct {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x foo) String() string {
|
|
|
|
return x.val
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringerToString(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2015-01-01 07:48:55 +03:00
|
|
|
var x foo
|
|
|
|
x.val = "bar"
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(ToString(x), qt.Equals, "bar")
|
2015-01-01 07:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type fu struct {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x fu) Error() string {
|
|
|
|
return x.val
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorToString(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2015-01-01 07:48:55 +03:00
|
|
|
var x fu
|
|
|
|
x.val = "bar"
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(ToString(x), qt.Equals, "bar")
|
2015-01-01 07:48:55 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestStringMapStringSliceE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2015-07-31 21:44:36 +03:00
|
|
|
// ToStringMapString inputs/outputs
|
|
|
|
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
|
|
|
|
// ToStringMapStringSlice inputs/outputs
|
2017-03-03 15:53:30 +03:00
|
|
|
var stringMapStringSlice = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
|
|
|
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
2017-03-03 21:39:13 +03:00
|
|
|
var stringMapInterfaceInterfaceSlice = map[string]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
|
2017-03-03 15:53:30 +03:00
|
|
|
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": {"value", "1"}, "key 2": {"value", "2"}, "key 3": {"value", "3"}}
|
|
|
|
var interfaceMapStringSlice = map[interface{}][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
|
|
|
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
2015-07-31 21:44:36 +03:00
|
|
|
|
2017-03-03 15:53:30 +03:00
|
|
|
var stringMapStringSliceMultiple = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
|
|
|
var stringMapStringSliceSingle = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}, "key 3": {"value 3"}}
|
2015-07-31 21:44:36 +03:00
|
|
|
|
2016-09-19 23:22:46 +03:00
|
|
|
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
2017-03-03 15:53:30 +03:00
|
|
|
var stringMapInterfaceResult1 = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}}
|
2016-09-19 23:22:46 +03:00
|
|
|
|
2018-02-14 19:36:58 +03:00
|
|
|
var jsonStringMapString = `{"key 1": "value 1", "key 2": "value 2"}`
|
|
|
|
var jsonStringMapStringArray = `{"key 1": ["value 1"], "key 2": ["value 2", "value 3"]}`
|
|
|
|
var jsonStringMapStringArrayResult = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2", "value 3"}}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
type Key struct {
|
|
|
|
k string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect map[string][]string
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{stringMapStringSlice, stringMapStringSlice, false},
|
|
|
|
{stringMapInterfaceSlice, stringMapStringSlice, false},
|
|
|
|
{stringMapInterfaceInterfaceSlice, stringMapStringSlice, false},
|
|
|
|
{stringMapStringSliceMultiple, stringMapStringSlice, false},
|
|
|
|
{stringMapStringSliceMultiple, stringMapStringSlice, false},
|
|
|
|
{stringMapString, stringMapStringSliceSingle, false},
|
|
|
|
{stringMapInterface, stringMapStringSliceSingle, false},
|
|
|
|
{stringMapInterface1, stringMapInterfaceResult1, false},
|
|
|
|
{interfaceMapStringSlice, stringMapStringSlice, false},
|
|
|
|
{interfaceMapInterfaceSlice, stringMapStringSlice, false},
|
|
|
|
{interfaceMapString, stringMapStringSingleSliceFieldsResult, false},
|
|
|
|
{interfaceMapInterface, stringMapStringSingleSliceFieldsResult, false},
|
2018-02-14 19:36:58 +03:00
|
|
|
{jsonStringMapStringArray, jsonStringMapStringArrayResult, false},
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
{map[interface{}]interface{}{"foo": testing.T{}}, nil, true},
|
|
|
|
{map[interface{}]interface{}{Key{"foo"}: "bar"}, nil, true}, // ToStringE(Key{"foo"}) should fail
|
2018-02-14 19:36:58 +03:00
|
|
|
{jsonStringMapString, nil, true},
|
|
|
|
{"", nil, true},
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToStringMapStringSliceE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil, errmsg)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringMapStringSlice(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
2014-04-05 09:21:52 +04:00
|
|
|
}
|
2014-08-31 05:16:09 +04:00
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToStringMapE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect map[string]interface{}
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{map[interface{}]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
|
|
|
{map[string]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
2018-02-14 19:36:58 +03:00
|
|
|
{`{"tag": "tags", "group": "groups"}`, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
|
|
|
{`{"tag": "tags", "group": true}`, map[string]interface{}{"tag": "tags", "group": true}, false},
|
2019-11-04 15:10:43 +03:00
|
|
|
{struct {
|
|
|
|
Hello string
|
|
|
|
}{
|
|
|
|
Hello: "World",
|
|
|
|
}, map[string]interface{}{"Hello": "World"}, false},
|
|
|
|
{testing.T{}, map[string]interface{}{}, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
2018-02-14 19:36:58 +03:00
|
|
|
{"", nil, true},
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToStringMapE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil, errmsg)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringMap(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
2015-02-24 13:58:16 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToStringMapBoolE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect map[string]bool
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{map[interface{}]interface{}{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
|
|
|
|
{map[string]interface{}{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
|
|
|
|
{map[string]bool{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
|
2018-02-14 19:36:58 +03:00
|
|
|
{`{"v1": true, "v2": false}`, map[string]bool{"v1": true, "v2": false}, false},
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
2018-02-14 19:36:58 +03:00
|
|
|
{"", nil, true},
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToStringMapBoolE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil, errmsg)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringMapBool(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:00:14 +03:00
|
|
|
func TestToStringMapIntE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2018-10-24 21:00:14 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect map[string]int
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{map[interface{}]interface{}{"v1": 1, "v2": 222}, map[string]int{"v1": 1, "v2": 222}, false},
|
|
|
|
{map[string]interface{}{"v1": 342, "v2": 5141}, map[string]int{"v1": 342, "v2": 5141}, false},
|
|
|
|
{map[string]int{"v1": 33, "v2": 88}, map[string]int{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]int32{"v1": int32(33), "v2": int32(88)}, map[string]int{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]uint16{"v1": uint16(33), "v2": uint16(88)}, map[string]int{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]float64{"v1": float64(8.22), "v2": float64(43.32)}, map[string]int{"v1": 8, "v2": 43}, false},
|
|
|
|
{`{"v1": 67, "v2": 56}`, map[string]int{"v1": 67, "v2": 56}, false},
|
|
|
|
|
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
{"", nil, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2018-10-24 21:00:14 +03:00
|
|
|
|
|
|
|
v, err := ToStringMapIntE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil, errmsg)
|
2018-10-24 21:00:14 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil, errmsg)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2018-10-24 21:00:14 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringMapInt(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2018-10-24 21:00:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToStringMapInt64E(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2018-10-24 21:00:14 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect map[string]int64
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{map[interface{}]interface{}{"v1": int32(8), "v2": int32(888)}, map[string]int64{"v1": int64(8), "v2": int64(888)}, false},
|
|
|
|
{map[string]interface{}{"v1": int64(45), "v2": int64(67)}, map[string]int64{"v1": 45, "v2": 67}, false},
|
|
|
|
{map[string]int64{"v1": 33, "v2": 88}, map[string]int64{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]int{"v1": 33, "v2": 88}, map[string]int64{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]int32{"v1": int32(33), "v2": int32(88)}, map[string]int64{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]uint16{"v1": uint16(33), "v2": uint16(88)}, map[string]int64{"v1": 33, "v2": 88}, false},
|
|
|
|
{map[string]float64{"v1": float64(8.22), "v2": float64(43.32)}, map[string]int64{"v1": 8, "v2": 43}, false},
|
2018-10-25 01:49:29 +03:00
|
|
|
{`{"v1": 67, "v2": 56}`, map[string]int64{"v1": 67, "v2": 56}, false},
|
2018-10-24 21:00:14 +03:00
|
|
|
|
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
{"", nil, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2018-10-24 21:00:14 +03:00
|
|
|
|
|
|
|
v, err := ToStringMapInt64E(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2018-10-24 21:00:14 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2018-10-24 21:00:14 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringMapInt64(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2018-10-24 21:00:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToStringMapStringE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
|
|
|
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
2018-02-14 19:36:58 +03:00
|
|
|
var jsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}`
|
|
|
|
var invalidJsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"`
|
|
|
|
var emptyString = ""
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect map[string]string
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{stringMapString, stringMapString, false},
|
|
|
|
{stringMapInterface, stringMapString, false},
|
|
|
|
{interfaceMapString, stringMapString, false},
|
|
|
|
{interfaceMapInterface, stringMapString, false},
|
2018-02-14 19:36:58 +03:00
|
|
|
{jsonString, stringMapString, false},
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
2018-02-14 19:36:58 +03:00
|
|
|
{invalidJsonString, nil, true},
|
|
|
|
{emptyString, nil, true},
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToStringMapStringE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringMapString(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToBoolSliceE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect []bool
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{[]bool{true, false, true}, []bool{true, false, true}, false},
|
|
|
|
{[]interface{}{true, false, true}, []bool{true, false, true}, false},
|
|
|
|
{[]int{1, 0, 1}, []bool{true, false, true}, false},
|
|
|
|
{[]string{"true", "false", "true"}, []bool{true, false, true}, false},
|
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
{[]string{"foo", "bar"}, nil, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToBoolSliceE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToBoolSlice(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToIntSliceE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect []int
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{[]int{1, 3}, []int{1, 3}, false},
|
|
|
|
{[]interface{}{1.2, 3.2}, []int{1, 3}, false},
|
|
|
|
{[]string{"2", "3"}, []int{2, 3}, false},
|
|
|
|
{[2]string{"2", "3"}, []int{2, 3}, false},
|
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
{[]string{"foo", "bar"}, nil, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToIntSliceE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToIntSlice(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToSliceE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect []interface{}
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{[]interface{}{1, 3}, []interface{}{1, 3}, false},
|
|
|
|
{[]map[string]interface{}{{"k1": 1}, {"k2": 2}}, []interface{}{map[string]interface{}{"k1": 1}, map[string]interface{}{"k2": 2}}, false},
|
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToSliceE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToSlice(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToStringSliceE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect []string
|
|
|
|
iserr bool
|
|
|
|
}{
|
2019-04-04 17:45:22 +03:00
|
|
|
{[]int{1, 2}, []string{"1", "2"}, false},
|
|
|
|
{[]int8{int8(1), int8(2)}, []string{"1", "2"}, false},
|
|
|
|
{[]int32{int32(1), int32(2)}, []string{"1", "2"}, false},
|
|
|
|
{[]int64{int64(1), int64(2)}, []string{"1", "2"}, false},
|
|
|
|
{[]float32{float32(1.01), float32(2.01)}, []string{"1.01", "2.01"}, false},
|
|
|
|
{[]float64{float64(1.01), float64(2.01)}, []string{"1.01", "2.01"}, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{[]string{"a", "b"}, []string{"a", "b"}, false},
|
|
|
|
{[]interface{}{1, 3}, []string{"1", "3"}, false},
|
|
|
|
{interface{}(1), []string{"1"}, false},
|
2020-07-24 00:45:38 +03:00
|
|
|
{[]error{errors.New("a"), errors.New("b")}, []string{"a", "b"}, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToStringSliceE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToStringSlice(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 11:50:28 +03:00
|
|
|
func TestToDurationSliceE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-04-13 11:50:28 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect []time.Duration
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{[]string{"1s", "1m"}, []time.Duration{time.Second, time.Minute}, false},
|
|
|
|
{[]int{1, 2}, []time.Duration{1, 2}, false},
|
|
|
|
{[]interface{}{1, 3}, []time.Duration{1, 3}, false},
|
2018-02-14 20:35:30 +03:00
|
|
|
{[]time.Duration{1, 3}, []time.Duration{1, 3}, false},
|
|
|
|
|
2017-04-13 11:50:28 +03:00
|
|
|
// errors
|
|
|
|
{nil, nil, true},
|
|
|
|
{testing.T{}, nil, true},
|
2018-02-14 20:35:30 +03:00
|
|
|
{[]string{"invalid"}, nil, true},
|
2017-04-13 11:50:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-04-13 11:50:28 +03:00
|
|
|
|
|
|
|
v, err := ToDurationSliceE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-04-13 11:50:28 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-04-13 11:50:28 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToDurationSlice(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
|
2017-04-13 11:50:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToBoolE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2022-01-15 15:43:35 +03:00
|
|
|
var jf, jt, je json.Number
|
|
|
|
_ = json.Unmarshal([]byte("0"), &jf)
|
|
|
|
_ = json.Unmarshal([]byte("1"), &jt)
|
|
|
|
_ = json.Unmarshal([]byte("1.0"), &je)
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect bool
|
|
|
|
iserr bool
|
|
|
|
}{
|
|
|
|
{0, false, false},
|
2022-08-16 09:24:43 +03:00
|
|
|
{int64(0), false, false},
|
|
|
|
{int32(0), false, false},
|
|
|
|
{int16(0), false, false},
|
|
|
|
{int8(0), false, false},
|
|
|
|
{uint(0), false, false},
|
|
|
|
{uint64(0), false, false},
|
|
|
|
{uint32(0), false, false},
|
|
|
|
{uint16(0), false, false},
|
|
|
|
{uint8(0), false, false},
|
|
|
|
{float64(0), false, false},
|
|
|
|
{float32(0), false, false},
|
|
|
|
{time.Duration(0), false, false},
|
2022-01-15 15:43:35 +03:00
|
|
|
{jf, false, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{nil, false, false},
|
|
|
|
{"false", false, false},
|
|
|
|
{"FALSE", false, false},
|
|
|
|
{"False", false, false},
|
|
|
|
{"f", false, false},
|
|
|
|
{"F", false, false},
|
|
|
|
{false, false, false},
|
|
|
|
|
|
|
|
{"true", true, false},
|
|
|
|
{"TRUE", true, false},
|
|
|
|
{"True", true, false},
|
|
|
|
{"t", true, false},
|
|
|
|
{"T", true, false},
|
|
|
|
{1, true, false},
|
2022-08-16 09:24:43 +03:00
|
|
|
{int64(1), true, false},
|
|
|
|
{int32(1), true, false},
|
|
|
|
{int16(1), true, false},
|
|
|
|
{int8(1), true, false},
|
|
|
|
{uint(1), true, false},
|
|
|
|
{uint64(1), true, false},
|
|
|
|
{uint32(1), true, false},
|
|
|
|
{uint16(1), true, false},
|
|
|
|
{uint8(1), true, false},
|
|
|
|
{float64(1), true, false},
|
|
|
|
{float32(1), true, false},
|
|
|
|
{time.Duration(1), true, false},
|
2022-01-15 15:43:35 +03:00
|
|
|
{jt, true, false},
|
2022-05-11 11:35:17 +03:00
|
|
|
{je, true, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{true, true, false},
|
|
|
|
{-1, true, false},
|
2022-08-16 09:24:43 +03:00
|
|
|
{int64(-1), true, false},
|
|
|
|
{int32(-1), true, false},
|
|
|
|
{int16(-1), true, false},
|
|
|
|
{int8(-1), true, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// errors
|
|
|
|
{"test", false, true},
|
|
|
|
{testing.T{}, false, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToBoolE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToBool(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
}
|
2014-08-31 05:16:09 +04:00
|
|
|
}
|
2015-01-01 08:23:46 +03:00
|
|
|
|
2016-09-26 11:25:08 +03:00
|
|
|
func BenchmarkTooBool(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !ToBool(true) {
|
|
|
|
b.Fatal("ToBool returned false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 14:07:20 +03:00
|
|
|
func BenchmarkTooInt(b *testing.B) {
|
|
|
|
convert := func(num52 interface{}) {
|
|
|
|
if v := ToInt(num52); v != 52 {
|
|
|
|
b.Fatalf("ToInt returned wrong value, got %d, want %d", v, 32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
convert("52")
|
|
|
|
convert(52.0)
|
|
|
|
convert(uint64(52))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func BenchmarkTrimZeroDecimal(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
trimZeroDecimal("")
|
|
|
|
trimZeroDecimal("123")
|
|
|
|
trimZeroDecimal("120")
|
|
|
|
trimZeroDecimal("120.00")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:09:44 +03:00
|
|
|
func BenchmarkCommonTimeLayouts(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, commonLayout := range []string{"2019-04-29", "2017-05-30T00:00:00Z"} {
|
|
|
|
_, err := StringToDateInDefaultLocation(commonLayout, time.UTC)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 08:23:46 +03:00
|
|
|
func TestIndirectPointers(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2015-01-01 08:23:46 +03:00
|
|
|
x := 13
|
|
|
|
y := &x
|
|
|
|
z := &y
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(ToInt(y), qt.Equals, 13)
|
|
|
|
c.Assert(ToInt(z), qt.Equals, 13)
|
|
|
|
|
2015-01-01 08:23:46 +03:00
|
|
|
}
|
2015-11-22 23:21:13 +03:00
|
|
|
|
2019-05-30 12:21:02 +03:00
|
|
|
func TestToTime(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2022-01-15 15:43:35 +03:00
|
|
|
var jntime, jnetime json.Number
|
|
|
|
_ = json.Unmarshal([]byte("1234567890"), &jntime)
|
|
|
|
_ = json.Unmarshal([]byte("123.4567890"), &jnetime)
|
2017-03-03 21:39:13 +03:00
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expect time.Time
|
|
|
|
iserr bool
|
2016-12-24 20:25:03 +03:00
|
|
|
}{
|
2017-03-03 21:39:13 +03:00
|
|
|
{"2009-11-10 23:00:00 +0000 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // Time.String()
|
|
|
|
{"Tue Nov 10 23:00:00 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // ANSIC
|
|
|
|
{"Tue Nov 10 23:00:00 UTC 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // UnixDate
|
|
|
|
{"Tue Nov 10 23:00:00 +0000 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RubyDate
|
|
|
|
{"10 Nov 09 23:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC822
|
|
|
|
{"10 Nov 09 23:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC822Z
|
|
|
|
{"Tuesday, 10-Nov-09 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC850
|
|
|
|
{"Tue, 10 Nov 2009 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC1123
|
|
|
|
{"Tue, 10 Nov 2009 23:00:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC1123Z
|
|
|
|
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC3339
|
2018-10-22 00:22:53 +03:00
|
|
|
{"2018-10-21T23:21:29+0200", time.Date(2018, 10, 21, 21, 21, 29, 0, time.UTC), false}, // RFC3339 without timezone hh:mm colon
|
2017-03-03 21:39:13 +03:00
|
|
|
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC3339Nano
|
|
|
|
{"11:00PM", time.Date(0, 1, 1, 23, 0, 0, 0, time.UTC), false}, // Kitchen
|
|
|
|
{"Nov 10 23:00:00", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // Stamp
|
|
|
|
{"Nov 10 23:00:00.000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampMilli
|
|
|
|
{"Nov 10 23:00:00.000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampMicro
|
|
|
|
{"Nov 10 23:00:00.000000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampNano
|
|
|
|
{"2016-03-06 15:28:01-00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false}, // RFC3339 without T
|
2018-10-22 00:22:53 +03:00
|
|
|
{"2016-03-06 15:28:01-0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false}, // RFC3339 without T or timezone hh:mm colon
|
2017-03-03 21:39:13 +03:00
|
|
|
{"2016-03-06 15:28:01", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
|
|
|
|
{"2016-03-06 15:28:01 -0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
|
|
|
|
{"2016-03-06 15:28:01 -00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
|
2021-08-15 20:48:01 +03:00
|
|
|
{"2016-03-06 15:28:01 +0900", time.Date(2016, 3, 6, 6, 28, 1, 0, time.UTC), false},
|
|
|
|
{"2016-03-06 15:28:01 +09:00", time.Date(2016, 3, 6, 6, 28, 1, 0, time.UTC), false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
|
|
|
|
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
|
|
|
|
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, time.UTC), false},
|
|
|
|
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
|
|
|
|
{int64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
|
|
|
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
2017-03-03 22:44:11 +03:00
|
|
|
{uint(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
|
|
|
|
{uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
|
|
|
{uint32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
2022-01-15 15:43:35 +03:00
|
|
|
{jntime, time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
|
|
|
// errors
|
|
|
|
{"2006", time.Time{}, true},
|
2022-01-15 15:43:35 +03:00
|
|
|
{jnetime, time.Time{}, true},
|
2017-03-03 21:39:13 +03:00
|
|
|
{testing.T{}, time.Time{}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToTimeE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v.UTC(), qt.Equals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToTime(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v.UTC(), qt.Equals, test.expect, errmsg)
|
2016-12-24 20:25:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:13 +03:00
|
|
|
func TestToDurationE(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2016-09-19 23:24:24 +03:00
|
|
|
var td time.Duration = 5
|
2022-01-15 15:43:35 +03:00
|
|
|
var jn json.Number
|
|
|
|
_ = json.Unmarshal([]byte("5"), &jn)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
2016-09-19 23:24:24 +03:00
|
|
|
tests := []struct {
|
2017-03-03 21:39:13 +03:00
|
|
|
input interface{}
|
|
|
|
expect time.Duration
|
|
|
|
iserr bool
|
2016-09-19 23:24:24 +03:00
|
|
|
}{
|
2017-03-03 21:39:13 +03:00
|
|
|
{time.Duration(5), td, false},
|
|
|
|
{int(5), td, false},
|
|
|
|
{int64(5), td, false},
|
|
|
|
{int32(5), td, false},
|
|
|
|
{int16(5), td, false},
|
|
|
|
{int8(5), td, false},
|
2017-03-03 22:44:11 +03:00
|
|
|
{uint(5), td, false},
|
|
|
|
{uint64(5), td, false},
|
|
|
|
{uint32(5), td, false},
|
|
|
|
{uint16(5), td, false},
|
|
|
|
{uint8(5), td, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{float64(5), td, false},
|
2017-03-03 22:44:11 +03:00
|
|
|
{float32(5), td, false},
|
2022-01-15 15:43:35 +03:00
|
|
|
{jn, td, false},
|
2017-03-03 21:39:13 +03:00
|
|
|
{string("5"), td, false},
|
|
|
|
{string("5ns"), td, false},
|
|
|
|
{string("5us"), time.Microsecond * td, false},
|
|
|
|
{string("5µs"), time.Microsecond * td, false},
|
|
|
|
{string("5ms"), time.Millisecond * td, false},
|
|
|
|
{string("5s"), time.Second * td, false},
|
|
|
|
{string("5m"), time.Minute * td, false},
|
|
|
|
{string("5h"), time.Hour * td, false},
|
|
|
|
// errors
|
|
|
|
{"test", 0, true},
|
|
|
|
{testing.T{}, 0, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2022-05-11 11:35:17 +03:00
|
|
|
errmsg := qt.Commentf("i = %d", i) // assert helper message
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
v, err := ToDurationE(test.input)
|
|
|
|
if test.iserr {
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNotNil)
|
2017-03-03 21:39:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2017-03-03 21:39:13 +03:00
|
|
|
|
|
|
|
// Non-E test
|
|
|
|
v = ToDuration(test.input)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(v, qt.Equals, test.expect, errmsg)
|
2016-09-19 23:24:24 +03:00
|
|
|
}
|
2015-11-22 23:21:13 +03:00
|
|
|
}
|
2016-03-16 21:25:51 +03:00
|
|
|
|
2019-05-30 12:21:02 +03:00
|
|
|
func TestToTimeWithTimezones(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
2019-05-30 12:21:02 +03:00
|
|
|
|
2016-03-16 21:25:51 +03:00
|
|
|
est, err := time.LoadLocation("EST")
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2016-03-16 21:25:51 +03:00
|
|
|
|
|
|
|
irn, err := time.LoadLocation("Iran")
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2016-03-16 21:25:51 +03:00
|
|
|
|
|
|
|
swd, err := time.LoadLocation("Europe/Stockholm")
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2016-03-16 21:25:51 +03:00
|
|
|
|
|
|
|
// Test same local time in different timezones
|
|
|
|
utc2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
est2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, est)
|
|
|
|
irn2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, irn)
|
|
|
|
swd2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, swd)
|
2019-05-30 12:21:02 +03:00
|
|
|
loc2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.Local)
|
2016-03-16 21:25:51 +03:00
|
|
|
|
2019-05-30 12:21:02 +03:00
|
|
|
for i, format := range timeFormats {
|
|
|
|
format := format
|
|
|
|
if format.typ == timeFormatTimeOnly {
|
2016-03-16 21:25:51 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:21:02 +03:00
|
|
|
nameBase := fmt.Sprintf("%d;timeFormatType=%d;%s", i, format.typ, format.format)
|
|
|
|
|
|
|
|
t.Run(path.Join(nameBase), func(t *testing.T) {
|
|
|
|
est2016str := est2016.Format(format.format)
|
|
|
|
swd2016str := swd2016.Format(format.format)
|
|
|
|
|
|
|
|
t.Run("without default location", func(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
2019-05-30 12:21:02 +03:00
|
|
|
converted, err := ToTimeE(est2016str)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-05-30 12:21:02 +03:00
|
|
|
if format.hasTimezone() {
|
|
|
|
// Converting inputs with a timezone should preserve it
|
|
|
|
assertTimeEqual(t, est2016, converted)
|
|
|
|
assertLocationEqual(t, est, converted.Location())
|
|
|
|
} else {
|
|
|
|
// Converting inputs without a timezone should be interpreted
|
|
|
|
// as a local time in UTC.
|
|
|
|
assertTimeEqual(t, utc2016, converted)
|
|
|
|
assertLocationEqual(t, time.UTC, converted.Location())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("local timezone without a default location", func(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
2019-05-30 12:21:02 +03:00
|
|
|
converted, err := ToTimeE(swd2016str)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-05-30 12:21:02 +03:00
|
|
|
if format.hasTimezone() {
|
|
|
|
// Converting inputs with a timezone should preserve it
|
|
|
|
assertTimeEqual(t, swd2016, converted)
|
|
|
|
assertLocationEqual(t, swd, converted.Location())
|
|
|
|
} else {
|
|
|
|
// Converting inputs without a timezone should be interpreted
|
|
|
|
// as a local time in UTC.
|
|
|
|
assertTimeEqual(t, utc2016, converted)
|
|
|
|
assertLocationEqual(t, time.UTC, converted.Location())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil default location", func(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
2019-05-30 12:21:02 +03:00
|
|
|
|
|
|
|
converted, err := ToTimeInDefaultLocationE(est2016str, nil)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-05-30 12:21:02 +03:00
|
|
|
if format.hasTimezone() {
|
|
|
|
// Converting inputs with a timezone should preserve it
|
|
|
|
assertTimeEqual(t, est2016, converted)
|
|
|
|
assertLocationEqual(t, est, converted.Location())
|
|
|
|
} else {
|
|
|
|
// Converting inputs without a timezone should be interpreted
|
|
|
|
// as a local time in the local timezone.
|
|
|
|
assertTimeEqual(t, loc2016, converted)
|
|
|
|
assertLocationEqual(t, time.Local, converted.Location())
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default location not UTC", func(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
2019-05-30 12:21:02 +03:00
|
|
|
|
|
|
|
converted, err := ToTimeInDefaultLocationE(est2016str, irn)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-05-30 12:21:02 +03:00
|
|
|
if format.hasTimezone() {
|
|
|
|
// Converting inputs with a timezone should preserve it
|
|
|
|
assertTimeEqual(t, est2016, converted)
|
|
|
|
assertLocationEqual(t, est, converted.Location())
|
|
|
|
} else {
|
|
|
|
// Converting inputs without a timezone should be interpreted
|
|
|
|
// as a local time in the given location.
|
|
|
|
assertTimeEqual(t, irn2016, converted)
|
|
|
|
assertLocationEqual(t, irn, converted.Location())
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("time in the local timezone default location not UTC", func(t *testing.T) {
|
2022-05-11 11:35:17 +03:00
|
|
|
c := qt.New(t)
|
2019-05-30 12:21:02 +03:00
|
|
|
|
|
|
|
converted, err := ToTimeInDefaultLocationE(swd2016str, irn)
|
2022-05-11 11:35:17 +03:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-05-30 12:21:02 +03:00
|
|
|
if format.hasTimezone() {
|
|
|
|
// Converting inputs with a timezone should preserve it
|
|
|
|
assertTimeEqual(t, swd2016, converted)
|
|
|
|
assertLocationEqual(t, swd, converted.Location())
|
|
|
|
} else {
|
|
|
|
// Converting inputs without a timezone should be interpreted
|
|
|
|
// as a local time in the given location.
|
|
|
|
assertTimeEqual(t, irn2016, converted)
|
|
|
|
assertLocationEqual(t, irn, converted.Location())
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2016-03-16 21:25:51 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:35:17 +03:00
|
|
|
func TestTrimZeroDecimal(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
c.Assert(trimZeroDecimal("10.0"), qt.Equals, "10")
|
|
|
|
c.Assert(trimZeroDecimal("10.00"), qt.Equals, "10")
|
|
|
|
c.Assert(trimZeroDecimal("10.010"), qt.Equals, "10.010")
|
|
|
|
c.Assert(trimZeroDecimal("0.0000000000"), qt.Equals, "0")
|
|
|
|
c.Assert(trimZeroDecimal("0.00000000001"), qt.Equals, "0.00000000001")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:21:02 +03:00
|
|
|
func assertTimeEqual(t *testing.T, expected, actual time.Time) {
|
|
|
|
t.Helper()
|
|
|
|
// Compare the dates using a numeric zone as there are cases where
|
|
|
|
// time.Parse will assign a dummy location.
|
2022-05-11 11:35:17 +03:00
|
|
|
qt.Assert(t, actual.Format(time.RFC1123Z), qt.Equals, expected.Format(time.RFC1123Z))
|
2016-03-16 21:25:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 12:21:02 +03:00
|
|
|
func assertLocationEqual(t *testing.T, expected, actual *time.Location) {
|
|
|
|
t.Helper()
|
2022-05-11 11:35:17 +03:00
|
|
|
qt.Assert(t, locationEqual(expected, actual), qt.IsTrue)
|
2016-03-16 21:25:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func locationEqual(a, b *time.Location) bool {
|
|
|
|
// A note about comparring time.Locations:
|
|
|
|
// - can't only compare pointers
|
|
|
|
// - can't compare loc.String() because locations with the same
|
|
|
|
// name can have different offsets
|
|
|
|
// - can't use reflect.DeepEqual because time.Location has internal
|
|
|
|
// caches
|
|
|
|
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
} else if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if they're equal by parsing times with a format that doesn't
|
|
|
|
// include a timezone, which will interpret it as being a local time in
|
|
|
|
// the given zone, and comparing the resulting local times.
|
|
|
|
tA, err := time.ParseInLocation("2006-01-02", "2016-01-01", a)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
tB, err := time.ParseInLocation("2006-01-02", "2016-01-01", b)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return tA.Equal(tB)
|
|
|
|
}
|