<commit_before>(ns lazytest.describe
  (:use	lazytest.expect
	lazytest.suite
	lazytest.find
	lazytest.test-case))

;;; Utilities

(defn- get-arg
  "Pops first argument from args if (pred arg) is true.
  Returns a vector [this-arg remaining-args] or [nil args]."
  [pred args]
  (if (pred (first args))
    [(first args) (next args)]
    [nil args]))

(defn- merged-metadata [body form docstring extra-attr-map]
  (merge (when (empty? body) {:pending true})
	 {:doc docstring, :file *file*, :ns *ns*}
	 (meta form)
	 extra-attr-map))

(defn- strcat
  "Concatenate strings, with spaces in between, skipping nil."
  [& args]
  (apply str (interpose \space (remove nil? args))))

;;; Public API

(defmacro testing
  "Like 'describe' but does not create a Var.  Used for nesting test
  suites inside 'describe'."
  [& decl]
  (let [[sym decl] (get-arg symbol? decl)
	[doc decl] (get-arg string? decl)
	[attr-map children] (get-arg map? decl)
	docstring (strcat (when sym (resolve sym)) doc)
	metadata (merged-metadata children &form docstring attr-map)]
    `(suite (fn []
	      (test-seq
	       (with-meta
		 (flatten (list ~@children))
		 ~metadata))))))

(defmacro describe
  "Defines a suite of tests assigned to a Var with a generated name.
  Evaluating the same 'describe' form multiple times yields multiple
  Vars with different names.

  decl is: sym? doc? attr-map? children*

  sym (optional) is a symbol; it will be resolved in the current namespace and
  prepended to the documentation string.

  doc (optional) is a documentation string

  attr-map (optional) is a metadata map

  children are test cases (see 'it') or nested test suites (see 'testing')"
  [& decl]
  `(def ~(gensym) (testing ~@decl)))

(defmacro using
  "Like 'testing' but takes a vector of context bindings before
  children.  The contexts will be added to each child test; stateful
  contexts may be dereferenced within test case bodies by the symbols
  they are bound to here."
  [& decl]
  (let [[sym decl] (get-arg symbol? decl)
	[doc decl] (get-arg string? decl)
	[attr-map decl] (get-arg map? decl)
	[bindings children] (get-arg vector? decl)
	docstring (strcat (when sym (resolve sym)) doc)
	metadata (merged-metadata children &form docstring attr-map)
	binding-symbols (take-nth 2 (drop 1 bindings))]
    (assert (vector? bindings))
    (assert (even? (count bindings)))
    `(let ~bindings
       (suite (fn []
		(test-seq
		 (with-meta
		   (map (fn [tc#] (add-context tc# ~@binding-symbols))
			(flatten (list ~@children)))
		   ~metadata)))))))

(defmacro using-once
  "Like 'using' but adds contexts to the test sequence, so they are
  only executed once for all child tests."
  [& decl]
  (let [[sym decl] (get-arg symbol? decl)
	[doc decl] (get-arg string? decl)
	[attr-map decl] (get-arg map? decl)
	[bindings children] (get-arg vector? decl)
	docstring (strcat (when sym (resolve sym)) doc)
	metadata (merged-metadata children &form docstring attr-map)
	binding-symbols (take-nth 2 (drop 1 bindings))]
    (assert (vector? bindings))
    (assert (even? (count bindings)))
    `(let ~bindings
       (suite (fn []
		(add-context
		 (test-seq
		  (with-meta
		    (flatten (list ~@children))
		    ~metadata))
		 ~@binding-symbols))))))

(defmacro it
  "Defines a single test case.

  decl is: doc? attr-map? expr

  doc (optional) is a documentation string

  attr-map (optional) is a metadata map

  expr is a single expression, which must return logical true to
  indicate the test case passes or logical false to indicate failure."
  [& decl]
  (let [[doc decl] (get-arg string? decl)
	[attr-map body] (get-arg map? decl)<commit_msg>Adjust call order in 'using-once'<commit_after>(ns lazytest.describe
  (:use	lazytest.expect
	lazytest.suite
	lazytest.find
	lazytest.test-case))

;;; Utilities

(defn- get-arg
  "Pops first argument from args if (pred arg) is true.
  Returns a vector [this-arg remaining-args] or [nil args]."
  [pred args]
  (if (pred (first args))
    [(first args) (next args)]
    [nil args]))

(defn- merged-metadata [body form docstring extra-attr-map]
  (merge (when (empty? body) {:pending true})
	 {:doc docstring, :file *file*, :ns *ns*}
	 (meta form)
	 extra-attr-map))

(defn- strcat
  "Concatenate strings, with spaces in between, skipping nil."
  [& args]
  (apply str (interpose \space (remove nil? args))))

;;; Public API

(defmacro testing
  "Like 'describe' but does not create a Var.  Used for nesting test
  suites inside 'describe'."
  [& decl]
  (let [[sym decl] (get-arg symbol? decl)
	[doc decl] (get-arg string? decl)
	[attr-map children] (get-arg map? decl)
	docstring (strcat (when sym (resolve sym)) doc)
	metadata (merged-metadata children &form docstring attr-map)]
    `(suite (fn []
	      (test-seq
	       (with-meta
		 (flatten (list ~@children))
		 ~metadata))))))

(defmacro describe
  "Defines a suite of tests assigned to a Var with a generated name.
  Evaluating the same 'describe' form multiple times yields multiple
  Vars with different names.

  decl is: sym? doc? attr-map? children*

  sym (optional) is a symbol; it will be resolved in the current namespace and
  prepended to the documentation string.

  doc (optional) is a documentation string

  attr-map (optional) is a metadata map

  children are test cases (see 'it') or nested test suites (see 'testing')"
  [& decl]
  `(def ~(gensym) (testing ~@decl)))

(defmacro using
  "Like 'testing' but takes a vector of context bindings before
  children.  The contexts will be added to each child test; stateful
  contexts may be dereferenced within test case bodies by the symbols
  they are bound to here."
  [& decl]
  (let [[sym decl] (get-arg symbol? decl)
	[doc decl] (get-arg string? decl)
	[attr-map decl] (get-arg map? decl)
	[bindings children] (get-arg vector? decl)
	docstring (strcat (when sym (resolve sym)) doc)
	metadata (merged-metadata children &form docstring attr-map)
	binding-symbols (take-nth 2 (drop 1 bindings))]
    (assert (vector? bindings))
    (assert (even? (count bindings)))
    `(let ~bindings
       (suite (fn []
		(test-seq
		 (with-meta
		   (map (fn [tc#] (add-context tc# ~@binding-symbols))
			(flatten (list ~@children)))
		   ~metadata)))))))

(defmacro using-once
  "Like 'using' but adds contexts to the test sequence, so they are
  only executed once for all child tests."
  [& decl]
  (let [[sym decl] (get-arg symbol? decl)
	[doc decl] (get-arg string? decl)
	[attr-map decl] (get-arg map? decl)
	[bindings children] (get-arg vector? decl)
	docstring (strcat (when sym (resolve sym)) doc)
	metadata (merged-metadata children &form docstring attr-map)
	binding-symbols (take-nth 2 (drop 1 bindings))]
    (assert (vector? bindings))
    (assert (even? (count bindings)))
    `(let ~bindings
       (suite (fn []
		(test-seq
		 (add-context
		  (with-meta
		    (flatten (list ~@children))
		    ~metadata)
		  ~@binding-symbols)))))))

(defmacro it
  "Defines a single test case.

  decl is: doc? attr-map? expr

  doc (optional) is a documentation string

  attr-map (optional) is a metadata map

  expr is a single expression, which must return logical true to
  indicate the test case passes or logical false to indicate failure."
  [& decl]
  (let [[doc decl] (get-arg string? decl)
	[attr-map body] (get-arg map? decl)
{'fraction_non_alphanumeric': 0.12501715383559764, 'fraction_numerical': 0.0010978454782489364, 'mean_word_length': 3.609740670461733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218844', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2346, 'n_words': 923}