-
Notifications
You must be signed in to change notification settings - Fork 68
/
morse_test.go
75 lines (62 loc) · 1.81 KB
/
morse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package dongle
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var morseTests = []struct {
input string // 输入值
separator string // 分隔符
output string // 期望值
}{
{"", "", ""},
{"1", "/", ".----"},
{"F", "/", "..-."},
{"dongle", "|", "-..|---|-.|--.|.-..|."},
{"SOS", "/", ".../---/..."},
}
func TestMorse_Encode_String(t *testing.T) {
for index, test := range morseTests {
e := Encode.FromString(test.input).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.output, e.ToString())
})
}
}
func TestMorse_Decode_String(t *testing.T) {
for index, test := range morseTests {
d := Decode.FromString(test.output).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, d.Error)
assert.Equal(t, strings.ToLower(test.input), d.ToString())
})
}
}
func TestMorse_Encode_Bytes(t *testing.T) {
for index, test := range morseTests {
e := Encode.FromBytes([]byte(test.input)).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.output), e.ToBytes())
})
}
}
func TestMorse_Decode_Bytes(t *testing.T) {
for index, test := range morseTests {
d := Decode.FromBytes([]byte(test.output)).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, d.Error)
assert.Equal(t, []byte(strings.ToLower(test.input)), d.ToBytes())
})
}
}
func TestMorse_Src_Error(t *testing.T) {
e := Encode.FromString("hello world").ByMorse()
assert.Equal(t, invalidMorseSrcError(), e.Error)
}
func TestMorse_Decoding_Error(t *testing.T) {
e := Decode.FromString("hello world").ByMorse()
assert.Equal(t, morseDecodingError(), e.Error)
}