From 45efe8fcae948545c4db5245d2950b76a22a5e54 Mon Sep 17 00:00:00 2001 From: aviaviavi Date: Sat, 21 Apr 2018 16:29:49 -0700 Subject: [PATCH] negative indexing for arrays --- curl-runnings.cabal | 4 ++-- examples/interpolation-spec.yaml | 2 +- package.yaml | 2 +- src/Testing/CurlRunnings.hs | 9 +++++---- src/Testing/CurlRunnings/Internal.hs | 7 +++++++ src/Testing/CurlRunnings/Internal/Parser.hs | 2 +- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/curl-runnings.cabal b/curl-runnings.cabal index 161b042..a19390e 100644 --- a/curl-runnings.cabal +++ b/curl-runnings.cabal @@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack -- --- hash: 249e734681fdb8d0a482590af67f1d55c84e5704eb1725392aeef2e446c107ba +-- hash: f7bf475463248ca5311f48309a9d417ac30a9591678a51010e5a1813dbaefd76 name: curl-runnings -version: 0.5.4 +version: 0.5.5 synopsis: A framework for declaratively writing curl based API tests description: Please see the README on Github at category: Testing diff --git a/examples/interpolation-spec.yaml b/examples/interpolation-spec.yaml index c7e316c..b8e6fd2 100644 --- a/examples/interpolation-spec.yaml +++ b/examples/interpolation-spec.yaml @@ -25,7 +25,7 @@ - name: test will fail url: https://tabdextension.com/ping requestMethod: GET - expectHeaders: "ping: $; ${ping_path}: pong" + expectHeaders: "ping: $; ${ping_path}: pong" expectStatus: 200 - name: test will fail diff --git a/package.yaml b/package.yaml index c9d1dd5..41619b0 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: curl-runnings -version: 0.5.4 +version: 0.5.5 github: aviaviavi/curl-runnings license: MIT author: Avi Press diff --git a/src/Testing/CurlRunnings.hs b/src/Testing/CurlRunnings.hs index fa4e90c..7bb2771 100644 --- a/src/Testing/CurlRunnings.hs +++ b/src/Testing/CurlRunnings.hs @@ -129,7 +129,7 @@ interpolateHeaders state (HeaderSet headerList) = (\(Header k v) -> case sequence [interpolateQueryString state k, interpolateQueryString state v] of - Left err -> Left err + Left err -> Left err Right [k', v'] -> Right $ Header k' v') headerList >>= (Right . HeaderSet) @@ -291,7 +291,8 @@ getStringValueForQuery state i@(InterpolatedQuery rawText (Query _)) = Left l -> Left l Right (String s) -> Right $ rawText <> s (Right o) -> Left $ QueryTypeMismatch "Expected a string" o -getStringValueForQuery (CurlRunningsState env _) (InterpolatedQuery rawText (EnvironmentVariable v)) = Right $ rawText <> H.lookupDefault "" v env +getStringValueForQuery (CurlRunningsState env _) (InterpolatedQuery rawText (EnvironmentVariable v)) = + Right $ rawText <> H.lookupDefault "" v env -- | Lookup the value for the specified query getValueForQuery :: CurlRunningsState -> InterpolatedQuery -> Either QueryError Value @@ -299,7 +300,7 @@ getValueForQuery _ (LiteralText rawText) = Right $ String rawText getValueForQuery (CurlRunningsState _ previousResults) full@(NonInterpolatedQuery (Query indexes)) = case head indexes of (CaseResultIndex i) -> - let (CasePass _ _ returnedJSON) = previousResults !! fromInteger i + let (CasePass _ _ returnedJSON) = arrayGet previousResults $ fromInteger i jsonToIndex = case returnedJSON of Just v -> Right v @@ -314,7 +315,7 @@ getValueForQuery (CurlRunningsState _ previousResults) full@(NonInterpolatedQuer (Left l, _) -> Left l (Right (Object o), KeyIndex k) -> Right $ H.lookupDefault Null k o - (Right (Array a), ArrayIndex i') -> Right $ a V.! fromInteger i' + (Right (Array a), ArrayIndex i') -> Right $ arrayGet (V.toList a) $ fromInteger i' (Right Null, q) -> Left $ NullPointer (T.pack $ show full) (T.pack $ show q) (Right o, _) -> Left $ QueryTypeMismatch (T.pack $ show index) o) diff --git a/src/Testing/CurlRunnings/Internal.hs b/src/Testing/CurlRunnings/Internal.hs index 24389ad..4fc3d97 100644 --- a/src/Testing/CurlRunnings/Internal.hs +++ b/src/Testing/CurlRunnings/Internal.hs @@ -5,6 +5,7 @@ module Testing.CurlRunnings.Internal , makeGreen , tracer , mapRight + , arrayGet ) where import Debug.Trace @@ -21,3 +22,9 @@ tracer a b = trace (a ++ ": " ++ show b) b mapRight :: (b -> c) -> Either a b -> Either a c mapRight f (Right v) = Right $ f v mapRight _ (Left v) = Left v + +-- | Array indexing with negative values allowed +arrayGet :: [a] -> Int -> a +arrayGet a i + | i >= 0 = a !! i + | otherwise = reverse a !! (-i) diff --git a/src/Testing/CurlRunnings/Internal/Parser.hs b/src/Testing/CurlRunnings/Internal/Parser.hs index 5a5460c..75a4387 100644 --- a/src/Testing/CurlRunnings/Internal/Parser.hs +++ b/src/Testing/CurlRunnings/Internal/Parser.hs @@ -87,7 +87,7 @@ brace :: Parser T.Text brace = symbol "{" <|> symbol "}" integer :: Parser Integer -integer = lexeme L.decimal +integer = lexeme $ L.signed spaceOrDot L.decimal dot :: Parser T.Text dot = symbol "."