akshaymankar / jsonpath-hs

Haskell implementation of JSONPath
BSD 3-Clause "New" or "Revised" License
6 stars 5 forks source link

Add error testing #40

Closed glyn closed 1 year ago

glyn commented 1 year ago

While adding function extension support, I'd like to be sure that certain malformed JSONPaths produce reasonable errors. Currently, JSONPathSpec.hs does not provide a way of testing parser errors - it treats all parser errors as bad tests.

glyn commented 1 year ago

One option would be to write programmatic parser tests for errors instead of building this feature into the JSON-based tests. An example of such is this:

{-# LANGUAGE OverloadedStrings #-}

module Spec.FilterSpec (spec) where

import Data.JSONPath.Parser(jsonPath)
import Data.Text
import Test.Hspec
import Test.Hspec.Megaparsec
import Text.Megaparsec

filterMissingEndBracketEtoks :: ET Text
filterMissingEndBracketEtoks =
  etoks "!="
    <> etoks "&&"
    <> etoks "<="
    <> etoks "=="
    <> etoks ">="
    <> etoks "||"
    <> etoks "!"
    <> etoks "."
    <> etoks "<"
    <> etoks ">"
    <> etoks "["
    <> etoks "]"

spec :: Spec
spec = do
  describe "filter" $ do
    it "ummatched filter bracket parse error" $
      parse (jsonPath eof) "" "$[?@.foo"
        `shouldFailWith` err 8 (ueof <> filterMissingEndBracketEtoks <> elabel "white space")
akshaymankar commented 1 year ago

Yeah, I think there is little point in making these tests JSON. Using megaparsec types to check for expected tokens sounds like a good idea :+1:

glyn commented 1 year ago

Thanks. Closing as we now have an agreed way forward.