recover tests

This commit is contained in:
Arpad Ryszka 2018-01-06 21:30:07 +01:00
parent eb8250c311
commit 184fa9e1ed
9 changed files with 637 additions and 714 deletions

View File

@ -1,17 +1 @@
package treerack package treerack
import (
"bufio"
"bytes"
"testing"
)
func TestCharBuildNoop(t *testing.T) {
c := newChar("foo", false, nil, nil)
c.init(newRegistry())
b := c.builder()
ctx := newContext(bufio.NewReader(bytes.NewBuffer(nil)))
if n, ok := b.build(ctx); len(n) != 0 || ok {
t.Error("char build not noop")
}
}

View File

@ -91,7 +91,3 @@ func unescape(escape rune, banned, chars []rune) ([]rune, error) {
return unescaped, nil return unescaped, nil
} }
func unescapeCharSequence(s string) ([]rune, error) {
return unescape('\\', []rune{'"', '\\'}, []rune(s))
}

View File

@ -14,7 +14,7 @@ unless explicitly stated otherwise, if treerack's license allows changing the
license of this source code. license of this source code.
Treerack's license: MIT https://opensource.org/licenses/MIT Treerack's license: MIT https://opensource.org/licenses/MIT
where YEAR=2018, COPYRIGHT HOLDER=Arpad Ryszka (arpad.ryszka@gmail.com) where YEAR=2017, COPYRIGHT HOLDER=Arpad Ryszka (arpad.ryszka@gmail.com)
The rest of the content of this file falls under the same license as the one The rest of the content of this file falls under the same license as the one
that the user of treerack generating this file declares for it, or it is that the user of treerack generating this file declares for it, or it is

File diff suppressed because one or more lines are too long

View File

@ -10,12 +10,6 @@ type Node struct {
} }
func (n *Node) Tokens() []rune { func (n *Node) Tokens() []rune {
defer func() {
if err := recover(); err != nil {
println(len(n.tokens), n.From, n.To)
panic(err)
}
}()
return n.tokens return n.tokens
} }

View File

@ -1,6 +1,10 @@
package treerack package treerack
import "testing" import (
"bufio"
"bytes"
"testing"
)
func TestRecursion(t *testing.T) { func TestRecursion(t *testing.T) {
runTests( runTests(
@ -733,3 +737,39 @@ func TestChoiceSequencePriority(t *testing.T) {
}}, }},
) )
} }
func TestCharBuildNoop(t *testing.T) {
c := newChar("foo", false, nil, nil)
c.init(newRegistry())
b := c.builder()
ctx := newContext(bufio.NewReader(bytes.NewBuffer(nil)))
if n, ok := b.build(ctx); len(n) != 0 || ok {
t.Error("char build not noop")
}
}
func TestCharBuilderProps(t *testing.T) {
b := &charBuilder{
name: "foo",
id: 42,
}
if b.nodeName() != "foo" {
t.Error("invalid char builder prop")
}
if b.nodeID() != 42 {
t.Error("invalid char builder prop")
}
}
func TestCharAddGeneralizationNoEffect(t *testing.T) {
(&charParser{}).addGeneralization(42)
}
func TestSequenceProps(t *testing.T) {
d := &sequenceParser{commit: Alias | userDefined}
if d.commitType() != Alias|userDefined {
t.Error("invalid commit type")
}
}

File diff suppressed because it is too large Load Diff

View File

@ -71,84 +71,12 @@ func parserNotFound(name string) error {
return fmt.Errorf("parser not found: %s", name) return fmt.Errorf("parser not found: %s", name)
} }
const symbolChars = "^\\\\ \\n\\t\\b\\f\\r\\v/.\\[\\]\\\"{}\\^+*?|():=;" var symbolChars = []rune("\\ \n\t\b\f\r\v/.[]\"{}^+*?|():=;")
func parseClass(class []rune) (not bool, chars []rune, ranges [][]rune, err error) {
if class[0] == '^' {
not = true
class = class[1:]
}
for {
if len(class) == 0 {
return
}
var c0 rune
c0, class = class[0], class[1:]
/*
this doesn't happen:
switch c0 {
case '[', ']', '^', '-':
err = errInvalidDefinition
return
}
*/
if c0 == '\\' {
/*
this doesn't happen:
if len(class) == 0 {
err = errInvalidDefinition
return
}
*/
c0, class = unescapeChar(class[0]), class[1:]
}
if len(class) < 2 || class[0] != '-' {
chars = append(chars, c0)
continue
}
var c1 rune
c1, class = class[1], class[2:]
/*
this doesn't happen:
switch c1 {
case '[', ']', '^', '-':
err = errInvalidDefinition
return
}
if c1 == '\\' {
if len(class) == 0 {
err = errInvalidDefinition
return
}
c1, class = unescapeChar(class[0]), class[1:]
}
*/
ranges = append(ranges, []rune{c0, c1})
}
}
func parseSymbolChars(c []rune) []rune {
_, chars, _, _ := parseClass(c)
return chars
}
var symbolCharRunes = parseSymbolChars([]rune(symbolChars))
func isValidSymbol(n string) bool { func isValidSymbol(n string) bool {
runes := []rune(n) runes := []rune(n)
for _, r := range runes { for _, r := range runes {
if !matchChar(symbolCharRunes, nil, true, r) { if !matchChar(symbolChars, nil, true, r) {
return false return false
} }
} }

View File

@ -339,8 +339,6 @@ func TestDefinition(t *testing.T) {
} }
func TestReadSyntax(t *testing.T) { func TestReadSyntax(t *testing.T) {
t.Skip()
t.Run("already initialized", func(t *testing.T) { t.Run("already initialized", func(t *testing.T) {
s := &Syntax{} s := &Syntax{}
if err := s.AnyChar("a", None); err != nil { if err := s.AnyChar("a", None); err != nil {
@ -358,38 +356,27 @@ func TestReadSyntax(t *testing.T) {
} }
}) })
t.Run("not implemented", func(t *testing.T) { t.Run("read invalid syntax", func(t *testing.T) {
s := &Syntax{} s := &Syntax{}
if err := s.ReadSyntax(bytes.NewBuffer(nil)); err == nil { if err := s.ReadSyntax(bytes.NewBufferString("foo")); err == nil {
t.Error(err) t.Error("failed to fail")
} }
}) })
} }
func TestGenerateSyntax(t *testing.T) { func TestUserDefinedClassRange(t *testing.T) {
t.Skip() s := &Syntax{}
if err := s.Class("symbol-char", None, false, []rune{'_'}, [][]rune{{'a', 'z'}, {'A', 'Z'}}); err != nil {
t.Error(err)
return
}
t.Run("init fails", func(t *testing.T) { if err := s.Sequence("symbol", None, SequenceItem{Name: "symbol-char", Min: 1, Max: -1}); err != nil {
s := &Syntax{} t.Error(err)
if err := s.Choice("a", None, "b"); err != nil { return
t.Error(err) }
return
}
if err := s.Generate(GeneratorOptions{}, bytes.NewBuffer(nil)); err == nil { if _, err := s.Parse(bytes.NewBufferString("_abc")); err != nil {
t.Error(err) t.Error(err)
} }
})
t.Run("not implemented", func(t *testing.T) {
s := &Syntax{}
if err := s.AnyChar("a", None); err != nil {
t.Error(err)
return
}
if err := s.Generate(GeneratorOptions{}, bytes.NewBuffer(nil)); err == nil {
t.Error(err)
}
})
} }