Exemplo n.º 1
0
 public function testStrEndsWith()
 {
     $this->assertFalse(py_str_ends_with('abcd', 'q'));
     $this->assertFalse(py_str_ends_with('abcd ', 'ab'));
     $this->assertFalse(py_str_ends_with('фыв ', 'ab'));
     $this->assertFalse(py_str_ends_with('', 'abcd'));
     $this->assertTrue(py_str_ends_with('source', 'ce'));
 }
Exemplo n.º 2
0
 /**
  * @param string $var
  * @throws TemplateSyntaxError
  */
 public function __construct($var)
 {
     $this->var = $var;
     $this->literal = null;
     $this->lookups = null;
     $this->translate = False;
     $this->message_context = null;
     try {
         // First try to treat this variable as a number.
         if (!is_numeric($var)) {
             throw new ValueError();
         }
         $this->literal = (double) $var;
         /*
          * So it's a float... is it an int? If the original value contained a
          * dot or an "e" then it was a float, not an int.
          */
         if (strpos($var, '.') === False && strpos(strtolower($var), 'e') === False) {
             $this->literal = (int) $this->literal;
         }
         // "2." is invalid
         if (py_str_ends_with($var, '.')) {
             throw new ValueError();
         }
     } catch (ValueError $e) {
         // A ValueError means that the variable isn't a number.
         if (py_str_starts_with($var, '_(') && py_str_ends_with($var, ')')) {
             // The result of the lookup should be translated at rendering time.
             $this->translate = True;
             $var = py_slice($var, 2, -1);
         }
         // If it's wrapped with quotes (single or double), then we're also dealing with a literal.
         try {
             $this->literal = mark_safe(unescape_string_literal($var));
         } catch (ValueError $e) {
             // Otherwise we'll set self.lookups so that resolve() knows we're dealing with a bonafide variable
             if (strpos($var, DjaBase::VARIABLE_ATTRIBUTE_SEPARATOR . '_') !== False || $var[0] == '_') {
                 throw new TemplateSyntaxError('Variables and attributes may not begin with underscores: \'' . $var . '\'');
             }
             $this->lookups = explode(DjaBase::VARIABLE_ATTRIBUTE_SEPARATOR, $var);
         }
     }
 }
Exemplo n.º 3
0
 public static function add_truncation_text($text, $truncate = null)
 {
     if ($truncate === null) {
         $truncate = Dja::getI18n()->pgettext('String to return when truncating text', '%(truncated_text)s...');
     }
     if (strpos($truncate, '%(truncated_text)s') !== False) {
         return str_replace('%(truncated_text)s', $text, $truncate);
     }
     /*
      * The truncation text didn't contain the %(truncated_text)s string
      * replacement argument so just append it to the text.
      */
     if (py_str_ends_with($text, $truncate)) {
         // But don't append the truncation text if the current text already ends in this.
         return $text;
     }
     return $text . $truncate;
 }
Exemplo n.º 4
0
 public function testLoaderDebugOrigin()
 {
     // Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with the compiled templates.
     $old_debug = Dja::getSetting('TEMPLATE_DEBUG');
     Dja::setSetting('TEMPLATE_DEBUG', True);
     $old_loaders = DjaLoader::$template_source_loaders;
     DjaLoader::$template_source_loaders = array(new FilesystemLoader());
     /*
      * We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
      * point to a directory containing a 404.html file. Also that
      * the file system and app directories loaders both inherit the
      * load_template method from the BaseLoader class, so we only need
      * to test one of them.
      */
     $load_name = '404.html';
     $template = DjaLoader::getTemplate($load_name);
     $template_name = $template->nodelist[0]->source[0]->name;
     $this->assertTrue(py_str_ends_with($template_name, $load_name), 'Template loaded by filesystem loader has incorrect name for debug page: ' . $template_name);
     // Aso test the cached loader, since it overrides load_template
     $cache_loader = new CachedLoader(array(''));
     $cache_loader->_cached_loaders = DjaLoader::$template_source_loaders;
     DjaLoader::$template_source_loaders = array($cache_loader);
     $template = DjaLoader::getTemplate($load_name);
     $template_name = $template->nodelist[0]->source[0]->name;
     $this->assertTrue(py_str_ends_with($template_name, $load_name), 'Template loaded through cached loader has incorrect name for debug page: ' . $template_name);
     $template = DjaLoader::getTemplate($load_name);
     $template_name = $template->nodelist[0]->source[0]->name;
     $this->assertTrue(py_str_ends_with($template_name, $load_name), 'Cached template loaded through cached loader has incorrect name for debug page: ' . $template_name);
     DjaLoader::$template_source_loaders = $old_loaders;
     Dja::setSetting('TEMPLATE_DEBUG', $old_debug);
 }