diff --git a/config.go b/config.go index e0138f6..92fe865 100644 --- a/config.go +++ b/config.go @@ -30,3 +30,22 @@ func (c *Config) set(key string, value interface{}) { } 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 "" +} diff --git a/lex.go b/lex.go index 01b9d0f..f73a843 100644 --- a/lex.go +++ b/lex.go @@ -28,11 +28,12 @@ func (t tokenType) String() string { } const ( - t_error tokenType = iota // a stored lex error - t_string // a string literal - t_name // a name - t_type // a type - t_equals // equals sign + t_error tokenType = iota // a stored lex error + t_string // a string literal + t_name // a name + t_type // a type + t_equals // equals sign + t_comment // a comment ) type stateFn func(*lexer) (stateFn, error) diff --git a/parse_test.go b/parse_test.go index 53688e1..4f4df83 100644 --- a/parse_test.go +++ b/parse_test.go @@ -4,6 +4,44 @@ import ( "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 type configPredicate func(*Config) bool @@ -58,39 +96,6 @@ type configTest struct { 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 func inv(fn configPredicate) configPredicate { 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) { for _, test := range parseTests { test.run(t)