##// END OF EJS Templates
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
Yuya Nishihara -
r38308:f9c42638 default
parent child Browse files
Show More
@@ -743,6 +743,9 b' class sessionvars(templateutil.wrapped):'
743 743 def show(self, context, mapping):
744 744 return self.join(context, '')
745 745
746 def tobool(self, context, mapping):
747 return bool(self._vars)
748
746 749 def tovalue(self, context, mapping):
747 750 return self._vars
748 751
@@ -85,6 +85,10 b' class wrapped(object):'
85 85 """
86 86
87 87 @abc.abstractmethod
88 def tobool(self, context, mapping):
89 """Return a boolean representation of the inner value"""
90
91 @abc.abstractmethod
88 92 def tovalue(self, context, mapping):
89 93 """Move the inner value object out or create a value representation
90 94
@@ -136,6 +140,9 b' class wrappedbytes(wrapped):'
136 140 def show(self, context, mapping):
137 141 return self._value
138 142
143 def tobool(self, context, mapping):
144 return bool(self._value)
145
139 146 def tovalue(self, context, mapping):
140 147 return self._value
141 148
@@ -169,6 +176,14 b' class wrappedvalue(wrapped):'
169 176 return b''
170 177 return pycompat.bytestr(self._value)
171 178
179 def tobool(self, context, mapping):
180 if self._value is None:
181 return False
182 if isinstance(self._value, bool):
183 return self._value
184 # otherwise evaluate as string, which means 0 is True
185 return bool(pycompat.bytestr(self._value))
186
172 187 def tovalue(self, context, mapping):
173 188 return self._value
174 189
@@ -201,6 +216,9 b' class date(mappable, wrapped):'
201 216 def tomap(self, context):
202 217 return {'unixtime': self._unixtime, 'tzoffset': self._tzoffset}
203 218
219 def tobool(self, context, mapping):
220 return True
221
204 222 def tovalue(self, context, mapping):
205 223 return (self._unixtime, self._tzoffset)
206 224
@@ -272,6 +290,9 b' class hybrid(wrapped):'
272 290 return gen()
273 291 return gen
274 292
293 def tobool(self, context, mapping):
294 return bool(self._values)
295
275 296 def tovalue(self, context, mapping):
276 297 # TODO: make it non-recursive for trivial lists/dicts
277 298 xs = self._values
@@ -327,6 +348,9 b' class hybriditem(mappable, wrapped):'
327 348 return gen()
328 349 return gen
329 350
351 def tobool(self, context, mapping):
352 return bool(self.tovalue(context, mapping))
353
330 354 def tovalue(self, context, mapping):
331 355 return _unthunk(context, mapping, self._value)
332 356
@@ -396,6 +420,9 b' class mappinggenerator(_mappingsequence)'
396 420 def itermaps(self, context):
397 421 return self._make(context, *self._args)
398 422
423 def tobool(self, context, mapping):
424 return _nonempty(self.itermaps(context))
425
399 426 class mappinglist(_mappingsequence):
400 427 """Wrapper for list of template mappings"""
401 428
@@ -406,6 +433,9 b' class mappinglist(_mappingsequence):'
406 433 def itermaps(self, context):
407 434 return iter(self._mappings)
408 435
436 def tobool(self, context, mapping):
437 return bool(self._mappings)
438
409 439 class mappedgenerator(wrapped):
410 440 """Wrapper for generator of strings which acts as a list
411 441
@@ -449,6 +479,9 b' class mappedgenerator(wrapped):'
449 479 def show(self, context, mapping):
450 480 return self.join(context, mapping, '')
451 481
482 def tobool(self, context, mapping):
483 return _nonempty(self._gen(context))
484
452 485 def tovalue(self, context, mapping):
453 486 return [stringify(context, mapping, x) for x in self._gen(context)]
454 487
@@ -607,6 +640,13 b' def findsymbolicname(arg):'
607 640 else:
608 641 return None
609 642
643 def _nonempty(xiter):
644 try:
645 next(xiter)
646 return True
647 except StopIteration:
648 return False
649
610 650 def _unthunk(context, mapping, thing):
611 651 """Evaluate a lazy byte string into value"""
612 652 if not isinstance(thing, types.GeneratorType):
@@ -655,13 +695,7 b' def evalboolean(context, mapping, arg):'
655 695 thing = stringutil.parsebool(data)
656 696 else:
657 697 thing = func(context, mapping, data)
658 if isinstance(thing, wrapped):
659 thing = thing.tovalue(context, mapping)
660 if isinstance(thing, bool):
661 return thing
662 # other objects are evaluated as strings, which means 0 is True, but
663 # empty dict/list should be False as they are expected to be ''
664 return bool(stringify(context, mapping, thing))
698 return makewrapped(context, mapping, thing).tobool(context, mapping)
665 699
666 700 def evaldate(context, mapping, arg, err=None):
667 701 """Evaluate given argument as a date tuple or a date string; returns
@@ -4151,6 +4151,10 b' Test boolean expression/literal passed t'
4151 4151 empty string is False
4152 4152 $ hg log -r 0 -T '{if(revset(r"0 - 0"), "", "empty list is False")}\n'
4153 4153 empty list is False
4154 $ hg log -r 0 -T '{if(revset(r"0"), "non-empty list is True")}\n'
4155 non-empty list is True
4156 $ hg log -r 0 -T '{if(revset(r"0") % "", "list of empty strings is True")}\n'
4157 list of empty strings is True
4154 4158 $ hg log -r 0 -T '{if(true, "true is True")}\n'
4155 4159 true is True
4156 4160 $ hg log -r 0 -T '{if(false, "", "false is False")}\n'
General Comments 0
You need to be logged in to leave comments. Login now