Fix missing coverage

This commit is contained in:
Adam Scarr 2017-08-09 21:58:36 +10:00
parent dc3c5a8325
commit d285a0badc
3 changed files with 44 additions and 3 deletions

View File

@ -71,6 +71,13 @@ func TestSome(t *testing.T) {
require.Equal(t, 10, p2.Pos) require.Equal(t, 10, p2.Pos)
}) })
t.Run("Matches sequence without trailing sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e1111", Some(Chars("a-g"), ","))
require.False(t, p2.Errored())
assertSequence(t, node, "a", "b", "c", "d", "e")
require.Equal(t, "1111", p2.Get())
})
t.Run("Matches sequence without sep", func(t *testing.T) { t.Run("Matches sequence without sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Some(Any(Chars("a-g"), ","))) node, p2 := runParser("a,b,c,d,e,", Some(Any(Chars("a-g"), ",")))
assertSequence(t, node, "a", ",", "b", ",", "c", ",", "d", ",", "e", ",") assertSequence(t, node, "a", ",", "b", ",", "c", ",", "d", ",", "e", ",")
@ -139,6 +146,22 @@ func TestMap(t *testing.T) {
}) })
} }
func TestBind(t *testing.T) {
parser := Bind("true", true)
t.Run("sucess", func(t *testing.T) {
result, _ := runParser("true", parser)
require.Equal(t, true, result.Result)
})
t.Run("error", func(t *testing.T) {
result, ps := runParser("nil", parser)
require.Nil(t, result.Result)
require.Equal(t, "offset 0: expected true", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
}
func TestMerge(t *testing.T) { func TestMerge(t *testing.T) {
var bracer Parser var bracer Parser
bracer = Seq("(", Maybe(&bracer), ")") bracer = Seq("(", Maybe(&bracer), ")")

View File

@ -48,8 +48,6 @@ type Parserish interface{}
// See Parserish for details. // See Parserish for details.
func Parsify(p Parserish) Parser { func Parsify(p Parserish) Parser {
switch p := p.(type) { switch p := p.(type) {
case nil:
return nil
case func(*State) Result: case func(*State) Result:
return NewParser("anonymous func", p) return NewParser("anonymous func", p)
case Parser: case Parser:
@ -171,7 +169,7 @@ func parseMatcher(matcher string) (alphabet string, ranges [][]rune) {
for i := 0; i < len(runes); i++ { for i := 0; i < len(runes); i++ {
if i+2 < len(runes) && runes[i+1] == '-' { if i+2 < len(runes) && runes[i+1] == '-' && runes[i] != '\\' {
start := runes[i] start := runes[i]
end := runes[i+2] end := runes[i+2]
if start <= end { if start <= end {

View File

@ -102,6 +102,13 @@ func TestChars(t *testing.T) {
require.False(t, ps.Errored()) require.False(t, ps.Errored())
}) })
t.Run("escaped hyphen", func(t *testing.T) {
node, ps := runParser(`ab-ab\cde`, Chars(`a\-b`))
require.Equal(t, "ab-ab", node.Token)
require.Equal(t, `\cde`, ps.Get())
require.False(t, ps.Errored())
})
t.Run("no match", func(t *testing.T) { t.Run("no match", func(t *testing.T) {
_, ps := runParser("ffffff", Chars("0-9")) _, ps := runParser("ffffff", Chars("0-9"))
require.Equal(t, "offset 0: expected 0-9", ps.Error.Error()) require.Equal(t, "offset 0: expected 0-9", ps.Error.Error())
@ -185,6 +192,19 @@ func TestParseString(t *testing.T) {
}) })
} }
func TestAutoWS(t *testing.T) {
t.Run("ws is not automatically consumed", func(t *testing.T) {
_, ps := runParser(" hello", NoAutoWS("hello"))
require.Equal(t, "offset 0: expected hello", ps.Error.Error())
})
t.Run("ws is can be explicitly consumed ", func(t *testing.T) {
result, ps := runParser(" hello", NoAutoWS(Seq(WS(), "hello")))
require.Equal(t, "hello", result.Child[1].Token)
require.Equal(t, "", ps.Get())
})
}
func runParser(input string, parser Parser) (Result, *State) { func runParser(input string, parser Parser) (Result, *State) {
ps := NewState(input) ps := NewState(input)
result := parser(ps) result := parser(ps)