|
| 1 | +; This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +; License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +; file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +(ns noahtheduke.splint.rules.lint.update-with-swap-test |
| 6 | + (:require |
| 7 | + [lazytest.core :refer [defdescribe it]] |
| 8 | + [noahtheduke.splint.test-helpers :refer [expect-match single-rule-config]])) |
| 9 | + |
| 10 | +(set! *warn-on-reflection* true) |
| 11 | + |
| 12 | +(def rule-name 'lint/update-with-swap) |
| 13 | + |
| 14 | +(defdescribe update-with-swap-test |
| 15 | + (it "works with update" |
| 16 | + (expect-match |
| 17 | + [{:rule-name rule-name |
| 18 | + :form '(update state :counter swap! + 5) |
| 19 | + :message "swap! in update derefs the value in the map." |
| 20 | + :alt '(swap! (:counter state) + 5)}] |
| 21 | + "(update state :counter swap! + 5)" |
| 22 | + (single-rule-config rule-name))) |
| 23 | + (it "works with update-in" |
| 24 | + (expect-match |
| 25 | + [{:rule-name rule-name |
| 26 | + :form '(update-in state [:users :counter] swap! + 5) |
| 27 | + :message "swap! in update derefs the value in the map." |
| 28 | + :alt '(swap! (get-in state [:users :counter]) + 5)}] |
| 29 | + "(update-in state [:users :counter] swap! + 5)" |
| 30 | + (single-rule-config rule-name))) |
| 31 | + (it "expects at least 1 arg to swap!" |
| 32 | + (expect-match |
| 33 | + nil |
| 34 | + "(update state :counter swap!)" |
| 35 | + (single-rule-config rule-name))) |
| 36 | + (it "doesn't care about the args to swap!" |
| 37 | + (expect-match |
| 38 | + [{:rule-name rule-name |
| 39 | + :form '(update state :counter swap! (fn [foo] foo)) |
| 40 | + :message "swap! in update derefs the value in the map." |
| 41 | + :alt '(swap! (:counter state) (fn [foo] foo))}] |
| 42 | + "(update state :counter swap! (fn [foo] foo))" |
| 43 | + (single-rule-config rule-name)))) |
0 commit comments