Files
net-deertopia/doerg/test/net/deertopia/doerg/element_test.clj
T
msyds 362d2a5088
build / build (push) Failing after 20s
wip: fix: latex paragraphs again lol
2026-03-17 21:25:35 -06:00

114 lines
4.7 KiB
Clojure

(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/testing "first paragraph should belong to a section"
(t/is (-> (parse-resource "first-paragraph-under-first-section.org")
first-paragraph-belongs-to-first-section?))
(t/is (not (-> (parse-resource "first-paragraph-under-heading.org")
first-paragraph-belongs-to-first-section?)))))
(defn walk-types [type & types]
[sut/postorder-walker #(apply sut/of-type? % type types)])
(defn- paragraph-ends-with-latex? [doc]
(let [type (-> (sp/select-first [(walk-types "paragraph")
(sp/must :children)
sp/LAST]
doc)
sut/type)]
(t/is type "latex-environment")))
(defn- paragraph-has-latex? [doc]
(t/is (sp/select-first [(walk-types "paragraph")
(sp/must :children)
sp/ALL
#(sut/of-type? % "latex-environment")]
doc)))
(defn- paragraph-has-multiple-latex? [doc]
(let [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)))))))
(defn- paragraph-has-separate-latex? [doc]
(let [cs (sp/select [(walk-types "section")
(sp/must :children)
sp/ALL
(sp/view sut/type)]
doc)]
(t/is (= cs ["paragraph" "latex-environment"]))))
(defn- paragraph-surrounding-separate-latex? [doc]
(let [cs (sp/select [(walk-types "section")
(sp/must :children)
sp/ALL
(sp/view sut/type)]
doc)]
(t/is (= cs ["paragraph" "latex-environment" "paragraph"]))))
(t/deftest paragraph-separation
(t/testing "paragraph ending with latex"
(-> (parse-resource "paragraph-ending-with-latex.org")
paragraph-ends-with-latex?))
(t/testing "paragraph surrounding latex"
(-> (parse-resource "paragraph-surrounding-latex.org")
paragraph-has-latex?))
(t/testing "paragraph with interleaved latex"
(-> (parse-resource "paragraph-with-multiple-latex.org")
paragraph-has-multiple-latex?))
(t/testing "paragraph with separate latex"
(-> (parse-resource "paragraph-with-separate-latex.org")
paragraph-has-separate-latex?))
(t/testing "paragraphs surrounding separate latex"
(-> (parse-resource "paragraph-surrounding-separate-latex.org")
paragraph-surrounding-separate-latex?)))