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

govis: switch to testify tests

testify makes most bog-standard test checks much easier to read and
maintain, and is quite widely used. It wasn't really well known back
when govis was first written, but the migration is fairly
straight-forward for most tests.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2025-09-20 06:08:34 +10:00
parent b0c7528ac6
commit d02f298ad4
No known key found for this signature in database
GPG key ID: 2897FAD2B7E9446F
4 changed files with 233 additions and 180 deletions

View file

@ -17,6 +17,11 @@
package govis package govis
import (
"strconv"
"strings"
)
// VisFlag manipulates how the characters are encoded/decoded // VisFlag manipulates how the characters are encoded/decoded
type VisFlag uint type VisFlag uint
@ -37,3 +42,40 @@ const (
VisWhite VisFlag = (VisSpace | VisTab | VisNewline) VisWhite VisFlag = (VisSpace | VisTab | VisNewline)
) )
// String pretty-prints VisFlag.
func (vflags VisFlag) String() string {
flagNames := []struct {
name string
bits VisFlag
}{
{"VisOctal", VisOctal},
{"VisCStyle", VisCStyle},
{"VisSpace", VisSpace},
{"VisTab", VisTab},
{"VisNewline", VisNewline},
{"VisSafe", VisSafe},
{"VisNoSlash", VisNoSlash},
{"VisHTTPStyle", VisHTTPStyle},
{"VisGlob", VisGlob},
}
var (
flagSet = make([]string, 0, len(flagNames))
seenBits VisFlag
)
for _, flag := range flagNames {
if vflags&flag.bits == flag.bits {
seenBits |= flag.bits
flagSet = append(flagSet, flag.name)
}
}
// If there were any remaining flags specified we don't know the name of,
// just add them in an 0x... format.
if remaining := vflags &^ seenBits; remaining != 0 {
flagSet = append(flagSet, "0x"+strconv.FormatUint(uint64(remaining), 16))
}
if len(flagSet) == 0 {
return "0"
}
return strings.Join(flagSet, "|")
}

View file

@ -18,9 +18,11 @@
package govis package govis
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
const DefaultVisFlags = VisWhite | VisOctal | VisGlob const DefaultVisFlags = VisWhite | VisOctal | VisGlob
@ -31,31 +33,29 @@ func TestRandomVisUnvis(t *testing.T) {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
testBytes := make([]byte, 256) testBytes := make([]byte, 256)
if n, err := rand.Read(testBytes); n != cap(testBytes) || err != nil {
t.Fatalf("could not read enough bytes: err=%v n=%d", err, n) n, err := rand.Read(testBytes)
} require.NoErrorf(t, err, "read %d random bytes", len(testBytes))
require.Equal(t, len(testBytes), n, "read unexpected number of bytes")
test := string(testBytes) test := string(testBytes)
for flag := VisFlag(0); flag <= visMask; flag++ { for flag := VisFlag(0); flag <= visMask; flag++ {
// VisNoSlash is frankly just a dumb flag, and it is impossible for us // VisNoSlash is frankly just a dumb flag, and it is impossible
// to actually preserve things in a round-trip. // for us to actually preserve things in a round-trip.
if flag&VisNoSlash == VisNoSlash { if flag&VisNoSlash == VisNoSlash {
continue continue
} }
enc, err := Vis(test, flag) enc, err := Vis(test, flag)
if err != nil { require.NoErrorf(t, err, "vis(%q, %s)", test, flag)
t.Errorf("unexpected error doing vis(%q, %b): %s", test, flag, err)
continue
}
dec, err := Unvis(enc, flag) dec, err := Unvis(enc, flag)
if err != nil { require.NoErrorf(t, err, "unvis(%q, %s)", enc, flag)
t.Errorf("unexpected error doing unvis(%q, %b): %s", enc, flag, err)
if !assert.Equalf(t, test, dec, "unvis(vis(%q, %b) = %q, %b) round-trip", test, flag, enc, flag) {
continue continue
} }
if dec != test {
t.Errorf("roundtrip failed: unvis(vis(%q, %b) = %q, %b) = %q", test, flag, enc, flag, dec)
}
} }
} }
} }
@ -66,93 +66,88 @@ func TestRandomVisVisUnvisUnvis(t *testing.T) {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
testBytes := make([]byte, 256) testBytes := make([]byte, 256)
if n, err := rand.Read(testBytes); n != cap(testBytes) || err != nil {
t.Fatalf("could not read enough bytes: err=%v n=%d", err, n) n, err := rand.Read(testBytes)
} require.NoErrorf(t, err, "read %d random bytes", len(testBytes))
require.Equal(t, len(testBytes), n, "read unexpected number of bytes")
test := string(testBytes) test := string(testBytes)
for flag := VisFlag(0); flag <= visMask; flag++ { for flag := VisFlag(0); flag <= visMask; flag++ {
// VisNoSlash is frankly just a dumb flag, and it is impossible for us // VisNoSlash is frankly just a dumb flag, and it is impossible
// to actually preserve things in a round-trip. // for us to actually preserve things in a round-trip.
if flag&VisNoSlash == VisNoSlash { if flag&VisNoSlash == VisNoSlash {
continue continue
} }
enc, err := Vis(test, flag) enc, err := Vis(test, flag)
if err != nil { require.NoErrorf(t, err, "vis(%q, %s)", test, flag)
t.Errorf("unexpected error doing vis(%q, %b): %s", test, flag, err)
continue
}
enc2, err := Vis(enc, flag) enc2, err := Vis(enc, flag)
if err != nil { require.NoErrorf(t, err, "vis(%q, %s)", enc, flag)
t.Errorf("unexpected error doing vis(%q, %b): %s", enc, flag, err)
dec2, err := Unvis(enc2, flag)
require.NoErrorf(t, err, "unvis(%q, %s)", enc2, flag)
dec, err := Unvis(dec2, flag)
require.NoErrorf(t, err, "unvis(%q, %s)", dec2, flag)
if !assert.Equalf(t, test, dec, "unvis(unvis(vis(vis(%q) = %q) = %q) = %q, %b) round-trip", test, enc, enc2, dec2, flag) {
continue continue
} }
dec, err := Unvis(enc2, flag)
if err != nil {
t.Errorf("unexpected error doing unvis(%q, %b): %s", enc2, flag, err)
continue
}
dec2, err := Unvis(dec, flag)
if err != nil {
t.Errorf("unexpected error doing unvis(%q, %b): %s", dec, flag, err)
continue
}
if dec2 != test {
t.Errorf("roundtrip failed: unvis(unvis(vis(vis(%q) = %q) = %q) = %q, %b) = %q", test, enc, enc2, dec, flag, dec2)
}
} }
} }
} }
func TestVisUnvis(t *testing.T) { func TestVisUnvis(t *testing.T) {
for flag := VisFlag(0); flag <= visMask; flag++ { // Round-trip testing.
// VisNoSlash is frankly just a dumb flag, and it is impossible for us for _, test := range []struct {
// to actually preserve things in a round-trip. name string
if flag&VisNoSlash == VisNoSlash { input string
continue }{
} {"Empty", ""},
{"Plain", "hello world"},
{"Backslash", "THIS\\IS_A_TEST1234"},
{"Punctuation", "this.is.a.normal_string"},
{"Unicode1", "AC_Ra\u00edz_Certic\u00e1mara_S.A..pem"},
{"Unicode2", "NetLock_Arany_=Class_Gold=_F\u0151tan\u00fas\u00edtv\u00e1ny.pem"},
{"Unicode3", "T\u00dcB\u0130TAK_UEKAE_K\u00f6k_Sertifika_Hizmet_Sa\u011flay\u0131c\u0131s\u0131_-_S\u00fcr\u00fcm_3.pem"},
{"ExtraPunctuation", "hello world [ this string needs=enco ding! ]"},
{"Whitespace", "even \n more encoding necessary\a\a "},
{"WeirdChars", "\024 <-- some more weird characters --> \u4f60\u597d\uff0c\u4e16\u754c"},
{"DoubleEncoding", "\\xff\\n double encoding is also great fun \\x"},
{"Unicode1-Encoded", "AC_Ra\\M-C\\M--z_Certic\\M-C\\M-!mara_S.A..pem"},
{"Rand1", "z^i3i$\u00d3\u008anqgh5/t\u00e5<86>\u00b2kzla\\e^lv\u00df\u0093nv\u00df\u00aea|3}\u00d8\u0088\u00d6\u0084"},
{"Rand2", `z^i3i$\M-C\M^S\M-B\M^Jnqgh5/t\M-C\M-%<86>\M-B\M-2kzla\\e^lv\M-C\M^_\M-B\M^Snv\M-C\M^_\M-B\M-.a|3}\M-C\M^X\M-B\M^H\M-C\M^V\M-B\M^D`},
{"Rand3", "@?e1xs+.R_Kjo]7s8pgRP:*nXCE4{!c"},
{"Rand4", "62_\u00c6\u00c62\u00ae\u00b7m\u00db\u00c3r^\u00bfp\u00c6u'q\u00fbc2\u00f0u\u00b8\u00dd\u00e8v\u00ff\u00b0\u00dc\u00c2\u00f53\u00db-k\u00f2sd4\\p\u00da\u00a6\u00d3\u00eea<\u00e6s{\u00a0p\u00f0\u00ffj\u00e0\u00e8\u00b8\u00b8\u00bc\u00fcb"},
{"Rand4-Encoded", `62_\M-C\M^F\M-C\M^F2\M-B\M-.\M-B\M-7m\M-C\M^[\M-C\M^Cr^\M-B\M-?p\M-C\M^Fu'q\M-C\M-;c2\M-C\M-0u\M-B\M-8\M-C\M^]\M-C\M-(v\M-C\M-?\M-B\M-0\M-C\M^\\M-C\M^B\M-C\M-53\M-C\M^[-k\M-C\M-2sd4\\p\M-C\M^Z\M-B\M-&\M-C\M^S\M-C\M-.a<\M-C\M-&s{\M-B\240p\M-C\M-0\M-C\M-?j\M-C\240\M-C\M-(\M-B\M-8\M-B\M-8\M-B\M-<\M-C\M-<b`},
{"Rand5", "\u9003\"9v1)T798|o;fly jnKX\u0489Be="},
{"Rand5-Encoded", `\M-i\M^@\M^C"9v1)T798|o;fly jnKX\M-R\M^IBe=`},
{"Rand6", "'3Ze\u050e|\u02del\u069du-Rpct4+Z5b={@_{b"},
{"Rand6-Encoded", `'3Ze\M-T\M^N|\M-K\M^^l\M-Z\M^]u-Rpct4+Z5b={@_{b`},
{"Rand7", "1\u00c6\u00abTcz+Vda?)k1%\\\"P;`po`h"},
{"Rand7-Encoded", `1%C3%86%C2%ABTcz+Vda%3F)k1%25%5C%22P%3B%60po%60h`},
} {
t.Run(test.name, func(t *testing.T) {
for flag := VisFlag(0); flag <= visMask; flag++ {
// VisNoSlash is frankly just a dumb flag, and it is impossible for us
// to actually preserve things in a round-trip.
if flag&VisNoSlash == VisNoSlash {
continue
}
// Round-trip testing. enc, err := Vis(test.input, flag)
for _, test := range []string{ require.NoErrorf(t, err, "vis(%q, %s)", test.input, flag)
"",
"hello world", dec, err := Unvis(enc, flag)
"THIS\\IS_A_TEST1234", require.NoErrorf(t, err, "unvis(%q, %s)", enc, flag)
"this.is.a.normal_string",
"AC_Ra\u00edz_Certic\u00e1mara_S.A..pem", if !assert.Equalf(t, test.input, dec, "unvis(vis(%q, %b) = %q, %b) round-trip", test, flag, enc, flag) {
"NetLock_Arany_=Class_Gold=_F\u0151tan\u00fas\u00edtv\u00e1ny.pem", continue
"T\u00dcB\u0130TAK_UEKAE_K\u00f6k_Sertifika_Hizmet_Sa\u011flay\u0131c\u0131s\u0131_-_S\u00fcr\u00fcm_3.pem", }
"hello world [ this string needs=enco ding! ]",
"even \n more encoding necessary\a\a ",
"\024 <-- some more weird characters --> \u4f60\u597d\uff0c\u4e16\u754c",
"\\xff\\n double encoding is also great fun \\x",
"AC_Ra\\M-C\\M--z_Certic\\M-C\\M-!mara_S.A..pem",
"z^i3i$\u00d3\u008anqgh5/t\u00e5<86>\u00b2kzla\\e^lv\u00df\u0093nv\u00df\u00aea|3}\u00d8\u0088\u00d6\u0084",
`z^i3i$\M-C\M^S\M-B\M^Jnqgh5/t\M-C\M-%<86>\M-B\M-2kzla\\e^lv\M-C\M^_\M-B\M^Snv\M-C\M^_\M-B\M-.a|3}\M-C\M^X\M-B\M^H\M-C\M^V\M-B\M^D`,
"@?e1xs+.R_Kjo]7s8pgRP:*nXCE4{!c",
"62_\u00c6\u00c62\u00ae\u00b7m\u00db\u00c3r^\u00bfp\u00c6u'q\u00fbc2\u00f0u\u00b8\u00dd\u00e8v\u00ff\u00b0\u00dc\u00c2\u00f53\u00db-k\u00f2sd4\\p\u00da\u00a6\u00d3\u00eea<\u00e6s{\u00a0p\u00f0\u00ffj\u00e0\u00e8\u00b8\u00b8\u00bc\u00fcb",
`62_\M-C\M^F\M-C\M^F2\M-B\M-.\M-B\M-7m\M-C\M^[\M-C\M^Cr^\M-B\M-?p\M-C\M^Fu'q\M-C\M-;c2\M-C\M-0u\M-B\M-8\M-C\M^]\M-C\M-(v\M-C\M-?\M-B\M-0\M-C\M^\\M-C\M^B\M-C\M-53\M-C\M^[-k\M-C\M-2sd4\\p\M-C\M^Z\M-B\M-&\M-C\M^S\M-C\M-.a<\M-C\M-&s{\M-B\240p\M-C\M-0\M-C\M-?j\M-C\240\M-C\M-(\M-B\M-8\M-B\M-8\M-B\M-<\M-C\M-<b`,
"\u9003\"9v1)T798|o;fly jnKX\u0489Be=",
`\M-i\M^@\M^C"9v1)T798|o;fly jnKX\M-R\M^IBe=`,
"'3Ze\u050e|\u02del\u069du-Rpct4+Z5b={@_{b",
`'3Ze\M-T\M^N|\M-K\M^^l\M-Z\M^]u-Rpct4+Z5b={@_{b`,
"1\u00c6\u00abTcz+Vda?)k1%\\\"P;`po`h",
`1%C3%86%C2%ABTcz+Vda%3F)k1%25%5C%22P%3B%60po%60h`,
} {
enc, err := Vis(test, flag)
if err != nil {
t.Errorf("unexpected error doing vis(%q, %b): %s", test, flag, err)
continue
} }
dec, err := Unvis(enc, flag) })
if err != nil {
t.Errorf("unexpected error doing unvis(%q, %b): %s", enc, flag, err)
continue
}
if dec != test {
t.Errorf("roundtrip failed: unvis(vis(%q, %b) = %q, %b) = %q", test, flag, enc, flag, dec)
}
}
} }
} }
@ -162,33 +157,60 @@ func TestByteStrings(t *testing.T) {
// identical but bit-stream non-identical strings (causing much confusion // identical but bit-stream non-identical strings (causing much confusion
// when trying to access such files). // when trying to access such files).
for _, test := range [][]byte{ for _, test := range []struct {
[]byte("This is a man in business suit levitating: \U0001f574"), name string
{0x7f, 0x17, 0x01, 0x33}, input []byte
}{
{"Unicode", []byte("This is a man in business suit levitating: \U0001f574")},
{"ASCII", []byte{0x7f, 0x17, 0x01, 0x33}},
// TODO: Test arbitrary byte streams like the one below. Currently this // TODO: Test arbitrary byte streams like the one below. Currently this
// fails because Vis() is messing around with it (converting it // fails because Vis() is messing around with it (converting it
// to a rune and spacing it out). // to a rune and spacing it out).
//{'\xef', '\xae', 'h', '\077', 'k'}, //{'\xef', '\xae', 'h', '\077', 'k'},
} { } {
testString := string(test) t.Run(test.name, func(t *testing.T) {
enc, err := Vis(testString, DefaultVisFlags) testString := string(test.input)
if err != nil {
t.Errorf("unexpected error doing vis(%q): %s", test, err)
continue
}
dec, err := Unvis(enc, DefaultVisFlags)
if err != nil {
t.Errorf("unexpected error doing unvis(%q): %s", enc, err)
continue
}
decBytes := []byte(dec)
if dec != testString { enc, err := Vis(testString, DefaultVisFlags)
t.Errorf("roundtrip failed [string comparison]: unvis(vis(%q) = %q) = %q", test, enc, dec) require.NoErrorf(t, err, "vis(%q)", testString)
}
if !bytes.Equal(decBytes, test) { dec, err := Unvis(enc, DefaultVisFlags)
t.Errorf("roundtrip failed [byte comparison]: unvis(vis(%q) = %q) = %q", test, enc, dec) require.NoErrorf(t, err, "unvis(%q)", enc)
}
assert.Equalf(t, testString, dec, "unvis(vis(%q) = %q) round-trip", testString, enc)
assert.Equalf(t, test.input, []byte(dec), "unvis(vis(%q) = %q) round-trip", testString, enc)
})
}
}
func TestVisFlagsString(t *testing.T) {
for _, test := range []struct {
name string
flags VisFlag
expected string
}{
{"Empty", VisFlag(0), "0"},
// Single valid flags.
{"Single-VisOctal", VisOctal, "VisOctal"},
{"Single-VisCStyle", VisCStyle, "VisCStyle"},
{"Single-VisSpace", VisSpace, "VisSpace"},
{"Single-VisTab", VisTab, "VisTab"},
{"Single-VisNewline", VisNewline, "VisNewline"},
{"Single-VisSafe", VisSafe, "VisSafe"},
{"Single-VisNoSlash", VisNoSlash, "VisNoSlash"},
{"Single-VisHTTPStyle", VisHTTPStyle, "VisHTTPStyle"},
{"Single-VisGlob", VisGlob, "VisGlob"},
// Invalid flag value.
{"Unknown", VisFlag(0xff000), "0xff000"},
// Multiple flag values.
{"VisWhite", VisWhite, "VisSpace|VisTab|VisNewline"},
{"DefaultVisFlags", DefaultVisFlags, "VisOctal|VisSpace|VisTab|VisNewline|VisGlob"},
{"Multiple-Valid", VisOctal | VisCStyle | VisNoSlash | VisHTTPStyle, "VisOctal|VisCStyle|VisNoSlash|VisHTTPStyle"},
{"Multiple-Mixed", VisOctal | VisSafe | 0xbeef0000, "VisOctal|VisSafe|0xbeef0000"},
} {
t.Run(test.name, func(t *testing.T) {
got := test.flags.String()
assert.Equal(t, test.expected, got, "VisFlag(%+x).String")
})
} }
} }

View file

@ -19,19 +19,24 @@ package govis
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestUnvisError(t *testing.T) { func TestUnvisError(t *testing.T) {
for _, test := range []string{ for _, test := range []string{
// Octal escape codes allow you to specify invalid byte values. // Octal escape codes allow you to specify invalid ASCII values.
"\\777", "\\777",
"\\420\\322\\455", "\\420\\322\\455",
"\\652\\233", "\\652\\233",
} { } {
got, err := Unvis(test, DefaultVisFlags) t.Run(test, func(t *testing.T) {
if err == nil { _, err := Unvis(test, DefaultVisFlags)
t.Errorf("expected unvis(%q) to give an error, got %q", test, got) require.Errorf(t, err, "invalid octal escape should give an error")
} assert.ErrorContains(t, err, "escape base 8")
assert.ErrorContains(t, err, "outside latin-1 encoding")
})
} }
} }
@ -52,14 +57,11 @@ func TestUnvisCStyleEscape(t *testing.T) {
{"test\\\ning", "testing"}, {"test\\\ning", "testing"},
{"test\\$\\$ing", "testing"}, {"test\\$\\$ing", "testing"},
} { } {
got, err := Unvis(test.input, DefaultVisFlags) t.Run(test.input, func(t *testing.T) {
if err != nil { got, err := Unvis(test.input, DefaultVisFlags)
t.Errorf("unexpected error doing unvis(%q): %q", test.input, err) require.NoErrorf(t, err, "unvis(%q)", test.input)
continue assert.Equal(t, test.expected, got, "unvis(%q)", test.input)
} })
if got != test.expected {
t.Errorf("expected unvis(%q) = %q, got %q", test.input, test.expected, got)
}
} }
} }
@ -76,14 +78,11 @@ func TestUnvisMetaEscape(t *testing.T) {
// TODO: Add some more of these tests, but I need to have some // TODO: Add some more of these tests, but I need to have some
// secondary source to verify these outputs properly. // secondary source to verify these outputs properly.
} { } {
got, err := Unvis(test.input, DefaultVisFlags) t.Run(test.input, func(t *testing.T) {
if err != nil { got, err := Unvis(test.input, DefaultVisFlags)
t.Errorf("unexpected error doing unvis(%q): %q", test.input, err) require.NoErrorf(t, err, "unvis(%q)", test.input)
continue assert.Equal(t, test.expected, got, "unvis(%q)", test.input)
} })
if got != test.expected {
t.Errorf("expected unvis(%q) = %q, got %q", test.input, test.expected, got)
}
} }
} }
@ -106,14 +105,11 @@ func TestUnvisOctalEscape(t *testing.T) {
// Some invalid characters... // Some invalid characters...
{"\\377\\2\\225\\264", "\xff\x02\x95\xb4"}, {"\\377\\2\\225\\264", "\xff\x02\x95\xb4"},
} { } {
got, err := Unvis(test.input, DefaultVisFlags) t.Run(test.input, func(t *testing.T) {
if err != nil { got, err := Unvis(test.input, DefaultVisFlags)
t.Errorf("unexpected error doing unvis(%q): %q", test.input, err) require.NoErrorf(t, err, "unvis(%q)", test.input)
continue assert.Equal(t, test.expected, got, "unvis(%q)", test.input)
} })
if got != test.expected {
t.Errorf("expected unvis(%q) = %q, got %q", test.input, test.expected, got)
}
} }
} }
@ -134,14 +130,11 @@ func TestUnvisHexEscape(t *testing.T) {
// Some invalid characters... // Some invalid characters...
{"\\xff\\x02\\x95\\xb4", "\xff\x02\x95\xb4"}, {"\\xff\\x02\\x95\\xb4", "\xff\x02\x95\xb4"},
} { } {
got, err := Unvis(test.input, DefaultVisFlags) t.Run(test.input, func(t *testing.T) {
if err != nil { got, err := Unvis(test.input, DefaultVisFlags)
t.Errorf("unexpected error doing unvis(%q): %q", test.input, err) require.NoErrorf(t, err, "unvis(%q)", test.input)
continue assert.Equal(t, test.expected, got, "unvis(%q)", test.input)
} })
if got != test.expected {
t.Errorf("expected unvis(%q) = %q, got %q", test.input, test.expected, got)
}
} }
} }
@ -154,13 +147,10 @@ func TestUnvisUnicode(t *testing.T) {
"NetLock_Arany_=Class_Gold=_Főtanúsítvány.pem", "NetLock_Arany_=Class_Gold=_Főtanúsítvány.pem",
"TÜBİTAK_UEKAE_Kök_Sertifika_Hizmet_Sağlayıcısı_-_Sürüm_3.pem", "TÜBİTAK_UEKAE_Kök_Sertifika_Hizmet_Sağlayıcısı_-_Sürüm_3.pem",
} { } {
got, err := Unvis(test, DefaultVisFlags) t.Run(test, func(t *testing.T) {
if err != nil { enc, err := Unvis(test, DefaultVisFlags)
t.Errorf("unexpected error doing unvis(%q): %s", test, err) require.NoErrorf(t, err, "unvis(%q)", test)
continue assert.Equalf(t, test, enc, "decoding of %q should be the same as original", test)
} })
if got != test {
t.Errorf("expected %q to be unchanged, got %q", test, got)
}
} }
} }

View file

@ -18,34 +18,37 @@
package govis package govis
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestVisUnchanged(t *testing.T) { func TestVisUnchanged(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {
name string
input string input string
flag VisFlag flag VisFlag
}{ }{
{"", DefaultVisFlags}, {"Empty", "", DefaultVisFlags},
{"helloworld", DefaultVisFlags}, {"Plain1", "helloworld", DefaultVisFlags},
{"THIS_IS_A_TEST1234", DefaultVisFlags}, {"Plain2", "THIS_IS_A_TEST1234", DefaultVisFlags},
{"SomeEncodingsAreCool", DefaultVisFlags}, {"Plain3", "SomeEncodingsAreCool", DefaultVisFlags},
{"spaces are totally safe", DefaultVisFlags &^ VisSpace}, {"Spaces", "spaces are totally safe", DefaultVisFlags &^ VisSpace},
{"tabs\tare\talso\tsafe!!", DefaultVisFlags &^ VisTab}, {"Tabs", "tabs\tare\talso\tsafe!!", DefaultVisFlags &^ VisTab},
{"just\a\atrustme\r\b\b!!", DefaultVisFlags | VisSafe}, {"BasicCtrlChars", "just\a\atrustme\r\b\b!!", DefaultVisFlags | VisSafe},
} { } {
enc, err := Vis(test.input, test.flag) t.Run(test.name, func(t *testing.T) {
if err != nil { enc, err := Vis(test.input, test.flag)
t.Errorf("unexpected error with %q: %s", test, err) require.NoErrorf(t, err, "vis(%q, %s)", test.input, test.flag)
} assert.Equalf(t, test.input, enc, "encoding of vis(%q, %s) should be unchanged", test.input, test.flag)
if enc != test.input { })
t.Errorf("expected encoding of %q (flag=%q) to be unchanged, got %q", test.input, test.flag, enc)
}
} }
} }
func TestVisFlags(t *testing.T) { func TestVisFlags(t *testing.T) {
for _, test := range []struct { for idx, test := range []struct {
input string input string
output string output string
flag VisFlag flag VisFlag
@ -100,13 +103,11 @@ func TestVisFlags(t *testing.T) {
{"'3Ze\u050e|\u02del\u069du-Rpct4+Z5b={@_{b", `'3Ze\M-T\M^N|\M-K\M^^l\M-Z\M^]u-Rpct4+Z5b={@_{b`, VisGlob}, {"'3Ze\u050e|\u02del\u069du-Rpct4+Z5b={@_{b", `'3Ze\M-T\M^N|\M-K\M^^l\M-Z\M^]u-Rpct4+Z5b={@_{b`, VisGlob},
{"'3Ze\u050e|\u02del\u069du-Rpct4+Z5b={@_{b", `'3Ze\324\216|\313\236l\332\235u-Rpct4+Z5b={@_{b`, VisGlob | VisOctal}, {"'3Ze\u050e|\u02del\u069du-Rpct4+Z5b={@_{b", `'3Ze\324\216|\313\236l\332\235u-Rpct4+Z5b={@_{b`, VisGlob | VisOctal},
} { } {
enc, err := Vis(test.input, test.flag) t.Run(fmt.Sprintf("Test%.2d", idx), func(t *testing.T) {
if err != nil { enc, err := Vis(test.input, test.flag)
t.Errorf("unexpected error with %q: %s", test, err) require.NoErrorf(t, err, "vis(%q, %s)", test.input, test.flag)
} assert.Equalf(t, test.output, enc, "vis(%q, %s)", test.input, test.flag)
if enc != test.output { })
t.Errorf("expected vis(%q, flag=%b) = %q, got %q", test.input, test.flag, test.output, enc)
}
} }
} }
@ -116,12 +117,10 @@ func TestVisChanged(t *testing.T) {
"THIS\\IS_A_TEST1234", "THIS\\IS_A_TEST1234",
"AC_Ra\u00edz_Certic\u00e1mara_S.A..pem", "AC_Ra\u00edz_Certic\u00e1mara_S.A..pem",
} { } {
enc, err := Vis(test, DefaultVisFlags) t.Run(test, func(t *testing.T) {
if err != nil { enc, err := Vis(test, DefaultVisFlags)
t.Errorf("unexpected error with %q: %s", test, err) require.NoErrorf(t, err, "vis(%q)", test)
} assert.NotEqualf(t, test, enc, "encoding of %q should be different to original", test)
if enc == test { })
t.Errorf("expected encoding of %q to be changed", test)
}
} }
} }