Skip to content

Commit aadf946

Browse files
return count for number of tags and add a testcase
1 parent e9dd1e0 commit aadf946

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pkg/tags/tags.go

+9
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ func (tags *tagSet) set(key, value string, failOnExist bool) error {
203203
return nil
204204
}
205205

206+
func (tags tagSet) count() int {
207+
return len(tags.tagMap)
208+
}
209+
206210
func (tags tagSet) toMap() map[string]string {
207211
m := make(map[string]string, len(tags.tagMap))
208212
for key, value := range tags.tagMap {
@@ -279,6 +283,11 @@ func (tags *Tags) Set(key, value string) error {
279283
return tags.TagSet.set(key, value, false)
280284
}
281285

286+
// Count - return number of tags accounted for
287+
func (tags Tags) Count() int {
288+
return tags.TagSet.count()
289+
}
290+
282291
// ToMap returns copy of tags.
283292
func (tags Tags) ToMap() map[string]string {
284293
return tags.TagSet.toMap()

pkg/tags/tags_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,58 @@ func TestParseTags(t *testing.T) {
2626
testCases := []struct {
2727
tags string
2828
expectedErr bool
29+
count int
2930
}{
3031
{
3132
"key1=value1&key2=value2",
3233
false,
34+
2,
3335
},
3436
{
3537
"store+forever=false&factory=true",
3638
false,
39+
2,
3740
},
3841
{
3942
" store forever =false&factory=true",
4043
false,
44+
2,
4145
},
4246
{
4347
fmt.Sprintf("%0128d=%0256d", 1, 1),
4448
false,
49+
1,
4550
},
4651
// Failure cases - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#tag-restrictions
4752
{
4853
"key1=value1&key1=value2",
4954
true,
55+
0,
5056
},
5157
{
5258
"key$=value1",
5359
true,
60+
0,
5461
},
5562
{
5663
"key1=value$",
5764
true,
65+
0,
5866
},
5967
{
6068
fmt.Sprintf("%0128d=%0257d", 1, 1),
6169
true,
70+
0,
6271
},
6372
{
6473
fmt.Sprintf("%0129d=%0256d", 1, 1),
6574
true,
75+
0,
6676
},
6777
{
6878
fmt.Sprintf("%0129d=%0257d", 1, 1),
6979
true,
80+
0,
7081
},
7182
}
7283

@@ -81,7 +92,11 @@ func TestParseTags(t *testing.T) {
8192
t.Error("Expected failure but found success")
8293
}
8394
if err == nil {
84-
t.Logf("%s", tt)
95+
if tt.Count() != testCase.count {
96+
t.Errorf("Expected count %d, got %d", testCase.count, tt.Count())
97+
} else {
98+
t.Logf("%s", tt)
99+
}
85100
}
86101
})
87102
}

0 commit comments

Comments
 (0)