Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions ruby/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ def scan_until(pattern)
%x{
var self = this;


pattern = self.$anchor(pattern);

var pos = self.pos,
working = self.working,
result;

while (true) {
var isEmpty = working.length === 0;
result = pattern.exec(working);
pos += 1;
var isEmpty = working.length === 0;
result = pattern.exec(working);
pos += 1;
working = working.substr(1);

if (result == null) {
Expand All @@ -41,12 +40,11 @@ def scan_until(pattern)
continue;
}

self.matched = self.string.substr(self.pos, pos - self.pos - 1 + result[0].length);
self.prev_pos = pos - 1;
self.pos = pos;
self.prev_pos = self.pos;
self.pos = pos + result[0].length - 1;
self.working = working.substr(result[0].length - 1);

return self.matched;
return self.matched = self.string.substr(self.prev_pos, self.pos - self.prev_pos);
}
}
end
Expand Down
20 changes: 20 additions & 0 deletions ts/choice_command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect } from 'chai';
import DynamicLoader from './loader/dynamic_loader';
import { I18n } from './internal';
import { mockRandomizer } from './test/randomizer';

describe('Choice command', () => {
it('keeps last item when multiple spaces between items', async () => {
I18n.$clear_translate_table();
const loader = new DynamicLoader();
const GameSystemClass = await loader.dynamicLoad('DiceBot');
const gameSystem = new GameSystemClass('choice 123 456 789');

const $random = mockRandomizer(gameSystem);
$random.onCall(0).returns(3);
$random.onCall(1).throwsException(new Error('Unexpected call for $random'));

const res = gameSystem.eval();
expect(res?.text).to.equal('(choice 123 456 789) > 789');
});
});