(.module: [library [lux #* ["_" test (#+ Test)] ["." type ("#\." equivalence)] [abstract [monad (#+ do)]] [control ["." try ("#\." functor)] ["." exception]] [data ["." text ("#\." equivalence)] ["." name ("#\." equivalence)] [format ["." xml ("#\." equivalence)]] [collection ["." dictionary] ["." list]]] [macro ["." template]] [math ["." random (#+ Random)] [number ["n" nat]]]]] [\\library ["." / ["/#" // ("#\." monad)]]]) (template: (!expect ) [(case true _ false)]) (template: (!failure ) [(with_expansions [<> (template.spliced )] (do {! random.monad} [expected (random.ascii/alpha 1)] (_.cover [] (`` (and (~~ (template [ ] [(|> (/.result (list )) (!expect (^multi (#try.Failure error) (exception.match? error))))] <>)))))))]) (def: random_label (Random Name) (random.and (random.ascii/alpha 1) (random.ascii/alpha 1))) (def: random_tag ..random_label) (def: random_attribute ..random_label) (def: .public test Test (<| (_.covering /._) (_.for [/.Parser]) ($_ _.and (do {! random.monad} [expected (random.ascii/alpha 1)] (_.cover [/.result /.text] (|> (/.result /.text (list (#xml.Text expected))) (!expect (^multi (#try.Success actual) (text\= expected actual)))))) (!failure /.unconsumed_inputs [[(//\in expected) (#xml.Text expected)]]) (do {! random.monad} [expected (\ ! each (|>> #xml.Text) (random.ascii/alpha 1))] (_.cover [/.any] (|> (/.result /.any (list expected)) (try\each (xml\= expected)) (try.else false)))) (do {! random.monad} [expected ..random_tag] (_.cover [/.tag] (|> (/.result (do //.monad [actual /.tag _ /.any] (in (name\= expected actual))) (list (#xml.Node expected (dictionary.empty name.hash) (list)))) (!expect (#try.Success #1))))) (do {! random.monad} [expected ..random_tag] (_.cover [/.node] (|> (/.result (/.node expected (//\in [])) (list (#xml.Node expected (dictionary.empty name.hash) (list)))) (!expect (#try.Success []))))) (!failure /.wrong_tag [[(/.node ["" expected] (//\in [])) (#xml.Node [expected ""] (dictionary.empty name.hash) (list))]]) (do {! random.monad} [expected_tag ..random_tag expected_attribute ..random_attribute expected_value (random.ascii/alpha 1)] (_.cover [/.attribute] (|> (/.result (<| (/.node expected_tag) (//.after (/.attribute expected_attribute)) (//\in [])) (list (#xml.Node expected_tag (|> (dictionary.empty name.hash) (dictionary.has expected_attribute expected_value)) (list)))) (!expect (#try.Success []))))) (!failure /.unknown_attribute [[(/.attribute ["" expected]) (#xml.Node [expected expected] (|> (dictionary.empty name.hash) (dictionary.has [expected ""] expected)) (list))]]) (!failure /.empty_input [[(do //.monad [_ /.any] /.any) (#xml.Text expected)] [(do //.monad [_ /.any] /.text) (#xml.Text expected)] [(do //.monad [_ /.any] (/.node [expected expected] (//\in []))) (#xml.Node [expected expected] (dictionary.empty name.hash) (list))] [(do //.monad [_ /.any] (/.node [expected expected] (/.attribute [expected expected]))) (#xml.Node [expected expected] (|> (dictionary.empty name.hash) (dictionary.has [expected expected] expected)) (list))]]) (!failure /.unexpected_input [[/.text (#xml.Node [expected expected] (dictionary.empty name.hash) (list))] [(/.node [expected expected] (//\in [])) (#xml.Text expected)] [(/.node [expected expected] (/.attribute [expected expected])) (#xml.Text expected)]]) (do {! random.monad} [.let [node (: (-> xml.Tag (List xml.XML) xml.XML) (function (_ tag children) (#xml.Node tag (dictionary.empty name.hash) children)))] parent ..random_tag right ..random_tag wrong (random.only (|>> (name\= right) not) ..random_tag) .let [parser (<| (/.node parent) (do //.monad [_ (<| /.somewhere (/.node right) (//\in [])) _ (//.some /.any)] (in [])))] repetitions (\ ! each (n.% 10) random.nat)] ($_ _.and (_.cover [/.somewhere] (|> (/.result parser (list (node parent (list.together (list (list.repeated repetitions (node wrong (list))) (list (node right (list))) (list.repeated repetitions (node wrong (list)))))))) (!expect (#try.Success [])))) (_.cover [/.nowhere] (|> (/.result parser (list (node parent (list.repeated repetitions (node wrong (list)))))) (!expect (^multi (#try.Failure error) (exception.match? /.nowhere error))))) )) )))