<commit_before>(ns com.rpl.specter.core-test
  (:use [clojure.test]
        [clojure.test.check.clojure-test]
        [com.rpl specter]
        [com.rpl.specter test-helpers])
  (:require [clojure.test.check             
             [generators :as gen]
             [properties :as prop]]
            [clojure.test.check :as qc]))

;;TODO:
;; test walk, codewalk
;; test keypath
;; test comp-structure-paths

(defn gen-map-with-keys [key-gen val-gen & keys]
  (gen/bind (gen/map key-gen val-gen)
            (fn [m]
              (gen/bind
               (apply gen/hash-map (mapcat (fn [k] [k val-gen]) keys))
               (fn [m2]
                 (gen/return (merge m m2)))))))

(defspec select-all-keyword-filter
  (for-all+
    [v (gen/vector (max-size 5 
                     (gen-map-with-keys gen/keyword gen/int :a)))
     pred (gen/elements [odd? even?])]
    (= (select [ALL :a pred] v)
       (->> v (map :a) (filter pred))
       )))

(defspec select-pos-extreme-pred
  (for-all+
    [v (gen/vector gen/int)
     pred (gen/elements [odd? even?])
     pos (gen/elements [[FIRST first] [LAST last]])]
    (= (select-one [(filterer pred) (first pos)] v)
       (->> v (filter pred) ((last pos)))
       )))

(defspec select-all-on-map
  (for-all+
    [m (max-size 5 (gen/map gen/keyword gen/int))]
    (= (select [ALL LAST] m)
       (for [[k v] m] v))
    ))

(deftest select-one-test
   (is (thrown? Exception (select-one [ALL even?] [1 2 3 4])))
   (is (= 1 (select-one [ALL odd?] [2 4 1 6])))
   )

(deftest select-first-test
  (is (= 7 (select-first [(filterer odd?) ALL #(> % 4)] [3 4 2 3 7 5 9 8])))
  (is (nil? (select-first [ALL even?] [1 3 5 9])))
  )

(defspec update-all-on-map
  (for-all+
    [m (max-size 5 (gen/map gen/keyword gen/int))]
    (= (update [ALL LAST] inc m)
       (into {} (for [[k v] m] [k (inc v)]))
       )))

(defspec update-all
  (for-all+
   [v (gen/vector gen/int)]
   (let [v2 (update [ALL] inc v)]
     (= v2 (map inc v))
     )))

(defspec update-all-filter
  (for-all+
   [v (gen/vector gen/int)
    pred (gen/elements [odd? even?])
    action (gen/elements [inc dec])]
   (let [v2 (update [ALL pred] action v)]
     (= v2 (map (fn [v] (if (pred v) (action v) v)) v))
     )))

(defspec update-last
  (for-all+
   [v (gen/not-empty (gen/vector gen/int))
    pred (gen/elements [inc dec])]
   (let [v2 (update [LAST] pred v)]
     (= v2 (concat (butlast v) [(pred (last v))]))
     )))

(defspec update-first
  (for-all+
   [v (gen/not-empty (gen/vector gen/int))
    pred (gen/elements [inc dec])]
   (let [v2 (update [FIRST] pred v)]
     (= v2 (concat [(pred (first v))] (rest v) ))
     )))

(defspec update-filterer-all-equivalency
  (prop/for-all
   [v (gen/vector gen/int)]
   (let [v2 (update [(filterer odd?) ALL] inc v)
         v3 (update [ALL odd?] inc v)]
     (= v2 v3))
     ))

(defspec update-with-context
  (for-all+
    [m (max-size 10 (gen-map-with-keys gen/keyword gen/int :a :b))
     pred (gen/elements [odd? even?])]
    (= (update [(val-selector-one :b) :a pred] + m)
       (if (pred (:a m))
          (assoc m :a (+ (:a m) (:b m)))
          m
          ))))

(defn differing-elements [v1 v2]
  (->> (map vector v1 v2)
       (map-indexed (fn [i [e1 e2]]
                      (if (not= e1 e2)<commit_msg>improve tests by generating fixed keywords rather than keep them constant<commit_after>(ns com.rpl.specter.core-test
  (:use [clojure.test]
        [clojure.test.check.clojure-test]
        [com.rpl specter]
        [com.rpl.specter test-helpers])
  (:require [clojure.test.check             
             [generators :as gen]
             [properties :as prop]]
            [clojure.test.check :as qc]))

;;TODO:
;; test walk, codewalk
;; test keypath
;; test comp-structure-paths

(defn gen-map-with-keys [key-gen val-gen & keys]
  (gen/bind (gen/map key-gen val-gen)
            (fn [m]
              (gen/bind
               (apply gen/hash-map (mapcat (fn [k] [k val-gen]) keys))
               (fn [m2]
                 (gen/return (merge m m2)))))))

(defspec select-all-keyword-filter
  (for-all+
    [kw gen/keyword
     v (gen/vector (max-size 5 
                     (gen-map-with-keys gen/keyword gen/int kw)))
     pred (gen/elements [odd? even?])]
    (= (select [ALL kw pred] v)
       (->> v (map kw) (filter pred))
       )))

(defspec select-pos-extreme-pred
  (for-all+
    [v (gen/vector gen/int)
     pred (gen/elements [odd? even?])
     pos (gen/elements [[FIRST first] [LAST last]])]
    (= (select-one [(filterer pred) (first pos)] v)
       (->> v (filter pred) ((last pos)))
       )))

(defspec select-all-on-map
  (for-all+
    [m (max-size 5 (gen/map gen/keyword gen/int))]
    (= (select [ALL LAST] m)
       (for [[k v] m] v))
    ))

(deftest select-one-test
   (is (thrown? Exception (select-one [ALL even?] [1 2 3 4])))
   (is (= 1 (select-one [ALL odd?] [2 4 1 6])))
   )

(deftest select-first-test
  (is (= 7 (select-first [(filterer odd?) ALL #(> % 4)] [3 4 2 3 7 5 9 8])))
  (is (nil? (select-first [ALL even?] [1 3 5 9])))
  )

(defspec update-all-on-map
  (for-all+
    [m (max-size 5 (gen/map gen/keyword gen/int))]
    (= (update [ALL LAST] inc m)
       (into {} (for [[k v] m] [k (inc v)]))
       )))

(defspec update-all
  (for-all+
   [v (gen/vector gen/int)]
   (let [v2 (update [ALL] inc v)]
     (= v2 (map inc v))
     )))

(defspec update-all-filter
  (for-all+
   [v (gen/vector gen/int)
    pred (gen/elements [odd? even?])
    action (gen/elements [inc dec])]
   (let [v2 (update [ALL pred] action v)]
     (= v2 (map (fn [v] (if (pred v) (action v) v)) v))
     )))

(defspec update-last
  (for-all+
   [v (gen/not-empty (gen/vector gen/int))
    pred (gen/elements [inc dec])]
   (let [v2 (update [LAST] pred v)]
     (= v2 (concat (butlast v) [(pred (last v))]))
     )))

(defspec update-first
  (for-all+
   [v (gen/not-empty (gen/vector gen/int))
    pred (gen/elements [inc dec])]
   (let [v2 (update [FIRST] pred v)]
     (= v2 (concat [(pred (first v))] (rest v) ))
     )))

(defspec update-filterer-all-equivalency
  (prop/for-all
   [v (gen/vector gen/int)]
   (let [v2 (update [(filterer odd?) ALL] inc v)
         v3 (update [ALL odd?] inc v)]
     (= v2 v3))
     ))

(defspec update-with-context
  (for-all+
    [kw1 gen/keyword
     kw2 gen/keyword
     m (max-size 10 (gen-map-with-keys gen/keyword gen/int kw1 kw2))
     pred (gen/elements [odd? even?])]
    (= (update [(val-selector-one kw2) kw1 pred] + m)
       (if (pred (kw1 m))
          (assoc m kw1 (+ (kw1 m) (kw2 m)))
          m
          ))))

(defn differing-elements [v1 v2]
  (->> (map vector v1 v2)
       (map-indexed (fn [i [e1 e2]]
                      (if (not= e1 e2)
{'fraction_non_alphanumeric': 0.1859146523425843, 'fraction_numerical': 0.016413011041480154, 'mean_word_length': 2.1813004271476033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3384843', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2607, 'n_words': 737}