logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject svn commit: r1187885 - in /logging/log4php/trunk/src: main/php/helpers/LoggerOptionConverter.php test/php/helpers/LoggerOptionConverterTest.php
Date Sun, 23 Oct 2011 14:06:10 GMT
Author: ihabunek
Date: Sun Oct 23 14:06:10 2011
New Revision: 1187885

URL: http://svn.apache.org/viewvc?rev=1187885&view=rev
Log:
Improved the new conversion methods, fixed typo, more tests.

Modified:
    logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
    logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php

Modified: logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php?rev=1187885&r1=1187884&r2=1187885&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php (original)
+++ logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php Sun Oct 23 14:06:10
2011
@@ -98,6 +98,9 @@ class LoggerOptionConverter {
 	/** Converts $value to boolean, or throws an exception if not possible. */
 	public static function toBooleanEx($value) {
 		if (isset($value)) {
+			if (is_bool($value)) {
+				return $value;
+			}
 			$value = strtolower(trim($value));
 			if (in_array($value, self::$trueValues)) {
 				return true;
@@ -107,7 +110,7 @@ class LoggerOptionConverter {
 			}
 		}
 		
-		throw new LoggerException("Givan value [" . var_export($value, true) . "] cannot be converted
to boolean.");
+		throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted
to boolean.");
 	}
 	
 	/**
@@ -127,13 +130,14 @@ class LoggerOptionConverter {
 	
 	/** Converts $value to integer, or throws an exception if not possible. */
 	public static function toIntegerEx($value) {
-		if (!empty($value)) {
-			if (is_numeric($value)) {
-				return (integer) $value;
-			}
+		if (is_integer($value)) {
+			return $value;
+		}
+		if (is_numeric($value)) {
+			return (integer) $value;
 		}
 	
-		throw new LoggerException("Givan value [" . var_export($value, true) . "] cannot be converted
to integer.");
+		throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted
to integer.");
 	}
 
 	/**

Modified: logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php?rev=1187885&r1=1187884&r2=1187885&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php (original)
+++ logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php Sun Oct 23 14:06:10
2011
@@ -52,12 +52,16 @@ class LoggerOptionConverterTest extends 
         self::assertFalse(LoggerOptionConverter::toBoolean("off"));
         self::assertFalse(LoggerOptionConverter::toBoolean("no"));
         
+        self::assertTrue(LoggerOptionConverter::toBooleanEx(1));
         self::assertTrue(LoggerOptionConverter::toBooleanEx("1"));
+        self::assertTrue(LoggerOptionConverter::toBooleanEx(true));
         self::assertTrue(LoggerOptionConverter::toBooleanEx("true"));
         self::assertTrue(LoggerOptionConverter::toBooleanEx("on"));
         self::assertTrue(LoggerOptionConverter::toBooleanEx("yes"));
         
+        self::assertFalse(LoggerOptionConverter::toBooleanEx(0));
         self::assertFalse(LoggerOptionConverter::toBooleanEx("0"));
+        self::assertFalse(LoggerOptionConverter::toBooleanEx(false));
         self::assertFalse(LoggerOptionConverter::toBooleanEx("false"));
         self::assertFalse(LoggerOptionConverter::toBooleanEx("off"));
         self::assertFalse(LoggerOptionConverter::toBooleanEx("no"));
@@ -66,7 +70,7 @@ class LoggerOptionConverterTest extends 
     /**
      * Test fail on NULL. 
  	 * @expectedException LoggerException
- 	 * @expectedExceptionMessage Givan value [NULL] cannot be converted to boolean.
+ 	 * @expectedExceptionMessage Given value [NULL] cannot be converted to boolean.
      */
     public function testToBooleanFailure1() {
     	LoggerOptionConverter::toBooleanEx(null);
@@ -75,7 +79,7 @@ class LoggerOptionConverterTest extends 
     /**
      * Test fail on empty string.
      * @expectedException LoggerException
-     * @expectedExceptionMessage Givan value [''] cannot be converted to boolean.
+     * @expectedExceptionMessage Given value [''] cannot be converted to boolean.
      */
     public function testToBooleanFailure2() {
     	LoggerOptionConverter::toBooleanEx('');
@@ -84,7 +88,7 @@ class LoggerOptionConverterTest extends 
     /**
      * Test fail on invalid string.
      * @expectedException LoggerException
-     * @expectedExceptionMessage Givan value ['foo'] cannot be converted to boolean.
+     * @expectedExceptionMessage Given value ['foo'] cannot be converted to boolean.
      */
     public function testToBooleanFailure3() {
     	LoggerOptionConverter::toBooleanEx('foo');
@@ -98,12 +102,17 @@ class LoggerOptionConverterTest extends 
     	self::assertSame(-10, LoggerOptionConverter::toInt('foo', -10));
     	
     	self::assertSame(1, LoggerOptionConverter::toIntegerEx('1'));
+    	self::assertSame(1, LoggerOptionConverter::toIntegerEx(1));
+    	self::assertSame(0, LoggerOptionConverter::toIntegerEx('0'));
+    	self::assertSame(0, LoggerOptionConverter::toIntegerEx(0));
+    	self::assertSame(-1, LoggerOptionConverter::toIntegerEx('-1'));
+    	self::assertSame(-1, LoggerOptionConverter::toIntegerEx(-1));
     }
     
     /**
     * Test fail on NULL.
     * @expectedException LoggerException
-    * @expectedExceptionMessage Givan value [NULL] cannot be converted to integer.
+    * @expectedExceptionMessage Given value [NULL] cannot be converted to integer.
     */
     public function testToIntegerFailure1() {
     	LoggerOptionConverter::toIntegerEx(null);
@@ -112,7 +121,7 @@ class LoggerOptionConverterTest extends 
     /**
      * Test fail on empty string.
      * @expectedException LoggerException
-     * @expectedExceptionMessage Givan value [''] cannot be converted to integer.
+     * @expectedExceptionMessage Given value [''] cannot be converted to integer.
      */
     public function testToIntegerFailure2() {
     	LoggerOptionConverter::toIntegerEx('');
@@ -121,12 +130,30 @@ class LoggerOptionConverterTest extends 
     /**
      * Test fail on invalid string.
      * @expectedException LoggerException
-     * @expectedExceptionMessage Givan value ['foo'] cannot be converted to integer.
+     * @expectedExceptionMessage Given value ['foo'] cannot be converted to integer.
      */
     public function testToIntegerFailure3() {
     	LoggerOptionConverter::toIntegerEx('foo');
     }
     
+    /**
+     * Test fail on boolean.
+     * @expectedException LoggerException
+     * @expectedExceptionMessage Given value [true] cannot be converted to integer.
+     */
+    public function testToIntegerFailure4() {
+    	LoggerOptionConverter::toIntegerEx(true);
+    }
+
+    /**
+     * Test fail on boolean.
+     * @expectedException LoggerException
+     * @expectedExceptionMessage Given value [false] cannot be converted to integer.
+     */
+    public function testToIntegerFailure5() {
+    	LoggerOptionConverter::toIntegerEx(false);
+    }
+    
     public function testSubstituteVars() {
     	$props['OTHER_CONSTANT'] = "OTHER";
     	$props['MY_CONSTANT'] = "TEST";



Mime
View raw message