test expand and add (!...)
authorMaxime Bourget <bmx007@gmail.com>
Sat, 25 May 2013 19:37:03 +0000 (20:37 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Sat, 25 May 2013 19:37:03 +0000 (20:37 +0100)
includes/utilities.inc
test/expandTest.php [new file with mode: 0644]

index 54cbcc5780d8e6de3a960a05eacc14e172286a8a..d0319d6dcdc816d6ad6eee0aed0d9b90a56541d6 100644 (file)
@@ -51,13 +51,19 @@ function expand_template($template, $value, $default_value=null) {
     $value = str_replace('@', $default_value, $value); 
   #echo "    default template=$template value=$value default_value=$default_value<br/>";
 
-      #evaluate expression 
+      #evaluate arithmtic expression 
       if(preg_match('/^\((.*)\)$/', $value, $matches)) {
-        // keep only arithmetical expression
-        $to_eval =  preg_replace('/[a-zA-Z]*/', "", $matches[1]);
-        #echo "eval trimmed '$matches[1]' => '$to_eval' <br/>";
-        eval("\$value=$to_eval;");
-  #echo "replaced template=$template value=$value default_value=$default_value<br/>";
+                               $expression = $matches[1];
+                               if(preg_match('/^!(.*)/', $expression, $matches)) {
+                                       $value = $matches[1];
+                               }
+                               else {
+                                       // keep only arithmetical expression
+                                       $to_eval =  preg_replace('/[a-zA-Z]*/', "", $expression);
+                                       #echo "eval trimmed '$matches[1]' => '$to_eval' <br/>";
+                                       eval("\$value=$to_eval;");
+                                       #echo "replaced template=$template value=$value default_value=$default_value<br/>";
+                                }
       }
     return $value;
   }
diff --git a/test/expandTest.php b/test/expandTest.php
new file mode 100644 (file)
index 0000000..9dc3fa3
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+//require_once('test/helper.php');
+require_once('includes/utilities.inc');
+
+class ExpandTest extends PHPUnit_Framework_TestCase {
+       /**
+        * @group template
+        * @dataProvider expandables
+        */
+       public function testExanpansion($template, $value, $default, $result) {
+               $this->assertEquals(expand_template($template, $value, $default), $result);
+       }
+
+       public function expandables() {
+               return array(
+                       array(null, '5', null, '5')  /** normal **/
+                       ,array('2', null, null, '2')  /** use template **/
+
+                       ,array('10', '5', null, '5') /** value override template one **/
+                       ,array('-#-', '5', 'default', '-5-') /** but not if # in it */
+                       ,array('-@-', '5', 'default', '5') /** value override if formulat use default */
+                       ,array('#-@', '5', 'default', '5-default') /** def can be use in template formula **/
+                       ,array('-@-', null, 'default', '-default-')
+
+                       ,array('-#-', '<<@>>', 'default', '-<<default>>-') /** ???? */
+
+                       ,array('10', ':#:', null, ':10:') /** use template in value formula **/
+                       ,array('10', ':#:@', 'default', ':10:default') /** use template  and default in value formula **/
+
+                       ,array('(10)', '5', null, '10') /** template formula win **/
+                       ,array('(@)', '5', '2+7', '9') /** template formula win **/
+                       ,array('(!@)', '5', '2+7', '2+7') /** don't evaluate the expression **/
+
+       
+               );
+       }
+       
+}
+?>