1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/cli/compose/template/template_test.go
Daniel Nephin f5af9b9738 Replace vendor of aanand/compose-file with a local copy.
Add go-bindata for including the schema.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
2016-12-27 16:17:24 -05:00

83 lines
1.9 KiB
Go

package template
import (
"testing"
"github.com/stretchr/testify/assert"
)
var defaults = map[string]string{
"FOO": "first",
"BAR": "",
}
func defaultMapping(name string) (string, bool) {
val, ok := defaults[name]
return val, ok
}
func TestEscaped(t *testing.T) {
result, err := Substitute("$${foo}", defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "${foo}", result)
}
func TestInvalid(t *testing.T) {
invalidTemplates := []string{
"${",
"$}",
"${}",
"${ }",
"${ foo}",
"${foo }",
"${foo!}",
}
for _, template := range invalidTemplates {
_, err := Substitute(template, defaultMapping)
assert.Error(t, err)
assert.IsType(t, &InvalidTemplateError{}, err)
}
}
func TestNoValueNoDefault(t *testing.T) {
for _, template := range []string{"This ${missing} var", "This ${BAR} var"} {
result, err := Substitute(template, defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "This var", result)
}
}
func TestValueNoDefault(t *testing.T) {
for _, template := range []string{"This $FOO var", "This ${FOO} var"} {
result, err := Substitute(template, defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "This first var", result)
}
}
func TestNoValueWithDefault(t *testing.T) {
for _, template := range []string{"ok ${missing:-def}", "ok ${missing-def}"} {
result, err := Substitute(template, defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "ok def", result)
}
}
func TestEmptyValueWithSoftDefault(t *testing.T) {
result, err := Substitute("ok ${BAR:-def}", defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "ok def", result)
}
func TestEmptyValueWithHardDefault(t *testing.T) {
result, err := Substitute("ok ${BAR-def}", defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "ok ", result)
}
func TestNonAlphanumericDefault(t *testing.T) {
result, err := Substitute("ok ${BAR:-/non:-alphanumeric}", defaultMapping)
assert.NoError(t, err)
assert.Equal(t, "ok /non:-alphanumeric", result)
}