diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-02-04 00:35:31 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-02-04 00:35:31 -0500 |
commit | 9c17b97cd0f83be3fff9fa4e87fd1d29052ea616 (patch) | |
tree | 0d97030a0d0c3ad983be281ce89f80571338887f /cpe/avstring_test.go | |
parent | 92400d731546557d110c9c3cc3906d700f83dda8 (diff) | |
download | cvez-9c17b97cd0f83be3fff9fa4e87fd1d29052ea616.tar.bz2 cvez-9c17b97cd0f83be3fff9fa4e87fd1d29052ea616.zip |
rename to github.com/pablotron/cvez, remove internal libs
Diffstat (limited to 'cpe/avstring_test.go')
-rw-r--r-- | cpe/avstring_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/cpe/avstring_test.go b/cpe/avstring_test.go new file mode 100644 index 0000000..8cbb14b --- /dev/null +++ b/cpe/avstring_test.go @@ -0,0 +1,95 @@ +package cpe + +import ( + "testing" +) + +func TestNewAvString(t *testing.T) { + passTests := []struct { + name string + val token + exp AvString + } { + { "any", token { Type: anyToken }, AvString { Type: AnyString } }, + { "na", token { Type: naToken }, AvString { Type: NaString } }, + { "empty", token { Type: valToken }, AvString { ValString, "" } }, + { "foo", token { valToken, "foo" }, AvString { ValString, "foo" } }, + } + + for _, test := range(passTests) { + t.Run(test.name, func(t *testing.T) { + got, err := newAvString(test.val) + if err != nil { + t.Error(err) + } else if got.Type != test.exp.Type { + t.Errorf("token: got %s, exp %s", got.Type, test.exp.Type) + } else if got.Type == ValString && got.Val != test.exp.Val { + t.Errorf("value: got \"%s\", exp \"%s\"", got.Val, test.exp.Val) + } + }) + } + + failTests := []struct { + name string + val token + exp string + } {{ + name: "invalid token", + val: token { Type: tokenType(127), Val: "foo" }, + exp: "invalid token type: 0x7f", + }} + + for _, test := range(failTests) { + t.Run(test.name, func(t *testing.T) { + got, err := newAvString(test.val) + if err == nil { + t.Errorf("got %v, exp error", got) + } else if err.Error() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp) + } + }) + } +} + +func TestAvStringString(t *testing.T) { + tests := []struct { + name string + val AvString + exp string + } { + { "any", AvString { AnyString, "" }, "*" }, + { "na", AvString { NaString, "" }, "-" }, + { "foo", AvString { ValString, "foo" }, "foo" }, + { "junk", AvString { AvStringType(255), "foo" }, "" }, + } + + for _, test := range(tests) { + t.Run(test.name, func(t *testing.T) { + got := test.val.String() + if got != test.exp { + t.Errorf("value: got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} + +func TestAvStringTypeString(t *testing.T) { + tests := []struct { + val AvStringType + exp string + } { + { AnyString, "any" }, + { NaString, "na" }, + { ValString, "val" }, + { AvStringType(255), "AvStringType(255)" }, + } + + for _, test := range(tests) { + t.Run(test.exp, func(t *testing.T) { + got := test.val.String() + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} |