/** * Performs measurement * * @param util.profiling.Measurement $m */ public function run($m) { $m->perform(newinstance('util.profiling.Run', [], ['before' => function ($iteration) { Console::write($iteration->name(), ': '); }, 'after' => function ($result) { Console::writeLinef('%d iteration(s), %.3f seconds, result= %s', $result->iteration()->times(), $result->elapsed(), Objects::stringOf($result->result())); }])); }
public function write_object_with_end_tag() { $this->assertWritten("[31;1m</>[22;39m", function () { Console::write('<red>', $this->value('</>'), '</>'); }); }
/** * Handles classes * * @param lang.XPClass class */ protected static function printClass(XPClass $class) { Console::write(implode(' ', Modifiers::namesOf($class->getModifiers()))); Console::write(' class ', self::displayNameOf($class)); if ($parent = $class->getParentClass()) { Console::write(' extends ', self::displayNameOf($parent)); } if ($interfaces = $class->getDeclaredInterfaces()) { Console::write(' implements '); $s = sizeof($interfaces) - 1; foreach ($interfaces as $i => $iface) { Console::write(self::displayNameOf($iface)); $i < $s && Console::write(', '); } } // Constants Console::writeLine(' {'); $i = 0; foreach ($class->getConstants() as $name => $value) { Console::writeLine(' const ', $name, ' = ', \xp::stringOf($value)); $i++; } // Fields $i && Console::writeLine(); $i = 0; foreach ($class->getFields() as $field) { Console::writeLine(' ', $field); $i++; } // Constructor $i && Console::writeLine(); $i = 0; if ($class->hasConstructor()) { Console::writeLine(' ', $class->getConstructor()); $i++; } // Methods $i && Console::writeLine(); self::printMethods($class->getMethods()); Console::writeLine('}'); }
/** * Fetches this origin into a given target folder * * @param io.Folder $target */ public function fetchInto(Folder $target) { $zip = $this->zipBallOf($this->url); $i = 0; with($iter = $zip->iterator()); $base = rtrim($iter->next()->getName() . '/', '/'); Console::write('Extracting (', $base, ') ['); while ($iter->hasNext()) { $entry = $iter->next(); $relative = str_replace($base, '', $entry->getName()); if ($entry->isDirectory()) { $folder = new Folder($target, $relative); $folder->exists() || $folder->create(0755); } else { $file = new File($target, $relative); $tran = new StreamTransfer($entry->getInputStream(), $file->getOutputStream()); $tran->transferAll(); $tran->close(); } $i++ % 10 || Console::write('.'); } $zip->close(); Console::writeLine(']'); }
public function exception_from_toString() { try { Console::write(new class extends Object { public function toString() { throw new IllegalStateException("Cannot render string"); } }); $this->fail('Expected exception not thrown', null, 'lang.IllegalStateException'); } catch (IllegalStateException $expected) { $this->assertEquals('', $this->streams[1]->getBytes()); } }
/** * Extract a ".ar" file into a given target directory * * @param string base * @param string ar * @param io.Folder target * @throws lang.IllegalStateException in case the target is not found * @throws lang.FormatException in case the .ar-file is not parseable */ protected function extract($base, $ar, Folder $target) { // Open a HTTP connection $url = new \peer\URL($base . $ar . '.ar'); $r = create(new HttpConnection($url))->get(); if (\peer\http\HttpConstants::STATUS_OK != $r->getStatusCode()) { throw new \lang\IllegalStateException(sprintf('Unexpected response %d:%s for %s', $r->getStatusCode(), $r->getMessage(), $url->getURL())); } $in = new BufferedInputStream($r->getInputStream()); do { // Seach for first section header, --[LENGTH]:[FILENAME]-- and parse it do { $line = $this->readLine($in); if (!$in->available()) { throw new \lang\FormatException('Cannot locate section header'); } } while (2 !== sscanf($line, '--%d:%[^:]--', $length, $filename)); // Calculate target file $file = new File($target, $filename); $folder = new Folder($file->getPath()); $folder->exists() || $folder->create(); \util\cmd\Console::writef(' >> [%-10s] %s (%.2f kB) [%s]%s', $ar, $filename, $length / 1024, str_repeat('.', self::PROGRESS_INDICATOR_WIDTH), str_repeat("", self::PROGRESS_INDICATOR_WIDTH + 1)); // Transfer length bytes into file $c = 0; $out = $file->getOutputStream(); $size = 0; while ($size < $length) { $chunk = $in->read(min(0x1000, $length - $size)); $size += strlen($chunk); $out->write($chunk); // Update progress $d = ceil($size / $length * self::PROGRESS_INDICATOR_WIDTH); if ($d == $c) { continue; } \util\cmd\Console::write(str_repeat('#', $d - $c)); $c = $d; } $out->close(); \util\cmd\Console::writeLine(); } while ($in->available() > 0); $in->close(); }
public function exceptionFromToString() { try { Console::write(newinstance('lang.Object', array(), '{ public function toString() { throw new IllegalStateException("Cannot render string"); } }')); $this->fail('Expected exception not thrown', null, 'lang.IllegalStateException'); } catch (\lang\IllegalStateException $expected) { $this->assertEquals('', $this->streams[1]->getBytes()); } }