Skip to content

Commit

Permalink
fix: converting an empty node set to a number should return NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebrunerie committed Apr 8, 2024
1 parent 7995282 commit 5116a24
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion func.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func asNumber(t iterator, o interface{}) float64 {
case query:
node := typ.Select(t)
if node == nil {
return float64(0)
return math.NaN()
}
if v, err := strconv.ParseFloat(node.Value(), 64); err == nil {
return v
Expand Down
2 changes: 2 additions & 0 deletions xpath_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func Test_func_number(t *testing.T) {
test_xpath_eval(t, empty_example, `number("10") > 10`, false)
test_xpath_eval(t, empty_example, `number("10") = 10`, true)
test_xpath_eval(t, empty_example, `number("123") < 1000`, true)
test_xpath_eval(t, empty_example, `number(//non-existent-node) = 0`, false)
assertTrue(t, math.IsNaN(MustCompile(`number(//non-existent-node)`).Evaluate(createNavigator(empty_example)).(float64)))
assertTrue(t, math.IsNaN(MustCompile(`number("123a")`).Evaluate(createNavigator(empty_example)).(float64)))
}

Expand Down

0 comments on commit 5116a24

Please sign in to comment.