2023-10-30 16:58:57 +00:00
|
|
|
package walletconnect
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2023-10-30 16:58:57 +00:00
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
func Test_parseCaip2(t *testing.T) {
|
2023-10-30 16:58:57 +00:00
|
|
|
type args struct {
|
|
|
|
str string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2023-11-24 15:27:05 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantChain uint64
|
|
|
|
wantErr bool
|
2023-10-30 16:58:57 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid",
|
|
|
|
args: args{
|
|
|
|
str: "eip155:5",
|
|
|
|
},
|
2023-11-24 15:27:05 +00:00
|
|
|
wantChain: 5,
|
|
|
|
wantErr: false,
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid_number",
|
|
|
|
args: args{
|
|
|
|
str: "eip155:5a",
|
|
|
|
},
|
2023-11-24 15:27:05 +00:00
|
|
|
wantChain: 0,
|
|
|
|
wantErr: true,
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid_caip2_too_many",
|
|
|
|
args: args{
|
|
|
|
str: "eip155:1:5",
|
|
|
|
},
|
2023-11-24 15:27:05 +00:00
|
|
|
wantChain: 0,
|
|
|
|
wantErr: true,
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid_caip2_not_enough",
|
|
|
|
args: args{
|
|
|
|
str: "eip1551",
|
|
|
|
},
|
2023-11-24 15:27:05 +00:00
|
|
|
wantChain: 0,
|
|
|
|
wantErr: true,
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-11-24 15:27:05 +00:00
|
|
|
gotNamespaceName, gotChainID, err := parseCaip2ChainID(tt.args.str)
|
2023-10-30 16:58:57 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("parseCaip2ChainID() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2023-11-24 15:27:05 +00:00
|
|
|
if !tt.wantErr && !strings.Contains(tt.args.str, gotNamespaceName) {
|
|
|
|
t.Errorf("parseCaip2ChainID() = %v, doesn't match %v", gotNamespaceName, tt.args.str)
|
|
|
|
}
|
|
|
|
if gotChainID != tt.wantChain {
|
|
|
|
t.Errorf("parseCaip2ChainID() = %v, want %v", gotChainID, tt.wantChain)
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|