Skip to content

Commit

Permalink
Merge pull request #10 from aviaviavi/negative-indexing
Browse files Browse the repository at this point in the history
negative indexing for arrays
  • Loading branch information
aviaviavi authored Apr 21, 2018
2 parents d77324d + 45efe8f commit 110e54d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions curl-runnings.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/aviaviavi/curl-runnings#readme>
category: Testing
Expand Down
2 changes: 1 addition & 1 deletion examples/interpolation-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- name: test will fail
url: https://tabdextension.com/ping
requestMethod: GET
expectHeaders: "ping: $<SUITE[0].ping>; ${ping_path}: pong"
expectHeaders: "ping: $<SUITE[-1].ping>; ${ping_path}: pong"
expectStatus: 200

- name: test will fail
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: curl-runnings
version: 0.5.4
version: 0.5.5
github: aviaviavi/curl-runnings
license: MIT
author: Avi Press
Expand Down
9 changes: 5 additions & 4 deletions src/Testing/CurlRunnings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -291,15 +291,16 @@ 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
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
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/Testing/CurlRunnings/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Testing.CurlRunnings.Internal
, makeGreen
, tracer
, mapRight
, arrayGet
) where

import Debug.Trace
Expand All @@ -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)
2 changes: 1 addition & 1 deletion src/Testing/CurlRunnings/Internal/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 "."
Expand Down

0 comments on commit 110e54d

Please sign in to comment.