Skip to content

Commit

Permalink
feat: updating to assets return types
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 13, 2024
1 parent 344b195 commit 57eecef
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
@go test ./...
17 changes: 8 additions & 9 deletions models/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ func (a *Assets) GetLovelace() uint64 {
return 0
}

func (a *Assets) PopAssetByUnit(unit string) Asset {
var filteredAssets Assets
filteredAssets = []Asset{}
func (a *Assets) PopAssetByUnit(unit string) *Asset {
var popAsset Asset
var filteredAssets Assets = []Asset{}
found := false
var filteredAsset Asset
for _, asset := range *a {
if asset.Unit == unit && !found {
filteredAsset = asset
popAsset = asset
found = true
} else {
filteredAssets = append(filteredAssets, asset)
}
}
a = &filteredAssets
return filteredAsset
*a = filteredAssets
return &popAsset
}

func (a *Assets) MergeAssets(assets []Asset) []Asset {
func (a *Assets) MergeAssets(assets []Asset) *[]Asset {
mergedAssets := make(map[string]Asset)
for _, asset := range assets {
if existingAsset, ok := mergedAssets[asset.Unit]; ok {
Expand All @@ -53,7 +52,7 @@ func (a *Assets) MergeAssets(assets []Asset) []Asset {
for _, asset := range mergedAssets {
result = append(result, asset)
}
return result
return &result
}

func AddQuantities(quantity1 string, quantity2 string) string {
Expand Down
57 changes: 57 additions & 0 deletions models/asset_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models_test

import (
"reflect"
"testing"

"github.com/sidan-lab/rum/models"
Expand Down Expand Up @@ -39,3 +40,59 @@ func TestGetLovelace(t *testing.T) {
t.Errorf("Expected 0, got %d", nilAssets.GetLovelace())
}
}

func TestPopAssetByUnit(t *testing.T) {
// Define test cases
tests := []struct {
name string
initialAssets models.Assets
unitToPop string
expectedAsset *models.Asset
expectedAssets models.Assets
}{
{
name: "Pop existing asset",
initialAssets: models.Assets{
{Unit: "lovelace", Quantity: "1000"},
{Unit: "asset1", Quantity: "2000"},
{Unit: "asset2", Quantity: "3000"},
},
unitToPop: "asset1",
expectedAsset: &models.Asset{Unit: "asset1", Quantity: "2000"},
expectedAssets: models.Assets{
{Unit: "lovelace", Quantity: "1000"},
{Unit: "asset2", Quantity: "3000"},
},
},
{
name: "Pop non-existing asset",
initialAssets: models.Assets{
{Unit: "lovelace", Quantity: "1000"},
{Unit: "asset1", Quantity: "2000"},
{Unit: "asset2", Quantity: "3000"},
},
unitToPop: "asset3",
expectedAsset: &models.Asset{},
expectedAssets: models.Assets{
{Unit: "lovelace", Quantity: "1000"},
{Unit: "asset1", Quantity: "2000"},
{Unit: "asset2", Quantity: "3000"},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assets := tt.initialAssets
poppedAsset := assets.PopAssetByUnit(tt.unitToPop)

if !reflect.DeepEqual(poppedAsset, tt.expectedAsset) {
t.Errorf("PopAssetByUnit() = %v, want %v", poppedAsset, tt.expectedAsset)
}

if !reflect.DeepEqual(assets, tt.expectedAssets) {
t.Errorf("Remaining assets = %v, want %v", assets, tt.expectedAssets)
}
})
}
}
4 changes: 2 additions & 2 deletions models/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ func (mv *Value) Merge(values ...*Value) {
}
}

func (mv *Value) ToAssets() []Asset {
func (mv *Value) ToAssets() *[]Asset {
assets := make([]Asset, 0, len(mv.Value))
for unit, quantity := range mv.Value {
assets = append(assets, Asset{
Unit: unit,
Quantity: strconv.FormatInt(quantity, 10),
})
}
return assets
return &assets
}

func FromAssets(assets []Asset) *Value {
Expand Down
2 changes: 1 addition & 1 deletion models/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestMergeFromNewMap(t *testing.T) {
func TestToAssets(t *testing.T) {
mv := NewValue()
mv.AddAsset(Asset{Unit: "USD", Quantity: "100"})
assets := mv.ToAssets()
assets := *mv.ToAssets()

if len(assets) != 1 {
t.Errorf("Expected 1 asset, got %d", len(assets))
Expand Down

0 comments on commit 57eecef

Please sign in to comment.