|
| 1 | +(ns clojure.core-test.underive |
| 2 | + (:require [clojure.test :refer [are deftest is testing]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists |
| 6 | + underive |
| 7 | + |
| 8 | + (def shape-hierarchy |
| 9 | + (-> (make-hierarchy) |
| 10 | + (derive ::circle ::shape) |
| 11 | + (derive ::rect ::shape) |
| 12 | + (derive ::square ::rect))) |
| 13 | + |
| 14 | + (def diamond-hierarchy |
| 15 | + (-> (make-hierarchy) |
| 16 | + (derive ::b ::a) |
| 17 | + (derive ::c ::a) |
| 18 | + (derive ::d ::b) |
| 19 | + (derive ::d ::c))) |
| 20 | + |
| 21 | + (deftest test-underive |
| 22 | + |
| 23 | + (testing "underive tag parent" |
| 24 | + |
| 25 | + (testing "when tag is child of parent" |
| 26 | + (derive ::t ::p) |
| 27 | + (is (isa? ::t ::p)) |
| 28 | + (is (nil? (underive ::t ::p))) |
| 29 | + (is (not (isa? ::t ::p)))) |
| 30 | + |
| 31 | + (testing "when tag is not child of parent" |
| 32 | + (are [tag parent] (nil? (underive tag parent)) |
| 33 | + ::square ::rect |
| 34 | + ::square ::shape |
| 35 | + ::rect ::shape |
| 36 | + nil nil |
| 37 | + :a :a |
| 38 | + 'a 'b |
| 39 | + 42 3.14 |
| 40 | + true false))) |
| 41 | + |
| 42 | + (testing "underive h tag parent" |
| 43 | + |
| 44 | + (testing "when tag is child of parent in h" |
| 45 | + (are [expected h tag parent] (= expected (underive h tag parent)) |
| 46 | + |
| 47 | + {:ancestors {::circle #{::shape}, ::rect #{::shape}} |
| 48 | + :descendants {::shape #{::circle ::rect}} |
| 49 | + :parents {::circle #{::shape}, ::rect #{::shape}}} shape-hierarchy ::square ::rect |
| 50 | + |
| 51 | + {:ancestors {::circle #{::shape}, ::square #{::rect}} |
| 52 | + :descendants {::shape #{::circle}, ::rect #{::square}} |
| 53 | + :parents {::circle #{::shape}, ::square #{::rect}}} shape-hierarchy ::rect ::shape |
| 54 | + |
| 55 | + {:parents {::b #{::a}, ::c #{::a}, ::d #{::c}}, |
| 56 | + :ancestors {::b #{::a}, ::c #{::a}, ::d #{::a ::c}}, |
| 57 | + :descendants {::a #{::d ::b ::c}, ::c #{::d}}} diamond-hierarchy ::d ::b |
| 58 | + |
| 59 | + {:parents {::b #{::a}, ::c #{::a}}, |
| 60 | + :ancestors {::b #{::a}, ::c #{::a}}, |
| 61 | + :descendants {::a #{::b ::c}}} (underive diamond-hierarchy ::d ::b) ::d ::c)) |
| 62 | + |
| 63 | + (testing "when tag is not child of parent in h" |
| 64 | + (are [h tag parent] (= h (underive h tag parent)) |
| 65 | + shape-hierarchy ::rect ::square |
| 66 | + shape-hierarchy ::square ::shape |
| 67 | + diamond-hierarchy ::b ::d |
| 68 | + diamond-hierarchy ::d ::a))) |
| 69 | + |
| 70 | + (testing "bad shape" |
| 71 | + (are [h tag parent] (thrown? #?(:cljs js/Error :default Exception) (underive h tag parent)) |
| 72 | + nil ::a ::b |
| 73 | + {} ::a ::b |
| 74 | + [[:parents {}] [:descendants {}] [:ancestors {}]] ::a ::b |
| 75 | + ::z ::a ::b |
| 76 | + true ::a ::b |
| 77 | + 42 ::a ::b |
| 78 | + 3.14 ::a ::b)))) |
0 commit comments