51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package unpackstring
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUnpack(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
expected string
|
|
}{
|
|
{input: "a4bc2d5e", expected: "aaaabccddddde"},
|
|
{input: "abccd", expected: "abccd"},
|
|
{input: "", expected: ""},
|
|
{input: "aaa0b", expected: "aab"},
|
|
{input: "a2", expected: "aa"},
|
|
{input: "aa", expected: "aa"},
|
|
{input: "a", expected: "a"},
|
|
{input: "日本語", expected: "日本語"},
|
|
{input: "日本語2", expected: "日本語語"},
|
|
{input: "d\n5abc", expected: "d\n\n\n\n\nabc"},
|
|
{input: `qwe\4\5`, expected: `qwe45`},
|
|
{input: `qwe\45`, expected: `qwe44444`},
|
|
{input: `qwe\\5`, expected: `qwe\\\\\`},
|
|
{input: `qwe\\\3`, expected: `qwe\3`},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
tc := tc
|
|
t.Run(tc.input, func(t *testing.T) {
|
|
result, err := Unpack(tc.input)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.expected, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnpackInvalidString(t *testing.T) {
|
|
invalidStrings := []string{"3abc", "45", "aaa10b", "1"}
|
|
for _, tc := range invalidStrings {
|
|
tc := tc
|
|
t.Run(tc, func(t *testing.T) {
|
|
_, err := Unpack(tc)
|
|
require.Truef(t, errors.Is(err, ErrInvalidString), "actual error %q", err)
|
|
})
|
|
}
|
|
}
|