From 99382f81649b00d5b9990853a913135f12ca4ca3 Mon Sep 17 00:00:00 2001 From: Shawn Lee Date: Tue, 17 Dec 2024 10:26:10 +0900 Subject: [PATCH] fix asset id parsing (#179) --- asset/id.go | 2 +- asset/id_test.go | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/asset/id.go b/asset/id.go index 4e92a9c..e14056c 100644 --- a/asset/id.go +++ b/asset/id.go @@ -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 diff --git a/asset/id_test.go b/asset/id_test.go index 6349d62..31e4e42 100644 --- a/asset/id_test.go +++ b/asset/id_test.go @@ -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) + }) } }