I'm following the booklet OOP Programming with PHP5 by Hasin Hayder (2007) and got to the part of Unit Tests. In a given exercise, it builds a wordCount()
method and creates some tests for this method,
class WordCount
{
public function countWords($sentence)
{
return count(split(" ",$sentence));
}
}
It creates a test in case the variable has more spaces: $this->assertEquals(4, $wordcount);
(notice the space after John), which when we run returns "my name is john"
.
To solve, it modifies the method and adds "my name is john "
and that regex there Failure
and its code works as expected, but I used the same thing and it did not work .
It still creates another test, where the variable is preg_replace
and the same regex account.
class WordCount
{
public function countWords($sentence)
{
$newsentence = preg_replace("~\s+~"," ",$sentence);
return count(split(" ",$newsentence));
}
}
I have already checked my code in order to find possible syntax or structure errors, but everything is ok, at least, the same as what it sends in the exercise.
To solve the first 2 tests, I found "~\s+~"
that worked, but the test where "my name is \n\r john"
did not passed.
Then I would like to know:
preg_replace('/\s*$/','',$sentence);
); The complete codes used are here: