Skip to content

Commit 91b63d2

Browse files
committed
Fixes #7 : Add an error message when a reference cannot be resolved.
1 parent 132ea44 commit 91b63d2

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Versions prior to v0.1.0 are considered experimental, their API may change.
1010
### Fixed
1111
- :fn with-condition in the `describe` function.
1212
Updated the minimap model to reflect that with-condition is valid for the :fn nodes.
13+
- Throw an error when a reference cannot be resolved in the model, instead of just let
14+
minimallist crash somewhere else with an unexpected `nil` model.
1315

1416
## [0.0.6] - 2020-08-09
1517
## [0.0.5] - 2020-08-09

src/minimallist/core.cljc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
~consequence
4242
true))
4343

44+
(defn- resolve-ref [context key]
45+
(when-not (contains? context key)
46+
(throw (ex-info "Cannot resolve reference." {:context context, :key key})))
47+
(get context key))
48+
4449
(declare -valid?)
4550

4651
(defn- left-overs
@@ -68,7 +73,7 @@
6873
(drop (:min model))
6974
(apply concat))
7075
:let (left-overs (into context (:bindings model)) (:body model) seq-data)
71-
:ref (left-overs context (get context (:key model)) seq-data))
76+
:ref (left-overs context (resolve-ref context (:key model)) seq-data))
7277
(if (and seq-data
7378
(-valid? context (dissoc model :inlined) (first seq-data)))
7479
[(next seq-data)]
@@ -132,7 +137,7 @@
132137
(implies (contains? model :condition-model)
133138
(-valid? context (:condition-model model) data)))
134139
:let (-valid? (into context (:bindings model)) (:body model) data)
135-
:ref (-valid? context (get context (:key model)) data)))
140+
:ref (-valid? context (resolve-ref context (:key model)) data)))
136141

137142
(declare -describe)
138143

@@ -185,7 +190,7 @@
185190
(reverse) ; longest repetitions first
186191
(apply concat))
187192
:let (sequence-descriptions (into context (:bindings model)) (:body model) seq-data)
188-
:ref (sequence-descriptions context (get context (:key model)) seq-data))
193+
:ref (sequence-descriptions context (resolve-ref context (:key model)) seq-data))
189194
(if seq-data
190195
(let [description (-describe context (dissoc model :inlined) (first seq-data))]
191196
(if (:valid? description)
@@ -311,7 +316,7 @@
311316
{:valid? false}))
312317
{:valid? false})
313318
:let (-describe (into context (:bindings model)) (:body model) data)
314-
:ref (-describe context (get context (:key model)) data)))
319+
:ref (-describe context (resolve-ref context (:key model)) data)))
315320

316321
;; TODO: Treat the attributes independently of the type of the node in which they appear.
317322
;; That's a kind of composition pattern a-la-unity.

test/minimallist/core_test.cljc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@
253253
(doseq [data valid-coll]
254254
(is (valid? model data)))
255255
(doseq [data invalid-coll]
256-
(is (not (valid? model data)))))))
256+
(is (not (valid? model data))))))
257+
258+
(is (thrown? #?(:clj Exception :cljs js/Object)
259+
(valid? (h/let [] (h/ref 'foo)) 'bar))))
257260

258261

259262
(deftest describe-test
@@ -477,4 +480,7 @@
477480
(doseq [[model data-description-pairs] (partition 2 test-data)]
478481
(doseq [[data description] (partition 2 data-description-pairs)]
479482
(is (= [data (describe model data)]
480-
[data description]))))))
483+
[data description])))))
484+
485+
(is (thrown? #?(:clj Exception :cljs js/Object)
486+
(describe (h/let [] (h/ref 'foo)) 'bar))))

0 commit comments

Comments
 (0)