Skip to content

Commit fdeaff3

Browse files
Merge pull request #5 from kashifkhan0771/enhancement/maps-toggle-state
added toggle state method in maps package
2 parents 9df7cbe + d7b872b commit fdeaff3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

maps/maps.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func (s StateMap) SetState(stateType string, value bool) {
1313
s[stateType] = value
1414
}
1515

16+
// ToggleState toggles the value of the state with the provided key in the StateMap.
17+
func (s StateMap) ToggleState(stateType string) {
18+
s[stateType] = !s[stateType]
19+
}
20+
1621
// IsState return the value of the state provided from StateMap
1722
func (s StateMap) IsState(stateType string) bool {
1823
if ok, v := s[stateType]; ok {

maps/maps_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,37 @@ func TestStateMap_HasState(t *testing.T) {
6565
})
6666
}
6767
}
68+
69+
func TestStateMap_ToggleState(t *testing.T) {
70+
type args struct {
71+
stateType string
72+
}
73+
tests := []struct {
74+
name string
75+
args args
76+
want bool
77+
}{
78+
{
79+
name: "success - toggled the state in the state map",
80+
args: args{stateType: "key1"},
81+
want: false,
82+
},
83+
{
84+
name: "success - toggled the state in the state map",
85+
args: args{stateType: "key2"},
86+
want: true,
87+
}}
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
state := NewStateMap()
91+
state.SetState(tt.args.stateType, !tt.want)
92+
93+
state.ToggleState(tt.args.stateType)
94+
95+
got := state.IsState(tt.args.stateType)
96+
if got != tt.want {
97+
t.Errorf("ToggleState() : %v, want : %v", got, tt.want)
98+
}
99+
})
100+
}
101+
}

0 commit comments

Comments
 (0)