Beispiel #1
0
func Test_UnmarshalFromConfig(t *testing.T) {
	myConfig := &MyConfig{}
	config, err := cfg.NewConfigFromReader(bytes.NewBufferString(configString))
	if err != nil {
		t.Errorf("Error parsing the config: %s\n", err)
	}

	err = cfg.UnmarshalFromConfig(config, myConfig)
	if err != nil {
		t.Errorf("Error unmarshaling data: %s\n", err)
	}

	if myConfig.Answer != 42 {
		t.Errorf("Expected %v, got %v\n", 42, myConfig.Answer)
	}

	if myConfig.Pi != 3.14 {
		t.Errorf("Expected %v, got %v\n", 3.14, myConfig.Pi)
	}

	if myConfig.IsActive != true {
		t.Errorf("Expected %v, got %v\n", true, myConfig.IsActive)
	}

	q := "Alea iacta est\nEt tu, Brute?"
	if myConfig.Quotes != q {
		t.Errorf("Expected %q, got %q\n", q, myConfig.Quotes)
	}
}
Beispiel #2
0
func newConfigFromFile(filename string, t *testing.T) *cfg.Config {
	f, err := os.Open(fmt.Sprintf("_testdata/%s.cfg", filename))
	if err != nil {
		t.Errorf("Error reading test data: %s\n", err)
	}
	config, err := cfg.NewConfigFromReader(f)
	if err != nil {
		t.Errorf("Error creating config: %s\n", err)
	}

	return config
}
Beispiel #3
0
func ExampleNewConfigFromReader() {
	const configString = `
# This is a comment

# An integer value
answer = 42

# A float value
pi = 3.14
# A boolean value
is_active = true

# A string value
quotes = Alea iacta est\nEt tu, Brute?
`

	r := bytes.NewBufferString(configString)
	config, err := cfg.NewConfigFromReader(r)
	if err != nil {
		// Handle error
	}

	a, _ := config.GetInt("answer")
	fmt.Println(a)
	p, _ := config.GetFloat("pi")
	fmt.Println(p)
	i, _ := config.GetBool("is_active")
	fmt.Println(i)
	q, _ := config.GetString("quotes")
	fmt.Println(q)

	// Output:
	// 42
	// 3.14
	// true
	// Alea iacta est
	// Et tu, Brute?
}
Beispiel #4
0
func Test_ParseError(t *testing.T) {
	_, err := cfg.NewConfigFromReader(errorReader{})
	if err == nil {
		t.Errorf("Expected parse error but got none")
	}
}