(ns net.deertopia.doerg.element-test (:require [net.deertopia.doerg.element :as sut] [babashka.process :as p] [clojure.test :as t] [clojure.zip :as z] [clojure.java.io :as io] [com.rpl.specter :as sp])) (defn- first-child-of-type [parent type] (some #(and (sut/of-type? % type) %) (:children parent))) (defn- parse-resource [path] (-> (str "net/deertopia/doerg/element_test/" path) io/resource slurp (sut/read-string))) (t/deftest known-greater-elements (t/testing "known greater elements satisfy `greater-element?`" (let [root (parse-resource "greater-elements.org") section (->> root (sp/select [sut/children-walker #(sut/of-type? % "section")]) second) headline (first-child-of-type section "headline") headline-text (first-child-of-type headline "text") paragraph (first-child-of-type section "paragraph") paragraph-text (first-child-of-type paragraph "text")] (t/is (sut/greater-element? root)) (t/is (sut/greater-element? section)) (t/is (sut/greater-element? headline)) (t/is (not (sut/greater-element? headline-text))) (t/is (sut/greater-element? paragraph)) (t/is (not (sut/greater-element? paragraph-text)))))) (defn- first-paragraph-belongs-to-first-section? [doc] (let [first-paragraph (sp/select-first [sut/postorder-walker #(sut/of-type? % "paragraph")] doc) first-section (sp/select-first [sut/postorder-walker #(sut/of-type? % "section")] doc)] (if (and first-paragraph first-section) (boolean (some #(= % first-paragraph) (:children first-section))) true))) (t/deftest first-paragraph-under-first-section (t/is (-> (parse-resource "first-paragraph-under-first-section.org") first-paragraph-belongs-to-first-section?))) (t/deftest first-paragraph-under-heading (t/is (-> (parse-resource "first-paragraph-under-heading.org") first-paragraph-belongs-to-first-section? not))) (defn- walk-types [type & types] [sut/postorder-walker #(apply sut/of-type? % type types)]) (t/deftest paragraph-ending-with-latex (let [doc (parse-resource "paragraph-ending-with-latex.org") type (-> (sp/select-first [(walk-types "paragraph") (sp/must :children) sp/LAST] doc) sut/type)] (t/is (= "latex-environment" type)))) (t/deftest paragraph-surrounding-latex (let [doc (parse-resource "paragraph-surrounding-latex.org") children (->> doc (sp/select-first [(walk-types "paragraph")]) :children (map sut/type))] (t/is (= ["text" "latex-environment" "text"] children)))) (t/deftest paragraph-ending-in-bold-surrounding-latex (let [doc (parse-resource "paragraph-ending-in-bold-surrounding-latex.org") children (->> doc (sp/select-first [(walk-types "paragraph")]) :children (map sut/type))] (t/is (= ["text" "bold" "latex-environment" "text"] children)))) (t/deftest paragraph-with-multiple-latex (let [doc (parse-resource "paragraph-with-multiple-latex.org") paragraphs (sp/select (walk-types "paragraph") doc)] (t/is (= 2 (count paragraphs))) (let [[p₁ p₂] paragraphs] (doseq [[p ts] [[p₁ ["text" "latex-environment" "text" "latex-environment"]] [p₂ ["text" "latex-environment" "text" "latex-environment" "text"]]]] (t/is (= ts (sp/select [(sp/must :children) sp/ALL (sp/view sut/type)] p))))))) (t/deftest paragraph-with-separate-latex (let [doc (parse-resource "paragraph-with-separate-latex.org") cs (sp/select [(walk-types "section") (sp/must :children) sp/ALL (sp/view sut/type)] doc)] (t/is (= ["paragraph" "latex-environment"] cs)))) (t/deftest paragraph-surrounding-separate-latex (let [doc (parse-resource "paragraph-surrounding-separate-latex.org") cs (sp/select [(walk-types "section") (sp/must :children) sp/ALL (sp/view sut/type)] doc)] (t/is (= ["paragraph" "latex-environment" "paragraph"] cs))))