Skip to content

Commit 76544d0

Browse files
authored
Merge pull request #111 from jasalt/master
Improve `binding` example
2 parents 98da3c7 + 1366a47 commit 76544d0

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

content/documentation/global-and-local-bindings.md

+36-26
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ Creates a new lexical context with assignments defined in bindings. Afterwards t
4040
```
4141
All assignments defined in _bindings_ are immutable and cannot be changed.
4242

43+
## Binding
44+
45+
While `let` creates a new lexical context, `binding` temporarily redefines existing definitions while executing the body. This can be useful when writing tests on functions depending on external state as `binding` allows to remap existing functions or values with mocks.
46+
47+
```phel
48+
(ns my-app\tests\demo
49+
(:require phel\test :refer [deftest is]))
50+
51+
# Function that would return e.g. "x86_64", depending on the environment:
52+
(defn get-system-architecture [] (php/php_uname "m"))
53+
54+
(defn greet-user-by-architecture []
55+
(print "Hello" (get-system-architecture) "user!"))
56+
57+
# Bindings with let are not effective outside it's lexical scope
58+
(deftest greeting-test-let
59+
(let [get-system-architecture |(str "i386")] # <- mock function
60+
(let [greeting-out (with-output-buffer (greet-user-by-architecture))]
61+
(is (= "Hello i386 user!" greeting-out)
62+
"i386 system user is greeted accordingly"))))
63+
64+
# Test fails on a x86_64 system, evaluating to "Hello x86_64 user!":
65+
# ✘ greeting-test-let: i386 system user is greeted accordingly
66+
67+
# With binding, a mock function can bound in place of the original one
68+
(deftest greeting-test-binding
69+
(binding [get-system-architecture |(str "i386")] # <- mock function
70+
(let [greeting-out (with-output-buffer (greet-user-by-architecture))]
71+
(is (= "Hello i386 user!" greeting-out)
72+
"i386 system user is greeted accordingly"))))
73+
74+
# Test is successful:
75+
# ✔ greet-test-binding: i386 system user is greeted accordingly
76+
77+
```
78+
4379
## Variables
4480

4581
```phel
@@ -69,29 +105,3 @@ To update a variable with a function the `swap!` function can be used.
69105
(swap! foo + 2) # Evaluates to 12
70106
(deref foo) # Evaluates to 12
71107
```
72-
73-
## Binding
74-
75-
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:
76-
77-
```phel
78-
(ns my-app\tests\demo
79-
(:require phel\test :refer [deftest is]))
80-
81-
# Function that would return e.g. "x86_64", depending on the environment:
82-
(defn get-system-architecture [] (php/php_uname "m"))
83-
84-
(defn greet-user-by-architecture []
85-
(print "Hello" (get-system-architecture) "user!"))
86-
87-
# With binding, a mock function can be used in place of the original one
88-
# allowing to write tests for cases that depend on system state:
89-
(deftest greeting-test
90-
(binding [get-system-architecture |(str "i386")] # <- mock function
91-
(let [greeting-out (with-output-buffer (greet-user-by-architecture))]
92-
(is (= "Hello i386 user!" greeting-out)
93-
"i386 system user is greeted accordingly"))))
94-
95-
# Test is successful:
96-
# ✔ greet-test: i386 system user is greeted accordingly
97-
```

0 commit comments

Comments
 (0)