Most of the time this method will be used with a constant and the error will be thrown away anyway. By not returning an error we can use this function to initialize global variables. The function EncoderByName should be used when working with user provided input and we care about the error.
40 lines
867 B
Go
40 lines
867 B
Go
package multibase
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
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 := NewEncoder(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.Errorf("EncoderByName(%s) failed: %v", name, err)
|
|
}
|
|
// Test that an encoder can be created from the single letter
|
|
// prefix
|
|
_, err = EncoderByName(str[0:1])
|
|
if err != nil {
|
|
t.Errorf("EncoderByName(%s) failed: %v", str[0:1], err)
|
|
}
|
|
}
|
|
}
|