From 534aa6ad5ba8bdb3cba124969e6433d8e3f78f23 Mon Sep 17 00:00:00 2001 From: nhyatt Date: Sat, 10 Feb 2024 15:31:49 -0600 Subject: [PATCH] adding more unit testing --- internal/config/envconfig_test.go | 134 ++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/internal/config/envconfig_test.go b/internal/config/envconfig_test.go index 073285c..abeaea0 100644 --- a/internal/config/envconfig_test.go +++ b/internal/config/envconfig_test.go @@ -5,6 +5,19 @@ import ( "testing" ) +type mock_config struct { + Notags string + Ignored string `ignored:"true"` + Info string `info:"This is an info string."` + Secret string `secret:"true"` + Env string `env:"TEST_ENV"` + Default_string string `default:"This is a default string."` + Default_bool bool `default:"true"` + Default_int int `default:"100"` + Default_int64 int64 `default:"100"` + Default_float64 float64 `default:"100.001"` +} + func TestGetEnv(t *testing.T) { var ( expected_string string = "This is a test string." @@ -74,3 +87,124 @@ func TestGetEnv(t *testing.T) { t.Fatalf("Expected %s, got %s", expected_unset_default, test_unset) } } + +func TestGetStructInfo(t *testing.T) { + test_config := mock_config{ + Notags: "notags", + Ignored: "ignored", + Secret: "secret", + } + + cfgInfo, err := getStructInfo(&test_config) + if err != nil { + t.Fatalf("Error calling getStructInfo: %v", err) + } + + for _, v := range cfgInfo { + switch v.Name { + case "Info": + if v.Info != "() This is an info string." { + t.Fatalf("Expected: %s, got: %s", "() This is an info string.", v.Info) + } + case "Secret": + if v.Secret != true { + t.Fatalf("Expected: %t, got: %t", true, false) + } + case "Env": + if v.Alt != "TEST_ENV" { + t.Fatalf("Expected: %s, got: %s", "TEST_ENV", v.Alt) + } + case "Default_value": + if v.DefaultValue != "This is a default string." { + t.Fatalf("Expected: %s, got: %s", "This is a default string.", v.DefaultValue) + } + } + } + +} + +func TestTypeConversion(t *testing.T) { + // 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) + } + + // int + output_int, _ := typeConversion("int", "100") + if int(output_int.(int64)) != int(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_int) + } + + // int8 + output_int8, _ := typeConversion("int8", "100") + if int8(output_int8.(int64)) != int8(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_int8) + } + + // int16 + output_int16, _ := typeConversion("int16", "100") + if int16(output_int16.(int64)) != int16(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_int16) + } + + // int32 + output_int32, _ := typeConversion("int32", "100") + if int32(output_int32.(int64)) != int32(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_int32) + } + + // int64 + output_int64, _ := typeConversion("int64", "100") + if output_int64.(int64) != int64(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_int64) + } + + // uint + output_uint, _ := typeConversion("uint", "100") + if uint(output_uint.(uint64)) != uint(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_uint) + } + + // uint16 + output_uint16, _ := typeConversion("uint16", "100") + if uint16(output_uint16.(uint64)) != uint16(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_uint16) + } + + // uint32 + output_uint32, _ := typeConversion("uint32", "100") + if uint32(output_uint32.(uint64)) != uint32(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_uint32) + } + + // uint64 + output_uint64, _ := typeConversion("uint64", "100") + if output_uint64.(uint64) != uint64(100) { + t.Fatalf("Expected: %d, got: %d", 100, output_uint64) + } + + /* + // float32 + output_float32, _ := typeConversion("float32", "100.001") + if float32(output_float32.(float64)) != float32(100.1) { + t.Fatalf("Expected: %f, got: %f", 100.1, output_float32) + } + + // float64 + output_float64, _ := typeConversion("float64", "100.001") + if output_float64.(float64) != 100.001 { + t.Fatalf("Expected: %f, got: %f", 100.001, output_float64) + } + */ +} + +func TestParseFlags(t *testing.T) { + test_config := Config{} + + cfgInfo, _ := getStructInfo(&test_config) + + if err := test_config.parseFlags(cfgInfo); err != nil { + t.Fatalf("Expected error, no return data to validate: %v", err) + } +}