Skip to content

Commit

Permalink
fix asset id parsing (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
iCell authored Dec 17, 2024
1 parent 6abd52f commit 99382f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion asset/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
)

func ParseID(id string) (uint, string, error) {
rawResult := strings.Split(id, "_")
rawResult := strings.SplitN(id, "_", 2)
resLen := len(rawResult)
if resLen < 1 {
return 0, "", ErrBadAssetID
Expand Down
40 changes: 31 additions & 9 deletions asset/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,78 @@ import (

func TestParseID(t *testing.T) {
testStruct := []struct {
name string
givenID string
wantedCoin uint
wantedToken string
wantedType CoinType
wantedError error
}{
{"c714_tTWT-8C2",
{
"c714_tTWT-8C2",
"c714_tTWT-8C2",
714,
"TWT-8C2",
Token,
nil,
},
{"tTWT-8C2_c714",
{
"tTWT-8C2_c714",
"tTWT-8C2_c714",
714,
"TWT-8C2",
Token,
nil,
},
{"c714",
{
"c714",
"c714",
714,
"",
Coin,
nil,
},
{"tTWT-8C2",
{
"tTWT-8C2",
"tTWT-8C2",
0,
"",
Coin,
ErrBadAssetID,
},
{"c714_TWT-8C2",
{
"c714_TWT-8C2",
"c714_TWT-8C2",
714,
"",
Coin,
nil,
},
{
name: "c60_",
givenID: "c60_",
wantedCoin: 60,
wantedToken: "",
wantedType: Coin,
wantedError: nil,
},
{
name: "c637_t0xe4ccb6d39136469f376242c31b34d10515c8eaaa38092f804db8e08a8f53c5b2::assets_v1::EchoCoin002",
givenID: "c637_t0xe4ccb6d39136469f376242c31b34d10515c8eaaa38092f804db8e08a8f53c5b2::assets_v1::EchoCoin002",
wantedCoin: 637,
wantedToken: "0xe4ccb6d39136469f376242c31b34d10515c8eaaa38092f804db8e08a8f53c5b2::assets_v1::EchoCoin002",
wantedType: Token,
wantedError: nil,
},
}

for _, tt := range testStruct {
coin, token, err := ParseID(tt.givenID)
assert.Equal(t, tt.wantedCoin, coin)
assert.Equal(t, tt.wantedToken, token)
assert.Equal(t, tt.wantedError, err)
t.Run(tt.name, func(t *testing.T) {
coin, token, err := ParseID(tt.givenID)
assert.Equal(t, tt.wantedCoin, coin)
assert.Equal(t, tt.wantedToken, token)
assert.Equal(t, tt.wantedError, err)
})
}
}

Expand Down

0 comments on commit 99382f8

Please sign in to comment.