2016-03-04 12:29:44 -05:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2017-01-26 15:49:40 -05:00
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-03-04 12:29:44 -05:00
|
|
|
)
|
|
|
|
|
2017-03-27 12:50:53 -04:00
|
|
|
// Github #32120
|
|
|
|
func TestParseJSONFunctions(t *testing.T) {
|
|
|
|
tm, err := Parse(`{{json .Ports}}`)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
assert.NoError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"}))
|
|
|
|
want := "\"0.0.0.0:2->8/udp\""
|
|
|
|
assert.Equal(t, want, b.String())
|
|
|
|
}
|
|
|
|
|
2016-03-04 12:29:44 -05:00
|
|
|
func TestParseStringFunctions(t *testing.T) {
|
|
|
|
tm, err := Parse(`{{join (split . ":") "/"}}`)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2016-03-04 12:29:44 -05:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, tm.Execute(&b, "text:with:colon"))
|
2016-03-04 12:29:44 -05:00
|
|
|
want := "text/with/colon"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, want, b.String())
|
2016-03-04 12:29:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewParse(t *testing.T) {
|
|
|
|
tm, err := NewParse("foo", "this is a {{ . }}")
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2016-03-04 12:29:44 -05:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, tm.Execute(&b, "string"))
|
2016-03-04 12:29:44 -05:00
|
|
|
want := "this is a string"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, want, b.String())
|
2017-01-26 15:49:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTruncateFunction(t *testing.T) {
|
|
|
|
source := "tupx5xzf6hvsrhnruz5cr8gwp"
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
template string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
template: `{{truncate . 5}}`,
|
|
|
|
expected: "tupx5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
template: `{{truncate . 25}}`,
|
|
|
|
expected: "tupx5xzf6hvsrhnruz5cr8gwp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
template: `{{truncate . 30}}`,
|
|
|
|
expected: "tupx5xzf6hvsrhnruz5cr8gwp",
|
|
|
|
},
|
2017-06-14 07:49:44 -04:00
|
|
|
{
|
|
|
|
template: `{{pad . 3 3}}`,
|
|
|
|
expected: " tupx5xzf6hvsrhnruz5cr8gwp ",
|
|
|
|
},
|
2017-01-26 15:49:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
tm, err := Parse(testCase.template)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2017-01-26 15:49:40 -05:00
|
|
|
|
2017-06-14 07:49:44 -04:00
|
|
|
t.Run("Non Empty Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
assert.NoError(t, tm.Execute(&b, source))
|
|
|
|
assert.Equal(t, testCase.expected, b.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Empty Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
|
|
var c bytes.Buffer
|
|
|
|
assert.NoError(t, tm.Execute(&c, ""))
|
|
|
|
assert.Equal(t, "", c.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Nil Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
|
|
var c bytes.Buffer
|
|
|
|
assert.Error(t, tm.Execute(&c, nil))
|
|
|
|
assert.Equal(t, "", c.String())
|
|
|
|
})
|
2016-03-04 12:29:44 -05:00
|
|
|
}
|
|
|
|
}
|