Skip to content

Commit 5301af2

Browse files
committed
fix(form): elsa-form-find-child should return nil if no child is found
1 parent f828e7e commit 5301af2

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

elsa-form.el

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ This only makes sense for the sequence forms:
9292
(defun elsa-form-find-child (form pred)
9393
"Find first child of FORM satisfying predicate PRED."
9494
(declare (indent 1))
95-
(catch 'found
96-
(elsa-form-visit form
97-
(lambda (child)
98-
(when (funcall pred child)
99-
(throw 'found child))))))
95+
(let (re)
96+
(catch 'found
97+
(elsa-form-visit form
98+
(lambda (child)
99+
(when (funcall pred child)
100+
(setq re child)
101+
(throw 'found child)))))
102+
re))
100103

101104
(defun elsa-locate-dominating-form (form name)
102105
"Starting at FORM, look up parent forms for form with NAME.

tests/test-reader.el

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,50 @@
55

66
(describe "Elsa reader"
77

8+
(describe "helpers"
9+
10+
(describe "elsa-locate-dominating-form"
11+
12+
(it "should look up first form with given name in the parent hierarchy"
13+
(elsa-test-with-read-form "|(progn (prog1 (when (foo))))" form
14+
(let ((foo-form (elsa-cadr (elsa-cadr (elsa-cadr form)))))
15+
(expect (elsa-locate-dominating-form foo-form 'progn)
16+
:to-be form))))
17+
18+
(it "should return nil if no form with name exists in the hierarchy"
19+
(elsa-test-with-read-form "|(progn (prog1 (when (foo))))" form
20+
(let ((foo-form (elsa-cadr (elsa-cadr (elsa-cadr form)))))
21+
(expect (elsa-locate-dominating-form foo-form 'let)
22+
:to-be nil))))
23+
24+
(it "should return itself if its name is name"
25+
(elsa-test-with-read-form "|(progn (prog1 (when (foo))))" form
26+
(let ((foo-form (elsa-cadr (elsa-cadr (elsa-cadr form)))))
27+
(expect (elsa-locate-dominating-form foo-form 'foo)
28+
:to-be foo-form))))
29+
30+
(it "should work with a list of names"
31+
(elsa-test-with-read-form "|(progn (prog1 (when (foo))))" form
32+
(let ((foo-form (elsa-cadr (elsa-cadr (elsa-cadr form))))
33+
(when-form (elsa-cadr (elsa-cadr form))))
34+
(expect (elsa-locate-dominating-form foo-form '(when progn))
35+
:to-be when-form)))))
36+
37+
(describe "elsa-form-find-child"
38+
39+
(it "should find a child form matching a predicate"
40+
(elsa-test-with-analysed-form "(progn (foo) (bar))" form
41+
(let ((foo-form (elsa-cadr form)))
42+
(expect (elsa-form-find-child form
43+
(lambda (f) (eq (elsa-get-name f) 'foo)))
44+
:to-be foo-form))))
45+
46+
(it "should return nil if no child form matches the predicate"
47+
(elsa-test-with-analysed-form "(progn (foo) (bar))" form
48+
(expect (elsa-form-find-child form
49+
(lambda (f) (eq (elsa-get-name f) 'baz)))
50+
:to-be nil)))))
51+
852
(describe "symbols"
953

1054
(it "should read a symbol"

0 commit comments

Comments
 (0)