Skip to content

Commit

Permalink
Add tests for pkg util
Browse files Browse the repository at this point in the history
  • Loading branch information
jvassev committed Apr 30, 2018
1 parent 80bfc65 commit eb9ce0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
12 changes: 9 additions & 3 deletions config-reloader/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ func Trim(s string) string {
}

func MakeFluentdSafeName(s string) string {
filter := func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_'
buf := &bytes.Buffer{}
for _, r := range s {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' {
buf.WriteRune('-')
} else {
buf.WriteRune(r)
}
}
return strings.TrimFunc(s, filter)

return buf.String()
}

func ToRubyMapLiteral(labels map[string]string) string {
Expand Down
43 changes: 43 additions & 0 deletions config-reloader/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © 2018 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause

package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMakeFluentdSafeName(t *testing.T) {
assert.Equal(t, "a", MakeFluentdSafeName("a"))
assert.Equal(t, "123", MakeFluentdSafeName("123"))
assert.Equal(t, "", MakeFluentdSafeName(""))
assert.Equal(t, "a-a", MakeFluentdSafeName("a.a"))
assert.Equal(t, "a-a", MakeFluentdSafeName("a\na"))
assert.Equal(t, "---", MakeFluentdSafeName(" "))
}

func TestToRubyMapLiteral(t *testing.T) {
assert.Equal(t, "{}", ToRubyMapLiteral(map[string]string{}))
assert.Equal(t, "{'a'=>'1'}", ToRubyMapLiteral(map[string]string{
"a": "1",
}))
assert.Equal(t, "{'a'=>'1','z'=>'2'}", ToRubyMapLiteral(map[string]string{
"a": "1",
"z": "2",
}))
}

func TestTrim(t *testing.T) {
assert.Equal(t, "a", Trim("a"))
assert.Equal(t, "a", Trim(" a"))
assert.Equal(t, "a", Trim("a \t "))
assert.Equal(t, "a", Trim(" \t a "))
}

func TestTrimTrailingComment(t *testing.T) {
assert.Equal(t, "a", TrimTrailingComment("a #12451345"))
assert.Equal(t, "a", TrimTrailingComment("a"))
assert.Equal(t, "a", TrimTrailingComment("a#########"))
}

0 comments on commit eb9ce0b

Please sign in to comment.