Skip to content

Commit

Permalink
Update to release v1.1.7 and fix for max()/min() functions
Browse files Browse the repository at this point in the history
  - Fix fn:max() and fn:min() error in case of an empty sequence: now
    the error is intercepted and the functions return an empty list.
  • Loading branch information
brunato committed Apr 25, 2019
1 parent 12b71ba commit 12f94db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
CHANGELOG
*********

`v1.1.7`_ (TBD)
===============
`v1.1.7`_ (2019-04-25)
======================
* Added Parser.is_spaced() method for checking if the current token has extra spaces before or after
* Fixes for '/' and ':' tokens
* Fixes for fn:max() and fn:min() functions

`v1.1.6`_ (2019-03-28)
======================
Expand Down
4 changes: 4 additions & 0 deletions elementpath/xpath2_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ def evaluate(self, context=None):
value = max(self[0].select(context))
except TypeError as err:
self.wrong_type(str(err))
except ValueError:
return []
else:
locale.setlocale(locale.LC_ALL, default_locale)
return value
Expand All @@ -355,6 +357,8 @@ def evaluate(self, context=None):
value = min(self[0].select(context))
except TypeError as err:
self.wrong_type(str(err))
except ValueError:
return []
else:
locale.setlocale(locale.LC_ALL, default_locale)
return value
Expand Down
14 changes: 13 additions & 1 deletion tests/test_xpath2_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ def test_number_functions2(self):
self.check_value("round-half-to-even(4.7564E-3, 2)", 0.0E0)
self.check_value("round-half-to-even(35612.25, -2)", 35600)

def test_aggregate_functions(self):
def test_sum_function(self):
self.check_value("sum((10, 15, 6, -2))", 29)

def test_avg_function(self):
context = XPathContext(root=self.etree.XML('<A/>'),
variables={
'd1': YearMonthDuration.fromstring("P20Y"),
Expand All @@ -290,6 +291,10 @@ def test_aggregate_functions(self):
root_token = self.parser.parse("fn:avg(($seq3, xs:float('NaN')))")
self.assertTrue(math.isnan(root_token.evaluate(context)))

root = self.etree.XML('<a><b>1</b><b>9</b></a>')
self.check_selector('avg(/a/b/number(text()))', root, 5)

def test_max_function(self):
self.check_value("fn:max((3,4,5))", 5)
self.check_value("fn:max((5, 5.0e0))", 5.0e0)
if PY3:
Expand All @@ -301,12 +306,19 @@ def test_aggregate_functions(self):
Date(dt.year, dt.month, dt.day, tzinfo=dt.tzinfo))
self.check_value('fn:max(("a", "b", "c"))', 'c')

root = self.etree.XML('<a><b>1</b><b>9</b></a>')
self.check_selector('max(/a/b/number(text()))', root, 9)

def test_min_function(self):
self.check_value("fn:min((3,4,5))", 3)
self.check_value("fn:min((5, 5.0e0))", 5.0e0)
self.check_value("fn:min((xs:float(0.0E0), xs:float(-0.0E0)))", 0.0)
self.check_value('fn:min((fn:current-date(), xs:date("2001-01-01")))', Date.fromstring("2001-01-01"))
self.check_value('fn:min(("a", "b", "c"))', 'a')

root = self.etree.XML('<a><b>1</b><b>9</b></a>')
self.check_selector('min(/a/b/number(text()))', root, 1)

###
# Functions on strings
def test_codepoints_to_string_function(self):
Expand Down

0 comments on commit 12f94db

Please sign in to comment.