Skip to content

Commit

Permalink
add Binding example to Global and local bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jasalt committed Dec 30, 2024
1 parent 29b047f commit 7b0d8b5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions content/documentation/global-and-local-bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,29 @@ To update a variable with a function the `swap!` function can be used.
(swap! foo + 2) # Evaluates to 12
(deref foo) # Evaluates to 12
```

## Binding

While writing tests on code depending on external state can be challenging, `binding` function allows to remap existing bindings which can be used for mocking functions or values during tests. As example, code depending on runtime environment:

```phel
(ns my-app\tests\demo
(:require phel\test :refer [deftest is]))
# Function that would return e.g. "x86_64", depending on the environment:
(defn get-system-architecture [] (php/php_uname "m"))
(defn greet-user-by-architecture []
(print "Hello" (get-system-architecture) "user!"))
# With binding, a mock function can be used in place of the original one
# allowing to write tests for cases that depend on system state:
(deftest greeting-test
(binding [get-system-architecture |(str "i386")] # <- mock function
(let [greeting-out (with-output-buffer (greet-user-by-architecture))]
(is (= "Hello i386 user!" greeting-out)
"i386 system user is greeted accordingly"))))
# Test is successful:
# ✔ greet-test: i386 system user is greeted accordingly
```

0 comments on commit 7b0d8b5

Please sign in to comment.