2018-07-26 12:50:11 -04:00
|
|
|
package multibase
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2018-08-31 20:08:55 -04:00
|
|
|
func TestInvalidCode(t *testing.T) {
|
|
|
|
|
_, err := NewEncoder('q')
|
2018-08-31 18:17:03 -04:00
|
|
|
if err == nil {
|
2018-08-31 20:08:55 -04:00
|
|
|
t.Error("expected failure")
|
2018-08-31 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 13:29:45 -04:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 15:06:09 -04:00
|
|
|
func TestEncoder(t *testing.T) {
|
|
|
|
|
for name, code := range Encodings {
|
2018-08-31 20:08:55 -04:00
|
|
|
encoder, err := NewEncoder(code)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
// Make sure the MustNewEncoder doesn't panic
|
|
|
|
|
MustNewEncoder(code)
|
2018-08-31 15:06:09 -04:00
|
|
|
str, err := Encode(code, sampleBytes)
|
2018-07-26 12:50:11 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2018-08-31 15:06:09 -04:00
|
|
|
str2 := encoder.Encode(sampleBytes)
|
|
|
|
|
if str != str2 {
|
|
|
|
|
t.Errorf("encoded string mismatch: %s != %s", str, str2)
|
|
|
|
|
}
|
|
|
|
|
_, err = EncoderByName(name)
|
|
|
|
|
if err != nil {
|
2018-08-31 20:08:55 -04:00
|
|
|
t.Fatalf("EncoderByName(%s) failed: %v", name, err)
|
2018-07-26 12:50:11 -04:00
|
|
|
}
|
2018-08-31 15:06:09 -04:00
|
|
|
// Test that an encoder can be created from the single letter
|
|
|
|
|
// prefix
|
|
|
|
|
_, err = EncoderByName(str[0:1])
|
2018-07-26 17:57:34 -04:00
|
|
|
if err != nil {
|
2018-08-31 20:08:55 -04:00
|
|
|
t.Fatalf("EncoderByName(%s) failed: %v", str[0:1], err)
|
2018-07-26 17:57:34 -04:00
|
|
|
}
|
2018-07-26 12:50:11 -04:00
|
|
|
}
|
|
|
|
|
}
|