did: add a MustParse function
This commit is contained in:
@@ -116,3 +116,11 @@ func Parse(str string) (DID, error) {
|
|||||||
buf = append(buf, suffix...)
|
buf = append(buf, suffix...)
|
||||||
return DID{str: string(buf), code: DIDCore}, nil
|
return DID{str: string(buf), code: DIDCore}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MustParse(str string) DID {
|
||||||
|
did, err := Parse(str)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return did
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package did
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseDIDKey(t *testing.T) {
|
func TestParseDIDKey(t *testing.T) {
|
||||||
@@ -15,6 +17,18 @@ func TestParseDIDKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMustParseDIDKey(t *testing.T) {
|
||||||
|
str := "did:key:z6Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
d := MustParse(str)
|
||||||
|
require.Equal(t, str, d.String())
|
||||||
|
})
|
||||||
|
str = "did:key:z7Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
|
||||||
|
require.Panics(t, func() {
|
||||||
|
MustParse(str)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecodeDIDKey(t *testing.T) {
|
func TestDecodeDIDKey(t *testing.T) {
|
||||||
str := "did:key:z6Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
|
str := "did:key:z6Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
|
||||||
d0, err := Parse(str)
|
d0, err := Parse(str)
|
||||||
|
|||||||
Reference in New Issue
Block a user