Skip to content

Commit

Permalink
added missing assertText methods to assert cross OS
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Feb 17, 2012
1 parent bc98a5e commit 7762455
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
82 changes: 82 additions & 0 deletions lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php
Expand Up @@ -256,4 +256,86 @@ public function testAssertTextEquals() {
$two = "\nOne\nTwo";
$this->assertTextEquals($one, $two);
}

/**
* test assertTextStartsWith()
*
* @return void
*/
public function testAssertTextStartsWith() {
$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
$stringClean = "some\nstring\nwith\ndifferent\nline endings!";

$this->assertStringStartsWith("some\nstring", $stringDirty);
$this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
$this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);

$this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
$this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
}

/**
* test assertTextStartsNotWith()
*
* @return void
*/
public function testAssertTextStartsNotWith() {
$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
$stringClean = "some\nstring\nwith\ndifferent\nline endings!";

$this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
}

/**
* test assertTextEndsWith()
*
* @return void
*/
public function testAssertTextEndsWith() {
$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
$stringClean = "some\nstring\nwith\ndifferent\nline endings!";

$this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
$this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
}

/**
* test assertTextEndsNotWith()
*
* @return void
*/
public function testAssertTextEndsNotWith() {
$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
$stringClean = "some\nstring\nwith\ndifferent\nline endings!";

$this->assertStringEndsNotWith("different\nline endings", $stringDirty);
$this->assertTextEndsNotWith("different\rline endings", $stringDirty);
}

/**
* test assertTextContains()
*
* @return void
*/
public function testAssertTextContains() {
$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
$stringClean = "some\nstring\nwith\ndifferent\nline endings!";

$this->assertContains("different", $stringDirty);
$this->assertNotContains("different\rline", $stringDirty);

$this->assertTextContains("different\rline", $stringDirty);
}

/**
* test assertTextNotContains()
*
* @return void
*/
public function testAssertTextNotContains() {
$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
$stringClean = "some\nstring\nwith\ndifferent\nline endings!";

$this->assertTextNotContains("different\rlines", $stringDirty);
}
}
94 changes: 94 additions & 0 deletions lib/Cake/TestSuite/CakeTestCase.php
Expand Up @@ -199,6 +199,7 @@ public function loadFixtures() {
* @param string $expected The expected value.
* @param string $result The actual value.
* @param message The message to use for failure.
* @return boolean
*/
public function assertTextNotEquals($expected, $result, $message = '') {
$expected = str_replace(array("\r\n", "\r"), "\n", $expected);
Expand All @@ -213,13 +214,106 @@ public function assertTextNotEquals($expected, $result, $message = '') {
* @param string $expected The expected value.
* @param string $result The actual value.
* @param message The message to use for failure.
* @return boolean
*/
public function assertTextEquals($expected, $result, $message = '') {
$expected = str_replace(array("\r\n", "\r"), "\n", $expected);
$result = str_replace(array("\r\n", "\r"), "\n", $result);
return $this->assertEquals($expected, $result, $message);
}

/**
* Asserts that a string starts with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $prefix
* @param string $string
* @param string $message
* @return boolean
*/
public function assertTextStartsWith($prefix, $string, $message = '') {
$prefix = str_replace(array("\r\n", "\r"), "\n", $prefix);
$string = str_replace(array("\r\n", "\r"), "\n", $string);
return $this->assertStringStartsWith($prefix, $string, $message);
}

/**
* Asserts that a string starts not with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $prefix
* @param string $string
* @param string $message
* @return boolean
*/
public function assertTextStartsNotWith($prefix, $string, $message = '') {
$prefix = str_replace(array("\r\n", "\r"), "\n", $prefix);
$string = str_replace(array("\r\n", "\r"), "\n", $string);
return $this->assertStringStartsNotWith($prefix, $string, $message);
}

/**
* Asserts that a string ends with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $suffix
* @param string $string
* @param string $message
* @return boolean
*/
public function assertTextEndsWith($suffix, $string, $message = '') {
$suffix = str_replace(array("\r\n", "\r"), "\n", $suffix);
$string = str_replace(array("\r\n", "\r"), "\n", $string);
return $this->assertStringEndsWith($suffix, $string, $message);
}

/**
* Asserts that a string ends not with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $suffix
* @param string $string
* @param string $message
* @return boolean
*/
public function assertTextEndsNotWith($suffix, $string, $message = '') {
$suffix = str_replace(array("\r\n", "\r"), "\n", $suffix);
$string = str_replace(array("\r\n", "\r"), "\n", $string);
return $this->assertStringEndsNotWith($suffix, $string, $message);
}

/**
* Assert that a string contains another string, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $needle
* @param string $haystack
* @param string $message
* @param boolean $ignoreCase
* @return boolean
*/
public function assertTextContains($needle, $haystack, $message = '', $ignoreCase = false) {
$needle = str_replace(array("\r\n", "\r"), "\n", $needle);
$haystack = str_replace(array("\r\n", "\r"), "\n", $haystack);
return $this->assertContains($needle, $haystack, $message, $ignoreCase);
}

/**
* Assert that a text doesn't contain another text, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $needle
* @param string $haystack
* @param string $message
* @param boolean $ignoreCase
* @return boolean
*/
public function assertTextNotContains($needle, $haystack, $message = '', $ignoreCase = false) {
$needle = str_replace(array("\r\n", "\r"), "\n", $needle);
$haystack = str_replace(array("\r\n", "\r"), "\n", $haystack);
return $this->assertNotContains($needle, $haystack, $message, $ignoreCase);
}

/**
* Takes an array $expected and generates a regex from it to match the provided $string.
* Samples for $expected:
Expand Down

0 comments on commit 7762455

Please sign in to comment.