Skip to content

Instantly share code, notes, and snippets.

@arrdem
Created February 5, 2018 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrdem/fa4a5ca8daa306b64e1123f1a60e621f to your computer and use it in GitHub Desktop.
Save arrdem/fa4a5ca8daa306b64e1123f1a60e621f to your computer and use it in GitHub Desktop.
(ns doc-test
(:require [clojure.test :as t]
[clojure.java.shell :refer [sh]]
[clojure.string :as str]
[clojure.java.io :as io])
(:import [java.io StringReader]))
(def aspell-pattern
#"& (?<word>\w+) (?<offset>\d+) (?<count>\d+): (?<alternatives>.*?)$")
(defn parse-aspell [text]
(->> (str/split text #"\n")
(keep (fn [line]
(let [matcher (re-matcher aspell-pattern line)]
(when (.matches matcher)
{:type :aspell/error
:word (.group matcher "word")
:offset (Long/parseLong (.group matcher "offset"))
:count (Long/parseLong (.group matcher "count"))
:corrections (str/split (.group matcher "alternatives") #", ")}))))))
(defn aspell [reader]
(let [{:keys [exit err out]} (sh "aspell" "-a" "--ignore=3" "list" :in reader)]
(when-not (and (= 0 out)
(or (not-empty err)
(not-empty out)))
(seq (parse-aspell out)))))
(defn format-error [{:keys [word corrections]}]
(format "Unknown word %s%s" word
(when corrections (format " possible corrections: %s" (str/join " " (take 3 corrections))))))
(defn format-spelling-errors [entity errors]
(format "Error checking spelling of %s:\n%s" entity
(->> errors
(map #(str "- " (format-error %)))
(str/join "\n"))))
(t/deftest test-docstring-spelling
(doseq [ns (all-ns)
[sym var] (ns-publics ns)
:when (and (var? var)
(.contains (str var) "shelving")
(not (.contains (str var) "-test")))
:let [{:keys [doc] :as meta} (meta var)]]
(t/is doc (format "Var %s is public and doesn't have a docstring!" var))
(when doc
(let [spell-errors (aspell (StringReader. doc))]
(when-not (empty? spell-errors)
(println
(format-spelling-errors var spell-errors)))))))
(t/deftest test-markdown-spelling
(doseq [f (file-seq (io/file "."))
:when (or (.endsWith (str (.toURI f)) ".md")
(.endsWith (str (.toURI f)) ".markdown"))]
(let [spell-errors (aspell (io/reader f))]
(t/is (empty? spell-errors)
(format-spelling-errors f spell-errors)))))
(t/deftest test-markdown-vars
(doseq [f (file-seq (io/file "."))
:when (or (.endsWith (str (.toURI f)) ".md")
(.endsWith (str (.toURI f)) ".markdown"))
:let [buff (slurp (io/reader f))
symbols (re-seq #"([\D&&[^/\(\)\[\]\{\},\s]].*/)(/|[\D&&[^/\(\)\[\]\{\},\s]][^/]*)" buff)]
symbol symbols
:when (and (.contains symbol "shelving.")
(.contains symbol "/"))
:let [the-sym (clojure.core/symbol symbol)]]
(t/is (find-var the-sym)
(format "In file %s, couldn't find mentioned var %s" f symbol))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment