@@ -157,4 +157,89 @@ public function consumeIfComesUpdatesLineNumber(): void
157157
158158 self ::assertSame (2 , $ subject ->currentLine ());
159159 }
160+
161+ /**
162+ * @return array<non-empty-string, array{0: string, 1: string}>
163+ */
164+ public static function provideWhitespaceMaybeWithCommentsAndExpectedConsumption (): array
165+ {
166+ return [
167+ 'nothing ' => ['' , '' ],
168+ 'space ' => [' ' , ' ' ],
169+ 'tab ' => ["\t" , "\t" ],
170+ 'line feed ' => ["\n" , "\n" ],
171+ 'carriage return ' => ["\r" , "\r" ],
172+ 'two spaces ' => [' ' , ' ' ],
173+ 'comment ' => ['/*hello*/ ' , '' ],
174+ 'comment with space to the left ' => [' /*hello*/ ' , ' ' ],
175+ 'comment with space to the right ' => ['/*hello*/ ' , ' ' ],
176+ 'two comments ' => ['/*hello*//*bye*/ ' , '' ],
177+ 'two comments with space between ' => ['/*hello*/ /*bye*/ ' , ' ' ],
178+ 'two comments with line feed between ' => ["/*hello*/ \n/*bye*/ " , "\n" ],
179+ ];
180+ }
181+
182+ /**
183+ * @test
184+ *
185+ * @dataProvider provideWhitespaceMaybeWithCommentsAndExpectedConsumption
186+ */
187+ public function consumeWhiteSpaceReturnsTheConsumed (
188+ string $ whitespaceMaybeWithComments ,
189+ string $ expectedConsumption
190+ ): void {
191+ $ subject = new ParserState ($ whitespaceMaybeWithComments , Settings::create ());
192+
193+ $ result = $ subject ->consumeWhiteSpace ();
194+
195+ self ::assertSame ($ expectedConsumption , $ result );
196+ }
197+
198+ /**
199+ * @test
200+ */
201+ public function consumeWhiteSpaceExtractsComment (): void
202+ {
203+ $ commentText = 'Did they get you to trade your heroes for ghosts? ' ;
204+ $ subject = new ParserState ('/* ' . $ commentText . '*/ ' , Settings::create ());
205+
206+ $ result = [];
207+ $ subject ->consumeWhiteSpace ($ result );
208+
209+ self ::assertInstanceOf (Comment::class, $ result [0 ]);
210+ self ::assertSame ($ commentText , $ result [0 ]->getComment ());
211+ }
212+
213+ /**
214+ * @test
215+ */
216+ public function consumeWhiteSpaceExtractsTwoComments (): void
217+ {
218+ $ commentText1 = 'Hot ashes for trees? Hot air for a cool breeze? ' ;
219+ $ commentText2 = 'Cold comfort for change? Did you exchange ' ;
220+ $ subject = new ParserState ('/* ' . $ commentText1 . '*//* ' . $ commentText2 . '*/ ' , Settings::create ());
221+
222+ $ result = [];
223+ $ subject ->consumeWhiteSpace ($ result );
224+
225+ self ::assertInstanceOf (Comment::class, $ result [0 ]);
226+ self ::assertSame ($ commentText1 , $ result [0 ]->getComment ());
227+ self ::assertInstanceOf (Comment::class, $ result [1 ]);
228+ self ::assertSame ($ commentText2 , $ result [1 ]->getComment ());
229+ }
230+
231+ /**
232+ * @test
233+ */
234+ public function consumeWhiteSpaceExtractsCommentWithSurroundingWhitespace (): void
235+ {
236+ $ commentText = 'A walk-on part in the war for a lead role in a cage? ' ;
237+ $ subject = new ParserState (' /* ' . $ commentText . '*/ ' , Settings::create ());
238+
239+ $ result = [];
240+ $ subject ->consumeWhiteSpace ($ result );
241+
242+ self ::assertInstanceOf (Comment::class, $ result [0 ]);
243+ self ::assertSame ($ commentText , $ result [0 ]->getComment ());
244+ }
160245}
0 commit comments