diff --git a/src/parser/grammars/grammar.pegjs b/src/parser/grammars/grammar.pegjs index 52e0fc1d..5099e652 100644 --- a/src/parser/grammars/grammar.pegjs +++ b/src/parser/grammars/grammar.pegjs @@ -197,6 +197,13 @@ Number Operator = "**" { return "^" } / "*" / "^" / "%" / "/" / "+" / "-" + / "?" _ expr:Expression _ ":" { + return [ + '?', + ...expr, + ":", + ]; + } _ "whitespace" diff --git a/tests/parser/Parser.test.js b/tests/parser/Parser.test.js index f0aca376..5d39fdbc 100644 --- a/tests/parser/Parser.test.js +++ b/tests/parser/Parser.test.js @@ -1743,5 +1743,19 @@ describe('Parser', () => { expect(parsed[2]).toBe(0); }); }); + describe('Conditional Operator', () => { + test('roll `1?5:0', () => { + const parsed = Parser.parse('1?5:0'); + + expect(parsed).toBeInstanceOf(Array); + expect(parsed).toHaveLength(5); + + expect(parsed[0]).toBe(1); + expect(parsed[1]).toEqual('?'); + expect(parsed[2]).toBe(5); + expect(parsed[3]).toEqual(':'); + expect(parsed[4]).toBe(0); + }); + }); }); }); diff --git a/tests/rolling.test.js b/tests/rolling.test.js index 87456d32..1ea055ee 100644 --- a/tests/rolling.test.js +++ b/tests/rolling.test.js @@ -399,6 +399,34 @@ describe('Rolling', () => { // remove the spy spy.mockRestore(); }); + + test('roll `(d6>3)?1:0', () => { + const rolls = new RollResults([4]); + const spy = jest.spyOn(StandardDice.prototype, 'roll') + .mockImplementationOnce(() => rolls); + const roll = roller.roll('(d6>3)?1:0'); + + expect(roll).toBeInstanceOf(DiceRoll); + expect(roll.notation).toEqual('(d6>3)?1:0'); + + expect(roll.rolls).toHaveLength(7); + expect(roll.rolls[0]).toEqual('('); + expect(roll.rolls[1]).toEqual(rolls); + expect(roll.rolls[2]).toEqual(')'); + expect(roll.rolls[3]).toEqual('?'); + expect(roll.rolls[4]).toBe(1); + expect(roll.rolls[5]).toEqual(':'); + expect(roll.rolls[6]).toBe(0); + + expect(roll.total).toBe(1); + expect(roll.output).toEqual('(d6>3)?1:0: ([4])?1:0 = 1'); + expect(roll.minTotal).toEqual(0); + expect(roll.maxTotal).toEqual(1); + expect(roll.averageTotal).toEqual(0.5); + + // remove the spy + spy.mockRestore(); + }); }); describe('Group rolls', () => {