Golang_HomeWork/hw02_unpack_string/unpack_test.go

108 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package hw02_unpack_string //nolint:golint,stylecheck
import (
"testing"
"github.com/stretchr/testify/require"
)
type test struct {
input string
expected string
err error
}
func TestUnpack(t *testing.T) {
for _, tst := range [...]test{
{
input: "a4bc2d5e",
expected: "aaaabccddddde",
},
{
input: "abccd",
expected: "abccd",
},
{
input: "3abc",
expected: "",
err: ErrInvalidString,
},
{
input: "45",
expected: "",
err: ErrInvalidString,
},
{
input: "aaa10b",
expected: "",
err: ErrInvalidString,
},
{
input: "",
expected: "",
},
{
input: "aaa0b",
expected: "aab",
},
{
input: "fftcwerv ervev sva5scs",
expected: "",
err: ErrInvalidString,
},
{
input: `||\||(8^`,
expected: "",
err: ErrInvalidString,
},
{
input: суРАкс2еу",
expected: "иссскуРАкссеу",
},
{
input: "рлрплотчрв",
expected: "рлрплотчрв",
},
{
input: string([]rune{1234, 67, 45, 2324, 899, 353, 2345, 65}),
expected: "",
err: ErrInvalidString,
},
} {
result, err := Unpack(tst.input)
require.Equal(t, tst.err, err)
require.Equal(t, tst.expected, result)
}
}
func TestUnpackWithEscape(t *testing.T) {
for _, tst := range [...]test{
{
input: `qwe\4\5`,
expected: `qwe45`,
},
{
input: `qwe\45`,
expected: `qwe44444`,
},
{
input: `qwe\\5`,
expected: `qwe\\\\\`,
},
{
input: `qwe\\\3`,
expected: `qwe\3`,
},
{
input: `3\\cds\4`,
expected: ``,
err: ErrInvalidString,
},
} {
result, err := Unpack(tst.input)
require.Equal(t, tst.err, err)
require.Equal(t, tst.expected, result)
}
}