Add more unit tests for new trimQuotes function (#416)

* add more unit tests for trimQuotes function

Signed-off-by: Vijay Pillai <vijay.pillai@anchore.com>
This commit is contained in:
Vijay Pillai 2021-09-14 17:27:15 -04:00 committed by GitHub
parent 83c6ee23a9
commit 7c082c2162
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
package version
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
@ -123,3 +124,92 @@ func TestSplitFuzzyPhrase(t *testing.T) {
})
}
}
func TestTrimQuotes(t *testing.T) {
tests := []struct {
name string
input string
expected string
err bool
}{
{
name: "no quotes",
input: "test",
expected: "test",
err: true,
},
{
name: "double quotes",
input: "\"test\"",
expected: "test",
err: false,
},
{
name: "single quotes",
input: "'test'",
expected: "test",
err: false,
},
{
name: "leading_single_quote",
input: "'test",
expected: "'test",
err: true,
},
{
name: "trailing_single_quote",
input: "test'",
expected: "test'",
err: true,
},
{
name: "leading_double_quote",
input: "'test",
expected: "'test",
err: true,
},
{
name: "trailing_double_quote",
input: "test'",
expected: "test'",
err: true,
},
{
// This raises an error, but I do not believe that this is a scenario that we need to account for, so should be ok.
name: "nested double/double quotes",
input: "\"t\"es\"t\"",
expected: "\"t\"es\"t\"",
err: true,
},
{
name: "nested single/single quotes",
input: "'t'es't'",
expected: "t'es't",
err: false,
},
{
name: "nested single/double quotes",
input: "'t\"es\"t'",
expected: "t\"es\"t",
err: false,
},
{
name: "nested double/single quotes",
input: "\"t'es't\"",
expected: "t'es't",
err: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := trimQuotes(test.input)
if test.err {
assert.NotNil(t, err, "expected an error but did not get one")
} else {
assert.Nil(t, err, "expected no error, got \"%+v\"", err)
}
assert.Equal(t, actual, test.expected, "output does not match expected: exp:%v got:%v", test.expected, actual)
})
}
}