config: tests, error cases

This commit is contained in:
Mitchell Hashimoto 2014-05-23 22:07:33 -07:00
parent 95ef186bf8
commit be83044f59
2 changed files with 66 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package config
import (
"fmt"
"reflect"
"regexp"
"strings"
@ -53,6 +54,13 @@ func (w *variableDetectWalker) Primitive(v reflect.Value) error {
var err error
var iv InterpolatedVariable
if strings.Index(key, ".") == -1 {
return fmt.Errorf(
"Interpolated variable '%s' has bad format. "+
"Did you mean 'var.%s'?",
key, key)
}
if strings.HasPrefix(key, "var.") {
iv, err = NewUserVariable(key)
} else {

58
config/variable_test.go Normal file
View File

@ -0,0 +1,58 @@
package config
import (
"reflect"
"testing"
)
func TestVariableDetectWalker(t *testing.T) {
w := new(variableDetectWalker)
str := `foo ${var.bar}`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) != 1 {
t.Fatalf("bad: %#v", w.Variables)
}
if w.Variables["var.bar"].(*UserVariable).FullKey() != "var.bar" {
t.Fatalf("bad: %#v", w.Variables)
}
}
func TestVariableDetectWalker_bad(t *testing.T) {
w := new(variableDetectWalker)
str := `foo ${bar}`
if err := w.Primitive(reflect.ValueOf(str)); err == nil {
t.Fatal("should error")
}
}
func TestVariableDetectWalker_escaped(t *testing.T) {
w := new(variableDetectWalker)
str := `foo $${var.bar}`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) > 0 {
t.Fatalf("bad: %#v", w.Variables)
}
}
func TestVariableDetectWalker_empty(t *testing.T) {
w := new(variableDetectWalker)
str := `foo`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) > 0 {
t.Fatalf("bad: %#v", w.Variables)
}
}