211 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			211 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package config
 | |
| 
 | |
| import (
 | |
| 	"strconv"
 | |
| 	"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."
 | |
| 		expected_bool          bool    = true
 | |
| 		expected_int           int     = 100
 | |
| 		expected_int64         int64   = 100
 | |
| 		expected_float64       float64 = 100.001
 | |
| 		expected_unset_default string  = "This is a default value."
 | |
| 	)
 | |
| 
 | |
| 	// string
 | |
| 	t.Setenv("TEST_STRING", expected_string)
 | |
| 	test_string, err := getEnv("TEST_STRING", "This is a default String")
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Error calling getEnv: %v", err)
 | |
| 	}
 | |
| 	if test_string != expected_string {
 | |
| 		t.Fatalf("Expected %s, got %s", expected_string, test_string)
 | |
| 	}
 | |
| 
 | |
| 	// bool
 | |
| 	t.Setenv("TEST_BOOL", strconv.FormatBool(expected_bool))
 | |
| 	test_bool, err := getEnv("TEST_BOOL", false)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Error calling getEnv: %v", err)
 | |
| 	}
 | |
| 	if test_bool != expected_bool {
 | |
| 		t.Fatalf("Expected %t, got %t", expected_bool, test_bool)
 | |
| 	}
 | |
| 
 | |
| 	// int
 | |
| 	t.Setenv("TEST_INT", strconv.FormatInt(int64(expected_int), 10))
 | |
| 	test_int, err := getEnv("TEST_INT", 999)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Error calling getEnv: %v", err)
 | |
| 	}
 | |
| 	if test_int != expected_int {
 | |
| 		t.Fatalf("Expected %d, got %d", expected_int, test_int)
 | |
| 	}
 | |
| 
 | |
| 	// int64
 | |
| 	t.Setenv("TEST_INT64", strconv.FormatInt(expected_int64, 10))
 | |
| 	test_int64, err := getEnv("TEST_INT64", int64(999))
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Error calling getEnv: %v", err)
 | |
| 	}
 | |
| 	if test_int64 != expected_int64 {
 | |
| 		t.Fatalf("Expected %d, got %d", expected_int64, test_int64)
 | |
| 	}
 | |
| 
 | |
| 	// float64
 | |
| 	t.Setenv("TEST_FLOAT64", strconv.FormatFloat(expected_float64, 'f', 3, 32))
 | |
| 	test_float64, err := getEnv("TEST_FLOAT64", 999.99)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Error calling getEnv: %v", err)
 | |
| 	}
 | |
| 	if test_float64 != expected_float64 {
 | |
| 		t.Fatalf("Expected %f, got %f", expected_float64, test_float64)
 | |
| 	}
 | |
| 
 | |
| 	// unset or missing environment variable
 | |
| 	test_unset, err := getEnv("TEST_DEFAULT", expected_unset_default)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Error calling getEnv: %v", err)
 | |
| 	}
 | |
| 	if test_unset != expected_unset_default {
 | |
| 		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)
 | |
| 	}
 | |
| }
 |