From 5116a248cb421592fcc3b6c5b0bf56220e754480 Mon Sep 17 00:00:00 2001 From: Guillaume Brunerie Date: Mon, 8 Apr 2024 16:05:51 +0200 Subject: [PATCH] fix: converting an empty node set to a number should return NaN --- func.go | 2 +- xpath_function_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/func.go b/func.go index 65386f0..a5e88ba 100644 --- a/func.go +++ b/func.go @@ -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 diff --git a/xpath_function_test.go b/xpath_function_test.go index 06687ae..8d9e33b 100644 --- a/xpath_function_test.go +++ b/xpath_function_test.go @@ -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))) }