Added booleans ...that was boring.

This commit is contained in:
Maurice Makaay 2019-06-18 23:24:23 +00:00
parent c29afaeacc
commit 1c8f5ffe7e
2 changed files with 39 additions and 0 deletions

19
value_boolean.go Normal file
View File

@ -0,0 +1,19 @@
package parser
import (
"git.makaay.nl/mauricem/go-parsekit/parse"
)
var (
// Booleans are just the tokens you're used to. Always lowercase.
trueOrFalse = a.Str("true").Or(a.Str("false"))
)
func (t *parser) startBoolean(p *parse.API) {
switch {
case p.Accept(tok.Boolean(nil, trueOrFalse)):
t.emitCommand(csetBoolVal, p.Result().Value(0).(bool))
default:
p.Expected("true or false")
}
}

20
value_boolean_test.go Normal file
View File

@ -0,0 +1,20 @@
package parser
import (
"testing"
)
func TestBoolean(t *testing.T) {
for _, test := range []parseTest{
{``, []string{`Error: unexpected end of file (expected true or false) at start of file`}},
{`true`, []string{`boolean(true)`}},
{`false`, []string{`boolean(false)`}},
{`yes`, []string{`Error: unexpected input (expected true or false) at start of file`}},
{`no`, []string{`Error: unexpected input (expected true or false) at start of file`}},
{`1`, []string{`Error: unexpected input (expected true or false) at start of file`}},
{`0`, []string{`Error: unexpected input (expected true or false) at start of file`}},
} {
p := &parser{}
testParseHandler(t, p, p.startBoolean, test)
}
}