@@ -29,7 +29,7 @@ describe('DiceInterpreter', () => {
2929 expect ( interpreter . evaluate ( exp , errors ) ) . toBe ( 21 ) ;
3030 expect ( dice . getChildCount ( ) ) . toBe ( 4 ) ;
3131 } ) ;
32- it ( 'evaluates a rerolling dice (4d6ro<3).' , ( ) => {
32+ it ( 'evaluates rerolling once dice (4d6ro<3).' , ( ) => {
3333 const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
3434 . setAttribute ( 'once' , true ) ;
3535
@@ -54,7 +54,7 @@ describe('DiceInterpreter', () => {
5454 expect ( interpreter . evaluate ( exp , errors ) ) . toBe ( 18 ) ;
5555 expect ( dice . getChildCount ( ) ) . toBe ( 4 ) ;
5656 } ) ;
57- it ( 'evaluates rerolling dice (4d6r).' , ( ) => {
57+ it ( 'evaluates rerolling dice no condition (4d6r).' , ( ) => {
5858 const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
5959 . setAttribute ( 'once' , false ) ;
6060
@@ -74,7 +74,7 @@ describe('DiceInterpreter', () => {
7474 expect ( interpreter . evaluate ( exp , errors ) ) . toBe ( 21 ) ;
7575 expect ( dice . getChildCount ( ) ) . toBe ( 4 ) ;
7676 } ) ;
77- it ( 'evaluates a rerolling dice no condition (4d6ro).' , ( ) => {
77+ it ( 'evaluates a rerolling once dice no condition (4d6ro).' , ( ) => {
7878 const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
7979 . setAttribute ( 'once' , true ) ;
8080
@@ -94,6 +94,56 @@ describe('DiceInterpreter', () => {
9494 expect ( interpreter . evaluate ( exp , errors ) ) . toBe ( 15 ) ;
9595 expect ( dice . getChildCount ( ) ) . toBe ( 4 ) ;
9696 } ) ;
97+ it ( 'evaluates rerolling dice equal condition (4d6r=3).' , ( ) => {
98+ const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
99+ . setAttribute ( 'once' , false ) ;
100+
101+ const dice = Ast . Factory . create ( Ast . NodeType . Dice ) ;
102+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 4 ) ) ;
103+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 6 ) ) ;
104+
105+ const equal = Ast . Factory . create ( Ast . NodeType . Equal ) ;
106+ equal . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 3 ) ) ;
107+
108+ exp . addChild ( dice ) ;
109+ exp . addChild ( equal ) ;
110+
111+ const mockList = new MockListRandomProvider ( ) ;
112+ mockList . numbers . push (
113+ 6 , 5 , 6 , 3 , // This 3 should get re-rolled into a 1
114+ 1 // Lowest dice faces (1) should not be automatically rerolled
115+ ) ;
116+
117+ const interpreter = new Interpreter . DiceInterpreter ( null , mockList ) ;
118+ const errors : Interpreter . InterpreterError [ ] = [ ] ;
119+ expect ( interpreter . evaluate ( exp , errors ) ) . toBe ( 18 ) ;
120+ expect ( dice . getChildCount ( ) ) . toBe ( 4 ) ;
121+ } ) ;
122+ it ( 'evaluates not rerolling smallest dice value if specified a condition (4d6r>=5).' , ( ) => {
123+ const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
124+ . setAttribute ( 'once' , false ) ;
125+
126+ const dice = Ast . Factory . create ( Ast . NodeType . Dice ) ;
127+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 4 ) ) ;
128+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 6 ) ) ;
129+
130+ const greater = Ast . Factory . create ( Ast . NodeType . GreaterOrEqual ) ;
131+ greater . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 6 ) ) ;
132+
133+ exp . addChild ( dice ) ;
134+ exp . addChild ( greater ) ;
135+
136+ const mockList = new MockListRandomProvider ( ) ;
137+ mockList . numbers . push (
138+ 2 , 4 , 3 , 6 , // This 6 should get re-rolled into a 1
139+ 1 // Lowest dice faces (1) should not be automatically rerolled
140+ ) ;
141+
142+ const interpreter = new Interpreter . DiceInterpreter ( null , mockList ) ;
143+ const errors : Interpreter . InterpreterError [ ] = [ ] ;
144+ expect ( interpreter . evaluate ( exp , errors ) ) . toBe ( 10 ) ;
145+ expect ( dice . getChildCount ( ) ) . toBe ( 4 ) ;
146+ } ) ;
97147 it ( 'errors on an invalid condition (4d6ro[dice]).' , ( ) => {
98148 const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
99149 . setAttribute ( 'once' , true ) ;
@@ -112,6 +162,72 @@ describe('DiceInterpreter', () => {
112162 const mockList = new MockListRandomProvider ( ) ;
113163 mockList . numbers . push ( 6 , 5 , 6 , 2 , 1 ) ;
114164
165+ const interpreter = new Interpreter . DiceInterpreter ( null , mockList ) ;
166+ const errors : Interpreter . InterpreterError [ ] = [ ] ;
167+ interpreter . evaluate ( exp , errors ) ;
168+ expect ( errors . length ) . toBeGreaterThanOrEqual ( 1 ) ;
169+ } ) ;
170+ it ( 'errors if condition includes all dice faces (4d6r<7).' , ( ) => {
171+ const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
172+ . setAttribute ( 'once' , false ) ;
173+
174+ const dice = Ast . Factory . create ( Ast . NodeType . Dice ) ;
175+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 4 ) ) ;
176+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 6 ) ) ;
177+
178+ const less = Ast . Factory . create ( Ast . NodeType . Less ) ;
179+ less . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 7 ) ) ;
180+
181+ exp . addChild ( dice ) ;
182+ exp . addChild ( less ) ;
183+
184+ const mockList = new MockListRandomProvider ( ) ;
185+ mockList . numbers . push ( 3 , 4 , 2 , 1 ) ;
186+
187+ const interpreter = new Interpreter . DiceInterpreter ( null , mockList ) ;
188+ const errors : Interpreter . InterpreterError [ ] = [ ] ;
189+ interpreter . evaluate ( exp , errors ) ;
190+ expect ( errors . length ) . toBeGreaterThanOrEqual ( 1 ) ;
191+ } ) ;
192+ it ( 'errors if condition includes all dice faces (4d6r>=1).' , ( ) => {
193+ const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
194+ . setAttribute ( 'once' , false ) ;
195+
196+ const dice = Ast . Factory . create ( Ast . NodeType . Dice ) ;
197+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 4 ) ) ;
198+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 6 ) ) ;
199+
200+ const moreOrEqual = Ast . Factory . create ( Ast . NodeType . GreaterOrEqual ) ;
201+ moreOrEqual . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 1 ) ) ;
202+
203+ exp . addChild ( dice ) ;
204+ exp . addChild ( moreOrEqual ) ;
205+
206+ const mockList = new MockListRandomProvider ( ) ;
207+ mockList . numbers . push ( 3 , 4 , 2 , 1 ) ;
208+
209+ const interpreter = new Interpreter . DiceInterpreter ( null , mockList ) ;
210+ const errors : Interpreter . InterpreterError [ ] = [ ] ;
211+ interpreter . evaluate ( exp , errors ) ;
212+ expect ( errors . length ) . toBeGreaterThanOrEqual ( 1 ) ;
213+ } ) ;
214+ it ( 'errors if condition includes all dice faces (4d1r=1).' , ( ) => {
215+ const exp = Ast . Factory . create ( Ast . NodeType . Reroll )
216+ . setAttribute ( 'once' , false ) ;
217+
218+ const dice = Ast . Factory . create ( Ast . NodeType . Dice ) ;
219+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 4 ) ) ;
220+ dice . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 1 ) ) ;
221+
222+ const equal = Ast . Factory . create ( Ast . NodeType . Equal ) ;
223+ equal . addChild ( Ast . Factory . create ( Ast . NodeType . Number ) . setAttribute ( 'value' , 1 ) ) ;
224+
225+ exp . addChild ( dice ) ;
226+ exp . addChild ( equal ) ;
227+
228+ const mockList = new MockListRandomProvider ( ) ;
229+ mockList . numbers . push ( 3 , 4 , 2 , 1 ) ;
230+
115231 const interpreter = new Interpreter . DiceInterpreter ( null , mockList ) ;
116232 const errors : Interpreter . InterpreterError [ ] = [ ] ;
117233 interpreter . evaluate ( exp , errors ) ;
0 commit comments