示例#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)
示例#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)
示例#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)
示例#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)
示例#5
0
 def test_attr(self):
     template = templates.compile('{user.id}', ['user'])
     self.assertEqual('user.id', template._parts[0]._expression)
示例#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)
示例#7
0
 def _compile_template(self, template):
     return templates.compile(template, self.params)
示例#8
0
 def test_item_not_found(self):
     template = templates.compile('{user["name"]}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': {'id': 1}})
示例#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)
示例#10
0
 def test_item_not_found(self):
     template = templates.compile('{user["name"]}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': {'id': 1}})
         
示例#11
0
 def test_attr_not_found(self):
     template = templates.compile('{user.name}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': Dict(id=1)})
示例#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)
示例#13
0
 def test_missing_var(self):
     with self.assertRaises(TemplateError):
         templates.compile('{d}', ['a', 'b', 'c'])
示例#14
0
 def test_item(self):
     template = templates.compile('{user["id"]}', ['user'])
     self.assertEqual('user["id"]', template._parts[0]._expression)
示例#15
0
 def test_attr(self):
     template = templates.compile('{user.id}', ['user'])
     self.assertEqual('user.id', template._parts[0]._expression)
示例#16
0
 def test_item(self):
     template = templates.compile('{user["id"]}', ['user'])
     self.assertEqual('user["id"]', template._parts[0]._expression)
示例#17
0
 def test_missing_var(self):
     with self.assertRaises(TemplateError):
         templates.compile('{d}', ['a', 'b', 'c'])
示例#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
示例#19
0
 def test_attr_not_found(self):
     template = templates.compile('{user.name}', ['user'])
     with self.assertRaises(TemplateError):
         template.eval({'user': Dict(id=1)})
示例#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
示例#21
0
 def _compile_template(self, template):
     return templates.compile(template, self.params)
示例#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)