Skip to content

Instantly share code, notes, and snippets.

@ekaschalk
Created August 16, 2017 21:49
Show Gist options
  • Save ekaschalk/240b213aabe83f565aa74bc70161bfaf to your computer and use it in GitHub Desktop.
Save ekaschalk/240b213aabe83f565aa74bc70161bfaf to your computer and use it in GitHub Desktop.
(defmacro deffixture [fn-name docstring params &rest body]
"Pytest parametrize reader."
`(with-decorator
(pytest.fixture :params ~params :scope "module")
(defn ~fn-name [request]
~docstring
(setv it request.param)
~@body)))
(defmacro with-fixture [fixture fn-name args &rest body]
`(defn ~fn-name [~fixture]
(setv ~args ~fixture)
~@body))
(defn assert~ [x y] (npt.assert-almost-equal x y))
(defmacro list-it [it &rest body]
"Selectively transform components of a collection, mimicking xi macro.
Usage: (list-it [1 2] (+ x1 2) x2) -> [3, 2]"
(setv flatbody (flatten body))
`((fn [[~@(genexpr (HySymbol (+ "x" (str i)))
[i (range 1
(inc (max (+ (list-comp (int (cut a 1))
[a flatbody]
(and (symbol? a)
(.startswith a 'x)
(.isdigit (cut a 1))))
[0]))))])
~@(if (in 'xi flatbody)
'(&rest xi)
'())]]
[~@body]) ~it))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment