github.com/yandex/pandora@v0.5.32/lib/confutil/property_var_resolver_test.go (about)

     1  package confutil
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPropertyTokenResolver(t *testing.T) {
    12  	fileContent := []byte(`name=John Doe
    13  age=25
    14  email=johndoe@example.com`)
    15  	tmpFile, err := ioutil.TempFile("", "testfile*.txt")
    16  	assert.NoError(t, err)
    17  	defer os.Remove(tmpFile.Name())
    18  	defer tmpFile.Close()
    19  	_, err = tmpFile.Write(fileContent)
    20  	assert.NoError(t, err)
    21  
    22  	testCases := []struct {
    23  		input          string
    24  		expectedResult string
    25  		expectedError  string
    26  	}{
    27  		{
    28  			input:          tmpFile.Name() + "#name",
    29  			expectedResult: "John Doe",
    30  			expectedError:  "",
    31  		},
    32  		{
    33  			input:          tmpFile.Name() + "#age",
    34  			expectedResult: "25",
    35  			expectedError:  "",
    36  		},
    37  		{
    38  			input:          tmpFile.Name() + "#email",
    39  			expectedResult: "johndoe@example.com",
    40  			expectedError:  "",
    41  		},
    42  		{
    43  			input:          tmpFile.Name() + "#address",
    44  			expectedResult: "",
    45  			expectedError:  "no such property 'address', in file '" + tmpFile.Name() + "'",
    46  		},
    47  		{
    48  			input:          "nonexistent.txt#property",
    49  			expectedResult: "",
    50  			expectedError:  "cannot open file: 'nonexistent.txt'",
    51  		},
    52  	}
    53  
    54  	for _, testCase := range testCases {
    55  		result, err := propertyTokenResolver(testCase.input)
    56  
    57  		assert.Equal(t, testCase.expectedResult, result)
    58  		if testCase.expectedError != "" {
    59  			assert.EqualError(t, err, testCase.expectedError)
    60  		} else {
    61  			assert.NoError(t, err)
    62  		}
    63  	}
    64  }