Example #1
0
 def test_begin_ends_with_vars(self):
     template = templates.compile('{a}{b}{c}', ['a', 'b', 'c'])
     self.assertEqual(3, len(template._parts))
     self.assertIsInstance(template._parts[0], VariablePart)
     self.assertEqual('a', template._parts[0]._expression)
     self.assertIsInstance(template._parts[1], VariablePart)
     self.assertEqual('b', template._parts[1]._expression)
     self.assertIsInstance(template._parts[2], VariablePart)
     self.assertEqual('c', template._parts[2]._expression)
Example #2
0
 def test_begin_ends_with_vars(self):
     template = templates.compile('{a}{b}{c}', ['a', 'b', 'c'])
     self.assertEqual(3, len(template._parts))
     self.assertIsInstance(template._parts[0], VariablePart)
     self.assertEqual('a', template._parts[0]._expression)
     self.assertIsInstance(template._parts[1], VariablePart)
     self.assertEqual('b', template._parts[1]._expression)
     self.assertIsInstance(template._parts[2], VariablePart)
     self.assertEqual('c', template._parts[2]._expression)
Example #3
0
 def test_normal(self):
     template = templates.compile('Calculating {a} + {b} ...', ['a', 'b', 'c'])
     self.assertEqual(5, len(template._parts))
     self.assertIsInstance(template._parts[0], StringPart)
     self.assertEqual('Calculating ', template._parts[0]._expression)
     self.assertIsInstance(template._parts[1], VariablePart)
     self.assertEqual('a', template._parts[1]._expression)
     self.assertIsInstance(template._parts[2], StringPart)
     self.assertEqual(' + ', template._parts[2]._expression)
     self.assertIsInstance(template._parts[3], VariablePart)
     self.assertEqual('b', template._parts[3]._expression)
     self.assertIsInstance(template._parts[4], StringPart)
     self.assertEqual(' ...', template._parts[4]._expression)
Example #4
0
 def test_normal(self):
     template = templates.compile('Calculating {a} + {b} ...',
                                  ['a', 'b', 'c'])
     self.assertEqual(5, len(template._parts))
     self.assertIsInstance(template._parts[0], StringPart)
     self.assertEqual('Calculating ', template._parts[0]._expression)
     self.assertIsInstance(template._parts[1], VariablePart)
     self.assertEqual('a', template._parts[1]._expression)
     self.assertIsInstance(template._parts[2], StringPart)
     self.assertEqual(' + ', template._parts[2]._expression)
     self.assertIsInstance(template._parts[3], VariablePart)
     self.assertEqual('b', template._parts[3]._expression)
     self.assertIsInstance(template._parts[4], StringPart)
     self.assertEqual(' ...', template._parts[4]._expression)
Example #5
0
 def test_attr(self):
     template = templates.compile('{user.id}', ['user'])
     self.assertEqual('user.id', template._parts[0]._expression)
Example #6
0
 def test_pure_string(self):
     template = templates.compile('Calculation succeeded.', ['a', 'b', 'c'])
     self.assertEqual(1, len(template._parts))
     self.assertIsInstance(template._parts[0], StringPart)
     self.assertEqual('Calculation succeeded.',
                      template._parts[0]._expression)
Example #7
0
 def _compile_template(self, template):
     return templates.compile(template, self.params)
Example #8
0
 def test_item_not_found(self):
     template = templates.compile('{user["name"]}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': {'id': 1}})
Example #9
0
 def test_succeed(self):
     template = templates.compile('Calculating {a} + {b} ...',
                                  ['a', 'b', 'c'])
     result = template.eval({'a': 1, 'b': 2, 'c': 3})
     self.assertEqual('Calculating 1 + 2 ...', result)
Example #10
0
 def test_item_not_found(self):
     template = templates.compile('{user["name"]}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': {'id': 1}})
         
Example #11
0
 def test_attr_not_found(self):
     template = templates.compile('{user.name}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': Dict(id=1)})
Example #12
0
 def test_succeed(self):
     template = templates.compile('Calculating {a} + {b} ...', ['a', 'b', 'c'])
     result = template.eval({'a': 1, 'b': 2, 'c': 3})
     self.assertEqual('Calculating 1 + 2 ...', result)
Example #13
0
 def test_missing_var(self):
     with self.assertRaises(TemplateError):
         templates.compile('{d}', ['a', 'b', 'c'])
Example #14
0
 def test_item(self):
     template = templates.compile('{user["id"]}', ['user'])
     self.assertEqual('user["id"]', template._parts[0]._expression)
Example #15
0
 def test_attr(self):
     template = templates.compile('{user.id}', ['user'])
     self.assertEqual('user.id', template._parts[0]._expression)
Example #16
0
 def test_item(self):
     template = templates.compile('{user["id"]}', ['user'])
     self.assertEqual('user["id"]', template._parts[0]._expression)
Example #17
0
 def test_missing_var(self):
     with self.assertRaises(TemplateError):
         templates.compile('{d}', ['a', 'b', 'c'])
Example #18
0
 def _decorate(self, func):
     super(CacheDecorator, self)._decorate(func)
     var_names = self.params + tuple(self._vars.keys())
     self._key_template = templates.compile(self._key, var_names)
     return self
Example #19
0
 def test_attr_not_found(self):
     template = templates.compile('{user.name}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': Dict(id=1)})
Example #20
0
 def _decorate(self, func):
     super(CacheDecorator, self)._decorate(func)
     var_names = self.params + tuple(self._extra_vars.keys())
     self._key_template = templates.compile(self._key, var_names)
     return self
Example #21
0
 def _compile_template(self, template):
     return templates.compile(template, self.params)
Example #22
0
 def test_pure_string(self):
     template = templates.compile('Calculation succeeded.', ['a', 'b', 'c'])
     self.assertEqual(1, len(template._parts))
     self.assertIsInstance(template._parts[0], StringPart)
     self.assertEqual('Calculation succeeded.', template._parts[0]._expression)