Changed match func strategy; added tests

As it turns out, closures are very hard to validate without running them.
Since table-driven tests tend to rely on value types that can be
compared directly, using structs that adhere to a generic callback
interface is more work, but is more easily tested.

* Changed jsonpath match functions to structs with Call() methods
* Added tests to verify the generation of jsonpath QueryPath data
* Added tests to verify jsonpath lexer
* Fixed jsonpath whitespace handling bug
* Fixed numerous flaws in jsonpath parser
This commit is contained in:
eanderton
2014-09-04 23:17:29 -04:00
parent 27416cc1b9
commit b74544d345
6 changed files with 447 additions and 156 deletions
+20 -14
View File
@@ -79,18 +79,26 @@ func (tt tokenType) String() string {
return "Unknown"
}
func (i token) String() string {
switch i.typ {
func (t token) Int() int {
if result, err := strconv.Atoi(t.val); err != nil {
panic(err)
} else {
return result
}
}
func (t token) String() string {
switch t.typ {
case tokenEOF:
return "EOF"
case tokenError:
return i.val
return t.val
}
if len(i.val) > 10 {
return fmt.Sprintf("%.10q...", i.val)
if len(t.val) > 10 {
return fmt.Sprintf("%.10q...", t.val)
}
return fmt.Sprintf("%q", i.val)
return fmt.Sprintf("%q", t.val)
}
func isSpace(r rune) bool {
@@ -283,6 +291,12 @@ func lexVoid(l *lexer) stateFn {
return lexString
}
if isSpace(next) {
l.next()
l.ignore()
continue
}
if isAlphanumeric(next) {
return lexKey
}
@@ -291,14 +305,6 @@ func lexVoid(l *lexer) stateFn {
return lexNumber
}
if isAlphanumeric(next) {
return lexKey
}
if isSpace(next) {
l.ignore()
}
if l.next() == eof {
break
}