Ver Mensaje Individual
  #7 (permalink)  
Antiguo 01/07/2013, 03:05
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Iterador se rebobina [Ggrrr]

Ya veo me vale expulsion por "Suma de mensajes" pero aclaro SUPERABA los 10.000 caracteres

Código PHP:
<?php
namespace FastyPatterns

require(
'patterns.php');
require_once 
'PHPUnit/Autoload.php'// PHPUnit

/**
 * Fasty project
 * 
 * Pruebas unitarias
 */

class tagSequenceTest extends PHPUnit_Framework_TestCase
{
    private 
$seq =null;

    private function 
loadSeq(){
        
$this->seq = (new TagSequence())
        ->
addTag((new Tag('select'))->set_Name('Paises'))
        ->
addTag((new Tag('option'))->set_Id('Uno'))    
        ->
addTag((new Tag('a'))->set_href('www.google.com')) 
        ->
addTag((new Tag('option'))->set_Id('Dos'))
        ->
addTag((new Tag('li')))
        ->
addTag((new Tag('li')))
        ->
addTag((new Tag('img')));
    }
    
    
/*
       Pruebo los metodos de busqueda first() y firstFirst()
    */
    
public function testIfCursorIsMovedAsExpected(){  
        
        
$this->loadSeq();   
        
$option = new Tag('option');
     
        
///// FIND METHOD  ///////////
        
$find_method='find';
  
        
$cursor0$this->seq->key();        
        
$f0 $this->seq->$find_method($option);
        
$cursor1$this->seq->key();        
        
        
$f1 =$this->seq->$find_method($option);
        
$cursor2$this->seq->key();
        
        
$f2 =$this->seq->$find_method($option,false);
        
$cursor3$this->seq->key();

        
$this->seq->rewind(); 
        
$f3 =$this->seq->$find_method(new Tag('a'));
        
$cursor4$this->seq->key();
     
        
$this->seq->rewind(); 
        
$f4 =$this->seq->$find_method((new Tag('select')));
        
$cursor5$this->seq->key();
        
        
$f5 =$this->seq->$find_method((new Tag('tag_inexistente')));
        
$cursor6$this->seq->key();

        
$this->AssertTrue($f0);
        
$this->AssertTrue($f1);
        
$this->AssertTrue($f2);
        
$this->AssertTrue($f3);
        
$this->AssertTrue($f4);
        
$this->AssertFalse($f5);


        
$this->AssertEquals(0,$cursor0); // antes de buscar
        
$this->AssertEquals(1,$cursor1); 
        
$this->AssertEquals(3,$cursor2); 
        
$this->AssertEquals(3,$cursor3); // busca desde el comienzo
        
$this->AssertEquals(2,$cursor4);
        
$this->AssertEquals(0,$cursor5);
        
$this->AssertEquals(NULL,$cursor6);

        
///// FIND FIRST METHOD  ///////////
        
$find_method='findFirst';

        
$this->seq->rewind();  
        
$cursor0$this->seq->key();        

        
$f0 $this->seq->$find_method($option);
        
$cursor1$this->seq->key();        
        
        
$f1 =$this->seq->$find_method($option);
        
$cursor2$this->seq->key();  
        
///
        ///
        
$f3 =$this->seq->$find_method(new Tag('a'));
        
$cursor4$this->seq->key();
             
        
$f4 =$this->seq->$find_method((new Tag('select')));
        
$cursor5$this->seq->key();
        
        
$this->AssertTrue($f0);
        
$this->AssertTrue($f1);
        
/// 
        
$this->AssertTrue($f3);
        
$this->AssertTrue($f4);
        
$this->AssertFalse($f5);


        
/* Ojo: el cursor queda 'pasado' en 1 o al final en NULL y es correcto! */
      
        
$this->AssertEquals(0,$cursor0); 
        
$this->AssertEquals(2,$cursor1); 
        
$this->AssertEquals(2,$cursor2); 
        
///
        
$this->AssertEquals(3,$cursor4); 
        
$this->AssertEquals(1,$cursor5); 
        
$this->AssertEquals(NULL,$cursor6); 
           
    }
}
Decia que el WHILE no rebobina porque tiene un parche al comienzo...olvide que lo habia puesto......y al FOREACH no encontre como ponerle el parche asi que por esa razon termino siendo un "findFirst()" ya que justamente rebobina y nada que hacer, siempre comienza del primer elemento...... lo unico que finalmente me funciono fue implementar un noRewindIterator()

Código PHP:
 /* desde ejecutada.. no deberian poder agregarse tags */
    
public function freeze(){

        
$it = new ArrayIterator($this->_tags); 
        
$this->_noRewindIterator = new NoRewindIterator($it);     
    }

    public function 
findNoRewind(tag $search){     

        
$ret false;               
        foreach (
$this->_noRewindIterator as $key=> $current_tag)
        {            
            if (
$current_tag->compareTo($search)>=0){
                
$ret True;                                    
                break;
            }             
        }          
        
        return 
$ret;     

    } 
__________________
Salu2!

Última edición por Italico76; 01/07/2013 a las 05:07