|
2 | 2 | (:import contextlib inspect os socket tempfile) |
3 | 3 | (:require |
4 | 4 | [basilisp.string :as str] |
5 | | - [basilisp.test :refer [deftest is testing]])) |
| 5 | + [basilisp.test :refer [deftest is are testing]])) |
6 | 6 |
|
7 | 7 | (deftest defn-test |
8 | 8 | (testing "single arity defn" |
|
739 | 739 | (g v)))))) |
740 | 740 |
|
741 | 741 | (deftest fn-associative-destructuring |
| 742 | + (testing "ignore non-associative types" |
| 743 | + (let [f (fn [{:keys [a]}] |
| 744 | + a)] |
| 745 | + (are [v] (nil? (f v)) |
| 746 | + nil |
| 747 | + [] |
| 748 | + [:a 5] |
| 749 | + "" |
| 750 | + "abc" |
| 751 | + 5 |
| 752 | + 5.5)) |
| 753 | + |
| 754 | + (testing "with defaults" |
| 755 | + (let [f (fn [{:keys [a] :or {a :not-defined}}] |
| 756 | + a)] |
| 757 | + (are [v] (= :not-defined (f v)) |
| 758 | + nil |
| 759 | + [] |
| 760 | + [:a 5] |
| 761 | + "" |
| 762 | + "abc" |
| 763 | + 5 |
| 764 | + 5.5)))) |
| 765 | + |
| 766 | + (testing "falsey values not overridden by default values" |
| 767 | + (let [f (fn [{:keys [a] :or {a :not-defined}}] |
| 768 | + a)] |
| 769 | + (are [v] (= v (f {:a v})) |
| 770 | + nil |
| 771 | + false))) |
| 772 | + |
742 | 773 | (testing "destructuring simple keys" |
743 | 774 | (let [m {:a "a" :b "b"} |
744 | 775 |
|
|
754 | 785 |
|
755 | 786 | (let [m {:a "a"} |
756 | 787 | m1 {:a "a" :c "d"} |
| 788 | + m2 '(:a "a" :c "d") |
| 789 | + m3 (list m1) |
757 | 790 |
|
758 | 791 | f (fn [{:keys [a c] :as m :or {c "c"}}] |
759 | 792 | [a c m])] |
760 | 793 | (is (= ["a" "c" m] (f m))) |
761 | | - (is (= ["a" "d" m1] (f m1))))) |
| 794 | + (is (= ["a" "d" m1] (f m1))) |
| 795 | + (is (= ["a" "d" (apply hash-map m2)] (f m2))) |
| 796 | + (is (= ["a" "d" m1] (f m3))))) |
762 | 797 |
|
763 | 798 | (testing "destructuring simple string keys" |
764 | 799 | (let [m {:a 1 :b "b" "a" :a "b" :b} |
|
1019 | 1054 | (is (= [[:a :b] [:c]] all))))) |
1020 | 1055 |
|
1021 | 1056 | (deftest let-associative-destructuring |
| 1057 | + (testing "ignore non-associative types" |
| 1058 | + (are [v] (nil? (let [{:keys [a]} v] a)) |
| 1059 | + nil |
| 1060 | + [] |
| 1061 | + [:a 5] |
| 1062 | + "" |
| 1063 | + "abc" |
| 1064 | + 5 |
| 1065 | + 5.5) |
| 1066 | + |
| 1067 | + (testing "with defaults" |
| 1068 | + (are [v] (= :not-defined (let [{:keys [a] :or {a :not-defined}} v] a)) |
| 1069 | + nil |
| 1070 | + [] |
| 1071 | + [:a 5] |
| 1072 | + "" |
| 1073 | + "abc" |
| 1074 | + 5 |
| 1075 | + 5.5))) |
| 1076 | + |
| 1077 | + (testing "falsey values not overridden by default values" |
| 1078 | + (are [v] (= v (let [{:keys [a] :or {a :not-defined}} {:a v}] |
| 1079 | + a)) |
| 1080 | + nil |
| 1081 | + false)) |
| 1082 | + |
1022 | 1083 | (testing "destructuring simple keys" |
1023 | 1084 | (let [{:keys [a b]} {:a "a" :b "b"}] |
1024 | 1085 | (is (= "a" a)) |
|
1032 | 1093 | (let [{:keys [a b] :as m :or {b "c"}} {:a "a"}] |
1033 | 1094 | (is (= "a" a)) |
1034 | 1095 | (is (= "c" b)) |
1035 | | - (is (= {:a "a"} m)))) |
| 1096 | + (is (= {:a "a"} m))) |
| 1097 | + |
| 1098 | + (let [{:keys [a b] :as m :or {b "c"}} '(:a "a")] |
| 1099 | + (is (= "a" a)) |
| 1100 | + (is (= "c" b)) |
| 1101 | + (is (= {:a "a"} m))) |
| 1102 | + |
| 1103 | + (let [{:keys [a b] :as m :or {b "c"}} '({:a "a" :b "b"})] |
| 1104 | + (is (= "a" a)) |
| 1105 | + (is (= "b" b)) |
| 1106 | + (is (= {:a "a" :b "b"} m)))) |
1036 | 1107 |
|
1037 | 1108 | (testing "destructuring simple string keys" |
1038 | 1109 | (let [{:strs [a b]} {:a 1 :b 2 "a" :a "b" :b}] |
|
1253 | 1324 | (g v))))))) |
1254 | 1325 |
|
1255 | 1326 | (deftest letfn-associative-destructuring |
| 1327 | + (testing "ignore non-associative types" |
| 1328 | + (letfn [(f [{:keys [a]}] |
| 1329 | + a)] |
| 1330 | + (are [v] (nil? (f v)) |
| 1331 | + nil |
| 1332 | + [] |
| 1333 | + [:a 5] |
| 1334 | + "" |
| 1335 | + "abc" |
| 1336 | + 5 |
| 1337 | + 5.5)) |
| 1338 | + |
| 1339 | + (testing "with defaults" |
| 1340 | + (letfn [(f [{:keys [a] :or {a :not-defined}}] |
| 1341 | + a)] |
| 1342 | + (are [v] (= :not-defined (f v)) |
| 1343 | + nil |
| 1344 | + [] |
| 1345 | + [:a 5] |
| 1346 | + "" |
| 1347 | + "abc" |
| 1348 | + 5 |
| 1349 | + 5.5)))) |
| 1350 | + |
| 1351 | + (testing "falsey values not overridden by default values" |
| 1352 | + (letfn [(f [{:keys [a] :or {a :not-defined}}] |
| 1353 | + a)] |
| 1354 | + (are [v] (= v (f {:a v})) |
| 1355 | + nil |
| 1356 | + false))) |
| 1357 | + |
1256 | 1358 | (testing "destructuring simple keys" |
1257 | 1359 | (let [m {:a "a" :b "b"}] |
1258 | 1360 | (letfn [(f [{:keys [a]}] |
|
1266 | 1368 | (is (= ["a" "b" m] (h m))))) |
1267 | 1369 |
|
1268 | 1370 | (let [m {:a "a"} |
1269 | | - m1 {:a "a" :c "d"}] |
| 1371 | + m1 {:a "a" :c "d"} |
| 1372 | + m2 '(:a "a" :c "d") |
| 1373 | + m3 (list m1)] |
1270 | 1374 | (letfn [(f [{:keys [a c] :as m :or {c "c"}}] |
1271 | 1375 | [a c m])] |
1272 | 1376 | (is (= ["a" "c" m] (f m))) |
1273 | | - (is (= ["a" "d" m1] (f m1)))))) |
| 1377 | + (is (= ["a" "d" m1] (f m1))) |
| 1378 | + (is (= ["a" "d" (apply hash-map m2)] (f m2))) |
| 1379 | + (is (= ["a" "d" m1] (f m3)))))) |
1274 | 1380 |
|
1275 | 1381 | (testing "destructuring simple string keys" |
1276 | 1382 | (let [m {:a 1 :b "b" "a" :a "b" :b}] |
|
1498 | 1604 | (recur r orig-all (conj pairs pair) (+ accum v1 v2)))))))) |
1499 | 1605 |
|
1500 | 1606 | (deftest loop-associative-destructuring |
| 1607 | + (testing "ignore non-associative types" |
| 1608 | + (is (= [] |
| 1609 | + (loop [items [nil [] [:a 5] "" "abc" 5 5.5] |
| 1610 | + {:keys [a]} (first items) |
| 1611 | + accum []] |
| 1612 | + (if (seq items) |
| 1613 | + (recur (rest items) |
| 1614 | + (first (rest items)) |
| 1615 | + (cond-> accum a (conj a))) |
| 1616 | + accum)))) |
| 1617 | + |
| 1618 | + (testing "with defaults" |
| 1619 | + (is (= (vec (repeat 7 :not-defined)) |
| 1620 | + (loop [items [nil [] [:a 5] "" "abc" 5 5.5] |
| 1621 | + {:keys [a] :or {a :not-defined}} (first items) |
| 1622 | + accum []] |
| 1623 | + (if (seq items) |
| 1624 | + (recur (rest items) |
| 1625 | + (first (rest items)) |
| 1626 | + (cond-> accum a (conj a))) |
| 1627 | + accum)))))) |
| 1628 | + |
| 1629 | + (testing "falsey values not overridden by default values" |
| 1630 | + (is (= [nil false] |
| 1631 | + (loop [items (map #(hash-map :a %) [nil false]) |
| 1632 | + {:keys [a] :as m :or {a :not-defined}} (first items) |
| 1633 | + accum []] |
| 1634 | + (if (seq items) |
| 1635 | + (recur (rest items) |
| 1636 | + (first (rest items)) |
| 1637 | + (conj accum a)) |
| 1638 | + accum))))) |
| 1639 | + |
1501 | 1640 | (testing "destructuring simple keys" |
1502 | | - (is (= [[{:a "a" :b "b"} {:a "c" :b "d"} {:a "e"}] "abcdee"] |
1503 | | - (loop [items [{:a "a" :b "b"} {:a "c" :b "d"} {:a "e"}] |
| 1641 | + (is (= [[{:a "a" :b "b"} {:a "c" :b "d"} {:a "e"} {:a "f"} {:a "g" :b "h"}] "abcdeefegh"] |
| 1642 | + (loop [items [{:a "a" :b "b"} {:a "c" :b "d"} {:a "e"} '(:a "f") '({:a "g" :b "h"})] |
1504 | 1643 | {:keys [a b] :as m :or {b "e"}} (first items) |
1505 | 1644 | coll [] |
1506 | 1645 | accum []] |
|
0 commit comments