mirror of
https://github.com/anchore/grype
synced 2024-11-10 06:34:13 +00:00
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:
parent
83c6ee23a9
commit
7c082c2162
1 changed files with 90 additions and 0 deletions
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue