yeah i don't really like this that much

master
Jordan Orelli 10 years ago
parent 4482a76feb
commit d369388fa8

@ -30,3 +30,22 @@ func (c *Config) set(key string, value interface{}) {
} }
c.items[key] = value c.items[key] = value
} }
func (c *Config) Get(key string) interface{} {
if c.items == nil {
return nil
}
return c.items[key]
}
func (c *Config) GetString(key string) string {
v := c.Get(key)
if v == nil {
return ""
}
s, ok := v.(string)
if ok {
return s
}
return ""
}

@ -33,6 +33,7 @@ const (
t_name // a name t_name // a name
t_type // a type t_type // a type
t_equals // equals sign t_equals // equals sign
t_comment // a comment
) )
type stateFn func(*lexer) (stateFn, error) type stateFn func(*lexer) (stateFn, error)

@ -4,6 +4,44 @@ import (
"testing" "testing"
) )
var parseTests = []parseTest{
{
in: ``,
desc: "an empty string is a valid config",
configTests: []configTest{
{
desc: "undefined name field should not exist",
pass: inv(hasKey("name")),
},
},
},
{
in: `name `,
desc: "eof after name",
errorType: e_unexpected_eof,
},
{
in: `firstname lastname`,
desc: "two names in a row",
errorType: e_unexpected_token,
},
{
in: `name = `,
desc: "eof after equals",
errorType: e_unexpected_eof,
},
{
in: `name = "jordan"`,
desc: "assign a value",
configTests: []configTest{
{
desc: "should have name",
pass: hasValue("name", "jordan"),
},
},
},
}
// a boolean statement about a config struct // a boolean statement about a config struct
type configPredicate func(*Config) bool type configPredicate func(*Config) bool
@ -58,39 +96,6 @@ type configTest struct {
pass configPredicate pass configPredicate
} }
var parseTests = []parseTest{
{
in: ``,
desc: "an empty string is a valid config",
configTests: []configTest{
{
desc: "undefined name field should not exist",
pass: inv(hasKey("name")),
},
},
},
{
in: `name `,
desc: "a name alone is not a valid config",
errorType: e_unexpected_eof,
},
{
in: `name = `,
desc: "dangling assignment",
errorType: e_unexpected_eof,
},
{
in: `name = "jordan"`,
desc: "assign a value",
configTests: []configTest{
{
desc: "should have name",
pass: hasKey("name"),
},
},
},
}
// inverts a given config predicate // inverts a given config predicate
func inv(fn configPredicate) configPredicate { func inv(fn configPredicate) configPredicate {
return func(c *Config) bool { return func(c *Config) bool {
@ -104,6 +109,21 @@ func hasKey(s string) configPredicate {
} }
} }
func hasValue(key string, expected interface{}) configPredicate {
switch t := expected.(type) {
case string:
return hasStringValue(key, t)
default:
panic("no we can't do that yet")
}
}
func hasStringValue(key string, expected string) configPredicate {
return func(c *Config) bool {
return c.GetString(key) == expected
}
}
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
for _, test := range parseTests { for _, test := range parseTests {
test.run(t) test.run(t)

Loading…
Cancel
Save