2023-10-30 15:32:15 +03:00
|
|
|
package unpackstring
|
2023-10-30 15:21:12 +03:00
|
|
|
|
|
|
|
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"},
|
2023-11-05 05:22:54 +03:00
|
|
|
{input: "a2", expected: "aa"},
|
|
|
|
{input: "aa", expected: "aa"},
|
|
|
|
{input: "a", expected: "a"},
|
2023-11-05 05:52:58 +03:00
|
|
|
{input: "日本語", expected: "日本語"},
|
|
|
|
{input: "日本語2", expected: "日本語語"},
|
2023-11-05 05:22:54 +03:00
|
|
|
{input: "d\n5abc", expected: "d\n\n\n\n\nabc"},
|
2023-10-30 15:32:15 +03:00
|
|
|
{input: `qwe\4\5`, expected: `qwe45`},
|
|
|
|
{input: `qwe\45`, expected: `qwe44444`},
|
|
|
|
{input: `qwe\\5`, expected: `qwe\\\\\`},
|
|
|
|
{input: `qwe\\\3`, expected: `qwe\3`},
|
2023-10-30 15:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-11-05 15:14:44 +03:00
|
|
|
invalidStrings := []string{"3abc", "45", "1"}
|
2023-10-30 15:21:12 +03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-05 15:14:44 +03:00
|
|
|
|
|
|
|
func TestStringContainsNumber(t *testing.T) {
|
|
|
|
testStrings := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{input: "aaa10b", expected: "aaaaaaaaaaaab"},
|
|
|
|
{input: "a12b4", expected: "aaaaaaaaaaaabbbb"},
|
|
|
|
}
|
|
|
|
for _, tc := range testStrings {
|
|
|
|
t.Run(tc.input, func(t *testing.T) {
|
|
|
|
result, err := Unpack(tc.input)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|