Skip to content

Commit

Permalink
test: add tests for components.positioning ns
Browse files Browse the repository at this point in the history
  • Loading branch information
Seryiza committed Nov 11, 2024
1 parent 74d602c commit db161cc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 21 deletions.
12 changes: 5 additions & 7 deletions src/remplater/components/builtin_components.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,17 @@
mtop (or (:margin-top attrs) (:margin attrs) 0)
mright (or (:margin-right attrs) (:margin attrs) 0)
mbottom (or (:margin-bottom attrs) (:margin attrs) 0)
new-attrs (fo/add-margin attrs mleft mtop mright mbottom)]
new-attrs (fo/margin attrs mleft mtop mright mbottom)]
(->> children
(mapv #(r/merge-unexisting-attrs % new-attrs)))))

(defmethod r/render :split
[_ attrs & children]
(let [split-points (fo/split attrs
(:direction attrs)
(:splits attrs))]
[_ {:as attrs :keys [direction splits]} & children]
(let [split-attrs (fo/split attrs direction splits)]
(->> children
(map-indexed
(fn [index child]
(r/merge-unexisting-attrs child (get split-points index))))
(r/merge-unexisting-attrs child (get split-attrs index))))
(vec))))

(defmethod r/render :grid
Expand Down Expand Up @@ -198,7 +196,7 @@
:horizontal-align :center
:vertical-align :center)
(fo/aligned-pattern-wrapper)
(fo/add-margin attrs))
(fo/margin attrs))
{:keys [cell line outline row col]} pattern
{:keys [cells lines outlines rows cols]} (fo/pattern-grid aligned-attrs)]
[:div
Expand Down
24 changes: 10 additions & 14 deletions src/remplater/components/positioning.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
(PDRectangle. x1 y1 width height)))

(defn split-one [attrs coordinate split-size]
(comment
(split-one {:x1 100 :y1 100 :x2 200 :y2 200} :x #(/ % 3))
(split-one {:x1 100 :y1 100 :x2 200 :y2 200} :x 20)
(split-one {:x1 100 :y1 100 :x2 200 :y2 200} :y 20))

(let [horizontal? (= :x coordinate)
[lower-kw upper-kw] (if horizontal?
[:x1 :x2]
Expand All @@ -39,9 +34,6 @@
[attrs-2 attrs-1])))

(defn split [attrs coordinate splits]
(comment
(split {:x1 100 :y1 100 :x2 200 :y2 200} :y [10 20]))

(let [attrs (select-keys attrs [:x1 :y1 :x2 :y2])]
(->> splits
(reduce (fn [all next-split]
Expand Down Expand Up @@ -77,14 +69,18 @@
:x2 (:x2 right-top-el)
:y2 (:y2 right-top-el)})))

(defn add-margin
([attrs margin]
(if (map? margin)
(add-margin attrs (:margin-left margin) (:margin-top margin) (:margin-right margin) (:margin-bottom margin))
(add-margin attrs margin margin margin margin)))
(defn margin
([attrs margin-units]
(if (number? margin-units)
(margin attrs margin-units margin-units margin-units margin-units)
(margin attrs
(or (:margin-left margin-units) 0)
(or (:margin-top margin-units) 0)
(or (:margin-right margin-units) 0)
(or (:margin-bottom margin-units) 0))))

([attrs margin-vertical margin-horizontal]
(add-margin attrs margin-horizontal margin-vertical margin-horizontal margin-vertical))
(margin attrs margin-horizontal margin-vertical margin-horizontal margin-vertical))

([attrs margin-left margin-top margin-right margin-bottom]
(-> attrs
Expand Down
82 changes: 82 additions & 0 deletions test/remplater/components/positioning_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(ns remplater.components.positioning-test
(:require
[clojure.test :refer [deftest is testing]]
[remplater.components.positioning :as pos]))

(deftest attrs->sizes
(is (= [100 100] (pos/attrs->sizes {:x1 0 :y1 0 :x2 100 :y2 100})))
(is (= [100 100] (pos/attrs->sizes {:x1 -50 :y1 -50 :x2 50 :y2 50})))
(is (= [100 100] (pos/attrs->sizes {:x1 50 :y1 50 :x2 -50 :y2 -50}))))

(deftest split-one
(testing "split by x (number)"
(is (= [{:x1 0 :y1 0 :x2 50 :y2 100}
{:x1 50 :y1 0 :x2 100 :y2 100}]
(pos/split-one {:x1 0 :y1 0 :x2 100 :y2 100} :x 50))))

(testing "split by y (number)"
(is (= [{:x1 0 :y1 50 :x2 100 :y2 100}
{:x1 0 :y1 0 :x2 100 :y2 50}]
(pos/split-one {:x1 0 :y1 0 :x2 100 :y2 100} :y 50))))

(testing "split by x (function)"
(is (= [{:x1 0 :y1 0 :x2 50 :y2 100}
{:x1 50 :y1 0 :x2 100 :y2 100}]
(pos/split-one {:x1 0 :y1 0 :x2 100 :y2 100} :x #(/ % 2))))))

(deftest split
(testing "split by x (numbers)"
(is (= [{:x1 0 :y1 0 :x2 25 :y2 100}
{:x1 25 :y1 0 :x2 50 :y2 100}
{:x1 50 :y1 0 :x2 100 :y2 100}]
(pos/split {:x1 0 :y1 0 :x2 100 :y2 100} :x [25 25]))))

(testing "split by y (numbers)"
(is (= [{:x1 0 :y1 75 :x2 100 :y2 100}
{:x1 0 :y1 50 :x2 100 :y2 75}
{:x1 0 :y1 0 :x2 100 :y2 50}]
(pos/split {:x1 0 :y1 0 :x2 100 :y2 100} :y [25 25]))))

(testing "split by x (numbers and function)"
(is (= [{:x1 0 :y1 0 :x2 50 :y2 100}
{:x1 50 :y1 0 :x2 75 :y2 100}
{:x1 75 :y1 0 :x2 100 :y2 100}]
(pos/split {:x1 0 :y1 0 :x2 100 :y2 100} :x [50 #(/ % 2)])))))

(deftest join-two
(testing "vertical join"
(is (= {:x1 0 :y1 0 :x2 100 :y2 100}
(pos/join-two
{:x1 0 :y1 0 :x2 100 :y2 50}
{:x1 0 :y1 50 :x2 100 :y2 100})))
(is (= {:x1 0 :y1 0 :x2 100 :y2 100}
(pos/join-two
{:x1 0 :y1 50 :x2 100 :y2 100}
{:x1 0 :y1 0 :x2 100 :y2 50}))))

(testing "incorrect join"
(is (nil? (pos/join-two
{:x1 0 :y1 0 :x2 100 :y2 50}
{:x1 0 :y1 200 :x2 100 :y2 400})))
(is (nil? (pos/join-two
{:x1 0 :y1 0 :x2 50 :y2 50}
{:x1 100 :y1 100 :x2 200 :y2 20})))))

(deftest margin
(is (= {:x1 25 :y1 25 :x2 75 :y2 75}
(pos/margin {:x1 0 :y1 0 :x2 100 :y2 100} 25)))
(is (= {:x1 25 :y1 0 :x2 75 :y2 100}
(pos/margin {:x1 0 :y1 0 :x2 100 :y2 100} 0 25)))
(is (= {:x1 25 :y1 0 :x2 100 :y2 100}
(pos/margin {:x1 0 :y1 0 :x2 100 :y2 100} 25 0 0 0)))
(is (= {:x1 25 :y1 0 :x2 100 :y2 100}
(pos/margin
{:x1 0 :y1 0 :x2 100 :y2 100}
{:margin-left 25})))
(is (= {:x1 25 :y1 25 :x2 75 :y2 75}
(pos/margin
{:x1 0 :y1 0 :x2 100 :y2 100}
{:margin-left 25
:margin-right 25
:margin-top 25
:margin-bottom 25}))))

0 comments on commit db161cc

Please sign in to comment.