Advertisement
fruffl

Untitled

Sep 16th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.66 KB | None | 0 0
  1. <?PHP
  2. class Unit
  3. {
  4.     const NL = "\n";
  5.     const HR = "=";
  6.    
  7.     private static function file($start, $end)
  8.     {
  9.         $fp = @fopen(__FILE__, "r") or die ("Can not read file.");
  10.         $c = 0;
  11.         $stack = array();
  12.         while($line = fgets($fp, 1024))
  13.         {
  14.             if($c >= $start && $c <= $end)
  15.                 $stack[] = $line;
  16.             $c++;
  17.         }
  18.         fclose($fp);
  19.        
  20.         return $stack;
  21.     }
  22.     protected static function h($title)
  23.     {
  24.         $nl = new String("\n");
  25.        
  26.         $hr = new String(self::HR);
  27.         $hr
  28.         ->padLeft(new Integer(80), $hr);
  29.        
  30.         $head  = new StringArray(array($hr, $title, $hr->duplicate()));
  31.         $head
  32.         ->rightJoin(new String("\n"));
  33.        
  34.         foreach($head as $line)
  35.             print $line->get();
  36.            
  37.         print $nl->get();
  38.     }
  39.     protected static function p($start, $end)
  40.     {
  41.         $nl    = new String("\n");
  42.         $sp    = new String(" ");
  43.        
  44.         $hr    = new String(self::HR);
  45.         $hr
  46.         ->padLeft(new Integer(76), $hr)
  47.         ->padLeft(new Integer(80), $sp);
  48.        
  49.         $title = new String("php code");
  50.         $title
  51.         ->padLeft(new Integer(80), $sp);
  52.        
  53.         $head = new StringArray(array($title, $hr));
  54.         $head
  55.         ->rightJoin($nl);
  56.        
  57.         $php   = new StringArray(self::file($start, $end));
  58.         $php
  59.         ->replace(new String("\r"))
  60.         ->replace($nl)
  61.         ->replace(new String("\t"), new String("  "));
  62.        
  63.         $lines = new StringArray(array_merge
  64.         (
  65.             array("<?PHP"),
  66.             $php->toArray(),
  67.             array("?>"))
  68.         );
  69.        
  70.         $lines
  71.         ->rightJoin(new String("\n"))
  72.         ->leftJoin(new String("    "));
  73.        
  74.         foreach($head as $line)
  75.             print $line->get();
  76.            
  77.         foreach($lines as $line)
  78.             print $line->get();
  79.            
  80.         print $hr->get();
  81.         print $nl->get();
  82.     }
  83.    
  84.     protected static function r($comment, $result)
  85.     {
  86.         $nl    = new String("\n");
  87.         $sp    = new String(" ");
  88.        
  89.         $hr    = new String(self::HR);
  90.         $hr
  91.         ->padLeft(new Integer(76), $hr)
  92.         ->padLeft(new Integer(80), $sp);
  93.        
  94.         $head = new StringArray(array("result", '>> '.$comment, $hr));
  95.         $head
  96.         ->padLeft(new Integer(80), $sp)
  97.         ->rightJoin($nl);
  98.        
  99.         $result = new String($result);
  100.         $lines = $result
  101.             ->split(new String("\n"))
  102.             ->replace(new String("\r"))
  103.             ->replace(new String("\t"), new String("  "))
  104.             ->rightJoin(new String("\n"))
  105.             ->leftJoin(new String("    "));
  106.            
  107.         foreach($head as $line)
  108.             print $line->get();
  109.            
  110.         foreach($lines as $line)
  111.             if($line->isWhitespace()->get() === FALSE)
  112.                 print $line->get();
  113.            
  114.         print $hr->get();
  115.         print $nl->get();
  116.     }
  117.    
  118.     protected static function f()
  119.     {
  120.         $nl  = new String("\n");
  121.         $sp  = new String(" ");
  122.         $hr  = new String(self::HR);
  123.         $eof = new String('end of unit');
  124.         print $nl->get();
  125.         print $eof->padLeft(new Integer(80), $sp)->get();
  126.         print $nl->get();
  127.         print $hr->padLeft(new Integer(80), $hr)->get();
  128.         print $nl->get();
  129.         print $nl->get();
  130.         print $nl->get();
  131.     }
  132. }
  133.  
  134.  
  135. class StringErrorUnit extends Unit
  136. {
  137.     public static function ConstructNoString()
  138.     {
  139.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 7); ob_start();
  140.         try
  141.         {
  142.             $string = new String(1);
  143.         }
  144.         catch(Exception $e)
  145.         {
  146.             print $e;
  147.         }
  148.         self::r("type error", ob_get_clean()); self::f();
  149.     }
  150.    
  151.     public static function SetNoString()
  152.     {
  153.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  154.         try
  155.         {
  156.             $string = new String();
  157.             print $string->set(4)->get();
  158.         }
  159.         catch(Exception $e)
  160.         {
  161.             print $e;
  162.         }
  163.         self::r("type error", ob_get_clean());
  164.         self::f();
  165.     }
  166.    
  167.     public static function LeftJoinEmpty()
  168.     {
  169.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  170.         try
  171.         {
  172.             $string = new String('foo');
  173.             print $string->leftJoin(new String())->get();
  174.         }
  175.         catch(Exception $e)
  176.         {
  177.             print $e;
  178.         }
  179.         self::r("expected error", ob_get_clean()); self::f();
  180.     }
  181.    
  182.     public static function RightJoinEmpty()
  183.     {
  184.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  185.         try
  186.         {
  187.             $string = new String('foo');
  188.             print $string->rightJoin(new String())->get();
  189.         }
  190.         catch(Exception $e)
  191.         {
  192.             print $e;
  193.         }
  194.         self::r("expected error", ob_get_clean()); self::f();
  195.     }
  196.    
  197.     public static function InsertEmpty()
  198.     {
  199.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  200.         try
  201.         {
  202.             $string = new String('foo');
  203.             print $string->insert(new Integer(5), new String())->get();
  204.         }
  205.         catch(Exception $e)
  206.         {
  207.             print $e;
  208.         }
  209.         self::r("expected error", ob_get_clean()); self::f();
  210.     }
  211.    
  212.     public static function InsertIndex()
  213.     {
  214.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  215.         try
  216.         {
  217.             $string = new String('foo');
  218.             print $string->insert(new Integer(-5), new String('bar'))->get();
  219.         }
  220.         catch(Exception $e)
  221.         {
  222.             print $e;
  223.         }
  224.         self::r("expected error", ob_get_clean()); self::f();
  225.     }
  226.    
  227.     public static function PadLeftCharLength()
  228.     {
  229.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  230.         try
  231.         {
  232.             $string = new String('foo');
  233.             print $string->padLeft(new Integer(-5), new String('b'))->get();
  234.         }
  235.         catch(Exception $e)
  236.         {
  237.             print $e;
  238.         }
  239.         self::r("expected error", ob_get_clean()); self::f();
  240.     }
  241.    
  242.     public static function PadRightCharLength()
  243.     {
  244.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  245.         try
  246.         {
  247.             $string = new String('foo');
  248.             print $string->padLeft(new Integer(-5), new String('b'))->get();
  249.         }
  250.         catch(Exception $e)
  251.         {
  252.             print $e;
  253.         }
  254.         self::r("expected error", ob_get_clean()); self::f();
  255.     }
  256.    
  257.     public static function ContainsEmpty()
  258.     {
  259.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  260.         try
  261.         {
  262.             $string = new String('foo');
  263.             var_dump($string->contains(new String())->get());
  264.         }
  265.         catch(Exception $e)
  266.         {
  267.             print $e;
  268.         }
  269.         self::r("expected error", ob_get_clean()); self::f();
  270.     }
  271.    
  272.     public static function EndsWithEmpty()
  273.     {
  274.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  275.         try
  276.         {
  277.             $string = new String('foo');
  278.             var_dump($string->endsWith(new String())->get());
  279.         }
  280.         catch(Exception $e)
  281.         {
  282.             print $e;
  283.         }
  284.         self::r("expected error", ob_get_clean()); self::f();
  285.     }
  286.    
  287.     public static function StartsWithEmpty()
  288.     {
  289.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  290.         try
  291.         {
  292.             $string = new String('foo');
  293.             var_dump($string->startsWith(new String())->get());
  294.         }
  295.         catch(Exception $e)
  296.         {
  297.             print $e;
  298.         }
  299.         self::r("expected error", ob_get_clean()); self::f();
  300.     }
  301.    
  302.     public static function IndexOfEmpty()
  303.     {
  304.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  305.         try
  306.         {
  307.             $string = new String('foo');
  308.             var_dump($string->indexOf(new String())->get());
  309.         }
  310.         catch(Exception $e)
  311.         {
  312.             print $e;
  313.         }
  314.         self::r("expected error", ob_get_clean()); self::f();
  315.     }
  316.    
  317.     public static function LastIndexOfEmpty()
  318.     {
  319.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  320.         try
  321.         {
  322.             $string = new String('foo');
  323.             var_dump($string->LastIndexOf(new String())->get());
  324.         }
  325.         catch(Exception $e)
  326.         {
  327.             print $e;
  328.         }
  329.         self::r("expected error", ob_get_clean()); self::f();
  330.     }
  331.    
  332.     public static function ReplaceEmpty()
  333.     {
  334.         self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
  335.         try
  336.         {
  337.             $string = new String('foo');
  338.             var_dump($string->replace(new String())->get());
  339.         }
  340.         catch(Exception $e)
  341.         {
  342.             print $e;
  343.         }
  344.         self::r("expected error", ob_get_clean()); self::f();
  345.     }
  346. }
  347. class StringUnit extends Unit
  348. {
  349.     public static function String()
  350.     {
  351.         self::h(__METHOD__);
  352.         self::p(__LINE__ + 1, __LINE__ + 2);   
  353.         ob_start();
  354.         $string = new String('foobar');
  355.         print $string->toString();
  356.         self::r("generic string-object", ob_get_clean());
  357.         self::f();
  358.     }
  359.    
  360.     public static function trimStart()
  361.     {
  362.         self::h(__METHOD__);
  363.         self::p(__LINE__, __LINE__ + 15);
  364.         $code = array
  365.         (
  366.             "using System;",
  367.             "",
  368.             "public class HelloWorld",
  369.             "{",
  370.             "   public static void Main()",
  371.             "   {",
  372.             "      // This code displays a simple greeting",
  373.             "      //   to the console.",
  374.             "      //   test:",
  375.             "      //   / ",
  376.             "      Console.WriteLine(\"Hello, World.\");",
  377.             "   }",
  378.             "}"
  379.         );
  380.        
  381.         self::p(__LINE__ + 1, __LINE__ + 4);       
  382.         ob_start();
  383.         $lines = new StringArray($code);
  384.         $lines->rightJoin(new String("\n"));
  385.         foreach($lines as $line)
  386.             print $line->get();
  387.         self::r("before strip comments", ob_get_clean());
  388.        
  389.         self::p(__LINE__ + 1, __LINE__ + 11);          
  390.         ob_start();    
  391.         $comment = new String('//');
  392.         $replace = new String('/ ');
  393.        
  394.         $lines   = new StringArray($code);
  395.         $lines
  396.         ->rightJoin(new String("\n"))
  397.         ->trimStart();
  398.        
  399.         foreach($lines as $line)
  400.             if($line->startsWith($comment)->get() === true)
  401.                 print $line->trimStart($replace)->get();
  402.         self::r("extracted comments", ob_get_clean());
  403.         self::f();
  404.     }
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement