Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Fix unmarshalling bug and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Oct 12, 2017
1 parent 9cf2fb4 commit ea53c90
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions metric/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func (t *Type) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
validTypeStrs := make([]string, 0, len(validTypes))
for _, valid := range validTypes {
if str == string(valid) {
if str == valid.String() {
*t = valid
return nil
}
validTypeStrs = append(validTypeStrs, string(valid))
validTypeStrs = append(validTypeStrs, valid.String())
}
return fmt.Errorf("invalid metric type '%s' valid types are: %s",
str, strings.Join(validTypeStrs, ", "))
Expand Down
57 changes: 57 additions & 0 deletions metric/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package metric

import (
"testing"

"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)

func TestTypeUnmarshalYAML(t *testing.T) {
inputs := []struct {
str string
expected Type
}{
{str: "counter", expected: CounterType},
{str: "timer", expected: TimerType},
{str: "gauge", expected: GaugeType},
}
for _, input := range inputs {
var typ Type
require.NoError(t, yaml.Unmarshal([]byte(input.str), &typ))
require.Equal(t, input.expected, typ)
}
}

func TestTypeUnmarshalYAMLErrors(t *testing.T) {
inputs := []string{
"huh",
"blah",
}
for _, input := range inputs {
var typ Type
err := yaml.Unmarshal([]byte(input), &typ)
require.Error(t, err)
require.Equal(t, "invalid metric type '"+input+"' valid types are: counter, timer, gauge", err.Error())
}
}

0 comments on commit ea53c90

Please sign in to comment.