check we handle malformed gpx input

for some very obvious xml parse error
This commit is contained in:
Daniel Barlow 2024-10-29 21:44:49 +00:00
parent 1007404a24
commit d81099075a
2 changed files with 10 additions and 11 deletions

View File

@ -79,18 +79,10 @@ getPoints c =
in
List.map elToPoint trkpts
-- TODO am sure we could use some amazing monad thing to reduce
-- the amount of pattern matching here
parse :: String -> Either SomeException [Point]
parse str =
case parseText def (T.pack str) of
Right gpx ->
let
points = getPoints $ fromDocument gpx
in Right points
Left err ->
Left err
parse str = do
gpx <- parseText def (T.pack str)
return (getPoints (fromDocument gpx))
length :: Track -> Int
length trk = Data.List.length trk

View File

@ -24,6 +24,12 @@ test1 = TestCase $
Right t -> assertEqual "empty track has no elements"
0 (Track.length t)
testMalformed :: Test
testMalformed = TestCase $
case Track.parse "<gpx><dgdsfg>></gpx>" of
Left err -> assertBool "syntax error" True
Right t -> assertFailure "no error message parsing bad xml"
test2 = TestCase $
case Track.parse onepoint
of
@ -52,6 +58,7 @@ test4 = TestCase $
tests :: Test
tests = TestList [
TestLabel "test1" test1,
testMalformed,
TestLabel "test2" test2,
TestLabel "test3" test3,
TestLabel "test4" test4