diff options
Diffstat (limited to 'context_test.go')
| -rw-r--r-- | context_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/context_test.go b/context_test.go index 16492e9a..f2780356 100644 --- a/context_test.go +++ b/context_test.go @@ -1,9 +1,12 @@ package gomplate import ( + "net/url" "os" "testing" + "github.com/hairyhenderson/gomplate/data" + "github.com/stretchr/testify/assert" ) @@ -19,3 +22,49 @@ func TestEnvGetsUpdatedEnvironment(t *testing.T) { assert.NoError(t, os.Setenv("FOO", "foo")) assert.Equal(t, c.Env()["FOO"], "foo") } + +func TestCreateContext(t *testing.T) { + c, err := createContext(nil, nil) + assert.NoError(t, err) + assert.Empty(t, c) + + fooURL := "env:///foo?type=application/yaml" + barURL := "env:///bar?type=application/yaml" + uf, _ := url.Parse(fooURL) + ub, _ := url.Parse(barURL) + d := &data.Data{ + Sources: map[string]*data.Source{ + "foo": {URL: uf}, + ".": {URL: ub}, + }, + } + os.Setenv("foo", "foo: bar") + defer os.Unsetenv("foo") + c, err = createContext([]string{"foo=" + fooURL}, d) + assert.NoError(t, err) + assert.IsType(t, &context{}, c) + ctx := c.(*context) + ds := ((*ctx)["foo"]).(map[string]interface{}) + assert.Equal(t, "bar", ds["foo"]) + + os.Setenv("bar", "bar: baz") + defer os.Unsetenv("bar") + c, err = createContext([]string{".=" + barURL}, d) + assert.NoError(t, err) + assert.IsType(t, map[string]interface{}{}, c) + ds = c.(map[string]interface{}) + assert.Equal(t, "baz", ds["bar"]) +} + +func TestParseAlias(t *testing.T) { + testdata := map[string]string{ + "": "", + "foo": "foo", + "foo.bar": "foo", + "a=b": "a", + ".=foo": ".", + } + for k, v := range testdata { + assert.Equal(t, v, parseAlias(k)) + } +} |
