logo       
Bookmark and Share

4151 - trunk/Template/tests/scripts [eZComponents: Trunk]: msg#00325

web.ezcomponents.cvs

Subject: 4151 - trunk/Template/tests/scripts [eZComponents: Trunk]

Author: Jan Borsodi
Date: 2006-11-30 14:57:47 +0100 (Thu, 30 Nov 2006)
New Revision: 4151

Log:
- Added support for separate blocks which can be synchronize its index value.
- New permutation test for nested while/foreach and delimiters.

Added:
trunk/Template/tests/scripts/permutation_block_subblock.php
Modified:
trunk/Template/tests/scripts/permutation.php

Modified: trunk/Template/tests/scripts/permutation.php
===================================================================
--- trunk/Template/tests/scripts/permutation.php 2006-11-30 13:57:18 UTC
(rev 4150)
+++ trunk/Template/tests/scripts/permutation.php 2006-11-30 13:57:47 UTC
(rev 4151)
@@ -22,6 +22,8 @@
$this->index = 0;
}

+ abstract public function replace( $from , $to );
+
abstract public function generate();

abstract public function increase();
@@ -88,6 +90,21 @@
$c = strlen( $lines[$i] );
return $c;
}
+
+ static public function replaceList( &$list, $from, $to )
+ {
+ foreach ( $list as $i => $item )
+ {
+ if ( is_object( $item ) )
+ {
+ $item->replace( $from, $to );
+ }
+ else
+ {
+ $list[$i] = str_replace( $from, $to, $item );
+ }
+ }
+ }
}

class ezcTemplatePermutationList extends ezcTemplatePermutation
@@ -99,6 +116,21 @@
$this->indentation = $indentation;
}

+ public function removeIndex( $index )
+ {
+ if ( $index < 0 || $index >= count( $this->list ) )
+ {
+ throw new Exception( "Index $index is out of range [0->" . count(
$this->list ) . "]" );
+ }
+ unset( $this->list[$index] );
+ $this->list = array_values( $this->list );
+ }
+
+ public function replace( $from , $to )
+ {
+ self::replaceList( $this->list, $from, $to );
+ }
+
public function generate()
{
return self::indentBlock( self::generateString( $this->list ),
@@ -131,6 +163,21 @@
}
}

+ public function removeIndex( $index )
+ {
+ if ( $index < 0 || $index >= count( $this->list ) )
+ {
+ throw new Exception( "Index $index is out of range [0->" . count(
$this->list ) . "]" );
+ }
+ unset( $this->list[$index] );
+ $this->list = array_values( $this->list );
+ }
+
+ public function replace( $from , $to )
+ {
+ self::replaceList( $this->list, $from, $to );
+ }
+
public function generate()
{
$p = $this->list[$this->index];
@@ -208,6 +255,10 @@
$this->max = $max;
}

+ public function replace( $from , $to )
+ {
+ }
+
public function generate()
{
return $this->min + $this->index;
@@ -220,6 +271,251 @@
}
}

+class ezcTemplatePermutationIndentChild extends ezcTemplatePermutation
+{
+ public function __construct( ezcTemplatePermutation $child, $indentation )
+ {
+ parent::__construct();
+ $this->child = $child;
+ $this->indentation = $indentation;
+ }
+
+ public function replace( $from , $to )
+ {
+ $this->child->replace( $from, $to );
+ }
+
+ public function generate()
+ {
+ $p = $this->child;
+ if ( is_object( $p ) )
+ {
+ $text = $p->generate();
+ }
+ else
+ {
+ $text = $p;
+ }
+ return self::indentBlock( $text,
+ $this->indentation );
+ }
+
+ public function increase()
+ {
+ $p = $this->child;
+ if ( is_object( $p ) )
+ {
+ return $p->increase();
+ }
+ return false;
+ }
+
+ public function reset()
+ {
+ $p = $this->child;
+ if ( is_object( $p ) )
+ {
+ $p->reset();
+ }
+ }
+}
+
+class ezcTemplatePermutationAlternativeMaster extends ezcTemplatePermutation
+{
+ public function __construct( array $slaves )
+ {
+ parent::__construct();
+ $this->slaves = $slaves;
+ $this->slaveIndex = count( $this->slaves ) - 1;
+ if ( count( $this->slaves ) == 0 )
+ {
+ throw new Exception( "Slave list cannot be empty" );
+ }
+ foreach ( $this->slaves as $i => $slave )
+ {
+ if ( !$slave instanceof ezcTemplatePermutationAlternativeSlave )
+ {
+ throw new Exception( "Slave entry {$i} is not an instance of
ezcTemplatePermutationAlternativeSlave" );
+ }
+ }
+ }
+
+ public function removeIndex( $index )
+ {
+ if ( $index < 0 || $index >= count( $this->slaves ) )
+ {
+ throw new Exception( "Index $index is out of range [0->" . count(
$this->slaves ) . "]" );
+ }
+ unset( $this->slaves[$index] );
+ $this->slaves = array_values( $this->slaves );
+ }
+
+ public function __clone()
+ {
+ foreach ( $this->slaves as $i => $v )
+ {
+ if ( is_object( $v ) )
+ {
+ $this->slaves[$i] = clone $v;
+ }
+ }
+ }
+
+ public function replace( $from , $to )
+ {
+ self::replaceList( $this->slaves, $from, $to );
+ }
+
+ public function generate()
+ {
+// echo "master::generate(): si: {$this->slaveIndex}, sc: " . count(
$this->slaves ) . "\n";
+ $p = $this->slaves[0];
+ if ( is_object( $p ) )
+ {
+ return $p->generate();
+ }
+ return $p;
+ }
+
+ public function increase()
+ {
+// echo "master::increase(): si: {$this->slaveIndex}, sc: " . count(
$this->slaves ) . "\n";
+
+ for ( $i = count( $this->slaves ) - 1; $i >= 0; --$i )
+ {
+ $p = $this->slaves[$i];
+ if ( is_object( $p ) )
+ {
+ if ( $p->increaseSlave() )
+ {
+// echo "master::increase(): increaseSlave() is true\n";
+ return true;
+ }
+// echo "master::increase(): increaseSlave() is false\n";
+ $p->resetSlave();
+ }
+ }
+
+ foreach ( $this->slaves as $slave )
+ {
+ $slave->index++;
+ }
+ $ret = $this->slaves[0]->index < count( $this->slaves[0]->list );
+// echo "master::increase()#3: ret: '$ret', si: {$this->slaveIndex},
sc: " . count( $this->slaves ) . "\n";
+ return $ret;
+
+ }
+
+ public function reset()
+ {
+ foreach ( $this->slaves as $slave )
+ {
+ $slave->resetSlaves();
+ }
+ }
+}
+
+class ezcTemplatePermutationAlternativeSlave extends ezcTemplatePermutation
+{
+ public function __construct( $list )
+ {
+ parent::__construct();
+ $this->list = $list;
+ $this->index = 0;
+ }
+
+ public function __clone()
+ {
+ foreach ( $this->list as $i => $v )
+ {
+ if ( is_object( $v ) )
+ {
+ $this->list[$i] = clone $v;
+ }
+ }
+ }
+
+ public function removeIndex( $index )
+ {
+ if ( $index < 0 || $index >= count( $this->list ) )
+ {
+ throw new Exception( "Index $index is out of range [0->" . count(
$this->list ) . "]" );
+ }
+ unset( $this->list[$index] );
+ $this->list = array_values( $this->list );
+ }
+
+ public function replace( $from , $to )
+ {
+ self::replaceList( $this->list, $from, $to );
+ }
+
+ public function generate()
+ {
+ $p = $this->list[$this->index];
+ if ( is_object( $p ) )
+ {
+ return $p->generate();
+ }
+ return $p;
+ }
+
+ public function increaseSlave()
+ {
+// echo "slave::increaseSlave(): i: {$this->index}\n";
+ $p = $this->list[$this->index];
+ if ( is_object( $p ) )
+ {
+ return $p->increase();
+ }
+ return false;
+ }
+
+ public function increase()
+ {
+ // Slaves do not control the increase(), this is up to the master.
+// echo "slave::increase(): i: {$this->index}\n";
+ return false;
+ }
+
+ public function setIndex( $index )
+ {
+// echo "slave::setIndex( $index )\n";
+ if ( $index >= count( $this->list ) )
+ {
+ throw new Exception( "New index {$index} is out of bounds (" .
count( $this->list ) . ")" );
+ }
+ $this->index = $index;
+ }
+
+ public function resetSlave()
+ {
+ $p = $this->list[$this->index];
+ if ( is_object( $p ) )
+ {
+ return $p->reset();
+ }
+ }
+
+ public function resetSlaves()
+ {
+ foreach ( $this->list as $p )
+ {
+ if ( is_object( $p ) )
+ {
+ return $p->reset();
+ }
+ }
+ $this->index = 0;
+ }
+
+ public function reset()
+ {
+ // Slaves do not control the reset(), this is up to the master.
+ return false;
+ }
+}
+
class ezcTemplatePermutationApp
{
public $outputToFile;
@@ -334,9 +630,26 @@
return new ezcTemplatePermutationAlternative( $args, $indent );
}

+function indent( $child, $indentation )
+{
+ return new ezcTemplatePermutationIndentChild( $child, $indentation );
+}
+
function num( $min = false, $max = false )
{
return new ezcTemplatePermutationNumber( $min, $max );
}

+function altMaster()
+{
+ $slaves = func_get_args();
+ return new ezcTemplatePermutationAlternativeMaster( $slaves );
+}
+
+function altSlave()
+{
+ $list = func_get_args();
+ return new ezcTemplatePermutationAlternativeSlave( $list );
+}
+
?>

Added: trunk/Template/tests/scripts/permutation_block_subblock.php
===================================================================
--- trunk/Template/tests/scripts/permutation_block_subblock.php 2006-11-30
13:57:18 UTC (rev 4150)
+++ trunk/Template/tests/scripts/permutation_block_subblock.php 2006-11-30
13:57:47 UTC (rev 4151)
@@ -0,0 +1,124 @@
+<?php
+
+require_once dirname( __FILE__ ) . "/permutation.php";
+
+// Writes to: regression_tests/foreach/correct/nested_with_delimiter_*.in
+
+$blockStart = altMaster( altSlave( "{\$i = 0}\n{foreach \$foo as \$bar}\n",
+ "{\$num = 0}{\$i = 0}\n{while
\$num<3}\n{\$bar = \$num}\n{\$num++}\n",
+ "{\$i = 0}\n{if \$foo[1]}\n"
+ ),
+ altSlave( "{++\$i}\n{/foreach}\n",
+ "{++\$i}\n{/while}\n",
+ "{/if}\n"
+ ) );
+$blockEnd = $blockStart->slaves[1];
+
+$innerBlocks1 = alt( "{delimiter}\n" .
+ " {\$i}:~~~~delim~~~~\n" .
+ "{/delimiter}\n",
+
+ "{if \$bar}\n" .
+ " {\$i}:[if]\$bar is true[/if]\n" .
+ "{else}\n" .
+ " {\$i}:[if]\$bar is false[/if]\n" .
+ "{/if}\n",
+
+ "{switch \$bar}\n" .
+ "{case 1}\n" .
+ " {\$i}:[switch]1[/switch]\n" .
+ "{/case}\n" .
+ "{case 2}\n" .
+ " {\$i}:[switch]2[/switch]\n" .
+ "{/case}\n" .
+ "{default}\n" .
+ " {\$i}:[switch]default({\$bar})[/switch]\n" .
+ "{/default}\n" .
+ "{/switch}\n"
+ );
+
+$delimBlocks = alt( "{delimiter}\n" .
+ " {\$i}:====delim====\n" .
+ "{/delimiter}\n" .
+ "{delimiter modulo 2}\n" .
+ " {\$i}:____delim____\n" .
+ "{/delimiter}\n"
+ );
+
+
+$blockStart2 = clone $blockStart;
+$blockEnd2 = $blockStart2->slaves[1];
+$innerBlocks2 = clone $innerBlocks1;
+/*$innerBlocks2->replace( "\$foo", "\$foo2" );
+$innerBlocks2->replace( "\$bar", "\$bar2" );
+$innerBlocks2->replace( "\$num", "\$num2" );
+$innerBlocks2->replace( "{\$i}", "{\$i}:{\$j}" );
+$innerBlocks2->replace( "{\$i = 0}", "{\$j = 0}" );
+$innerBlocks2->replace( "~~~~delim~~~~", "~~delim~~" );*/
+
+$outerBlocks2 = perm( $blockStart2,
+ indent( perm( $innerBlocks2,
+ $delimBlocks ),
+ " " ),
+ $blockEnd2
+ );
+
+
+$outerBlocks2->replace( "\$foo", "\$foo2" );
+$outerBlocks2->replace( "\$bar", "\$bar2" );
+$outerBlocks2->replace( "\$num", "\$num2" );
+$outerBlocks2->replace( "{\$i}", "{\$i}:{\$j}" );
+$outerBlocks2->replace( "{\$i = 0}", "{\$j = 0}" );
+$outerBlocks2->replace( "{++\$i}", "{++\$j}" );
+$outerBlocks2->replace( "~~~~delim~~~~", "~~delim~~" );
+
+$outerBlocks = perm( "\{%num%\n",
+ "{foreach 1..1 as \$blackhole}\n",
+ indent( perm( $blockStart,
+ indent( perm( $innerBlocks1,
+ $outerBlocks2
+// clone $delimBlocks
+ ),
+ " " ),
+ $blockEnd
+ ),
+ " " ),
+ "{/foreach}\n",
+ "\}\n"
+ );
+
+$list = perm( $outerBlocks );
+
+$dir = dirname( __FILE__ ) . "/../regression_tests/";
+
+$a = app( "", $argv );
+
+$i = 1;
+$top = "{var \$foo = array( 0, 1, 3 ), \$foo2 = array( '', 'foo' ), \$bar = 0,
\$bar2 = 0, \$num = 0, \$num2 = 0, \$i = 0, \$j = 0}\n";
+//$a->output( $top );
+do
+{
+ $num = sprintf( "%04d", $i );
+ $str = $list->generate();
+ $str = str_replace( "%num%", $num, $str );
+ $fileIn = $dir . "foreach/correct/nested_with_delimiter_" . $num . ".in";
+// $fileOut = $dir . "foreach/correct/nested_with_delimiter_" . $num .
".out";
+// $name = $alternatives->generate();
+ $a->store( "{* file: " . "nested_with_delimiter_" . $num . ".in" . " *}\n"
. $top . $str,
+ $fileIn );
+/* $inner2Delimiter = '';
+ $inner2 = '';
+ if ( $innerBlocks2->index == 0 )
+ {
+ $inner2Delimiter = ( "{}:~~delim~~\n" .
+ "{$i}:====delim====\n" );
+ $inner2 = '';
+ }
+ implode( $inner2Delimiter, array_pad( array(), 2 - 1, $inner2 );
+ $a->store( "{* file: " . "nested_with_delimiter_" . $num . ".out" . "
*}\n" . $out,
+ $fileOut );*/
+// $a->output( $str );
+ ++$i;
+} while( $list->increase() );
+
+?>


Property changes on: trunk/Template/tests/scripts/permutation_block_subblock.php
___________________________________________________________________
Name: svn:eol-style
+ native



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | Mail Home | sitemap | FAQ | advertise