Skip to content

Commit eb9ce0b

Browse files
committed
Add tests for pkg util
1 parent 80bfc65 commit eb9ce0b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

config-reloader/util/util.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ func Trim(s string) string {
2424
}
2525

2626
func MakeFluentdSafeName(s string) string {
27-
filter := func(r rune) bool {
28-
return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_'
27+
buf := &bytes.Buffer{}
28+
for _, r := range s {
29+
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' {
30+
buf.WriteRune('-')
31+
} else {
32+
buf.WriteRune(r)
33+
}
2934
}
30-
return strings.TrimFunc(s, filter)
35+
36+
return buf.String()
3137
}
3238

3339
func ToRubyMapLiteral(labels map[string]string) string {

config-reloader/util/util_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright © 2018 VMware, Inc. All Rights Reserved.
2+
// SPDX-License-Identifier: BSD-2-Clause
3+
4+
package util
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestMakeFluentdSafeName(t *testing.T) {
13+
assert.Equal(t, "a", MakeFluentdSafeName("a"))
14+
assert.Equal(t, "123", MakeFluentdSafeName("123"))
15+
assert.Equal(t, "", MakeFluentdSafeName(""))
16+
assert.Equal(t, "a-a", MakeFluentdSafeName("a.a"))
17+
assert.Equal(t, "a-a", MakeFluentdSafeName("a\na"))
18+
assert.Equal(t, "---", MakeFluentdSafeName(" "))
19+
}
20+
21+
func TestToRubyMapLiteral(t *testing.T) {
22+
assert.Equal(t, "{}", ToRubyMapLiteral(map[string]string{}))
23+
assert.Equal(t, "{'a'=>'1'}", ToRubyMapLiteral(map[string]string{
24+
"a": "1",
25+
}))
26+
assert.Equal(t, "{'a'=>'1','z'=>'2'}", ToRubyMapLiteral(map[string]string{
27+
"a": "1",
28+
"z": "2",
29+
}))
30+
}
31+
32+
func TestTrim(t *testing.T) {
33+
assert.Equal(t, "a", Trim("a"))
34+
assert.Equal(t, "a", Trim(" a"))
35+
assert.Equal(t, "a", Trim("a \t "))
36+
assert.Equal(t, "a", Trim(" \t a "))
37+
}
38+
39+
func TestTrimTrailingComment(t *testing.T) {
40+
assert.Equal(t, "a", TrimTrailingComment("a #12451345"))
41+
assert.Equal(t, "a", TrimTrailingComment("a"))
42+
assert.Equal(t, "a", TrimTrailingComment("a#########"))
43+
}

0 commit comments

Comments
 (0)