8 Commits

Author SHA1 Message Date
Kevin Atkinson
bb91b53e56 gx publish 0.3.0 2018-08-31 20:32:43 -04:00
Steven Allen
964f55ad40 Merge pull request #23 from multiformats/kevina/encoder
Don't return an error on NewEncoder, panic on invalid encodings instead.
2018-09-01 00:20:04 +00:00
Kevin Atkinson
3b3047873d Use MustNewEncoder instead for version that does not panic. 2018-08-31 20:08:55 -04:00
Kevin Atkinson
2170058ef9 Add CheckEncoding function. 2018-08-31 18:59:07 -04:00
Kevin Atkinson
ac3d23441b Don't return an error on NewEncoder, panic on invalid encodings instead.
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.
2018-08-31 15:23:22 -04:00
Kevin Atkinson
ecd5d58562 Gofmt. 2018-08-31 15:01:41 -04:00
Steven Allen
b46f1c99f0 Merge pull request #22 from ianlopshire/master
Improve test coverage
2018-08-23 17:41:56 +00:00
Ian Lopshire
5fb339e88a Improve test coverage 2018-08-23 13:29:45 -04:00
5 changed files with 52 additions and 22 deletions

View File

@@ -1 +1 @@
0.2.7: QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR
0.3.0: QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd

View File

@@ -19,6 +19,16 @@ func NewEncoder(base Encoding) (Encoder, error) {
return Encoder{base}, nil
}
// MustNewEncoder is like NewEncoder but will panic if the encoding is
// invalid.
func MustNewEncoder(base Encoding) Encoder {
_, ok := EncodingToStr[base]
if !ok {
panic("Unsupported multibase encoding")
}
return Encoder{base}
}
// EncoderByName creates an encoder from a string, the string can
// either be the multibase name or single character multibase prefix
func EncoderByName(str string) (Encoder, error) {

View File

@@ -4,30 +4,48 @@ import (
"testing"
)
func TestInvalidPrefix(t *testing.T) {
func TestInvalidCode(t *testing.T) {
_, err := NewEncoder('q')
if err == nil {
t.Error("expected failure")
}
}
func TestPrefix(t *testing.T) {
for str, base := range Encodings {
prefix, err := NewEncoder(base)
if err != nil {
t.Fatalf("NewEncoder(%c) failed: %v", base, err)
}
str1, err := Encode(base, sampleBytes)
if err != nil {
t.Fatal(err)
}
str2 := prefix.Encode(sampleBytes)
if str1 != str2 {
t.Errorf("encoded string mismatch: %s != %s", str1, str2)
}
_, err = EncoderByName(str)
if err != nil {
t.Fatalf("NewEncoder(%s) failed: %v", str, err)
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
_, err = EncoderByName(str[0:1])
if err != nil {
t.Fatalf("EncoderByName(%s) failed: %v", str[0:1], err)
}
}
}

View File

@@ -7,13 +7,13 @@ import (
)
func TestMap(t *testing.T) {
for s,e := range Encodings {
for s, e := range Encodings {
s2 := EncodingToStr[e]
if s != s2 {
t.Errorf("round trip failed on encoding map: %s != %s", s, s2)
}
}
for e,s := range EncodingToStr {
for e, s := range EncodingToStr {
e2 := Encodings[s]
if e != e2 {
t.Errorf("round trip failed on encoding map: '%c' != '%c'", e, e2)
@@ -35,6 +35,8 @@ var encodedSamples = map[Encoding]string{
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
Base64: "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
}

View File

@@ -25,6 +25,6 @@
"license": "",
"name": "go-multibase",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.2.7"
"version": "0.3.0"
}