Skip to content

Commit

Permalink
Merge pull request #435 from AssetMantle/deepanshutr/chore
Browse files Browse the repository at this point in the history
Deepanshutr/chore
  • Loading branch information
deepanshutr authored Apr 15, 2024
2 parents 175feb6 + 5fdd1dd commit 4b95837
Show file tree
Hide file tree
Showing 164 changed files with 4,004 additions and 2,903 deletions.
17 changes: 16 additions & 1 deletion helpers/base/auxiliaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package base

import "github.com/AssetMantle/modules/helpers"
import (
"github.com/AssetMantle/modules/helpers"
)

type auxiliaries struct {
auxiliaryList []helpers.Auxiliary
Expand All @@ -25,6 +27,19 @@ func (auxiliaries auxiliaries) Get() []helpers.Auxiliary {
}

func NewAuxiliaries(auxiliaryList ...helpers.Auxiliary) helpers.Auxiliaries {
for i, auxiliary := range auxiliaryList {

if auxiliary == nil {
panic("nil auxiliary")
}

for j, checkAuxiliary := range auxiliaryList {
if i != j && auxiliary.GetName() == checkAuxiliary.GetName() {
panic("repeated auxiliary")
}
}
}

return auxiliaries{
auxiliaryList: auxiliaryList,
}
Expand Down
193 changes: 193 additions & 0 deletions helpers/base/auxiliaries_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package base

import (
"github.com/AssetMantle/modules/helpers"
"github.com/AssetMantle/modules/utilities/random"
"github.com/stretchr/testify/assert"
"testing"
)

var (
randomUniqueIdentifiers = random.GenerateUniqueIdentifierList("", 2)
)

type mockAuxiliary struct {
name string
}

func (m mockAuxiliary) GetName() string {
return m.name
}

func (m mockAuxiliary) GetKeeper() helpers.AuxiliaryKeeper {
// Stubbed return as it is not used in GetAuxiliary
return nil
}

func (m mockAuxiliary) Initialize(helpers.Mapper, helpers.ParameterManager, ...interface{}) helpers.Auxiliary {
// Stubbed return as it is not used in GetAuxiliary
return nil
}

func TestGetAuxiliary(t *testing.T) {
tests := []struct {
name string
auxiliaries auxiliaries
arg string
want helpers.Auxiliary
}{
{
name: "Empty AuxiliaryList",
auxiliaries: auxiliaries{},
arg: "auxiliary1",
want: nil,
},
{
name: "Nil Return Non-Existing Name",
auxiliaries: auxiliaries{auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}}},
arg: "nonexistent",
want: nil,
},
{
name: "Flyweight1",
auxiliaries: auxiliaries{auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}}},
arg: randomUniqueIdentifiers[0],
want: mockAuxiliary{randomUniqueIdentifiers[0]},
},
{
name: "Flyweight2",
auxiliaries: auxiliaries{auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}}},
arg: randomUniqueIdentifiers[1],
want: mockAuxiliary{randomUniqueIdentifiers[1]},
},
{
name: "Unordered Flyweight1",
auxiliaries: auxiliaries{auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[1]}, mockAuxiliary{randomUniqueIdentifiers[0]}}},
arg: randomUniqueIdentifiers[0],
want: mockAuxiliary{randomUniqueIdentifiers[0]},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.auxiliaries.GetAuxiliary(tt.arg); got != tt.want {
t.Errorf("GetAuxiliary() = %v, want %v", got, tt.want)
}
})
}
}

func Test_auxiliaries_Get(t *testing.T) {
type fields struct {
auxiliaryList []helpers.Auxiliary
}
tests := []struct {
name string
fields fields
want []helpers.Auxiliary
}{
{
name: "Empty AuxiliaryList",
fields: fields{
auxiliaryList: []helpers.Auxiliary{},
},
want: []helpers.Auxiliary{},
},
{
name: "Non-empty AuxiliaryList",
fields: fields{
auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
want: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
{
name: "Nil Auxiliary",
fields: fields{
auxiliaryList: []helpers.Auxiliary{nil},
},
want: []helpers.Auxiliary{nil},
},
{
name: "Nil Auxiliary in list",
fields: fields{
auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, nil, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
want: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, nil, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
auxiliaries := auxiliaries{
auxiliaryList: tt.fields.auxiliaryList,
}
assert.Equalf(t, tt.want, auxiliaries.Get(), "Get()")
})
}
}

func TestNewAuxiliaries(t *testing.T) {
type args struct {
auxiliaryList []helpers.Auxiliary
}
tests := []struct {
name string
args args
want helpers.Auxiliaries
panic bool
}{
{
name: "Empty AuxiliaryList",
args: args{
auxiliaryList: []helpers.Auxiliary{},
},
want: auxiliaries{
auxiliaryList: []helpers.Auxiliary{},
},
},
{
name: "Non-empty AuxiliaryList",
args: args{
auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
want: auxiliaries{
auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
},
{
name: "Nil Auxiliary",
args: args{
auxiliaryList: []helpers.Auxiliary{nil},
},
panic: true,
},
{
name: "Nil Auxiliary in list",
args: args{
auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, nil, mockAuxiliary{randomUniqueIdentifiers[1]}},
},
panic: true,
},
{
name: "Repeated Auxiliary",
args: args{
auxiliaryList: []helpers.Auxiliary{mockAuxiliary{randomUniqueIdentifiers[0]}, mockAuxiliary{randomUniqueIdentifiers[0]}},
},
panic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
auxListInterface := make([]interface{}, len(tt.args.auxiliaryList))
for i, v := range tt.args.auxiliaryList {
auxListInterface[i] = v
}
if tt.panic {
assert.Panicsf(t, func() { NewAuxiliaries(tt.args.auxiliaryList...) }, "NewAuxiliaries(%v)", auxListInterface...)
return
} else {
assert.Equalf(t, tt.want, NewAuxiliaries(tt.args.auxiliaryList...), "NewAuxiliaries(%v)", auxListInterface...)
}
})
}
}
9 changes: 9 additions & 0 deletions helpers/base/auxiliary.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ func (auxiliary auxiliary) Initialize(mapper helpers.Mapper, parameterManager he
return auxiliary
}
func NewAuxiliary(name string, keeperPrototype func() helpers.AuxiliaryKeeper) helpers.Auxiliary {
if name == "" {
panic("empty name")
}

if keeperPrototype == nil {
panic("nil keeper prototype")

}

return auxiliary{
name: name,
keeperPrototype: keeperPrototype,
Expand Down
Loading

0 comments on commit 4b95837

Please sign in to comment.