diff --git a/ruby/patch.rb b/ruby/patch.rb index 11f2308..8fb0c65 100644 --- a/ruby/patch.rb +++ b/ruby/patch.rb @@ -20,7 +20,6 @@ def scan_until(pattern) %x{ var self = this; - pattern = self.$anchor(pattern); var pos = self.pos, @@ -28,9 +27,9 @@ def scan_until(pattern) 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) { @@ -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 diff --git a/ts/choice_command.test.ts b/ts/choice_command.test.ts new file mode 100644 index 0000000..8c773d9 --- /dev/null +++ b/ts/choice_command.test.ts @@ -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'); + }); +});