From d285a0badc988be2cef6e01d224bf8f108bb33bb Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Wed, 9 Aug 2017 21:58:36 +1000 Subject: [PATCH] Fix missing coverage --- combinator_test.go | 23 +++++++++++++++++++++++ parser.go | 4 +--- parser_test.go | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/combinator_test.go b/combinator_test.go index e5f3b01..283bb2a 100644 --- a/combinator_test.go +++ b/combinator_test.go @@ -71,6 +71,13 @@ func TestSome(t *testing.T) { 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) { node, p2 := runParser("a,b,c,d,e,", Some(Any(Chars("a-g"), ","))) 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) { var bracer Parser bracer = Seq("(", Maybe(&bracer), ")") diff --git a/parser.go b/parser.go index a23cef5..f084af4 100644 --- a/parser.go +++ b/parser.go @@ -48,8 +48,6 @@ type Parserish interface{} // See Parserish for details. func Parsify(p Parserish) Parser { switch p := p.(type) { - case nil: - return nil case func(*State) Result: return NewParser("anonymous func", p) case Parser: @@ -171,7 +169,7 @@ func parseMatcher(matcher string) (alphabet string, ranges [][]rune) { 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] end := runes[i+2] if start <= end { diff --git a/parser_test.go b/parser_test.go index e9da404..09cf7f4 100644 --- a/parser_test.go +++ b/parser_test.go @@ -102,6 +102,13 @@ func TestChars(t *testing.T) { 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) { _, ps := runParser("ffffff", Chars("0-9")) 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) { ps := NewState(input) result := parser(ps)