@@ -157,4 +157,105 @@ 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 provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption (): 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 provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption
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+ * @return array<non-empty-string, array{0: non-empty-string}>
233+ */
234+ public static function provideWhitespace (): array
235+ {
236+ return [
237+ 'space ' => [' ' ],
238+ 'tab ' => ["\t" ],
239+ 'line feed ' => ["\n" ],
240+ 'carriage return ' => ["\r" ],
241+ 'two spaces ' => [' ' ],
242+ ];
243+ }
244+
245+ /**
246+ * @test
247+ *
248+ * @dataProvider provideWhitespace
249+ */
250+ public function consumeWhiteSpaceExtractsCommentWithSurroundingWhitespace (string $ whitespace ): void
251+ {
252+ $ commentText = 'A walk-on part in the war for a lead role in a cage? ' ;
253+ $ subject = new ParserState ($ whitespace . '/* ' . $ commentText . '*/ ' . $ whitespace , Settings::create ());
254+
255+ $ result = [];
256+ $ subject ->consumeWhiteSpace ($ result );
257+
258+ self ::assertInstanceOf (Comment::class, $ result [0 ]);
259+ self ::assertSame ($ commentText , $ result [0 ]->getComment ());
260+ }
160261}
0 commit comments