aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-01 18:21:42 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-01 18:21:42 +0000
commit21b002554452e8eee4e5e65006863bf8b30c92c3 (patch)
tree4d6b1259fa344bca189677575ce0d271937c3ee1 /spec
parentadd README (diff)
downloadalive-21b002554452e8eee4e5e65006863bf8b30c92c3.tar.gz
alive-21b002554452e8eee4e5e65006863bf8b30c92c3.zip
more flexible AST\walk
Diffstat (limited to 'spec')
-rw-r--r--spec/ast_spec.moon47
1 files changed, 34 insertions, 13 deletions
diff --git a/spec/ast_spec.moon b/spec/ast_spec.moon
index cfb64cf..80fbb56 100644
--- a/spec/ast_spec.moon
+++ b/spec/ast_spec.moon
@@ -1,15 +1,36 @@
import Atom, Xpr from require 'ast'
-describe 'Xpr', ->
- it 'can be walked', ->
- aa = Xpr { '', (Atom '1', '', 1), '' }
- ab = Xpr { '', (Atom '2', '', 2), '' }
- ac = Xpr { '', (Atom '3', '', 3), '' }
- b = Xpr { '', aa, ' ', ab, '' }
-
- root = Xpr { '', ac, ' ', b, '' }, 'naked'
-
- iter = root\walk_sexpr!
- for res in *{ ac, aa, ab, b }
- assert.is.equal res, iter!
- assert.is.nil iter!
+describe 'AST', ->
+ describe 'can be walked', ->
+ a1 = Atom '1', '', 1
+ a2 = Atom '2', '', 2
+ a3 = Atom '3', '', 3
+ x1 = Xpr { '', a1, '' }
+ x21 = Xpr { '', a2, '' }
+ x22 = Xpr { '', a3, '' }
+ x2 = Xpr { '', x21, ' ', x22, '' }
+
+ root = Xpr { '', x1, ' ', x2, '' }, 'naked'
+
+ assert_yields = (expected_order, iter) ->
+ for val in *expected_order
+ got_typ, got_val = iter!
+ assert.is.equal val.type, got_typ
+ assert.is.equal val, got_val
+ assert.is.nil iter!
+
+ it 'inside-out', ->
+ assert_yields { a1, x1, a2, x21, a3, x22, x2, root }, root\walk 'inout'
+
+ it 'inside-out, skipping the root', ->
+ assert_yields { a1, x1, a2, x21, a3, x22, x2 }, root\walk 'inout', false
+
+ it 'outside-in', ->
+ assert_yields { root, x1, a1, x2, x21, a2, x22, a3 }, root\walk 'outin'
+
+ it 'outside-in, skipping the root', ->
+ assert_yields { x1, a1, x2, x21, a2, x22, a3 }, root\walk 'outin', false
+
+ it 'errors when direction is wrong or absent', ->
+ assert.has.errors -> root\walk!
+ assert.has.errors -> root\walk 'backandforth'