From eb9ce0bcc8aa55a48fff594020306ba992ae68e5 Mon Sep 17 00:00:00 2001 From: Julian Vassev Date: Mon, 30 Apr 2018 08:10:22 -0700 Subject: [PATCH] Add tests for pkg util --- config-reloader/util/util.go | 12 ++++++--- config-reloader/util/util_test.go | 43 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 config-reloader/util/util_test.go diff --git a/config-reloader/util/util.go b/config-reloader/util/util.go index 539bb7ae..97414d12 100644 --- a/config-reloader/util/util.go +++ b/config-reloader/util/util.go @@ -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 { diff --git a/config-reloader/util/util_test.go b/config-reloader/util/util_test.go new file mode 100644 index 00000000..d54a3ff9 --- /dev/null +++ b/config-reloader/util/util_test.go @@ -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#########")) +}