2
0
config/utils_test.go

100 lines
2.5 KiB
Go

/*
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type data struct {
BoolValue bool
Int8Value int8
IntValue int
Uint16Value uint16
UintValue uint
Float32Value float32
Names []string
}
func TestSetValueWithBool(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("BoolValue")
assert := assert.New(t)
assert.NoError(setValueWithBool(v, "true"))
assert.Equal(true, d.BoolValue)
}
func TestSetValueWithFloat32(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("Float32Value")
assert := assert.New(t)
assert.NoError(setValueWithFloatX(v, "123.456", 32))
assert.Equal(float32(123.456), d.Float32Value)
}
func TestSetValueWithInt8(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("Int8Value")
assert := assert.New(t)
assert.NoError(setValueWithIntX(v, "10", 8))
assert.Equal(int8(10), d.Int8Value)
}
func TestSetValueWithInt(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("IntValue")
assert := assert.New(t)
assert.NoError(setValueWithIntX(v, "10000", 32))
assert.Equal(10000, d.IntValue)
}
func TestSetValueWithUint16(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("Uint16Value")
assert := assert.New(t)
assert.NoError(setValueWithUintX(v, "100", 16))
assert.Equal(uint16(100), d.Uint16Value)
}
func TestSetValueWithUint(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("UintValue")
assert := assert.New(t)
assert.NoError(setValueWithUintX(v, "2000", 32))
assert.Equal(uint(2000), d.UintValue)
}
func TestSetValueWithSlice(t *testing.T) {
d := data{}
v := reflect.ValueOf(&d).Elem().FieldByName("Names")
assert := assert.New(t)
assert.NoError(setValueWithSlice(v, "xx:yy:zz", ":"))
assert.Equal(3, len(d.Names))
assert.Equal("xx", d.Names[0])
assert.Equal("yy", d.Names[1])
assert.Equal("zz", d.Names[2])
}