Guest User

Untitled

a guest
Feb 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. (ns doc-test
  2. (:require [clojure.test :as t]
  3. [clojure.java.shell :refer [sh]]
  4. [clojure.string :as str]
  5. [clojure.java.io :as io])
  6. (:import [java.io StringReader]))
  7.  
  8. (def aspell-pattern
  9. #"& (?<word>\w+) (?<offset>\d+) (?<count>\d+): (?<alternatives>.*?)$")
  10.  
  11. (defn parse-aspell [text]
  12. (->> (str/split text #"\n")
  13. (keep (fn [line]
  14. (let [matcher (re-matcher aspell-pattern line)]
  15. (when (.matches matcher)
  16. {:type :aspell/error
  17. :word (.group matcher "word")
  18. :offset (Long/parseLong (.group matcher "offset"))
  19. :count (Long/parseLong (.group matcher "count"))
  20. :corrections (str/split (.group matcher "alternatives") #", ")}))))))
  21.  
  22. (defn aspell [reader]
  23. (let [{:keys [exit err out]} (sh "aspell" "-a" "--ignore=3" "list" :in reader)]
  24. (when-not (and (= 0 out)
  25. (or (not-empty err)
  26. (not-empty out)))
  27. (seq (parse-aspell out)))))
  28.  
  29. (defn format-error [{:keys [word corrections]}]
  30. (format "Unknown word %s%s" word
  31. (when corrections (format " possible corrections: %s" (str/join " " (take 3 corrections))))))
  32.  
  33. (defn format-spelling-errors [entity errors]
  34. (format "Error checking spelling of %s:\n%s" entity
  35. (->> errors
  36. (map #(str "- " (format-error %)))
  37. (str/join "\n"))))
  38.  
  39. (t/deftest test-docstring-spelling
  40. (doseq [ns (all-ns)
  41. [sym var] (ns-publics ns)
  42. :when (and (var? var)
  43. (.contains (str var) "shelving")
  44. (not (.contains (str var) "-test")))
  45. :let [{:keys [doc] :as meta} (meta var)]]
  46. (t/is doc (format "Var %s is public and doesn't have a docstring!" var))
  47. (when doc
  48. (let [spell-errors (aspell (StringReader. doc))]
  49. (when-not (empty? spell-errors)
  50. (println
  51. (format-spelling-errors var spell-errors)))))))
  52.  
  53. (t/deftest test-markdown-spelling
  54. (doseq [f (file-seq (io/file "."))
  55. :when (or (.endsWith (str (.toURI f)) ".md")
  56. (.endsWith (str (.toURI f)) ".markdown"))]
  57. (let [spell-errors (aspell (io/reader f))]
  58. (t/is (empty? spell-errors)
  59. (format-spelling-errors f spell-errors)))))
  60.  
  61. (t/deftest test-markdown-vars
  62. (doseq [f (file-seq (io/file "."))
  63. :when (or (.endsWith (str (.toURI f)) ".md")
  64. (.endsWith (str (.toURI f)) ".markdown"))
  65. :let [buff (slurp (io/reader f))
  66. symbols (re-seq #"([\D&&[^/\(\)\[\]\{\},\s]].*/)(/|[\D&&[^/\(\)\[\]\{\},\s]][^/]*)" buff)]
  67. symbol symbols
  68. :when (and (.contains symbol "shelving.")
  69. (.contains symbol "/"))
  70. :let [the-sym (clojure.core/symbol symbol)]]
  71. (t/is (find-var the-sym)
  72. (format "In file %s, couldn't find mentioned var %s" f symbol))))
Add Comment
Please, Sign In to add comment