Просмотр исходного кода

Moved asprintf() to utilities file

Taddeus Kroes 13 лет назад
Родитель
Сommit
b91f9709b6
4 измененных файлов с 30 добавлено и 30 удалено
  1. 0 21
      base.php
  2. 0 8
      tests/test_base.php
  3. 9 1
      tests/test_utils.php
  4. 21 0
      utils.php

+ 0 - 21
base.php

@@ -120,25 +120,4 @@ interface Singleton {
 	public static function getInstance();
 	public static function getInstance();
 }
 }
 
 
-/**
- * Format a string using parameters in an associative array.
- * 
- * <code>
- * echo asprintf('foo %(bar)', array('bar' => 'baz'));  // prints 'foo baz'
- * </code>
- * 
- * @param string $format The string to format.
- * @param array $params An associative array with parameters that are used in $format.
- * @package WebBasics
- */
-function asprintf($format, array $params) {
-	return preg_replace_callback(
-		'/%\(([a-z0-9-_ ]*)\)/i',
-		function($matches) use ($params) {
-			return (string)$params[$matches[1]];
-		},
-		$format
-	);
-}
-
 ?>
 ?>

+ 0 - 8
tests/test_base.php

@@ -15,14 +15,6 @@ class BaseTest extends PHPUnit_Framework_TestCase {
 		$this->assertEquals(BaseExtension::create('a', 'b'), new BaseExtension('a', 'b'));
 		$this->assertEquals(BaseExtension::create('a', 'b'), new BaseExtension('a', 'b'));
 	}
 	}
 	
 	
-	function testAsprintf() {
-		$this->assertEquals(webbasics\asprintf('%(foo) baz', array('foo' => 'bar')), 'bar baz');
-		$this->assertEquals(webbasics\asprintf('%(foo) baz %(foo)',
-			array('foo' => 'bar')), 'bar baz bar');
-		$this->assertEquals(webbasics\asprintf('%(bar) baz %(foo)',
-			array('foo' => 'bar', 'bar' => 'foobar')), 'foobar baz bar');
-	}
-	
 	function testPathWithSlash() {
 	function testPathWithSlash() {
 		$this->assertEquals(Base::pathWithSlash('dirname'), 'dirname/');
 		$this->assertEquals(Base::pathWithSlash('dirname'), 'dirname/');
 		$this->assertEquals(Base::pathWithSlash('dirname/'), 'dirname/');
 		$this->assertEquals(Base::pathWithSlash('dirname/'), 'dirname/');

+ 9 - 1
tests/test_utils.php

@@ -4,7 +4,15 @@ require_once 'utils.php';
 use webbasics as wb;
 use webbasics as wb;
 
 
 class UtilsTest extends PHPUnit_Framework_TestCase {
 class UtilsTest extends PHPUnit_Framework_TestCase {
-	function testCamelize() {
+	function testAsprintf() {
+		$this->assertEquals(webbasics\asprintf('%(foo) baz', array('foo' => 'bar')), 'bar baz');
+		$this->assertEquals(webbasics\asprintf('%(foo) baz %(foo)',
+			array('foo' => 'bar')), 'bar baz bar');
+		$this->assertEquals(webbasics\asprintf('%(bar) baz %(foo)',
+			array('foo' => 'bar', 'bar' => 'foobar')), 'foobar baz bar');
+	}
+	
+	function testCamelizeSimple() {
 		$this->assertEquals('fooBarBaz', wb\camelize('foo bar baz'));
 		$this->assertEquals('fooBarBaz', wb\camelize('foo bar baz'));
 		$this->assertEquals('fooBarBaz', wb\camelize('foo_bar_baz'));
 		$this->assertEquals('fooBarBaz', wb\camelize('foo_bar_baz'));
 		$this->assertEquals('fooBarBaz', wb\camelize('foo-bar-baz'));
 		$this->assertEquals('fooBarBaz', wb\camelize('foo-bar-baz'));

+ 21 - 0
utils.php

@@ -9,6 +9,27 @@
 
 
 namespace webbasics;
 namespace webbasics;
 
 
+/**
+ * Format a string using parameters in an associative array.
+ * 
+ * <code>
+ * echo asprintf('foo %(bar)', array('bar' => 'baz'));  // prints 'foo baz'
+ * </code>
+ * 
+ * @param string $format The string to format.
+ * @param array $params An associative array with parameters that are used in $format.
+ * @package WebBasics
+ */
+function asprintf($format, array $params) {
+	return preg_replace_callback(
+		'/%\(([a-z0-9-_ ]*)\)/i',
+		function($matches) use ($params) {
+			return (string)$params[$matches[1]];
+		},
+		$format
+	);
+}
+
 /**
 /**
  * Camelize a string.
  * Camelize a string.
  * 
  *