1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-10-04 04:31:00 +00:00

unvis: add some more error tests

These error cases were already handled correctly, but we really should
have tests for them anyway. Now that we have proper error variables
declared we can also test for specific errors as well.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2025-09-21 00:55:56 +10:00
parent fbada2e081
commit bcdb71fb56
No known key found for this signature in database
GPG key ID: 2897FAD2B7E9446F

View file

@ -19,6 +19,7 @@
package govis package govis
import ( import (
"strconv"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -26,17 +27,33 @@ import (
) )
func TestUnvisError(t *testing.T) { func TestUnvisError(t *testing.T) {
for _, test := range []string{ for _, test := range []struct {
// Octal escape codes allow you to specify invalid ASCII values. input string
"\\777", err error
"\\420\\322\\455",
"\\652\\233",
}{ }{
t.Run(test, func(t *testing.T) { // Octal escape codes allow you to specify invalid ASCII values.
_, err := Unvis(test, DefaultVisFlags) {"\\777", errOutsideLatin1},
require.Errorf(t, err, "invalid octal escape should give an error") {"\\420\\322\\455", errOutsideLatin1},
assert.ErrorContains(t, err, "escape base 8") {"\\652\\233", errOutsideLatin1},
assert.ErrorContains(t, err, "outside latin-1 encoding") // Escapes that end abruptly.
{"\\", errEndOfString},
{"\\J", errUnknownEscapeChar},
{"a bad slash: \\", errEndOfString},
{"testing -- \\x", errEndOfString},
{"\\xG0 test", strconv.ErrSyntax},
{" abc \\Mx", errUnknownEscapeChar},
{"\\Mx", errUnknownEscapeChar},
{"\\M-", errEndOfString},
{"\\M-\u5000", errOutsideLatin1},
{"\\M^", errEndOfString},
{"\\^", errEndOfString},
{"\\^\u5000", errOutsideLatin1},
{"\\M", errEndOfString},
} {
t.Run(test.input, func(t *testing.T) {
_, err := Unvis(test.input, DefaultVisFlags)
require.Errorf(t, err, "invalid escape string should give an error")
assert.ErrorIs(t, err, test.err, "unexpected error from invalid escape string")
}) })
} }
} }