##// 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 def show(self, context, mapping):
743 def show(self, context, mapping):
744 return self.join(context, '')
744 return self.join(context, '')
745
745
746 def tobool(self, context, mapping):
747 return bool(self._vars)
748
746 def tovalue(self, context, mapping):
749 def tovalue(self, context, mapping):
747 return self._vars
750 return self._vars
748
751
@@ -85,6 +85,10 b' class wrapped(object):'
85 """
85 """
86
86
87 @abc.abstractmethod
87 @abc.abstractmethod
88 def tobool(self, context, mapping):
89 """Return a boolean representation of the inner value"""
90
91 @abc.abstractmethod
88 def tovalue(self, context, mapping):
92 def tovalue(self, context, mapping):
89 """Move the inner value object out or create a value representation
93 """Move the inner value object out or create a value representation
90
94
@@ -136,6 +140,9 b' class wrappedbytes(wrapped):'
136 def show(self, context, mapping):
140 def show(self, context, mapping):
137 return self._value
141 return self._value
138
142
143 def tobool(self, context, mapping):
144 return bool(self._value)
145
139 def tovalue(self, context, mapping):
146 def tovalue(self, context, mapping):
140 return self._value
147 return self._value
141
148
@@ -169,6 +176,14 b' class wrappedvalue(wrapped):'
169 return b''
176 return b''
170 return pycompat.bytestr(self._value)
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 def tovalue(self, context, mapping):
187 def tovalue(self, context, mapping):
173 return self._value
188 return self._value
174
189
@@ -201,6 +216,9 b' class date(mappable, wrapped):'
201 def tomap(self, context):
216 def tomap(self, context):
202 return {'unixtime': self._unixtime, 'tzoffset': self._tzoffset}
217 return {'unixtime': self._unixtime, 'tzoffset': self._tzoffset}
203
218
219 def tobool(self, context, mapping):
220 return True
221
204 def tovalue(self, context, mapping):
222 def tovalue(self, context, mapping):
205 return (self._unixtime, self._tzoffset)
223 return (self._unixtime, self._tzoffset)
206
224
@@ -272,6 +290,9 b' class hybrid(wrapped):'
272 return gen()
290 return gen()
273 return gen
291 return gen
274
292
293 def tobool(self, context, mapping):
294 return bool(self._values)
295
275 def tovalue(self, context, mapping):
296 def tovalue(self, context, mapping):
276 # TODO: make it non-recursive for trivial lists/dicts
297 # TODO: make it non-recursive for trivial lists/dicts
277 xs = self._values
298 xs = self._values
@@ -327,6 +348,9 b' class hybriditem(mappable, wrapped):'
327 return gen()
348 return gen()
328 return gen
349 return gen
329
350
351 def tobool(self, context, mapping):
352 return bool(self.tovalue(context, mapping))
353
330 def tovalue(self, context, mapping):
354 def tovalue(self, context, mapping):
331 return _unthunk(context, mapping, self._value)
355 return _unthunk(context, mapping, self._value)
332
356
@@ -396,6 +420,9 b' class mappinggenerator(_mappingsequence)'
396 def itermaps(self, context):
420 def itermaps(self, context):
397 return self._make(context, *self._args)
421 return self._make(context, *self._args)
398
422
423 def tobool(self, context, mapping):
424 return _nonempty(self.itermaps(context))
425
399 class mappinglist(_mappingsequence):
426 class mappinglist(_mappingsequence):
400 """Wrapper for list of template mappings"""
427 """Wrapper for list of template mappings"""
401
428
@@ -406,6 +433,9 b' class mappinglist(_mappingsequence):'
406 def itermaps(self, context):
433 def itermaps(self, context):
407 return iter(self._mappings)
434 return iter(self._mappings)
408
435
436 def tobool(self, context, mapping):
437 return bool(self._mappings)
438
409 class mappedgenerator(wrapped):
439 class mappedgenerator(wrapped):
410 """Wrapper for generator of strings which acts as a list
440 """Wrapper for generator of strings which acts as a list
411
441
@@ -449,6 +479,9 b' class mappedgenerator(wrapped):'
449 def show(self, context, mapping):
479 def show(self, context, mapping):
450 return self.join(context, mapping, '')
480 return self.join(context, mapping, '')
451
481
482 def tobool(self, context, mapping):
483 return _nonempty(self._gen(context))
484
452 def tovalue(self, context, mapping):
485 def tovalue(self, context, mapping):
453 return [stringify(context, mapping, x) for x in self._gen(context)]
486 return [stringify(context, mapping, x) for x in self._gen(context)]
454
487
@@ -607,6 +640,13 b' def findsymbolicname(arg):'
607 else:
640 else:
608 return None
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 def _unthunk(context, mapping, thing):
650 def _unthunk(context, mapping, thing):
611 """Evaluate a lazy byte string into value"""
651 """Evaluate a lazy byte string into value"""
612 if not isinstance(thing, types.GeneratorType):
652 if not isinstance(thing, types.GeneratorType):
@@ -655,13 +695,7 b' def evalboolean(context, mapping, arg):'
655 thing = stringutil.parsebool(data)
695 thing = stringutil.parsebool(data)
656 else:
696 else:
657 thing = func(context, mapping, data)
697 thing = func(context, mapping, data)
658 if isinstance(thing, wrapped):
698 return makewrapped(context, mapping, thing).tobool(context, mapping)
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))
665
699
666 def evaldate(context, mapping, arg, err=None):
700 def evaldate(context, mapping, arg, err=None):
667 """Evaluate given argument as a date tuple or a date string; returns
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 empty string is False
4151 empty string is False
4152 $ hg log -r 0 -T '{if(revset(r"0 - 0"), "", "empty list is False")}\n'
4152 $ hg log -r 0 -T '{if(revset(r"0 - 0"), "", "empty list is False")}\n'
4153 empty list is False
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 $ hg log -r 0 -T '{if(true, "true is True")}\n'
4158 $ hg log -r 0 -T '{if(true, "true is True")}\n'
4155 true is True
4159 true is True
4156 $ hg log -r 0 -T '{if(false, "", "false is False")}\n'
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