Skip to content

Commit 8de15e5

Browse files
committed
All unit tests with new fork syntax
1 parent 1891dfe commit 8de15e5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/talkpipe/chatterlang/parsers.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,21 @@ def fork_section():
290290
yield lexeme(')')
291291
return ForkNode(branches=branches, params=bracket_content)
292292

293-
transforms_section = (
294-
# First transform can be without a leading pipe
295-
(fork_section | segment | variable).map(lambda x: [x]) |
296-
# Subsequent transforms require a leading pipe
297-
(lexeme('|') >> (fork_section | segment | variable)).many()
298-
)
299-
"""A parser for the transforms section. Transforms are separated by the '|' character."""
293+
@generate
294+
def transforms_section():
295+
"""A parser for the transforms section. Transforms are separated by the '|' character."""
296+
# First transform may or may not have a leading pipe (optional to allow empty transforms)
297+
first_transform = yield (lexeme('|').optional() >> (fork_section | segment | variable)).optional()
298+
if first_transform is None:
299+
return []
300+
301+
transforms = [first_transform]
302+
303+
# Parse remaining segments (with leading pipes)
304+
remaining = yield (lexeme('|') >> (fork_section | segment | variable)).many()
305+
transforms.extend(remaining)
306+
307+
return transforms
300308

301309
# Parser for arrow fork target: -> identifier
302310
arrow_fork_target = (lexeme('->') >> identifier).map(lambda x: x.name)

tests/talkpipe/chatterlang/test_compiler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ def test_fork_arrow_syntax_multiple_forks():
169169
script = """
170170
INPUT FROM range[lower=0, upper=3] -> fork1;
171171
INPUT FROM range[lower=10, upper=13] -> fork2;
172-
fork1 -> lambda[expression="item * 2"] -> fork3;
172+
fork1 -> lambda[expression="item * 2"] | lambda[expression="item + 1"] -> fork3;
173173
fork2 -> lambda[expression="item + 100"] -> fork3;
174174
fork3 -> toList
175175
"""
176176
f = compiler.compile(script).as_function(single_in=True,single_out=True)
177177
ans = f()
178-
assert sorted(ans) == sorted([0, 2, 4, 110, 111, 112])
178+
assert sorted(ans) == sorted([1, 3, 5, 110, 111, 112])
179179

180180
def test_fork_arrow_syntax_metadata():
181181

@@ -187,10 +187,10 @@ def test_fork_arrow_syntax_metadata():
187187

188188
script = """
189189
INPUT FROM range[lower=0, upper=3] | flushN[n=1] -> fork1;
190-
fork1 -> collectMetadata -> toList
190+
fork1 -> collectMetadata | toList
191191
"""
192-
f = compiler.compile(script)#.as_function(single_in=True,single_out=True)
193-
ans2 = list(f())
192+
f = compiler.compile(script).as_function(single_in=True,single_out=True)
193+
ans2 = f()
194194
assert ans == ans2
195195

196196
def test_variables_as_parameters():

0 commit comments

Comments
 (0)