adds more unit tests

This commit is contained in:
Hyatt 2024-02-12 12:49:17 -06:00
parent 3a44c4ff7a
commit bfc2b4d434
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA

View File

@ -41,6 +41,9 @@ func TestGetEnv(t *testing.T) {
// bool // bool
t.Setenv("TEST_BOOL", strconv.FormatBool(expected_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) test_bool, err := getEnv("TEST_BOOL", false)
if err != nil { if err != nil {
t.Fatalf("Error calling getEnv: %v", err) t.Fatalf("Error calling getEnv: %v", err)
@ -50,6 +53,9 @@ func TestGetEnv(t *testing.T) {
} }
// int // 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)) t.Setenv("TEST_INT", strconv.FormatInt(int64(expected_int), 10))
test_int, err := getEnv("TEST_INT", 999) test_int, err := getEnv("TEST_INT", 999)
if err != nil { if err != nil {
@ -61,6 +67,9 @@ func TestGetEnv(t *testing.T) {
// int64 // int64
t.Setenv("TEST_INT64", strconv.FormatInt(expected_int64, 10)) 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)) test_int64, err := getEnv("TEST_INT64", int64(999))
if err != nil { if err != nil {
t.Fatalf("Error calling getEnv: %v", err) t.Fatalf("Error calling getEnv: %v", err)
@ -71,6 +80,9 @@ func TestGetEnv(t *testing.T) {
// float64 // float64
t.Setenv("TEST_FLOAT64", strconv.FormatFloat(expected_float64, 'f', 3, 32)) 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) test_float64, err := getEnv("TEST_FLOAT64", 999.99)
if err != nil { if err != nil {
t.Fatalf("Error calling getEnv: %v", err) t.Fatalf("Error calling getEnv: %v", err)
@ -121,86 +133,101 @@ func TestGetStructInfo(t *testing.T) {
} }
} }
} }
} }
func TestTypeConversion(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 // string
output_string, _ := typeConversion("string", "This is a string.") output_string, _ := typeConversion("string", expected_string)
if output_string != "This is a string." { if output_string != expected_string {
t.Fatalf("Expected: %s, got: %s", "This is a string.", output_string) t.Fatalf("Expected: %s, got: %s", expected_string, output_string)
} }
// int // int
output_int, _ := typeConversion("int", "100") output_int, _ := typeConversion("int", "100")
if int(output_int.(int64)) != int(100) { if int(output_int.(int64)) != expected_int {
t.Fatalf("Expected: %d, got: %d", 100, output_int) t.Fatalf("Expected: %d, got: %d", expected_int, output_int)
} }
// int8 // int8
output_int8, _ := typeConversion("int8", "100") output_int8, _ := typeConversion("int8", "100")
if int8(output_int8.(int64)) != int8(100) { if int8(output_int8.(int64)) != expected_int8 {
t.Fatalf("Expected: %d, got: %d", 100, output_int8) t.Fatalf("Expected: %d, got: %d", expected_int8, output_int8)
} }
// int16 // int16
output_int16, _ := typeConversion("int16", "100") output_int16, _ := typeConversion("int16", "100")
if int16(output_int16.(int64)) != int16(100) { if int16(output_int16.(int64)) != expected_int16 {
t.Fatalf("Expected: %d, got: %d", 100, output_int16) t.Fatalf("Expected: %d, got: %d", expected_int16, output_int16)
} }
// int32 // int32
output_int32, _ := typeConversion("int32", "100") output_int32, _ := typeConversion("int32", "100")
if int32(output_int32.(int64)) != int32(100) { if int32(output_int32.(int64)) != expected_int32 {
t.Fatalf("Expected: %d, got: %d", 100, output_int32) t.Fatalf("Expected: %d, got: %d", expected_int32, output_int32)
} }
// int64 // int64
output_int64, _ := typeConversion("int64", "100") output_int64, _ := typeConversion("int64", "100")
if output_int64.(int64) != int64(100) { if output_int64.(int64) != expected_int64 {
t.Fatalf("Expected: %d, got: %d", 100, output_int64) t.Fatalf("Expected: %d, got: %d", expected_int64, output_int64)
} }
// uint // uint
output_uint, _ := typeConversion("uint", "100") output_uint, _ := typeConversion("uint", "100")
if uint(output_uint.(uint64)) != uint(100) { if uint(output_uint.(uint64)) != expected_uint {
t.Fatalf("Expected: %d, got: %d", 100, output_uint) t.Fatalf("Expected: %d, got: %d", expected_uint, output_uint)
} }
// uint16 // uint16
output_uint16, _ := typeConversion("uint16", "100") output_uint16, _ := typeConversion("uint16", "100")
if uint16(output_uint16.(uint64)) != uint16(100) { if uint16(output_uint16.(uint64)) != expected_uint16 {
t.Fatalf("Expected: %d, got: %d", 100, output_uint16) t.Fatalf("Expected: %d, got: %d", expected_uint16, output_uint16)
} }
// uint32 // uint32
output_uint32, _ := typeConversion("uint32", "100") output_uint32, _ := typeConversion("uint32", "100")
if uint32(output_uint32.(uint64)) != uint32(100) { if uint32(output_uint32.(uint64)) != expected_uint32 {
t.Fatalf("Expected: %d, got: %d", 100, output_uint32) t.Fatalf("Expected: %d, got: %d", expected_uint32, output_uint32)
} }
// uint64 // uint64
output_uint64, _ := typeConversion("uint64", "100") output_uint64, _ := typeConversion("uint64", "100")
if output_uint64.(uint64) != uint64(100) { if output_uint64.(uint64) != expected_uint64 {
t.Fatalf("Expected: %d, got: %d", 100, output_uint64) t.Fatalf("Expected: %d, got: %d", expected_uint64, output_uint64)
} }
// float32 // float32
output_float32, _ := typeConversion("float32", "100.001") output_float32, _ := typeConversion("float32", "100.001")
if float32(math.Round(output_float32.(float64)*1000)/1000) != float32(math.Round(100.001*1000)/1000) { if float32(math.Round(output_float32.(float64)*1000)/1000) != expected_float32 {
t.Fatalf("Expected: %f, got: %f", float32(math.Round(100.001*1000)/1000), float32(math.Round(output_float32.(float64)*1000)/1000)) t.Fatalf("Expected: %f, got: %f", expected_float32, float32(math.Round(output_float32.(float64)*1000)/1000))
} }
// float64 // float64
output_float64, _ := typeConversion("float64", "100.001") output_float64, _ := typeConversion("float64", "100.001")
if math.Round(output_float64.(float64)*1000)/1000 != math.Round(100.001*1000)/1000 { if math.Round(output_float64.(float64)*1000)/1000 != expected_float64 {
t.Fatalf("Expected: %f, got: %f", math.Round(100.001*1000)/1000, math.Round(output_float64.(float64)*1000)/1000) t.Fatalf("Expected: %f, got: %f", expected_float64, math.Round(output_float64.(float64)*1000)/1000)
} }
// bool // bool
output_bool, _ := typeConversion("bool", "true") output_bool, _ := typeConversion("bool", "true")
if output_bool.(bool) != true { if output_bool.(bool) != expected_bool {
t.Fatalf("Expected: %t, got: %t", true, output_bool.(bool)) t.Fatalf("Expected: %t, got: %t", expected_bool, output_bool.(bool))
} }
} }