-
Notifications
You must be signed in to change notification settings - Fork 17
/
encoder_test.go
53 lines (49 loc) · 1.13 KB
/
encoder_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
package multibase
import (
"testing"
"unicode/utf8"
)
func TestInvalidCode(t *testing.T) {
_, err := NewEncoder('q')
if err == nil {
t.Error("expected failure")
}
}
func TestInvalidName(t *testing.T) {
values := []string{"invalid", "", "q"}
for _, val := range values {
_, err := EncoderByName(val)
if err == nil {
t.Errorf("EncoderByName(%v) expected failure", val)
}
}
}
func TestEncoder(t *testing.T) {
for name, code := range Encodings {
encoder, err := NewEncoder(code)
if err != nil {
t.Fatal(err)
}
// Make sure the MustNewEncoder doesn't panic
MustNewEncoder(code)
str, err := Encode(code, sampleBytes)
if err != nil {
t.Fatal(err)
}
str2 := encoder.Encode(sampleBytes)
if str != str2 {
t.Errorf("encoded string mismatch: %s != %s", str, str2)
}
_, err = EncoderByName(name)
if err != nil {
t.Fatalf("EncoderByName(%s) failed: %v", name, err)
}
// Test that an encoder can be created from the single letter
// prefix
r, _ := utf8.DecodeRuneInString(str)
_, err = EncoderByName(string(r))
if err != nil {
t.Fatalf("EncoderByName(%s) failed: %v", string(r), err)
}
}
}