Adding documentation and tests for xml/path

This commit is contained in:
Jay McCarthy 2011-07-24 16:45:04 -04:00
parent 75631c6f2a
commit 6e59cbf1d7
3 changed files with 65 additions and 10 deletions

View File

@ -2,7 +2,32 @@
(require xml/path
tests/eli-tester)
(test
(se-path*/list '(p) '(html (body (p "Hey") (p "Bar")))) => (list "Hey" "Bar")
(se-path* '(p) '(html (body (p "Hey")))) => "Hey"
(se-path* '(p #:bar) '(html (body (p ([bar "Zog"]) "Hey")))) => "Zog")
(define some-page
'(html (body (p ([class "awesome"]) "Hey") (p "Bar"))))
(test (se-path*/list '(p) some-page)
=> '("Hey" "Bar")
(se-path* '(p) some-page)
=> "Hey"
(se-path* '(p #:class) some-page)
=> "awesome"
(se-path*/list '(body) some-page)
=> '((p ((class "awesome")) "Hey") (p "Bar"))
(se-path*/list '() '(p ((class "awesome")) "Hey"))
=> '((p ((class "awesome")) "Hey")
"Hey")
(se-path*/list '() some-page)
=> '((html (body (p ((class "awesome")) "Hey") (p "Bar")))
(body (p ((class "awesome")) "Hey") (p "Bar"))
(p ((class "awesome")) "Hey")
"Hey"
(p "Bar")
"Bar")
(se-path*/list '(p) '(html (body (p "Hey") (p "Bar"))))
=> (list "Hey" "Bar")
(se-path* '(p) '(html (body (p "Hey"))))
=> "Hey"
(se-path* '(p #:bar) '(html (body (p ([bar "Zog"]) "Hey"))))
=> "Zog")

View File

@ -33,7 +33,7 @@
[(list-rest (? symbol?) _)
(match x
[(list-rest (list (list (? symbol?) (? string?)) ...) rs)
(se-path/tag-body p rs)]
(append-map (curry se-path/xexpr p) rs)]
[(? list?)
(append-map (curry se-path/xexpr p) x)]
[_
@ -51,10 +51,10 @@
(define (se-path*/list p x)
(append (se-path/xexpr p x)
(match x
[(list-rest (list (cons (? symbol?) (? string?)) ...) rs)
(se-path*/list p rs)]
[(? list?)
(append-map (curry se-path*/list p) x)]
[(list (? symbol? tag) (list (list (? symbol?) (? string?)) ...) rs ...)
(append-map (curry se-path*/list p) rs)]
[(list (? symbol? tag) rs ...)
(append-map (curry se-path*/list p) rs)]
[_
empty])))
(define (se-path* p x)

View File

@ -475,6 +475,36 @@ looks like the following, if re-formatted by:
@section{Simple X-expression Path Queries}
@(require (for-label xml/path))
@defmodule[xml/path]
XXX
This library provides a simple path query library for X-expressions.
@defthing[se-path? contract?]{
A sequence of symbols followed by an optional keyword.
The prefix of symbols specifies a path of tags from the leaves with an implicit any sequence to the root. The final, optional keyword specifies an attribute.
}
@defproc[(se-path*/list [p se-path?] [xe xexpr?])
(listof any/c)]{
Returns a list of all values specified by the path @racket[p] in the X-expression @racket[xe].
}
@defproc[(se-path* [p se-path?] [xe xexpr?])
any/c]{
Returns the first answer from @racket[(se-path*/list p xe)].
}
@(define path-eval (make-base-eval))
@interaction-eval[#:eval path-eval (require xml/path)]
@examples[
#:eval path-eval
(define some-page
'(html (body (p ([class "awesome"]) "Hey") (p "Bar"))))
(se-path*/list '(p) some-page)
(se-path* '(p) some-page)
(se-path* '(p #:class) some-page)
(se-path*/list '(body) some-page)
(se-path*/list '() some-page)
]