Repository navigation
Expand file tree
/
Copy pathdecoder_test.go
More file actions
54 lines (47 loc) · 1.52 KB
/
Copy pathdecoder_test.go
File metadata and controls
54 lines (47 loc) · 1.52 KB
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
package morsegode
import "testing"
func TestDecode(t *testing.T) {
tests := []struct {
name string
msg string
want string
}{
{name: "single word", msg: ".... . .-.. .-.. ---", want: "hello"},
{name: "multiple words", msg: ".... . .-.. .-.. --- / .-- --- .-. .-.. -..", want: "hello world"},
{name: "extra whitespace around separator", msg: " .... . .-.. .-.. --- / .-- --- .-. .-.. -.. ", want: "hello world"},
{name: "empty message", msg: "", want: ""},
{name: "whitespace only", msg: " ", want: ""},
{name: "sos", msg: "... --- ...", want: "sos"},
}
for _, tt := range tests {
got, err := AlphabeticDecoder.Decode(tt.msg)
if err != nil {
t.Errorf("%s: unexpected error: %v", tt.name, err)
continue
}
if got != tt.want {
t.Errorf("%s: Decode() = %q, want %q", tt.name, got, tt.want)
}
}
}
func TestDecodeInvalid(t *testing.T) {
// "....-" walks off the alphabetic tree.
if _, err := AlphabeticDecoder.Decode(".... . ....-"); err == nil {
t.Errorf("Decode with an invalid sequence should return an error")
}
// "x" is not a dot or dash.
if _, err := AlphabeticDecoder.Decode(".... . x"); err == nil {
t.Errorf("Decode with an unknown symbol should return an error")
}
}
func TestDecodeRoundTripWithWords(t *testing.T) {
msg := ".... . .-.. .-.. --- / .. / .-.. --- ...- . / -- --- .-. ... ."
want := "hello i love morse"
got, err := AlphabeticDecoder.Decode(msg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != want {
t.Errorf("Decode() = %q, want %q", got, want)
}
}