コード例 #1
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #2
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #3
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #4
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #5
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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}")
}
コード例 #6
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #7
0
ファイル: configuration_test.go プロジェクト: kung-foo/golib
// TestValueSuccess tests the successful retrieval of values.
func TestValueSuccess(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := `{config
	{a  Hello}
	{b true}
	{c -1}
	{d     47.11     }
	{sub
		{a
			World}
		{b
			42}}}`
	config, err := configuration.Read(strings.NewReader(source))
	assert.Nil(err)

	v, err := config.At("a").Value()
	assert.Nil(err)
	assert.Equal(v, "Hello")
	v, err = config.At("b").Value()
	assert.Nil(err)
	assert.Equal(v, "true")
	v, err = config.At("c").Value()
	assert.Nil(err)
	assert.Equal(v, "-1")
	v, err = config.At("d").Value()
	assert.Nil(err)
	assert.Equal(v, "47.11")
	v, err = config.At("sub", "a").Value()
	assert.Nil(err)
	assert.Equal(v, "World")
	v, err = config.At("sub", "b").Value()
	assert.Nil(err)
	assert.Equal(v, "42")

	d := stringex.NewDefaulter("config", true)
	vi := d.AsInt(config.At("c"), 42)
	assert.Equal(vi, -1)
	vf := d.AsFloat64(config.At("d"), 12.34)
	assert.Equal(vf, 47.11)
}
コード例 #8
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #9
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	now := time.Now()
	d := stringex.NewDefaulter("AsTime", true)
	tests := []struct {
		v        valuer
		layout   string
		dv       time.Time
		expected time.Time
	}{
		{valuer{now.Format(time.RFC3339Nano), false}, time.RFC3339Nano, y2k, now},
		{valuer{now.Format(time.RFC3339Nano), true}, time.RFC3339Nano, y2k, y2k},
		{valuer{now.Format(time.RFC3339Nano), 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)
	}
}
コード例 #10
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}
コード例 #11
0
ファイル: defaulter_test.go プロジェクト: kung-foo/golib
// 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)
	}
}