[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/macro/tests/cases/ -> lmbMacroTagTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  lmb_require('limb/macro/src/lmbMacroNode.class.php'); 
  11  lmb_require('limb/macro/src/lmbMacroTag.class.php');
  12  lmb_require('limb/macro/src/lmbMacroTagInfo.class.php');
  13  lmb_require('limb/macro/src/lmbMacroSourceLocation.class.php');
  14  lmb_require('limb/macro/src/lmbMacroCodeWriter.class.php'); 
  15   
  16  class MacroTagClass1CompilerTest extends lmbMacroTag{}
  17  class MacroTagClass2CompilerTest extends lmbMacroTag{}
  18  
  19  Mock::generate('lmbMacroNode', 'MockMacroNode');
  20  Mock::generate('lmbMacroCodeWriter', 'MockMacroCodeWriter');
  21  
  22  class lmbMacroTagTest extends UnitTestCase
  23  {
  24    protected $node;
  25    protected $tag_info;
  26    protected $source_location;
  27  
  28    function setUp()
  29    {
  30      $this->tag_info = new lmbMacroTagInfo('MacroTag', 'whatever');
  31      $this->source_location = new lmbMacroSourceLocation('my_file', 10);
  32      $this->node = $this->_createNode();
  33    }
  34  
  35    protected function _createNode()
  36    {
  37      return new lmbMacroTag($this->source_location, 'my_tag', $this->tag_info);
  38    }
  39    
  40    function testGetIdAttribute()
  41    {
  42      $this->node->setId('Test');
  43      $this->assertEqual($this->node->getId(), 'Test');
  44    }
  45  
  46    function testGetIdGenerated()
  47    {
  48      $id = $this->node->getId();
  49      $this->assertEqual($this->node->getId(), $id);
  50    }
  51  
  52    function testFindChild()
  53    {
  54      $mock = new MockMacroNode();
  55      $mock->setReturnValue('getId', 'Test');
  56      $this->node->addChild($mock);
  57      $this->assertIsA($this->node->findChild('Test'), 'MockMacroNode');
  58    }
  59  
  60    function testFindChildNotFound()
  61    {
  62      $this->assertFalse($this->node->findChild('Test'));
  63    }
  64  
  65    function testFindChildByClass()
  66    {
  67      $mock = new MockMacroNode();
  68      $this->node->addChild($mock);
  69      $this->assertIsA($this->node->findChildByClass('MockMacroNode'), 'MockMacroNode');
  70    }
  71  
  72    function testFindChildByClassNotFound()
  73    {
  74      $this->assertFalse($this->node->findChildByClass('Booo'));
  75    }
  76  
  77    function testFindParentByChilld()
  78    {
  79      $parent = new lmbMacroNode;
  80      $parent->addChild($this->node);
  81      $this->assertIsA($this->node->findParentByClass('lmbMacroNode'), 'lmbMacroNode');
  82    }
  83  
  84    function testFindParentByClassNotFound()
  85    {
  86      $this->assertFalse($this->node->findParentByClass('Test'));
  87    }
  88  
  89    function testRemoveChild()
  90    {
  91      $mock = new MockMacroNode();
  92      $mock->setReturnValue('getId', 'Test');
  93      $this->node->addChild($mock);
  94      $this->assertIsA($this->node->removeChild('Test'), 'MockMacroNode');
  95    }
  96  
  97    function testGetChildren()
  98    {
  99      $mock = new MockMacroNode();
 100      $this->node->addChild($mock);
 101      $children = $this->node->getChildren();
 102      $this->assertReference($mock, $children[0]);
 103    }
 104  
 105    function testPrepare()
 106    {
 107      $child = new MockMacroNode();
 108      $this->node->addChild($child);
 109      $child->expectCallCount('prepare', 1);
 110      $this->node->prepare();
 111    }
 112  
 113    function testGenerateConstructor()
 114    {
 115      $code_writer = new MockMacroCodeWriter();
 116      $child = new MockMacroNode();
 117      $child->expectCallCount('generateConstructor', 1);
 118      $this->node->addChild($child);
 119      $this->node->generateConstructor($code_writer);
 120    }
 121  
 122    function testGenerateContents()
 123    {
 124      $code_writer = new MockMacroCodeWriter();
 125      $child = new MockMacroNode();
 126      $child->expectCallCount('generate', 1);
 127      $this->node->addChild($child);
 128      $this->node->generateContents($code_writer);
 129    }
 130  
 131    function testGenerate()
 132    {
 133      $code_writer = new MockMacroCodeWriter();
 134      $child = new MockMacroNode();
 135      $child->expectCallCount('generate', 1);
 136      $this->node->addChild($child);
 137      $this->node->generate($code_writer);
 138    }
 139  
 140    function testCheckIdsOk()
 141    {
 142      $root = new lmbMacroNode;
 143      $child1 = new lmbMacroNode;
 144      $child1->setId('id1');
 145  
 146      $child2 = new lmbMacroNode;
 147      $child2->setId('id2');
 148  
 149      $root->addChild($child1);
 150      $root->addChild($child2);
 151  
 152      $root->checkChildrenIds();
 153    }
 154  
 155    function testDuplicateIdsError()
 156    {
 157      $root = new lmbMacroNode;
 158      $child1 = new lmbMacroNode(new lmbMacroSourceLocation('my_file', 10));
 159      $child1->setId('my_tag');
 160      $root->addChild($child1);
 161  
 162      $child2 = new lmbMacroNode(new lmbMacroSourceLocation('my_file2', 15));
 163      $child2->setId('my_tag');
 164      $root->addChild($child2);
 165  
 166      try
 167      {
 168        $root->checkChildrenIds();
 169        $this->assertTrue(false);
 170      }
 171      catch(lmbMacroException $e)
 172      {
 173        $this->assertWantedPattern('/Duplicate "id" attribute/', $e->getMessage());
 174        $params = $e->getParams();
 175        $this->assertEqual($params['file'], 'my_file2');
 176        $this->assertEqual($params['line'], 15);
 177        $this->assertEqual($params['duplicate_node_file'], 'my_file');
 178        $this->assertEqual($params['duplicate_node_line'], 10);
 179      }
 180    }
 181  
 182    function testDuplicateIdIsLegalInDifferentBranches()
 183    {
 184      $root = new lmbMacroNode;
 185  
 186      $Branch = new lmbMacroNode;
 187      $root->addChild($Branch);
 188  
 189      $child1 = new lmbMacroNode;
 190      $child1->setId('my_tag');
 191      $Branch->addChild($child1);
 192  
 193      $child2 = new MockMacroNode();
 194      $child2->setId('my_tag');
 195      $root->addChild($child2);
 196  
 197      $root->checkChildrenIds();
 198    }  
 199    
 200    function testGetIdByDefault()
 201    {
 202      $this->assertNotNull($this->node->getId());
 203    }  
 204  
 205    function testGetId()
 206    {
 207      $this->node->setId('TestId');
 208      $this->assertEqual($this->node->getId(), 'TestId');
 209    }
 210    
 211    function testGetAttributeUnset()
 212    {
 213      $this->assertNull($this->node->get('foo'));
 214    }  
 215  
 216    function testGetAttribute()
 217    {
 218      $this->node->set('foo', 'bar');
 219      $this->assertEqual($this->node->get('foo'), 'bar');
 220      $this->assertEqual($this->node->get('FOO'), 'bar');
 221    }
 222  
 223    function testHasAttribute()
 224    {
 225      $this->node->set('foo', 'bar');
 226      $this->node->set('tricky', NULL);
 227      $this->assertTrue($this->node->has('foo'));
 228      $this->assertTrue($this->node->has('tricky'));
 229      $this->assertFalse($this->node->has('missing'));
 230      $this->assertTrue($this->node->has('FOO'));
 231      $this->assertTrue($this->node->has('TRICKY'));
 232      $this->assertFalse($this->node->has('MISSING'));
 233    }
 234  
 235    function testRemoveAttribute()
 236    {
 237      $this->node->set('foo', 'bar');
 238      $this->node->set('untouched', 'value');
 239      $this->assertTrue($this->node->has('foo'));
 240      $this->node->remove('FOO');
 241      $this->assertFalse($this->node->has('foo'));
 242    }
 243  
 244    function testBooleanAttribute()
 245    {
 246      //true cases

 247      $this->node->set('B', 'True');
 248      $this->assertTrue($this->node->getBool('B'));
 249  
 250      $this->node->set('C', 'Something');
 251      $this->assertTrue($this->node->getBool('C'));
 252  
 253      //false cases

 254      $this->node->set('A', NULL);
 255      $this->assertFalse($this->node->getBool('A'));
 256      
 257      $this->node->set('D', 'False');
 258      $this->assertFalse($this->node->getBool('D'));
 259  
 260      $this->assertFalse($this->node->getBool('E'));
 261  
 262      $this->node->set('F', 'n');
 263      $this->assertFalse($this->node->getBool('F'));
 264  
 265      $this->node->set('G', 'No');
 266      $this->assertFalse($this->node->getBool('G'));
 267  
 268      $this->node->set('H', 'none');
 269      $this->assertFalse($this->node->getBool('H'));
 270  
 271      $this->node->set('I', '0');
 272      $this->assertFalse($this->node->getBool('I'));
 273    }
 274  
 275    function testPreparseAndCheckForRequiredAttributes()
 276    {
 277      $this->tag_info->setRequiredAttributes(array('bar'));
 278      $this->node->set('bar', null);
 279      $this->node->preParse();
 280    }
 281  
 282    function testPreparseAndCheckForMissedRequiredAttributes()
 283    {
 284      $this->tag_info->setRequiredAttributes(array('bar'));
 285  
 286      try
 287      {
 288        $this->node->preParse();
 289        $this->assertTrue(false);
 290      }
 291      catch(lmbMacroException $e)
 292      {
 293        $this->assertWantedPattern('/Missing required attribute/', $e->getMessage());
 294        $this->assertEqual($e->getParam('attribute'), 'bar');
 295      }
 296    }
 297  
 298    function testRestrictSelfNesting()
 299    {
 300      $tag_info = new lmbMacroTagInfo('CompilerTag', 'whatever');
 301      $tag_info->setRestrictSelfNesting(true);
 302  
 303      $node = new lmbMacroTag(new lmbMacroSourceLocation('my_file', 13), 'whatever', $tag_info);
 304  
 305      $parent = new lmbMacroTag(new lmbMacroSourceLocation('my_file', 10), 'whatEver', $tag_info);
 306      $node->setParent($parent);
 307  
 308      try
 309      {
 310        $node->preParse();
 311        $this->assertTrue(false);
 312      }
 313      catch(lmbMacroException $e)
 314      {
 315        $this->assertWantedPattern('/Tag cannot be nested within the same tag/', $e->getMessage());
 316        $this->assertEqual($e->getParam('same_tag_file'), 'my_file');
 317        $this->assertEqual($e->getParam('same_tag_line'), 10);
 318      }
 319    }
 320  
 321    function testCheckParentTagClassOk()
 322    {
 323      $this->tag_info->setParentClass('MacroTagClass1CompilerTest');
 324  
 325      $parent = new MacroTagClass1CompilerTest(null, null, null);
 326      $this->node->setParent($parent);
 327  
 328      $this->node->preParse();
 329    }
 330  
 331    function testCheckParentTagClassException()
 332    {
 333      $this->tag_info->setParentClass('MacroTagClass1CompilerTest');
 334  
 335      $parent = new MacroTagClass2CompilerTest(null, null, null);
 336      $this->node->setParent($parent);
 337  
 338      try
 339      {
 340        $this->node->preParse();
 341        $this->assertTrue(false);
 342      }
 343      catch(lmbMacroException $e)
 344      {
 345        $this->assertWantedPattern('/Tag must be enclosed by a proper parent tag/', $e->getMessage());
 346        $this->assertEqual($e->getParam('required_parent_tag_class'), 'MacroTagClass1CompilerTest');
 347        $this->assertEqual($e->getParam('file'), $this->source_location->getFile());
 348        $this->assertEqual($e->getParam('line'), $this->source_location->getLine());
 349      }
 350    }  
 351  }
 352  ?>