Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added missing assertText methods to assert cross OS #486

Merged
merged 1 commit into from

2 participants

Mark S. Mark Story
Mark S.
Collaborator

@see
http://cakephp.lighthouseapp.com/projects/42648/tickets/2148-test-cases-fail-on-windows#ticket-2148-29

I added those methods to assert text cross OS:

  • assertTextStarts(Not)With
  • assertTextEnds(Not)With
  • assertText(Not)Contains
Mark Story
Owner

These seem reasonable to me.

Mark Story markstory merged commit 9762824 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 17, 2012
  1. Mark S.
This page is out of date. Refresh to see the latest.
82 lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php
View
@@ -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 lib/Cake/TestSuite/CakeTestCase.php
View
@@ -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);
@@ -213,6 +214,7 @@ 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);
@@ -221,6 +223,98 @@ public function assertTextEquals($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:
*
Something went wrong with that request. Please try again.