Browse Source

Add assertNotWithinRange() and add tests for both methods.

euromark 11 years ago
parent
commit
60600fd619
2 changed files with 35 additions and 0 deletions
  1. 15 0
      src/TestSuite/TestCase.php
  2. 20 0
      tests/TestCase/TestSuite/TestCaseTest.php

+ 15 - 0
src/TestSuite/TestCase.php

@@ -511,6 +511,21 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
 	}
 
 /**
+ * Compatibility function to test if a value is not between an acceptable range.
+ *
+ * @param float $expected
+ * @param float $result
+ * @param float $margin the rage of acceptation
+ * @param string $message the text to display if the assertion is not correct
+ * @return void
+ */
+	protected static function assertNotWithinRange($expected, $result, $margin, $message = '') {
+		$upper = $result + $margin;
+		$lower = $result - $margin;
+		static::assertTrue((($expected > $upper) || ($expected < $lower)), $message);
+	}
+
+/**
  * Compatibility function to test paths.
  *
  * @param string $expected

+ 20 - 0
tests/TestCase/TestSuite/TestCaseTest.php

@@ -321,6 +321,26 @@ class TestCaseTest extends TestCase {
 	}
 
 /**
+ * test testAssertWithinRange()
+ *
+ * @return void
+ */
+	public function testAssertWithinRange() {
+		$this->assertWithinRange(21, 22, 1, 'Not within range');
+		$this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
+	}
+
+/**
+ * test testAssertNotWithinRange()
+ *
+ * @return void
+ */
+	public function testAssertNotWithinRange() {
+		$this->assertNotWithinRange(21, 23, 1, 'Within range');
+		$this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
+	}
+
+/**
  * test getMockForModel()
  *
  * @return void