Skip to content

Commit

Permalink
Add tests for ExtractParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
moorereason committed Mar 19, 2015
1 parent 10755eb commit 2a2a20d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package helpers

import "testing"

var extractParameterTests = []struct {
s string
params map[string]interface{}
value string
ok bool
}{
{"a", map[string]interface{}{"a": "z"}, "z", true},
{"a.b", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "z", true},
{"a.b.c", map[string]interface{}{"a": map[string]interface{}{"b": map[string]interface{}{"c": "z"}}}, "z", true},
{"a.1.b", map[string]interface{}{"a": []interface{}{map[string]interface{}{"b": "y"}, map[string]interface{}{"b": "z"}}}, "z", true},
// failures
{"a.X", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "", false},
{"a.500.b", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "", false},
{"a.501.b", map[string]interface{}{"a": []interface{}{map[string]interface{}{"b": "y"}, map[string]interface{}{"b": "z"}}}, "", false},
}

func TestExtractParameter(t *testing.T) {
for _, tt := range extractParameterTests {
s, ok := ExtractParameter(tt.s, tt.params)
if ok != tt.ok || s != tt.value {
t.Errorf("failed to extract parameter %q:\nexpected {value:%#v, ok:%#v},\ngot {value:%#v, ok:%#v}", tt.s, tt.value, tt.ok, s, ok)
}
}
}

0 comments on commit 2a2a20d

Please sign in to comment.