Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support REPL languages that use colons #393

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/flow-control/input-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,50 @@ it('forwards input stream to target index specified in input', () => {
controller.handle(commands);

inputStream.write('1:something');
inputStream.write(' 1:ignore_leading_whitespace');
inputStream.write('1:multi\nline\n');

expect(commands[0].stdin?.write).not.toHaveBeenCalled();
expect(commands[1].stdin?.write).toHaveBeenCalledTimes(1);
expect(commands[1].stdin?.write).toHaveBeenCalledTimes(3);
expect(commands[1].stdin?.write).toHaveBeenCalledWith('something');
expect(commands[1].stdin?.write).toHaveBeenCalledWith('ignore_leading_whitespace');
expect(commands[1].stdin?.write).toHaveBeenCalledWith('multi\nline\n');
});

it('forwards input stream to target index specified in input when input contains colon', () => {
controller.handle(commands);

inputStream.emit('data', Buffer.from('1::something'));
inputStream.emit('data', Buffer.from('1:some:thing'));
inputStream.emit('data', Buffer.from('1: :something'));
inputStream.emit('data', Buffer.from('1::something')); // double colon is NOT forwarded

expect(commands[0].stdin?.write).not.toHaveBeenCalled();
expect(commands[0].stdin?.write).toHaveBeenCalledTimes(1);
expect(commands[0].stdin?.write).toHaveBeenCalledWith('1::something');
expect(commands[1].stdin?.write).toHaveBeenCalledTimes(2);
expect(commands[1].stdin?.write).toHaveBeenCalledWith(':something');
expect(commands[1].stdin?.write).toHaveBeenCalledWith('some:thing');
expect(commands[1].stdin?.write).toHaveBeenCalledWith(' :something');
});

it('does not forward input stream when input contains colon in a different format', () => {
controller.handle(commands);

inputStream.emit('data', Buffer.from('Ruby0::Const::Syntax'));
inputStream.emit('data', Buffer.from('1:Ruby1::Const::Syntax'));
inputStream.emit('data', Buffer.from('ruby_symbol_arg :my_symbol'));
inputStream.emit('data', Buffer.from('ruby_symbol_arg(:my_symbol)'));
inputStream.emit('data', Buffer.from('{ruby_key: :my_val}'));
inputStream.emit('data', Buffer.from('{:ruby_key=>:my_val}'));
inputStream.emit('data', Buffer.from('js_obj = {key: "my_val"}'));

expect(commands[1].stdin?.write).toHaveBeenCalledTimes(1);
expect(commands[1].stdin?.write).toHaveBeenCalledWith('Ruby1::Const::Syntax');
expect(commands[0].stdin?.write).toHaveBeenCalledTimes(6);
expect(commands[0].stdin?.write).toHaveBeenCalledWith('Ruby0::Const::Syntax');
expect(commands[0].stdin?.write).toHaveBeenCalledWith('ruby_symbol_arg :my_symbol');
expect(commands[0].stdin?.write).toHaveBeenCalledWith('ruby_symbol_arg(:my_symbol)');
expect(commands[0].stdin?.write).toHaveBeenCalledWith('{ruby_key: :my_val}');
expect(commands[0].stdin?.write).toHaveBeenCalledWith('{:ruby_key=>:my_val}');
expect(commands[0].stdin?.write).toHaveBeenCalledWith('js_obj = {key: "my_val"}');
});

it('forwards input stream to target name specified in input', () => {
Expand Down
18 changes: 10 additions & 8 deletions src/flow-control/input-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ export class InputHandler implements FlowController {
return { commands };
}

const commandsMap = new Map<string, Command>();
for (const command of commands) {
commandsMap.set(command.index.toString(), command);
commandsMap.set(command.name, command);
}

Rx.fromEvent(inputStream, 'data')
.pipe(map((data) => String(data)))
.subscribe((data) => {
const dataParts = data.split(/:(.+)/);
const targetId = dataParts.length > 1 ? dataParts[0] : this.defaultInputTarget;
const input = dataParts[1] || data;
const dataMatch = data.match(/^\s*([\w-]+):(?!:)(.+)$/s);
const targetId = dataMatch ? dataMatch[1] : this.defaultInputTarget.toString();
const input = dataMatch ? dataMatch[2] : data;

const command = commands.find(
(command) =>
command.name === targetId ||
command.index.toString() === targetId.toString()
);
const command = commandsMap.get(targetId);

if (command && command.stdin) {
command.stdin.write(input);
Expand Down