Skip to content

Commit 5a3d05e

Browse files
committed
implement basic set
1 parent 5490c5b commit 5a3d05e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/mash/shell/ast/set_definition.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ def __init__(self, items, condition=None):
2424
self.condition = condition
2525

2626
def run(self, prev_result='', shell: BaseShell = None, lazy=False):
27-
items = [shell.run_commands(item) for item in self.items]
27+
items = []
28+
for item in self.items.values:
29+
results = shell.run_commands(item)
30+
for row in results:
31+
items.append(row.splitlines())
2832

2933
if lazy:
3034
return f'{{ {self.items} | {self.condition} }}'
3135

32-
return list(self.apply(items, shell))
36+
result = list(self.apply(items, shell))
37+
return ['\n'.join(c) for c in result]
3338

3439
def apply(self, items, shell: BaseShell = None):
40+
"""Returns the outer product of a nested list.
41+
"""
3542
if self.condition is None:
3643
yield from product(*items)
3744
return

test/shell/test_parse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ def test_parse_set_with_filter():
569569
assert isinstance(result, SetDefinition)
570570
result = parse_line('{ users groups | x.id == y.id }')
571571
assert isinstance(result, SetDefinition)
572+
result = parse_line('x <- { users }')
573+
assert isinstance(result, Assign)
574+
assert isinstance(result.rhs, SetDefinition)
572575

573576
# TODO
574577
if 0:

test/shell/test_shell_variables.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,16 @@ def test_variable_assignment_with_if_then():
266266

267267
run_command('a <- if "" then echo 10', shell=shell)
268268
assert shell.env['a'] == ''
269+
270+
def test_set_definition():
271+
shell = Shell()
272+
shell.ignore_invalid_syntax = False
273+
274+
run_command('a <- range 3', shell=shell)
275+
run_command('b <- range 3', shell=shell)
276+
run_command('c <- { $a }', shell=shell)
277+
assert shell.env['c'] == '0 1 2'
278+
279+
# TODO
280+
run_command('c <- { a b }', shell=shell)
281+
run_command('c <- { a b | a == b }', shell=shell)

0 commit comments

Comments
 (0)