func TestGetValues(t *testing.T) {
	out := struct {
		Foo string
	}{}
	f := testfile(`foo bar`)
	defer rm(t, f)
	err := Parse(out, f, nil)
	if err == nil {
		t.Fatal("Err is nil")
	}
	switch err.(type) {
	case *reflect.ValueError:
		t.Fatal("still reflect.ValueError")
	}
}
Make sure we give a sane error
package sconfig
import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"strings"
	"testing"
	"time"
)
func testfile(data string) (filename string) {
	fp, err := ioutil.TempFile(os.TempDir(), "sconfigtest")
	if err != nil {
		panic(err)
	}
	defer fp.Close()
	_, err = fp.WriteString(data)
	if err != nil {
		panic(err)
	}
	return fp.Name()
}
func rm(t *testing.T, path string) {
	err := os.Remove(path)
	if err != nil {
		t.Errorf("cannot remove %#v: %v", path, err)
	}
}
func rmAll(t *testing.T, path string) {
	err := os.RemoveAll(path)
	if err != nil {
		t.Errorf("cannot remove %#v: %v", path, err)
	}
}
func TestRegisterType(t *testing.T) {
	defer func() {
		typeHandlers["int64"] = []TypeHandler{ValidateSingleValue(), handleInt64}
		delete(typeHandlers, "int")
	}()
	didint := false
	didint64 := false
	RegisterType("int", func(v []string) (interface{}, error) {
		didint = true
		return int(42), nil
	})
	RegisterType("int64", func(v []string) (interface{}, error) {
		didint64 = true
		return int64(42), nil
	})
	f := testfile("hello 42\nworld 42")
	defer rm(t, f)
	c := &struct {
		Hello int64
		World int
	}{}
	err := Parse(c, f, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !didint {
		t.Error("didint was not true")
	}
	if !didint64 {
		t.Error("didint64 was not true")
	}
}
func TestReadFileError(t *testing.T) {
	out, err := readFile("/nonexistent-file")
	if err == nil {
		t.Error("no error on reading /nonexistent-file")
	}
	if len(out) > 0 {
		t.Fail()
	}
	f := testfile("source /nonexistent-file")
	defer rm(t, f)
	out, err = readFile(f)
	if err == nil {
		t.Error("no error on sourcing /nonexistent-file")
	}
	if len(out) > 0 {
		t.Error("len(out) > 0")
	}
	f2 := testfile(" indented")
	defer rm(t, f2)
	out, err = readFile(f2)
	if err == nil {
		t.Error("no error when first line is indented")
	}
	if len(out) > 0 {
		t.Error("len(out) > 0")
	}
}
func TestReadFile(t *testing.T) {
	source := testfile("sourced file")
	defer rm(t, source)
	test := fmt.Sprintf(`
# A comment
key value # Ignore this too
key
 value1 # Also ignored
			value2
another−€¡ Hé€ Well...
collapse     many			   whitespaces  
ig\#nore comments \# like this
uni-code    　 white    　 space   　
pre_serve \ spaces \ \ like \		\	so
back s\\la\sh
source %v
`, source)
	expected := [][]string{
		{"3", "key value"},
		{"5", "key value1 value2"},
		{"9", "another−€¡ Hé€ Well..."},
		{"11", "collapse many whitespaces"},
		{"13", "ig#nore comments # like this"},
		{"15", "uni-code white space"},
		{"16", "pre_serve  spaces   like 		so"},
		{"18", `back s\lash`},
		{"1", "sourced file"},
	}
	f := testfile(test)
	defer rm(t, f)
	out, err := readFile(f)
	if err != nil {
		t.Errorf("readFile: got err: %v", err)
	}
	if len(out) != len(expected) {
		t.Logf("len(out) != len(expected)\nout: %#v", out)
		t.FailNow()
	}
	for i := range expected {
		if out[i][0] != expected[i][0] || out[i][1] != expected[i][1] {
			t.Errorf("%v failed\nexpected:  %#v\nout:       %#v\n",
				i, expected[i], out[i])
		}
	}
}
func TestFindConfigErrors(t *testing.T) {
	f := FindConfig("hieperdepiephoera")
	if f != "" {
		t.Fail()
	}
}
func TestFindConfig(t *testing.T) {
	find := FindConfig("sure_this_wont_exist/anywhere")
	if find != "" {
		t.Fail()
	}
	dir, err := ioutil.TempDir(os.TempDir(), "sconfig_test")
	if err != nil {
		t.Error(err)
	}
	defer rmAll(t, dir)
	f, err := ioutil.TempFile(dir, "config")
	if err != nil {
		t.Fatal(err)
	}
	err = os.Setenv("XDG_CONFIG", dir)
	if err != nil {
		t.Fatal(err)
	}
	find = FindConfig(filepath.Base(f.Name()))
	if find != f.Name() {
		t.Fail()
	}
}
type testPrimitives struct {
	Str     string
	Int64   int64
	UInt64  uint64
	Bool    bool
	Bool2   bool
	Bool3   bool
	Bool4   bool
	Float32 float32
	Float64 float64
	TimeType time.Time
}
func TestMustParse(t *testing.T) {
	out := testPrimitives{}
	f := testfile("str okay")
	defer rm(t, f)
	MustParse(&out, f, nil)
	defer func() {
		err := recover()
		if err == nil {
			t.Errorf("expected panic")
		}
		expected := " line 1: error parsing not: unknown option (field Not or Nots is missing)"
		if !strings.HasSuffix(err.(error).Error(), expected) {
			t.Errorf("\nexpected:  %#v\nout:       %#v\n", expected, err.(error).Error())
		}
	}()
	f2 := testfile("not okay")
	defer rm(t, f2)
	MustParse(&out, f2, nil)
}
func TestParseError(t *testing.T) {
	out := testPrimitives{}
	err := Parse(&out, "/nonexistent-file", nil)
	if err == nil {
		t.Error("no error when parsing /nonexistent-file")
	}
	e := testPrimitives{}
	if out != e {
		t.Error("out isn't empty")
	}
}