Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- class Unit
- {
- const NL = "\n";
- const HR = "=";
- private static function file($start, $end)
- {
- $fp = @fopen(__FILE__, "r") or die ("Can not read file.");
- $c = 0;
- $stack = array();
- while($line = fgets($fp, 1024))
- {
- if($c >= $start && $c <= $end)
- $stack[] = $line;
- $c++;
- }
- fclose($fp);
- return $stack;
- }
- protected static function h($title)
- {
- $nl = new String("\n");
- $hr = new String(self::HR);
- $hr
- ->padLeft(new Integer(80), $hr);
- $head = new StringArray(array($hr, $title, $hr->duplicate()));
- $head
- ->rightJoin(new String("\n"));
- foreach($head as $line)
- print $line->get();
- print $nl->get();
- }
- protected static function p($start, $end)
- {
- $nl = new String("\n");
- $sp = new String(" ");
- $hr = new String(self::HR);
- $hr
- ->padLeft(new Integer(76), $hr)
- ->padLeft(new Integer(80), $sp);
- $title = new String("php code");
- $title
- ->padLeft(new Integer(80), $sp);
- $head = new StringArray(array($title, $hr));
- $head
- ->rightJoin($nl);
- $php = new StringArray(self::file($start, $end));
- $php
- ->replace(new String("\r"))
- ->replace($nl)
- ->replace(new String("\t"), new String(" "));
- $lines = new StringArray(array_merge
- (
- array("<?PHP"),
- $php->toArray(),
- array("?>"))
- );
- $lines
- ->rightJoin(new String("\n"))
- ->leftJoin(new String(" "));
- foreach($head as $line)
- print $line->get();
- foreach($lines as $line)
- print $line->get();
- print $hr->get();
- print $nl->get();
- }
- protected static function r($comment, $result)
- {
- $nl = new String("\n");
- $sp = new String(" ");
- $hr = new String(self::HR);
- $hr
- ->padLeft(new Integer(76), $hr)
- ->padLeft(new Integer(80), $sp);
- $head = new StringArray(array("result", '>> '.$comment, $hr));
- $head
- ->padLeft(new Integer(80), $sp)
- ->rightJoin($nl);
- $result = new String($result);
- $lines = $result
- ->split(new String("\n"))
- ->replace(new String("\r"))
- ->replace(new String("\t"), new String(" "))
- ->rightJoin(new String("\n"))
- ->leftJoin(new String(" "));
- foreach($head as $line)
- print $line->get();
- foreach($lines as $line)
- if($line->isWhitespace()->get() === FALSE)
- print $line->get();
- print $hr->get();
- print $nl->get();
- }
- protected static function f()
- {
- $nl = new String("\n");
- $sp = new String(" ");
- $hr = new String(self::HR);
- $eof = new String('end of unit');
- print $nl->get();
- print $eof->padLeft(new Integer(80), $sp)->get();
- print $nl->get();
- print $hr->padLeft(new Integer(80), $hr)->get();
- print $nl->get();
- print $nl->get();
- print $nl->get();
- }
- }
- class StringErrorUnit extends Unit
- {
- public static function ConstructNoString()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 7); ob_start();
- try
- {
- $string = new String(1);
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("type error", ob_get_clean()); self::f();
- }
- public static function SetNoString()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String();
- print $string->set(4)->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("type error", ob_get_clean());
- self::f();
- }
- public static function LeftJoinEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- print $string->leftJoin(new String())->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function RightJoinEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- print $string->rightJoin(new String())->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function InsertEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- print $string->insert(new Integer(5), new String())->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function InsertIndex()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- print $string->insert(new Integer(-5), new String('bar'))->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function PadLeftCharLength()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- print $string->padLeft(new Integer(-5), new String('b'))->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function PadRightCharLength()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- print $string->padLeft(new Integer(-5), new String('b'))->get();
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function ContainsEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- var_dump($string->contains(new String())->get());
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function EndsWithEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- var_dump($string->endsWith(new String())->get());
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function StartsWithEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- var_dump($string->startsWith(new String())->get());
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function IndexOfEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- var_dump($string->indexOf(new String())->get());
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function LastIndexOfEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- var_dump($string->LastIndexOf(new String())->get());
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- public static function ReplaceEmpty()
- {
- self::h(__METHOD__); self::p(__LINE__, __LINE__ + 8); ob_start();
- try
- {
- $string = new String('foo');
- var_dump($string->replace(new String())->get());
- }
- catch(Exception $e)
- {
- print $e;
- }
- self::r("expected error", ob_get_clean()); self::f();
- }
- }
- class StringUnit extends Unit
- {
- public static function String()
- {
- self::h(__METHOD__);
- self::p(__LINE__ + 1, __LINE__ + 2);
- ob_start();
- $string = new String('foobar');
- print $string->toString();
- self::r("generic string-object", ob_get_clean());
- self::f();
- }
- public static function trimStart()
- {
- self::h(__METHOD__);
- self::p(__LINE__, __LINE__ + 15);
- $code = array
- (
- "using System;",
- "",
- "public class HelloWorld",
- "{",
- " public static void Main()",
- " {",
- " // This code displays a simple greeting",
- " // to the console.",
- " // test:",
- " // / ",
- " Console.WriteLine(\"Hello, World.\");",
- " }",
- "}"
- );
- self::p(__LINE__ + 1, __LINE__ + 4);
- ob_start();
- $lines = new StringArray($code);
- $lines->rightJoin(new String("\n"));
- foreach($lines as $line)
- print $line->get();
- self::r("before strip comments", ob_get_clean());
- self::p(__LINE__ + 1, __LINE__ + 11);
- ob_start();
- $comment = new String('//');
- $replace = new String('/ ');
- $lines = new StringArray($code);
- $lines
- ->rightJoin(new String("\n"))
- ->trimStart();
- foreach($lines as $line)
- if($line->startsWith($comment)->get() === true)
- print $line->trimStart($replace)->get();
- self::r("extracted comments", ob_get_clean());
- self::f();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement