Add full implementation of duplicate shape action.

This commit is contained in:
Andrey Antukh 2016-02-19 21:08:56 +02:00
parent e7ddd65ee2
commit 747c9b45aa
5 changed files with 154 additions and 19 deletions

View file

@ -168,5 +168,3 @@
;; (pprint result)
(t/is (= result expected))
(t/is (vector? (get-in result [:pages-by-id 1 :shapes])))))

View file

@ -0,0 +1,93 @@
(ns uxbox.state.shapes-tests
(:require [cljs.test :as t :include-macros true]
[cljs.pprint :refer (pprint)]
[uxbox.state.shapes :as ssh]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Duplicate (one shape)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn constantly-inc
[init]
(let [v (atom init)]
(fn [& args]
(let [result @v]
(swap! v inc)
result))))
;; duplicate shape: duplicate simple shape
(t/deftest duplicate-shapes-test1
(let [initial {:pages-by-id {1 {:id 1 :shapes [1]}}
:shapes-by-id {1 {:id 1 :page 1}}}
expected (-> initial
(assoc-in [:pages-by-id 1 :shapes] [1 2])
(assoc-in [:shapes-by-id 2] {:id 2 :page 1}))]
(with-redefs [cljs.core/random-uuid (constantly 2)]
(let [result (ssh/duplicate-shapes initial [1])]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected))))))
;; duplicate shape: duplicate inside group
(t/deftest duplicate-shapes-test2
(let [initial {:pages-by-id {1 {:id 1 :shapes [1]}}
:shapes-by-id {1 {:id 1 :page 1
:type :builtin/group
:items [2 3]}
2 {:id 2 :page 1 :group 1}
3 {:id 3 :page 1 :group 1}}}
expected (-> initial
(assoc-in [:shapes-by-id 1 :items] [2 3 4 5])
(assoc-in [:shapes-by-id 4] {:id 4 :page 1 :group 1})
(assoc-in [:shapes-by-id 5] {:id 5 :page 1 :group 1}))]
(with-redefs [cljs.core/random-uuid (constantly-inc 4)]
(let [result (ssh/duplicate-shapes initial [2 3])]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected))))))
;; duplicate shape: duplicate mixed bag
(t/deftest duplicate-shapes-test3
(let [initial {:pages-by-id {1 {:id 1 :shapes [1 4]}}
:shapes-by-id {1 {:id 1 :page 1
:type :builtin/group
:items [2 3]}
2 {:id 2 :page 1 :group 1}
3 {:id 3 :page 1 :group 1}
4 {:id 4 :page 1}}}
expected (-> initial
(assoc-in [:pages-by-id 1 :shapes] [1 4 5 6])
(assoc-in [:shapes-by-id 5] {:id 5 :page 1})
(assoc-in [:shapes-by-id 6] {:id 6 :page 1}))]
(with-redefs [cljs.core/random-uuid (constantly-inc 5)]
(let [result (ssh/duplicate-shapes initial [3 4])]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected))))))
;; duplicate shape: duplicate one group
(t/deftest duplicate-shapes-test4
(let [initial {:pages-by-id {1 {:id 1 :shapes [1]}}
:shapes-by-id {1 {:id 1 :page 1
:type :builtin/group
:items [2]}
2 {:id 3 :page 1 :group 1}}}
expected (-> initial
(assoc-in [:pages-by-id 1 :shapes] [1 3])
(assoc-in [:shapes-by-id 3] {:id 3 :page 1
:type :builtin/group
:items [4]})
(assoc-in [:shapes-by-id 4] {:id 4 :page 1 :group 3}))]
(with-redefs [cljs.core/random-uuid (constantly-inc 3)]
(let [result (ssh/duplicate-shapes initial [1])]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected))))))

View file

@ -1,5 +1,6 @@
(ns uxbox.test-runner
(:require [cljs.test :as test]
[uxbox.state.shapes-tests]
[uxbox.data.workspace-tests]
[uxbox.util.geom-tests]))
@ -11,6 +12,7 @@
(test/empty-env)
'uxbox.data.workspace-tests
'uxbox.util.geom-tests
'uxbox.state.shapes-tests
))
(defmethod test/report [:cljs.test/default :end-run-tests]