adds more unit tests
This commit is contained in:
parent
3a44c4ff7a
commit
bfc2b4d434
@ -41,6 +41,9 @@ func TestGetEnv(t *testing.T) {
|
||||
|
||||
// bool
|
||||
t.Setenv("TEST_BOOL", strconv.FormatBool(expected_bool))
|
||||
if _, err := getEnv("TEST_STRING", expected_bool); err == nil {
|
||||
t.Fatal("getEnv should have failed with invalid type")
|
||||
}
|
||||
test_bool, err := getEnv("TEST_BOOL", false)
|
||||
if err != nil {
|
||||
t.Fatalf("Error calling getEnv: %v", err)
|
||||
@ -50,6 +53,9 @@ func TestGetEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
// int
|
||||
if _, err := getEnv("TEST_STRING", expected_int); err == nil {
|
||||
t.Fatal("getEnv should have failed with invalid type")
|
||||
}
|
||||
t.Setenv("TEST_INT", strconv.FormatInt(int64(expected_int), 10))
|
||||
test_int, err := getEnv("TEST_INT", 999)
|
||||
if err != nil {
|
||||
@ -61,6 +67,9 @@ func TestGetEnv(t *testing.T) {
|
||||
|
||||
// int64
|
||||
t.Setenv("TEST_INT64", strconv.FormatInt(expected_int64, 10))
|
||||
if _, err := getEnv("TEST_STRING", expected_int64); err == nil {
|
||||
t.Fatal("getEnv should have failed with invalid type")
|
||||
}
|
||||
test_int64, err := getEnv("TEST_INT64", int64(999))
|
||||
if err != nil {
|
||||
t.Fatalf("Error calling getEnv: %v", err)
|
||||
@ -71,6 +80,9 @@ func TestGetEnv(t *testing.T) {
|
||||
|
||||
// float64
|
||||
t.Setenv("TEST_FLOAT64", strconv.FormatFloat(expected_float64, 'f', 3, 32))
|
||||
if _, err := getEnv("TEST_STRING", expected_float64); err == nil {
|
||||
t.Fatal("getEnv should have failed with invalid type")
|
||||
}
|
||||
test_float64, err := getEnv("TEST_FLOAT64", 999.99)
|
||||
if err != nil {
|
||||
t.Fatalf("Error calling getEnv: %v", err)
|
||||
@ -121,86 +133,101 @@ func TestGetStructInfo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTypeConversion(t *testing.T) {
|
||||
var (
|
||||
expected_string string = "This is a string."
|
||||
expected_int int = 100
|
||||
expected_int8 int8 = 100
|
||||
expected_int16 int16 = 100
|
||||
expected_int32 int32 = 100
|
||||
expected_int64 int64 = 100
|
||||
expected_uint uint = 100
|
||||
expected_uint16 uint16 = 100
|
||||
expected_uint32 uint32 = 100
|
||||
expected_uint64 uint64 = 100
|
||||
expected_float32 float32 = 100.001
|
||||
expected_float64 float64 = 100.001
|
||||
expected_bool bool = true
|
||||
)
|
||||
|
||||
// string
|
||||
output_string, _ := typeConversion("string", "This is a string.")
|
||||
if output_string != "This is a string." {
|
||||
t.Fatalf("Expected: %s, got: %s", "This is a string.", output_string)
|
||||
output_string, _ := typeConversion("string", expected_string)
|
||||
if output_string != expected_string {
|
||||
t.Fatalf("Expected: %s, got: %s", expected_string, output_string)
|
||||
}
|
||||
|
||||
// int
|
||||
output_int, _ := typeConversion("int", "100")
|
||||
if int(output_int.(int64)) != int(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_int)
|
||||
if int(output_int.(int64)) != expected_int {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_int, output_int)
|
||||
}
|
||||
|
||||
// int8
|
||||
output_int8, _ := typeConversion("int8", "100")
|
||||
if int8(output_int8.(int64)) != int8(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_int8)
|
||||
if int8(output_int8.(int64)) != expected_int8 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_int8, output_int8)
|
||||
}
|
||||
|
||||
// int16
|
||||
output_int16, _ := typeConversion("int16", "100")
|
||||
if int16(output_int16.(int64)) != int16(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_int16)
|
||||
if int16(output_int16.(int64)) != expected_int16 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_int16, output_int16)
|
||||
}
|
||||
|
||||
// int32
|
||||
output_int32, _ := typeConversion("int32", "100")
|
||||
if int32(output_int32.(int64)) != int32(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_int32)
|
||||
if int32(output_int32.(int64)) != expected_int32 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_int32, output_int32)
|
||||
}
|
||||
|
||||
// int64
|
||||
output_int64, _ := typeConversion("int64", "100")
|
||||
if output_int64.(int64) != int64(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_int64)
|
||||
if output_int64.(int64) != expected_int64 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_int64, output_int64)
|
||||
}
|
||||
|
||||
// uint
|
||||
output_uint, _ := typeConversion("uint", "100")
|
||||
if uint(output_uint.(uint64)) != uint(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_uint)
|
||||
if uint(output_uint.(uint64)) != expected_uint {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_uint, output_uint)
|
||||
}
|
||||
|
||||
// uint16
|
||||
output_uint16, _ := typeConversion("uint16", "100")
|
||||
if uint16(output_uint16.(uint64)) != uint16(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_uint16)
|
||||
if uint16(output_uint16.(uint64)) != expected_uint16 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_uint16, output_uint16)
|
||||
}
|
||||
|
||||
// uint32
|
||||
output_uint32, _ := typeConversion("uint32", "100")
|
||||
if uint32(output_uint32.(uint64)) != uint32(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_uint32)
|
||||
if uint32(output_uint32.(uint64)) != expected_uint32 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_uint32, output_uint32)
|
||||
}
|
||||
|
||||
// uint64
|
||||
output_uint64, _ := typeConversion("uint64", "100")
|
||||
if output_uint64.(uint64) != uint64(100) {
|
||||
t.Fatalf("Expected: %d, got: %d", 100, output_uint64)
|
||||
if output_uint64.(uint64) != expected_uint64 {
|
||||
t.Fatalf("Expected: %d, got: %d", expected_uint64, output_uint64)
|
||||
}
|
||||
|
||||
// float32
|
||||
output_float32, _ := typeConversion("float32", "100.001")
|
||||
if float32(math.Round(output_float32.(float64)*1000)/1000) != float32(math.Round(100.001*1000)/1000) {
|
||||
t.Fatalf("Expected: %f, got: %f", float32(math.Round(100.001*1000)/1000), float32(math.Round(output_float32.(float64)*1000)/1000))
|
||||
if float32(math.Round(output_float32.(float64)*1000)/1000) != expected_float32 {
|
||||
t.Fatalf("Expected: %f, got: %f", expected_float32, float32(math.Round(output_float32.(float64)*1000)/1000))
|
||||
}
|
||||
|
||||
// float64
|
||||
output_float64, _ := typeConversion("float64", "100.001")
|
||||
if math.Round(output_float64.(float64)*1000)/1000 != math.Round(100.001*1000)/1000 {
|
||||
t.Fatalf("Expected: %f, got: %f", math.Round(100.001*1000)/1000, math.Round(output_float64.(float64)*1000)/1000)
|
||||
if math.Round(output_float64.(float64)*1000)/1000 != expected_float64 {
|
||||
t.Fatalf("Expected: %f, got: %f", expected_float64, math.Round(output_float64.(float64)*1000)/1000)
|
||||
}
|
||||
|
||||
// bool
|
||||
output_bool, _ := typeConversion("bool", "true")
|
||||
if output_bool.(bool) != true {
|
||||
t.Fatalf("Expected: %t, got: %t", true, output_bool.(bool))
|
||||
if output_bool.(bool) != expected_bool {
|
||||
t.Fatalf("Expected: %t, got: %t", expected_bool, output_bool.(bool))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user