Esempio n. 1
0
// TestRead tests reading a configuration out of a reader.
func TestRead(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := "{config {foo 42}{bar 24}}"
	config, err := configuration.Read(strings.NewReader(source))
	assert.Nil(err)

	source = "{something {gnagnagna}}"
	config, err = configuration.Read(strings.NewReader(source))
	assert.Nil(config)
	assert.ErrorMatch(err, `*. illegal source format: .* node not found`)

	source = "{config {gna 1}{gna 2}}"
	config, err = configuration.Read(strings.NewReader(source))
	assert.Nil(config)
	assert.ErrorMatch(err, `*. illegal source format: .* cannot build node structure: node has multiple values`)

	source = "{config {gna 1 {foo x} 2}}"
	config, err = configuration.Read(strings.NewReader(source))
	assert.Nil(config)
	assert.ErrorMatch(err, `*. illegal source format: .* cannot build node structure: node has multiple values`)

	source = "{config {foo/bar 1}{bar/foo 2}}"
	config, err = configuration.Read(strings.NewReader(source))
	assert.Nil(config)
	assert.ErrorMatch(err, `*. illegal source format: .*`)
}
Esempio n. 2
0
// TestGetFail tests the failing retrieval of values.
func TestGetFail(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := "{config {a Hello}{sub {a World}}}"
	config, err := configuration.Read(strings.NewReader(source))
	assert.Nil(err)

	_, err = config.At("x").Value()
	assert.ErrorMatch(err, `.* invalid configuration path "/config/x"`)
	_, err = config.At("sub", "x").Value()
	assert.ErrorMatch(err, `.* invalid configuration path "/config/sub/x"`)
}
Esempio n. 3
0
// TestGetSuccess tests the successful retrieval of values.
func TestGetSuccess(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := `{config
	{a  Hello}
	{b true}
	{c -1}
	{d     47.11     }
	{e 2015-06-25T23:45:00+02:00}
	{f 2h15m30s}
	{sub
		{a
			World}
		{b
			42}}}`
	config, err := configuration.Read(strings.NewReader(source))
	assert.Nil(err)

	vs, err := config.At("a").Get()
	assert.Nil(err)
	assert.Equal(vs, "Hello")
	vb, err := config.At("b").GetBool()
	assert.Nil(err)
	assert.Equal(vb, true)
	vi, err := config.At("c").GetInt()
	assert.Nil(err)
	assert.Equal(vi, -1)
	vd, err := config.At("d").GetFloat64()
	assert.Nil(err)
	assert.Equal(vd, 47.11)
	vtim, err := config.At("e").GetTime()
	assert.Nil(err)
	loc, err := time.LoadLocation("CET")
	assert.Nil(err)
	assert.Equal(vtim.String(), time.Date(2015, time.June, 25, 23, 45, 00, 0, loc).String())
	vdur, err := config.At("f").GetDuration()
	assert.Nil(err)
	assert.Equal(vdur, 8130*time.Second)

	vs, err = config.At("sub", "a").Get()
	assert.Nil(err)
	assert.Equal(vs, "World")
	vi, err = config.At("sub", "b").GetInt()
	assert.Nil(err)
	assert.Equal(vi, 42)
}
Esempio n. 4
0
// TestValueSuccess tests the successful retrieval of values.
func TestValueSuccess(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := `{config
	{a  Hello}
	{b true}
	{c -1}
	{d     47.11     }
	{sub
		{a
			World}
		{b
			42}}}`
	config, err := configuration.Read(strings.NewReader(source))
	assert.Nil(err)

	v, err := config.At("a").Value()
	assert.Nil(err)
	assert.Equal(v, "Hello")
	v, err = config.At("b").Value()
	assert.Nil(err)
	assert.Equal(v, "true")
	v, err = config.At("c").Value()
	assert.Nil(err)
	assert.Equal(v, "-1")
	v, err = config.At("d").Value()
	assert.Nil(err)
	assert.Equal(v, "47.11")
	v, err = config.At("sub", "a").Value()
	assert.Nil(err)
	assert.Equal(v, "World")
	v, err = config.At("sub", "b").Value()
	assert.Nil(err)
	assert.Equal(v, "42")

	d := stringex.NewDefaulter("config", true)
	vi := d.AsInt(config.At("c"), 42)
	assert.Equal(vi, -1)
	vf := d.AsFloat64(config.At("d"), 12.34)
	assert.Equal(vf, 47.11)
}
Esempio n. 5
0
// TestGetFail tests the failing retrieval of values.
func TestGetFail(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := "{config {a Hello}{sub {a World}}}"
	config, err := configuration.Read(strings.NewReader(source))
	assert.Nil(err)

	_, err = config.At("x").Get()
	assert.ErrorMatch(err, `.* invalid configuration path "/config/x"`)
	_, err = config.At("sub", "x").Get()
	assert.ErrorMatch(err, `.* invalid configuration path "/config/sub/x"`)

	vb, err := config.At("a").GetBool()
	assert.Equal(vb, false)
	assert.ErrorMatch(err, `.* invalid value format of "Hello": strconv.ParseBool: .*`)
	vi, err := config.At("a").GetInt()
	assert.Equal(vi, 0)
	assert.ErrorMatch(err, `.* invalid value format of "Hello": strconv.ParseInt: .*`)
	vf, err := config.At("a").GetFloat64()
	assert.Equal(vf, 0.0)
	assert.ErrorMatch(err, `.* invalid value format of "Hello": strconv.ParseFloat: .*`)
}