github.com/42z-io/confik@v0.0.2-0.20231103050132-21d8f377356c/field_test.go (about)

     1  package confik
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestFieldNameToEnvironmentVariable(t *testing.T) {
    11  	res := map[string]string{
    12  		"test":           "TEST",
    13  		"test3":          "TEST_3",
    14  		"testCamelCase":  "TEST_CAMEL_CASE",
    15  		"testCamel3Case": "TEST_CAMEL_3_CASE",
    16  		"other_name":     "OTHER_NAME",
    17  	}
    18  	for input, expect := range res {
    19  		result := toEnvName(input)
    20  		assert.Equal(t, expect, result)
    21  	}
    22  }
    23  func TestValidateEnvironmentVariable(t *testing.T) {
    24  	valid := []string{
    25  		"TEST_1",
    26  		"TEST_1_OTHER",
    27  		"OTHEROTHEROTHER",
    28  		"T",
    29  		"TEST_1_2_3333333",
    30  		"3_3_3",
    31  		"33____3",
    32  	}
    33  
    34  	invalid := []string{
    35  		"@",
    36  		"",
    37  		"MY-NAME",
    38  		"PEICE3$",
    39  		"^_3030",
    40  	}
    41  
    42  	for _, v := range valid {
    43  		err := verifyEnvName(v)
    44  		assert.Nil(t, err, "expected variable %s to be valid", v)
    45  	}
    46  
    47  	for _, i := range invalid {
    48  		err := verifyEnvName(i)
    49  		if assert.Error(t, err) {
    50  			assert.Equal(t, fmt.Sprintf("invalid environment variable name: %s must be [A-Z0-9_]+", i), err.Error(), "expected variable %s to be invalid", i)
    51  		}
    52  	}
    53  }